diff options
| author | Anatoli Arkhipenko <arkhipenko@hotmail.com> | 2015-10-15 23:33:31 -0400 |
|---|---|---|
| committer | Anatoli Arkhipenko <arkhipenko@hotmail.com> | 2015-10-15 23:37:01 -0400 |
| commit | bb20df9c654cbed77bb5007814ae06f20f08ec36 (patch) | |
| tree | 0fe3b67dcfa7e070d10261d99cd0ec564ec3bf61 /examples | |
| parent | 07b0a9e49fd4c918821099aec940d0927c3f9ff5 (diff) | |
TaskScheduler version 1.8.0 - added support for event completion signallingv1.8.0
* Introduced option to compile with support for Status Rquests - ability to wait for and signal event completion
* Moved all code to one header file allowing control at compile time via #define statments.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/Scheduler_example4_StatusRequest/Scheduler_example4_StatusRequest.ino | 81 | ||||
| -rw-r--r-- | examples/Scheduler_example5_StatusRequest/Scheduler_example5_StatusRequest.ino | 165 |
2 files changed, 246 insertions, 0 deletions
diff --git a/examples/Scheduler_example4_StatusRequest/Scheduler_example4_StatusRequest.ino b/examples/Scheduler_example4_StatusRequest/Scheduler_example4_StatusRequest.ino new file mode 100644 index 0000000..ffe4e3a --- /dev/null +++ b/examples/Scheduler_example4_StatusRequest/Scheduler_example4_StatusRequest.ino @@ -0,0 +1,81 @@ +/** This test demonstrates interaction between three simple tasks via StatusRequest object. + * Task T1 runs every 5 seconds and signals completion of a status request st. + * Tasks T2 and T3 are waiting on the same request (st) + * Task T3 does not renew its interest in status request st, so it is only invoked once (first iteration) + * Task T2 is invoked every time st completes, because it renews its interest in status of status request object st every iteration of T1 + */ + +#define _TASK_SLEEP_ON_IDLE_RUN +#define _TASK_STATUS_REQUEST + +#include <TaskScheduler.h> + +StatusRequest st; + +Scheduler ts; + +Task t1(5000, 1, &Callback1, &ts, true, NULL, &Disable1); +Task t2(&Callback2, &ts); +Task t3(&Callback3, &ts); + +/** T1 callback + * T1 just signals completion of st every 5 seconds + */ +void Callback1() { + Serial.println("T1: Signaling completion of ST"); + st.signalComplete(); +} + +/** T1 On Disable callback + * This callback renews the status request and restarts T1 delayed to run again in 5 seconds + */ +void Disable1() { + PrepareStatus(); + t1.restartDelayed(); +} + +/** T2 callback + * Invoked when status request st completes + */ +void Callback2() { + Serial.println("T2: Invoked due to completion of ST"); +} + + +/** T3 callback + * Invoked when status request st completes. + * This is only run once since T3 does not renew its interest in the status request st after first iteration + */ + void Callback3() { + Serial.println("T3: Invoked due to completion of ST"); + +} + +/** Prepare Status request st for another iteration + * + */ +void PrepareStatus() { + st.setWaiting(); // set the statusrequest object for waiting + t2.waitFor(&st); // request tasks 1 & 2 to wait on the object st +} + + +/** Main Arduino code + * Not much to do here. Just init Serial and set the initial status request + */ +void setup() { + + Serial.begin(115200); + Serial.println("TaskScheduler: Status Request Test 1. Simple Test."); + // put your setup code here, to run once: + PrepareStatus(); + t3.waitFor(&st); + + t1.delay(); +} + +void loop() { + // put your main code here, to run repeatedly: + ts.execute(); + +}
diff --git a/examples/Scheduler_example5_StatusRequest/Scheduler_example5_StatusRequest.ino b/examples/Scheduler_example5_StatusRequest/Scheduler_example5_StatusRequest.ino new file mode 100644 index 0000000..d1f2e5c --- /dev/null +++ b/examples/Scheduler_example5_StatusRequest/Scheduler_example5_StatusRequest.ino @@ -0,0 +1,165 @@ +/** This test emulates querying 3 sensors once every 10 seconds, each could respond with a different delay + * (ultrasonic sensors for instance) and printing a min value of the three when all three have reported their values. + * The overall timeout of 1 second is setup as well. + * An error message needs to be printed if a timeout occurred instead of a value. + */ + + +#define _TASK_SLEEP_ON_IDLE_RUN +#define _TASK_STATUS_REQUEST + +#include <TaskScheduler.h> + + + + +StatusRequest measure; + +Scheduler ts; + +Task tCycle(10000, -1, &CycleCallback, &ts, true); +Task tMeasure(1000, 1, &MeasureCallback, &ts, false, &MeasureEnable, &MeasureDisable); +Task tCalculate(&CalcCallback, &ts); +Task tSensor1(0, 1, &S1Callback, &ts, false, &S1Enable); +Task tSensor2(0, 1, &S2Callback, &ts, false, &S2Enable); +Task tSensor3(0, 1, &S3Callback, &ts, false, &S3Enable); + + +long distance, d1, d2, d3; + +void CycleCallback() { + Serial.println("CycleCallback: Initiating measurement cycle every 10 seconds"); + + tMeasure.restartDelayed(); +} + + + +bool MeasureEnable() { + Serial.println("MeasureEnable: Activating sensors"); + + distance = 0; + measure.setWaiting(3); // Set the StatusRequest to wait for 3 signals. + tCalculate.waitFor(&measure); + + tSensor1.restart(); + tSensor2.restart(); + tSensor3.restart(); + + return true; +} + +void MeasureCallback() { + Serial.println("MeasureCallback: Invoked by calculate task or one second later"); + + if (measure.pending()) { + tCalculate.disable(); + measure.signalComplete(-1); // signal error + Serial.println("MeasureCallback: Timeout!"); + } + else { + Serial.print("MeasureCallback: Min distance=");Serial.println(distance); + } +} + +void MeasureDisable() { + Serial.println("MeasureDisable: Cleaning up"); + + tSensor1.disable(); + tSensor2.disable(); + tSensor3.disable(); +} + + +void CalcCallback() { + Serial.println("CalcCallback: calculating"); + distance = -1; + if ( measure.getStatus() >= 0) { // only calculate if statusrequest ended successfully + distance = d1 < d2 ? d1 : d2; + distance = d3 < distance ? d3 : distance; + tMeasure.forceNextIteration(); + } +} + + +/** Simulation code for sensor 1 + * ---------------------------- + */ +bool S1Enable() { + Serial.print("S1Enable: Triggering sensor1. Delay="); + + tSensor1.setInterval( random(1200) ); // Simulating sensor delay, which could go over 1 second and cause timeout + d1 = 0; + + Serial.println( tSensor1.getInterval() ); + return true; +} + +void S1Callback() { + Serial.print("S1Callback: Emulating measurement. d1="); + d1 = random(501); // pick a value from 0 to 500 "centimeters" simulating a measurement + measure.signal(); + + Serial.println(d1); +} + + +/** Simulation code for sensor 2 + * ---------------------------- + */ +bool S2Enable() { + Serial.print("S2Enable: Triggering sensor2. Delay="); + + tSensor2.setInterval( random(1200) ); // Simulating sensor delay, which could go over 1 second and cause timeout + d2 = 0; + + Serial.println( tSensor2.getInterval() ); + return true; +} + +void S2Callback() { + Serial.print("S2Callback: Emulating measurement. d2="); + d2 = random(501); // pick a value from 0 to 500 "centimeters" simulating a measurement + measure.signal(); + + Serial.println(d2); +} + + +/** Simulation code for sensor 3 + * ---------------------------- + */ +bool S3Enable() { + Serial.print("S3Enable: Triggering sensor3. Delay="); + + tSensor3.setInterval( random(1200) ); // Simulating sensor delay, which could go over 1 second and cause timeout + d3 = 0; + + Serial.println( tSensor3.getInterval() ); + return true; +} + +void S3Callback() { + Serial.print("S3Callback: Emulating measurement. d3="); + d3 = random(501); // pick a value from 0 to 500 "centimeters" simulating a measurement + measure.signal(); + + Serial.println(d3); +} + + +/** Main Arduino code + * Not much is left here - everything is taken care of by the framework + */ +void setup() { + + Serial.begin(115200); + Serial.println("TaskScheduler StatusRequest Sensor Emulation Test. Complex Test."); + randomSeed(analogRead(A1)+millis()); +} + +void loop() { + + ts.execute(); + +}
|
