diff options
| author | Anatoli Arkhipenko <arkhipenko@hotmail.com> | 2015-10-29 23:39:52 -0400 |
|---|---|---|
| committer | Anatoli Arkhipenko <arkhipenko@hotmail.com> | 2015-11-03 13:59:13 -0500 |
| commit | ef2329bdeade723f560cd0f7e933c9f3119677f3 (patch) | |
| tree | 8ec69af2ba7e74d9577f9daccefce7325590b58e /examples | |
| parent | ccd1047d0286027ff98ee7f1dfdfa1f9d5e6e4e3 (diff) | |
TaskScheduler v1.8.2 Local Task Storage and bug fixesv1.8.2
* implemented Local Task Storage Pointer (allow use of same callback code for different tasks)
* bug fix: currentTask() method returns incorrect Task reference if called within OnEnable and OnDisable methods
* protection against infinite loop in OnEnable (if enable() methods are called within OnEnable)
* new currentLts() method in the scheduler class returns current task's LTS pointer in one call
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/Scheduler_example8_LTS/Scheduler_example8_LTS.ino | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/examples/Scheduler_example8_LTS/Scheduler_example8_LTS.ino b/examples/Scheduler_example8_LTS/Scheduler_example8_LTS.ino new file mode 100644 index 0000000..238617e --- /dev/null +++ b/examples/Scheduler_example8_LTS/Scheduler_example8_LTS.ino @@ -0,0 +1,142 @@ +/** + * TaskScheduler Test sketch - use of task's Local Task Storage pointer + * Test case: + * Overall test runs for 5 seconds + * A number of calculator tasks run every one second, and update their respective variables using Local Task Storage pointer + * All calculator tasks use the same callback code, which obtains reference to appropriate variables via LTS pointer + * Calculaotr tasks perform simple calculation (as an example): + * adding task id number to itself + * multiplying task id number by 10 + * + * Upon completion of the overall test, all results are printed out. + * Test could be repeated with various number of calculator tasks. + * All that needs to change is data definitions - code is completely agnostic of number of tasks + */ + +#define _TASK_SLEEP_ON_IDLE_RUN // Compile with support for entering IDLE SLEEP state for 1 ms if not tasks are scheduled to run +#define _TASK_WDT_IDS // Compile with support for Task IDs and Watchdog timer +#define _TASK_LTS_POINTER // Compile with support for Local Task Storage pointer +#include <TaskScheduler.h> + +// Overall number of calculator tasks: +#define NO_TASKS 3 + +Scheduler ts; + +// Calculator tasks. +// Note that all three tasks use the same callback methods +// They will be updating specific variables based on the +// Locat Task Storage pointers +Task t1(1000, -1, &Calculate, &ts, false, &CalcOn); +Task t2(1000, -1, &Calculate, &ts, false, &CalcOn); +Task t3(1000, -1, &Calculate, &ts, false, &CalcOn); +// add more calc tasks here if necessary + +Task tWrapper(5000, 1, NULL, &ts, false, &WrapperOn, &WrapperOff); + +// The below structure is an object referenced by LTS pointer +typedef struct { + unsigned int id; + long sum; + long product; +} task_var; + +// These are actual structures which hold tasks specific values +task_var v1; +task_var v2; +task_var v3; + +// Arrays below allow indexed access to specific tasks and tasks variables +Task *tasks[] = { &t1, &t2, &t3 }; +task_var *vars[] = { &v1, &v2, &v3 }; + + +/** + * This method is called when a wrapper task is enabled + * The purpose is to supply LTS pointers to all the tasks + */ +bool WrapperOn() { + + for (int i=0; i < NO_TASKS; i++) { + Task& T = *tasks[i]; + T.setLtsPointer( vars[i] ); + T.enableDelayed(); + } + + return true; // Signal that Task could be enabled +} + +/** + * This method is called when Wrapper task is disabled (after first and only iteration is executed) + * For each of the calculor tasks the results are printed out. + */ +void WrapperOff() { + Serial.println("Finished processing"); + + ts.disableAll(); + + for (int i=0; i < NO_TASKS; i++) { + Serial.print("ID: "); Serial.println(vars[i]->id); + Serial.print("Sum: "); Serial.println(vars[i]->sum); + Serial.print("Product: "); Serial.println(vars[i]->product); + Serial.println(); + } +} + + +/** + * This method is executed when each calculator task is enabled + * The purpose is to initiate all local variables + */ +bool CalcOn() { + Task& T = ts.currentTask(); + task_var& var = *((task_var*) T.getLtsPointer()); + +// Initialize local variables + var.id = T.getId(); + var.sum = 0; + var.product = var.id; + + return true; +} + + +/** + * This method performs simple calculations on task's local variables + */ +void Calculate() { + Task& T = ts.currentTask(); +// Another way to get to LTS pointer: + task_var& var = *((task_var*) ts.currentLts()); + + + Serial.print("Calculating for task: "); + Serial.print(T.getId()); + Serial.print("; Task id per LTS is: "); + Serial.println( var.id ); + + var.sum += T.getId(); + var.product = var.product * 10; + +} + + +/** + * Standard Arduino setup and loo methods + */ +void setup() { + Serial.begin(115200); + + randomSeed(analogRead(0)+analogRead(5)); + + pinMode(13, OUTPUT); + digitalWrite(13, LOW); + + Serial.println("Local Task Storage pointer test"); + + tWrapper.enableDelayed(); +} + +void loop() { + ts.execute(); +}
|
