1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
<TITLE></TITLE>
<META NAME="GENERATOR" CONTENT="LibreOffice 3.5 (Linux)">
<META NAME="CREATED" CONTENT="20150206;16300000">
<META NAME="CHANGEDBY" CONTENT="Anatoli Arkhipenko">
<META NAME="CHANGED" CONTENT="20151015;23250000">
<META NAME="Info 1" CONTENT="">
<META NAME="Info 2" CONTENT="">
<META NAME="Info 3" CONTENT="">
<META NAME="Info 4" CONTENT="">
<STYLE TYPE="text/css">
<!--
@page { margin: 0.79in }
P { margin-bottom: 0.08in; direction: ltr; color: #000000; widows: 0; orphans: 0 }
P.western { font-family: "Liberation Serif", "MS PMincho", serif; font-size: 12pt; so-language: en-US }
P.cjk { font-family: "WenQuanYi Micro Hei", "MS Mincho"; font-size: 12pt; so-language: zh-CN }
P.ctl { font-family: "Lohit Hindi", "MS Mincho"; font-size: 12pt; so-language: hi-IN }
-->
</STYLE>
</HEAD>
<BODY LANG="en-US" TEXT="#000000" BGCOLOR="#ffffff" DIR="LTR">
<P CLASS="western" STYLE="margin-bottom: 0in"><B>Task Scheduler –
cooperative multitasking for Arduino microcontrollers</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>Version 1.8.0:
2015-10-15</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>OVERVIEW</B>:</P>
<P CLASS="western" STYLE="margin-bottom: 0in">A lightweight
implementation of cooperative multitasking (task scheduling)
supporting:</P>
<OL>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">Periodic task
execution (with dynamic execution period in milliseconds)</P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">Number of
iterations (n times)</P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">Execution of tasks
in predefined sequence</P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">Dynamic change of
task execution parameters (frequency, number of iterations, callback
function)</P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">Power saving via
entering IDLE sleep mode between tasks are scheduled to run</P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">Support for task
invocation via Status Request object</P>
</OL>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>TASK</B>:</P>
<P CLASS="western" STYLE="margin-bottom: 0in">“Task” is a
container concept that links together:</P>
<OL>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">Execution interval</P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">Execution event
(Status Request)</P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">Number of
execution iterations</P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">Piece of code
performing task activities (callback functions)</P>
</OL>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>Tasks</B> are linked
into execution <B>chains</B>, which are processed by the “Scheduler”
in the order they are linked.</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Each task performs its
function via callback function. Scheduler calls Task’s callback
function periodically until task is disabled or task runs out of
iterations. In addition to “regular” callback, two methods could
be enabled for each task: a callback function invoked once when task
is enabled, and a callback function invoked once when the task is
disabled. Those two special methods allows task to properly initiate
themselves for execution and clean-up after execution is over.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Tasks are responsible
for supporting <B>cooperative</B> <B>multitasking</B> by being “good
neighbors”, i.e., running their callback functions quickly in a
non-blocking way and releasing control as soon as possible.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">“Scheduler” is
executing Tasks' callback functions in the order the tasks were added
to the chain, from first to last. Scheduler stops and exists after
processing the chain once in order to allow other statements in the
main code of <B>loop()</B> function to run. This <B>a “scheduling
pass”.</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">If compiled with
<FONT FACE="Courier New, monospace">_TASK_SLEEP_ON_IDLE_RUN</FONT>
enabled, the scheduler will place processor into IDLE sleep mode (for
approximately 1 ms, as the timer interrupt will wake it up), after
what is determined to be an “idle” pass. An Idle Pass is a pass
through the task chain when no Tasks were scheduled to run their
callback functions. This is done to avoid repetitive empty passes
through the chain when no tasks need to be executed. If any of the
tasks in the chain always requires immediate execution (aInterval =
0), then there will be no IDLE sleep between task callback execution.</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<B>Below is the flowchart of a Task lifecycle:</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><IMG SRC="TaskScheduler_html.png" NAME="graphics1" ALIGN=BOTTOM WIDTH=664 HEIGHT=747 BORDER=0></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>Note: </B>Task
Scheduler uses <B>millis()</B> to determine if tasks are ready to be
invoked. Therefore, if you put your device to any “deep” sleep
mode disabling timer interrupts, the <B>millis()</B> count will be
suspended, leading to effective suspension of scheduling. Upon wake
up, active tasks need to be re-enabled, which will effectively reset
their internal time scheduling variables to the new value of
<B>millis(). </B>Time spent in deep sleep mode should be considered
“frozen”, i.e., if a task was scheduled to run in 1 second from
now, and device was put to sleep for 5 minutes, upon wake up, the
task will still be scheduled 1 second from the time of wake up.
Executing <B>enable() </B>function on this tasks will make it run as
soon as possible. This is a concern only for tasks which are required
to run in a truly periodical manner (in absolute time terms).
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">In addition to
time-only invocation, tasks can be scheduled to wait on an event
employing StatusRequest objects (more about Status Requests later).</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Consider a scenario
when one task (t1) is performing a function which affects execution
of many tasks (t2, t3). In this case the task t1 will “signal”
completion of its function via Status Request object. Tasks t2 and t3
are “waiting” on the same Status Request object. As soon as
status request completes, t2 and t3 are activated.</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Alternative scenario is
the ne task (t1) and waiting for the completion of a number of tasks
(t2, t3). When done, t2 and t3 signal completion of their functions,
t1 is invoked.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Please see the examples
at the end of this document.</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>COMPILE PARAMETERS:</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in">This library could be
compiled with several options.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">These parameters must
be defined before inclusion of the library header file into the
sketch.</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">#define
<B>_TASK_TIMECRITICAL</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in">...will compile the
library with time critical tracking option enabled.</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Time critical option
keeps track where next execution time of the task falls, and makes it
available via API through Task::<B> getOverrun() </B>function. If
<B>getOverrun </B>returns a negative value, this Task’s next
execution time is in the past, and task is behind schedule. This most
probably means that either task’s callback function runtime is too
long, or the execution interval is too short (then schedule is too
aggressive).</P>
<P CLASS="western" STYLE="margin-bottom: 0in">A positive value
indicates that task is on schedule, and callback functions have
enough time to finish before the next scheduled pass.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">#define
<B>_TASK_SLEEP_ON_IDLE_RUN</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in">...will compile the
library with the <B>sleep</B> option enabled (AVR boards only).</P>
<P CLASS="western" STYLE="margin-bottom: 0in">When enabled, scheduler
will put the microcontroller into <B>SLEEP_MODE_IDLE</B> state if
none of the tasks’ callback functions were activated during pass.
<B>IDLE</B> state is interrupted by timers once every 1 ms. Helps
conserve power. Device in SLEEP_MODE_IDLE wakes up to all hardware
and timer interrupts, so scheduling is kept current.</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">#define
<B>_TASK_STATUS_REQUEST</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in">…will compile
TaskScheduler with support for StatusRequest object. Status Requests
are objects allowing tasks to wait on an event, and signal event
completion to each other.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>NOTE: above
parameters are DISABLED by default, and need to be explicitly
enabled.</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>API DOCUMENTATION:</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<B>TASKS:</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">CREATION:</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>Task();</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Default
constructor.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Takes
no parameters and creates a task that could be scheduled to run at
every scheduling pass indefinitely, but does not have a callback
function defined, so no execution will actually take place.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">All
tasks are created <B>disabled</B> by default.</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<B>Task(unsigned long aInterval, long aIterations, void
(*aCallback)(), Scheduler* aScheduler, bool aEnable, bool
(*aOnEnable)(), void (*aOnDisable)())</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Constructor
with parameters.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Creates
a task that is scheduled to run every <aInterval> milliseconds,
<aIterations> times, executing <aCallback> function on
every pass.
</P>
<OL>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">aInterval is in
milliseconds (<B>default = 0)</B></P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">aIteration in
number of times, -1 for indefinite execution (<B>default = -1)</B><BR><B>Note:
</B>Tasks do not remember the number of iteration set initially.
After the iterations are done, internal iteration counter is 0. If
you need to perform another set of iterations, you need to set the
number of iterations again. <BR><B>Note: </B>Tasks which performed
all their iterations remain active.
</P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">aCallback is a
pointer to a void callback function without parameters (<B>default =
NULL)</B></P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">aScheduler –
<B>optional</B> reference to existing scheduler. If supplied (not
NULL) this task will be appended to the task chain of the current
scheduler). (<B>default = NULL)</B></P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">aEnable –
<B>optional</B>. Value of <B>true </B>will create task enabled.
(<B>default = false)</B></P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">aOnEnable is a
pointer to a bool callback function without parameters, invoked when
task is enabled. If OnEnable function returns <B>true</B>, task is
enabled. If <B>OnEnable</B> function return <B>false</B>, task
remains disabled (<B>default = NULL)</B></P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">aOnDisable is a
pointer to a void callback function without parameters, invoked when
task is disabled (<B>default = NULL)</B></P>
</OL>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">All
tasks are created <B>disabled</B> by default (unless aEnable = true).
You have to explicitly enable the task for execution.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enabled
task is scheduled for execution immediately. Enable tasks with delay
(standard execution interval or specific execution interval) in order
to defer first run of the task.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<B>Task(void (*aCallback)(), Scheduler* aScheduler, bool
(*aOnEnable)(), void (*aOnDisable)())</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
compiled with support for Status Request objects, this constructor
creates a Task for activation on event (since such tasks must run
<B>waitFor() </B>method, their <I>interval</I>, <I>iteration</I> and
<I>enabled</I> status will be set by that method.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>INFORMATION</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">The
following 3 “getter” functions return task status
(enabled/disabled), execution interval in milliseconds, number of
<I><B>remaining</B></I> iterations.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>bool
isEnabled() </B>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>unsigned
long getInterval()</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>long
getIterations() </B>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>long getOverrun()</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
library is compiled with <FONT FACE="Courier New, monospace">_TASK_TIMECRITICAL</FONT>
enabled, tasks are monitored for “long running” scenario. A “long
running” task is a task that does not finish processing its
callback functions quickly, and thus creates a situation for itself
and other tasks where they don't run on a scheduled interval, but
rather “catch up” and are behind. When task scheduler sets the
next execution target time, it adds Task's execution interval to the
previously scheduled execution time:</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <B>next
execution time = previous execution time + task execution interval</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
<B>next execution time</B> happens to be already in the past (<B>next
execution time</B> < <B>millis()</B>), then task is considered
<I><B>overrun</B></I>. <B>GetOverrun</B> function returns number of
milliseconds between next execution time and current time. If the
<B>value is negative</B>, the task is overrun by that many
milliseconds.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Positive
value indicate number of milliseconds of slack this task has for
execution purposes.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>unsigned long
getRunCounter()</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
the number of the current run. “Current run” is the number of
times a callback function has been invoked since the last time a task
was enabled. <BR><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:
</B>The <B>runCounter</B> value is incremented <I>before</I> callback
function is invoked. If a task is checking the <B>runCounter</B>
value within its callback function, then the first run value is 1.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
task T1 is checking the <B>runCounter</B> value of another task (T2)
, then value = 0 indicates that T2 has not been invoked yet, and
value = 1 indicates that T2 has run once.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>bool
isFirstIteration()</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Indicates
whether current pass is a first iteration of the task.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>bool
isLastIteration()</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">For
tasks with a limited number of iterations only, indicates whether
current pass is the last iteration.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<B>CONTROL:</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void enable();</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
the task, and schedules it for immediate execution (without delay) at
this or next scheduling pass depending on when the task was enabled.
Scheduler will execute the next pass without any delay because there
is a task which was enabled and requires execution.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
</B>enable() invokes task’s <B>OnEnable</B> method (if not NULL),
which can prepare task for execution. <B>OnEnable</B> must return a
value of <B>true</B> for task to be enabled. If <B>OnEnable</B>
returns <B>false</B>, task remains disabled. <B>OnEnable</B> is
invoked every time <B>enable</B> is called, regardless if task is
already enabled or not.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>bool enableIfNot();</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
the task only if it was previously disabled. Returns previous enable
state: <B>true</B> if task was already enabled, and <B>false</B> if
task was disabled.</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void delay();</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Schedules
the task for execution after a delay (aInterval), but does not change
the enabled/disabled status of the task.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
</B>a delay of 0 (zero) will delay task for current execution
interval. Use <B>forceNextIteration() </B>method to force execution
of the task’s callback during immediate next scheduling pass.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void
forceNextIteration();</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Schedules
the task for execution during immediate next scheduling pass.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
</B>Task’s schedule is adjusted to run from this moment in time.
For instance: if a task was running every 10 seconds: 10, 20, 30, ..,
calling <B>forceNextIteration </B>at 44th second of task execution
will make subsequent schedule look like: 44, 54, 64, 74, ..</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void
enableDelayed();</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
the task, and schedules it for execution after a delay (aInterval).
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void enableDelayed
(unsigned long aDelay);</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
the task, and schedules it for execution after a specific delay
(aDelay, which maybe different from aInterval).
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void restart();</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">For
tasks with limited number of iterations only, <B>restart</B> function
will re-enable the task, set the number of iterations back to when
the task was created and and schedule the task for execution as soon
as possible.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void restartDelayed
(unsigned long aDelay);</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Same
as <B>restart() </B>function, with the only difference being that
Task is scheduled to run first iteration after a delay = <B>aDelay</B>
milliseconds.</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>bool disable();</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Disables
the task. Scheduler will not execute this task any longer, even if it
remains in the chain. Task can be later re-enabled for execution.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Return
previous enabled state: <B>true</B> if task was enabled prior to
calling disable, and <B>false</B> otherwise.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
not NULL, task’s <B>OnDisable</B> method is invoked. <B>OnDisable</B>
is invoked only if task was enabled. Calling <B>disable</B> 3 times
for instance will invoke <B>OnDisable</B> only once.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void set(unsigned
long aInterval, long aIterations, void (*aCallback)() , bool
(*aOnEnable)() , void (*aOnDisable)());</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Allows
dynamic control of task execution parameters in one function call.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note</B><B>:
</B>OnEnable and OnDisable parameters can be omitted. In that case
they will be assigned to NULL and not called.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Next five “setter”
functions allow changes of individual task execution control
parameters.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
setInterval (unsigned long aInterval) </B>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
setIterations (long aIterations) </B>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
setCallback (void (*aCallback)()) </B>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
setOnEnable (bool (*aCallback)()) </B>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
setOnDisable (void (*aCallback)()) </B>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>Note: </B>Next
execution time calculation takes place <B>after</B> the callback
function is called, so new interval will be used immediately by the
scheduler. For the situations when one task is changing the interval
parameter for the other, <B>setInterval</B> function calls <B>delay
</B>explicitly to guarantee schedule change, however it <B>does not
</B>enable the task if task is disabled.</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>Note: </B>Tasks that
ran through all their allocated iterations are disabled.
<B>SetIterations()</B> method <B>DOES NOT</B> enable the task. Either
<B>enable</B> explicitly, or use <B>restart</B> methods.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<B>void waitFor(StatusRequest* aStatusRequest);</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
compiled with support for Status Requests, this method makes task
wait for the completion of <B>aStatusRequest</B> event.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>waitFor()
</B>sets tasks interval to <B>0 (zero)</B> for immediate execution
when event happens, and also sets the number of <B>iterations to 1</B>.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
aStatusRequest</B> should be “activated” by calling <B>setWaiting()
</B>method before making a task wait on it. Otherwise, the task will
execute immediately.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>StatusRequest*
getStatusRequest()</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
a StatusReqeust object this Task was waiting on.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<B>TASK SCHEDULER:</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<B>CREATION:</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>Scheduler()</B><BR><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Default
constructor.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Takes
no parameters. Creates task scheduler with default parameters and an
empty task queue.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void init()</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Initializes
the task queue and scheduler parameters, Executed as part of
constructor, so don't need to be explicitly called after creation.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
</B>be default (if compiled with <FONT FACE="Courier New, monospace">_TASK_TIMECRITICAL</FONT>
enabled) scheduler is allowed to put processor to IDLE sleep mode. If
this behavior was changed via <B>allowSleep() </B>function, <B>inti()
</B>will <B>NOT</B> reset allow sleep particular parameter.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void
addTask(Task& aTask)</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Adds
task aTask to the execution queue (or chain) of tasks by appending it
to the end of the chain. If two tasks are scheduled for execution,
the sequence will match the order in which tasks were appended to the
chain. However, in reality, due to different timing of task
execution, the actual order may be different.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
</B>Currently, changing the execution sequence in a chain dynamically
is not supported.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
you need to reorder the chain sequence – initialize the scheduler
and re-add the tasks in a different order.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void
deleteTask(Task& aTask)</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Deletes
task aTask from the execution chain. The chain of remaining tasks is
linked together (i.e</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">if
original task chain is 1 → 2 → 3 → 4, deleting 3 will result in
1 → 2 → 4).</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
</B>it is not required to delete a task from the chain. A disabled
task will not be executed anyway, but you save a few microseconds per
scheduling pass by deleting it, since it is not even considered for
execution.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">An
example of proper use of this function would be running some sort of
<B>initialize</B> task in the chain, and then deleting it from the
chain since it only needs to run once.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void allowSleep(bool
aState) </B>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Available
in API only if compiled with <FONT FACE="Courier New, monospace">_TASK_TIMECRITICAL</FONT>
enabled. Controls whether scheduler is allowed (<B>aState =true</B>),
or not (<B>aState =false</B>) to put processor into IDLE sleep mode
in case not tasks are scheduled to run.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">The
<B>default</B> behavior of scheduler upon creation is to allow sleep
mode.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void enableAll()</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void disableAll()</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">enables
and disables (respectively) all tasks in the chain. Convenient if
your need to enable/disable majority of the tasks (i.e. disable all
and then enable one).
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>Task&
currentTask()<BR></B><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
reference to the task, currently executing via <B>execute()</B> loop.
Could be used by callback functions to identify which of the Tasks
invoked callback function.</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void execute()</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Executes
one scheduling pass, including end-of-pass sleep. This function
typically placed inside the <B>loop()</B> function of the sketch.
Since <B>execute</B> exits after every pass, you can put additional
statements after <B>execute</B> inside the <B>loop()</B>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<B>STATUS REQUEST:</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<B>CREATION:</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>StatusRequest()</B><BR><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Default
constructor.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Takes
no parameters. Creates Status Request object, which is assigned a
status of “completed” on creation.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>void
setWaiting(unsigned int aCount)</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Activates
Status Request object. By default each object is set to wait on one
event only, however, if <B>aCount</B> is supplied, Status Request can
wait on multiple events. For instance, <B>setWaiting(3)</B> will wait
on three signals. An example could be waiting for completion of
measurements from 3 sensors.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>bool signal(int
aStatus)</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Signals
completion of the event to the Status Request object, and passes a
completion code, which could be interrogated later.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
</B> passing a <B>negative</B> status code to the status request
object is considered reporting an error condition, and will complete
the status request regardless of how many outstanding signals it is
still waiting for.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note</B>:
only the latest status code is kept.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>bool signalComplete
(int aStatus)</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Signals
completion of <B>ALL</B> events to the Status Request object, and
passes a completion code, which could be interrogated later. The
status request completes regardless of how many events it is still
waiting on.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>bool pending() </B>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
<B>true</B> if status request is still waiting for event or events to
happen.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>bool completed () </B>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
<B>true</B> if status has completed.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><B>int getStatus()</B></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
the status code passed to the status request object by the <B>signal()
</B>and <B>signalComplete() </B>methods.
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Any
<B>positive</B> number is considered a successful completion status.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">A
0 (zero) is considered a default successful completion status.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Any
<B>negative</B> number is considered an error code and unsuccessful
completion of a request.</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
<B>IMPLEMENTATION SCENARIOS AND IDEAS:</B></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<OL>
<LI><P CLASS="western" STYLE="margin-bottom: 0in"><B>EVENT DRIVEN
PROGRAMMING</B></P>
</OL>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Each of the processes
of your application becomes a separate and distinct programming area,
which may or may not interact and control each other.
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Example:
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">In a plant watering
system you need to measure soil humidity, control pump and display
the results</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Each of the areas
becomes a task:</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
<B>tMeasure</B> (TMEASURE_INTERVAL*SECOND, -1,
&measureCallback);<BR>Task <B>tWater</B>
(TWATER_INTERVAL*SECOND, RETRIES, &waterCallback);<BR>Task
<B>tDisplay</B> (TDISPLAY_INTERVAL*SECOND, -1, &displayCallback);
<BR></FONT></FONT><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler
</FONT></FONT><FONT FACE="Courier New, monospace"><FONT SIZE=2><B>taskManager</B></FONT></FONT><FONT FACE="Courier New, monospace"><FONT SIZE=2>;</FONT></FONT><BR><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Further, once you turn
on the pump, you keep it running for TWATER_INTERVAL interval and
then turn it off. Turning off a pump is also a task which only needs
to run once for every time the pump is turned on:</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
</FONT></FONT><FONT FACE="Courier New, monospace"><FONT SIZE=2><B>tWaterOff</B></FONT></FONT><FONT FACE="Courier New, monospace"><FONT SIZE=2>
(WATERTIME*SECOND, 1,</FONT></FONT><FONT FACE="Courier New, monospace">
&waterOffCallback);</FONT><BR><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in">Example of the callback
function:</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
waterOffCallback() {<BR> motorOff();<BR>
tWater.enableDelayed();<BR>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>or</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
waterCallback() {<BR> if (tWater.getIterations()) {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
If this is not the last iteration = turn the pump on<BR>
motorOn();<BR> tWaterOff.set(parameters.watertime * SECOND, 1,
&waterOffCallback);<BR> tWaterOff.enableDelayed();<BR>
return;<BR> }</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
We could not reach target humidity – something is wrong<BR>
motorOff;<BR> taskManager.disableAll();<BR> tError.enable();<BR>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Your
sample <B>setup</B>() and <B>loop</B>() (partially) are as follows. </FONT>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>Note:
</B>please note that tWater is <B>not</B> activated during setup().
It is activated by tMeasure callback once the watering conditions are
met. </FONT>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"> </FONT><FONT FACE="Courier New, monospace">setup()</FONT></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Courier New, monospace">
...</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace">tWater.setIterations(parameters.retries);<BR>
tWaterOff.setInterval(parameters.watertime * SECOND);<BR><BR><BR>
taskManager.init();<BR> taskManager.addTask(tMeasure);<BR>
taskManager.addTask(tDisplay);<BR> taskManager.addTask(tWater);<BR>
taskManager.addTask(tWaterOff);<BR> <BR> tMeasure.enable();<BR>
tDisplay.enable();<BR><BR> currentHumidity =
measureHumidity();<BR>}<BR><BR><BR>void loop ()<BR>{<BR>
taskManager.execute();<BR>}</FONT><FONT FACE="Times New Roman, serif"><BR></FONT><BR>
</P>
<OL START=2>
<LI><P CLASS="western" STYLE="margin-bottom: 0in">“<FONT FACE="Times New Roman, serif"><B>NATIVE”
SUPPORT FOR FINITE STATE MACHINE</B></FONT></P>
</OL>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Define
“states” as callback function or functions. Each callback
function executes activities specific to a “state” and then
“transitions” to the next state by assigning next callback
function to the task. </FONT>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Transition
from one state to the next is achieved by setting next callback
function at the end of preceding one. </FONT>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>Note:
</B>do not call the next callback function. Let the schedule take
care of that during the next pass. (Thus letting other tasks run). </FONT>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Example:
Blinking LED 2 times a second could be achieved this way</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace">Task
tLedBlinker (500, -1, &ledOnCallback);</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace">Scheduler
taskManager;</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace">void
ledOnCallback() {</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"> turnLedOn();</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"> tLedBlinker.setCallback(&ledOffCallback);</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace">}</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace">void
ledOffCallback() {</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"> turnLedOff();</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"> tLedBlinker.setCallback(&ledOnCallback);</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace">}</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace">setup()
{</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"> taskManager.init();<BR>
taskManager.addTask(tLedBlinker);</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"> </FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"> tLedBlinker.enable();</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace">}</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace">loop
() {</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"> taskManager.execute();</FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace">}</FONT><FONT FACE="Times New Roman, serif"><BR></FONT><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Obviously
the example is simple, but gives the idea of how the tasks could be
used to go through states.</FONT></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<OL START=3>
<LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>MULTIPLE
POSSIBLE CALLBACKS FOR TASK</B></FONT></P>
</OL>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">There
may be a need to select an option for callback function based on
certain criteria, or randomly. </FONT>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">You
can achieve that by defining an array of callback function pointers
and selecting one based on the criteria you need. </FONT>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Example:
when a robot detects an obstacle, it may go left, right backwards,
etc. Each of the “directions” or “behaviors” are represented
by a different callback function. </FONT>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Another
example of using multiple callbacks:</FONT></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">You
may need to “initialize” variables for a particular task.</FONT></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">In
this case, define a tasks with two callbacks:</FONT></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
<B>tWork</B> (T_INTERVAL, -1, &workCallbackInit);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">…</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
workCallbackInit() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
do your initializationstuff here</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> </FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
finally assigne the main callback function </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> tWork.setCallback(&workCallback);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
workCallback() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
main callback function</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> …</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">The
task will initialize during first execution pass and switch to
“regular” callback execution starting with second pass. There is
a delay between first and second passes of the task (scheduling
period, if defined). In order to execute the second pass immediately
after initialization first pass, change the above code like this:</FONT></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
workCallbackInit() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
do your initializationstuff here</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> </FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
finally assigne the main callback function </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> tWork.setCallback(&workCallback);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> tWork.enable();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">The
task will run initialization first, then immediately second pass, and
then switch to processing at regular intervals starting with a third
pass. </FONT>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<OL START=3>
<LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>INTERRUP-DRIVEN
EXECUTION SUPPORT </B></FONT>
</P>
</OL>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">In
case of interrupt-driven program flow, tasks could be scheduled to
run once to request asynchronous execution (request), and then
re-enabled (restarted) again with a different callback function to
process the results. </FONT>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>Example</B>:
event driven distance calculation for ultrasonic pulses. EchoPin #6
triggers pin change interrupts on rising and falling edges to
determine the length of ultrasonic pulse.</FONT></P>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#include
<DirectIO.h><BR>#include <TaskScheduler.h><BR>#include
<PinChangeInt.h><BR><BR><BR>#define TRIGGERPIN 5<BR>#define
ECHOPIN 6<BR><BR>Output<TRIGGERPIN>
pTrigger;<BR>Input<ECHOPIN> pEcho;<BR><BR>Scheduler
r;<BR><BR>Task tMeasure(1000, -1, &measureCallback, &r,
true);<BR>Task tDisplay(1000, -1, &displayCallback, &r,
true);<BR>Task tPing(0, 1, &pingCalcCallback, &r,
false);<BR><BR><BR>volatile bool pulseBusy = false;<BR>volatile bool
pulseTimeout = false;<BR>volatile unsigned long pulseStart =
0;<BR>volatile unsigned long pulseStop = 0;<BR>volatile unsigned long
pingDistance = 0;<BR><BR><BR>void pingTrigger(unsigned long aTimeout)
{<BR> if (pulseBusy) return; // do not trigger if in the middle of
a pulse<BR> if (pEcho == HIGH) return; // do not trigger if ECHO pin
is high<BR> <BR> pulseBusy = true;<BR> pulseTimeout = false;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2><BR>
pTrigger = LOW;<BR> delayMicroseconds(4);<BR> pTrigger = HIGH;<BR><BR>
tPing.setInterval (aTimeout);<BR><BR> delayMicroseconds(10);<BR>
pTrigger = LOW; <BR></FONT></FONT><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tPing.restartDelayed();
// timeout countdown starts now<BR></FONT></FONT><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
will start the pulse clock on the rising edge of ECHO pin</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>PCintPort::attachInterrupt(ECHOPIN,
&pingStartClock, RISING); <BR>}<BR><BR></FONT></FONT><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
Start clock on the <B>rising</B> edge of the ultrasonic pulse<BR>void
pingStartClock() {<BR> pulseStart = micros();<BR>
PCintPort::detachInterrupt(ECHOPIN); // not sure this is necessary<BR>
PCintPort::attachInterrupt(ECHOPIN, &pingStopClock, FALLING); <BR>
tPing.restartDelayed();<BR>}<BR><BR>// Stop clock on the <B>falling</B>
edge of the ultrasonic pulse<BR>void pingStopClock() {<BR> pulseStop
= micros();<BR> PcintPort::detachInterrupt(ECHOPIN);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>pingDistance =
pulseStop - pulseStart;<BR> pulseBusy = false;<BR> tPing.disable();
// disable timeout<BR>}<BR></FONT></FONT><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
Stop clock because of the timeout – the wave did not return<BR>void
pingCalcCallback() {<BR> if (pulseBusy) {<BR> pingStopClock();<BR>
}<BR> pulseTimeout = true;<BR>}<BR><BR><BR><BR>// Initial measure
callback sets the trigger<BR>void measureCallback() {<BR> if
(pulseBusy) { // already measuring, try again<BR>
tMeasure.enable();<BR> return;<BR> }<BR> pingTrigger(30); // 30
milliseconds or max range of ~5.1 meters<BR>
tMeasure.setCallback(&measureCallbackWait);<BR>}<BR></FONT></FONT><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
Wait for the measurement to <BR>void measureCallbackWait() {<BR> if
(pulseBusy) return;<BR> tMeasure.setCallback(&measureCallback);
<BR>}<BR><BR><BR>bool state = true;<BR><BR>void displayCallback() {<BR>
char d[256];<BR> <BR> unsigned long cm = pingDistance * 17 / 100;
// cm<BR> <BR> snprintf(d, 256, "pulseStart =
%8lu\tpulseStop=%8lu\tdistance, cm=%8lu", pulseStart, pulseStop,
cm);<BR> Serial.println(d);<BR> <BR>}<BR><BR>void setup() {<BR> //
put your setup code here, to run once:<BR> <BR>
Serial.begin(115200);<BR> <BR><BR> pTrigger = LOW;<BR> pEcho =
LOW;<BR> <BR>}<BR><BR>void loop() {<BR> // put your main code here,
to run repeatedly:<BR> r.execute();<BR>}<BR></FONT></FONT><BR>
</P>
<OL START=3>
<LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>USING
ONENABLE AND ONDISBALE METHODS </B></FONT>
</P>
</OL>
<P CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Consider
a task to flash onboard LED for 5 seconds with random frequency. Task
should be repeated every 30 seconds indefinitely. Since frequency is
random, there are two challenges:</FONT></P>
<OL START=3>
<OL>
<LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">We
need to make sure LED is turned OFF at the last iteration</FONT></P>
<LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">We
need to calculate random frequency every time</FONT></P>
</OL>
</OL>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Below
is the implementation using TaskScheduler </FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2><BR></FONT></FONT><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
_TASK_SLEEP_ON_IDLE_RUN</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#include
<TaskScheduler.h></FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
LEDPIN 13</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler
ts;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
tWrapper(30000, -1, &WrapperCallback, &ts, true);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
tBlink(5000, 1, NULL, &ts, false, &BlinkOnEnable,
&BlinkOnDisable);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
tLED(0, -1, NULL, &ts, false, NULL, &LEDOff);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
WrapperCallback() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("In
WrapperCallback");</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tBlink.restartDelayed();
// LED blinking is initiated </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>//every
30 seconds for 5 seconds</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
Upon being enabled, tBlink will define the parameters</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
and enable LED blinking task, which actually controls</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
the hardware (LED in this example)</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
BlinkOnEnable() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("In
BlinkOnEnable");</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setInterval(
500 + random(501) );</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setCallback(
&LEDOn);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.enable();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>return true; //
Task should be enabled</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
tBlink does not really need a callback function</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
since it just waits for 5 seconds for the first </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
and only iteration to occur. Once the iteration</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
takes place, tBlink is disabled by the Scheduler, </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
thus executing its OnDisable method below.</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
BlinkOnDisable() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("In
BlinkOnDisable");</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.disable();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
LEDOn () {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("In
LEDOn");</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>digitalWrite(LEDPIN,
HIGH);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setCallback(
&LEDOff);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
LEDOff () {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("In
LEDOff");</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>digitalWrite(LEDPIN,
LOW);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setCallback(
&LEDOn);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
Note that LEDOff method serves as OnDisable method</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
to make sure the LED is turned off when the tBlink</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
task finishes (or disabled ahead of time)</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
setup() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.begin(115200);
</FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>pinMode(LEDPIN,
OUTPUT); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
loop() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>// put your main
code here, to run repeatedly:</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>ts.execute();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<OL START=3>
<LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>USING
STATUS REQUEST OBJECTS </B></FONT>
</P>
</OL>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=2 STYLE="font-size: 11pt">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.</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=2 STYLE="font-size: 11pt">The
overall timeout of 1 second is setup as well.</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=2 STYLE="font-size: 11pt">An
error message needs to be printed if a timeout occurred instead of a
value.</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
_TASK_SLEEP_ON_IDLE_RUN</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
_TASK_STATUS_REQUEST</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#include
<TaskScheduler.h></FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>StatusRequest
measure;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler
ts; </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
tCycle(10000, -1, &CycleCallback, &ts, true);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
tMeasure(1000, 1, &MeasureCallback, &ts, false,
&MeasureEnable, &MeasureDisable);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
tCalculate(&CalcCallback, &ts);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
tSensor1(0, 1, &S1Callback, &ts, false, &S1Enable);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
tSensor2(0, 1, &S2Callback, &ts, false, &S2Enable);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
tSensor3(0, 1, &S3Callback, &ts, false, &S3Enable);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>long
distance, d1, d2, d3;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
CycleCallback() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("CycleCallback:
Initiating measurement cycle every 10 seconds");</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tMeasure.restartDelayed();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
MeasureEnable() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("MeasureEnable:
Activating sensors"); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = 0;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.setWaiting(3);
// Set the StatusRequest to wait for 3 signals. </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tCalculate.waitFor(&measure);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor1.restart();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor2.restart();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor3.restart();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
MeasureCallback() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("MeasureCallback:
Invoked by calculate task or one second later"); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>if
(measure.pending()) {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tCalculate.disable();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signalComplete(-1);
// signal error</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("MeasureCallback:
Timeout!");</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>else {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print("MeasureCallback:
Min distance=");Serial.println(distance);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
MeasureDisable() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("MeasureDisable:
Cleaning up"); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor1.disable();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor2.disable();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor3.disable();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
CalcCallback() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("CalcCallback:
calculating"); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = -1;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>if (
measure.getStatus() >= 0) { // only calculate if statusrequest
ended successfully</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = d1 <
d2 ? d1 : d2;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = d3 <
distance ? d3 : distance;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tMeasure.forceNextIteration();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
Simulation code for sensor 1</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
----------------------------</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
S1Enable() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print("S1Enable:
Triggering sensor1. Delay="); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor1.setInterval(
random(1200) ); // Simulating sensor delay, which could go over 1
second and cause timeout</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>d1 = 0;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(
tSensor1.getInterval() );</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
S1Callback() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print("S1Callback:
Emulating measurement. d1="); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>d1 = random(501); //
pick a value from 0 to 500 "centimeters" simulating a
measurement </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signal();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(d1); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
Simulation code for sensor 2</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
----------------------------</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
S2Enable() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print("S2Enable:
Triggering sensor2. Delay="); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor2.setInterval(
random(1200) ); // Simulating sensor delay, which could go over 1
second and cause timeout</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>d2 = 0;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(
tSensor2.getInterval() );</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
S2Callback() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print("S2Callback:
Emulating measurement. d2="); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>d2 = random(501); //
pick a value from 0 to 500 "centimeters" simulating a
measurement</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signal();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(d2);
</FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
Simulation code for sensor 3</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
----------------------------</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
S3Enable() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print("S3Enable:
Triggering sensor3. Delay="); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor3.setInterval(
random(1200) ); // Simulating sensor delay, which could go over 1
second and cause timeout</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>d3 = 0;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(
tSensor3.getInterval() );</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
S3Callback() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print("S3Callback:
Emulating measurement. d3="); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>d3 = random(501); //
pick a value from 0 to 500 "centimeters" simulating a
measurement</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signal();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(d3); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
Main Arduino code</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
Not much is left here - everything is taken care of by the framework</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
setup() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.begin(115200);</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println("TaskScheduler
StatusRequest Sensor Emulation Test. Complex Test."); </FONT></FONT>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>randomSeed(analogRead(A1)+millis());</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
loop() {</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
<FONT FACE="Courier New, monospace"><FONT SIZE=2>ts.execute();</FONT></FONT></P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
</P>
<P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
</BODY>
</HTML>
|