浏览代码

Example of std::function usage

BlackEdder 8 年之前
父节点
当前提交
48f128b46b
共有 1 个文件被更改,包括 48 次插入0 次删除
  1. 48 0
      examples/Scheduler_example15_STDFunction/Scheduler_example15_STDFunction.ino

+ 48 - 0
examples/Scheduler_example15_STDFunction/Scheduler_example15_STDFunction.ino

@@ -0,0 +1,48 @@
+/** 
+ * TaskScheduler Test sketch - Showing how to use std::function
+ * to get acces to variables from within the task callback function 
+ */
+#define _TASK_STD_FUNCTION   // Compile with support for std::function 
+#include <TaskScheduler.h>
+
+Scheduler ts;
+
+class Calculator {
+public:
+    size_t cumSum = 0; // cumulative sum
+    Calculator(size_t b) {
+        // Pass the this pointer, so that we get access to this->cumSum
+        // Also pass a copy of b
+        calculateTask.set(TASK_SECOND, TASK_FOREVER, [this, b]() {
+            Serial.printf("cumSum = %u + %u\n", cumSum, b);
+            cumSum += b;
+            Serial.printf("Resulting cumulative sum: %u\n", cumSum);
+        });
+        ts.addTask(calculateTask);
+        calculateTask.enable();
+    }
+
+    Task calculateTask;
+};
+
+Calculator calc1(2);
+Calculator calc2(4);
+Calculator calc3(8);
+
+// Disable tasks after 10 seconds
+Task tWrapper(10*TASK_SECOND, TASK_ONCE, []() {
+    ts.disableAll();
+}, &ts); 
+
+/**
+ * Standard Arduino setup and loop methods
+ */
+void setup() {
+  Serial.begin(115200);
+  Serial.println("std::function test");
+  tWrapper.enableDelayed();
+}
+
+void loop() {
+  ts.execute();
+}