TaskScheduler.html 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
  5. <TITLE></TITLE>
  6. <META NAME="GENERATOR" CONTENT="LibreOffice 3.5 (Linux)">
  7. <META NAME="CREATED" CONTENT="20150206;16300000">
  8. <META NAME="CHANGEDBY" CONTENT="Anatoli Arkhipenko">
  9. <META NAME="CHANGED" CONTENT="20151105;22050000">
  10. <META NAME="Info 1" CONTENT="">
  11. <META NAME="Info 2" CONTENT="">
  12. <META NAME="Info 3" CONTENT="">
  13. <META NAME="Info 4" CONTENT="">
  14. <STYLE TYPE="text/css">
  15. <!--
  16. @page { margin: 0.79in }
  17. P { margin-bottom: 0.08in; direction: ltr; color: #000000; widows: 0; orphans: 0 }
  18. P.western { font-family: "Liberation Serif", "Times New Roman", serif; font-size: 12pt; so-language: en-US }
  19. P.cjk { font-family: "WenQuanYi Micro Hei", "MS Mincho"; font-size: 12pt; so-language: zh-CN }
  20. P.ctl { font-family: "Lohit Hindi", "MS Mincho"; font-size: 12pt; so-language: hi-IN }
  21. A:link { color: #0000ff }
  22. -->
  23. </STYLE>
  24. </HEAD>
  25. <BODY LANG="en-US" TEXT="#000000" LINK="#0000ff" BGCOLOR="#ffffff" DIR="LTR">
  26. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT SIZE=4 STYLE="font-size: 15pt"><B>Task
  27. Scheduler</B></FONT></P>
  28. <P CLASS="western" STYLE="margin-bottom: 0in"><B>cooperative
  29. multitasking for Arduino microcontrollers</B></P>
  30. <P CLASS="western" STYLE="margin-bottom: 0in; border-top: none; border-bottom: 1px solid #000000; border-left: none; border-right: none; padding-top: 0in; padding-bottom: 0.01in; padding-left: 0in; padding-right: 0in">
  31. <FONT SIZE=2 STYLE="font-size: 11pt"><B>Version 1.8.3: 2015-11-05</B></FONT></P>
  32. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  33. </P>
  34. <P CLASS="western" STYLE="margin-bottom: 0in"><B>OVERVIEW</B>:</P>
  35. <P CLASS="western" STYLE="margin-bottom: 0in">A lightweight
  36. implementation of cooperative multitasking (task scheduling)
  37. supporting:</P>
  38. <OL>
  39. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Periodic task
  40. execution (with dynamic execution period in milliseconds –
  41. frequency of execution)</P>
  42. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Number of
  43. iterations (limited or infinite number of iterations)</P>
  44. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Execution of tasks
  45. in predefined sequence</P>
  46. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Dynamic change of
  47. task execution parameters (frequency, number of iterations, callback
  48. methods)</P>
  49. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Power saving via
  50. entering IDLE sleep mode when tasks are not scheduled to run</P>
  51. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Support for
  52. event-driven task invocation via Status Request object</P>
  53. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Support for task
  54. IDs and Control Points for error handling and watchdog timer</P>
  55. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Support for Local
  56. Task Storage pointer (allowing use of same callback code for
  57. multiple tasks)</P>
  58. </OL>
  59. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  60. </P>
  61. <P CLASS="western" STYLE="margin-bottom: 0in"><B>TASK</B>:</P>
  62. <P CLASS="western" STYLE="margin-bottom: 0in">“Task” is a
  63. container concept that links together:</P>
  64. <OL>
  65. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Program code
  66. performing specific task activities (callback methods)</P>
  67. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Execution interval</P>
  68. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Number of
  69. execution iterations</P>
  70. <LI><P CLASS="western" STYLE="margin-bottom: 0in">(Optionally)
  71. Execution event (Status Request)</P>
  72. <LI><P CLASS="western" STYLE="margin-bottom: 0in">(Optionally)
  73. Pointer to a Local Task Storage area</P>
  74. </OL>
  75. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  76. </P>
  77. <P CLASS="western" STYLE="margin-bottom: 0in"><B>Tasks</B> perform
  78. certain functions, which could require periodic or one-time
  79. execution, update of specific variables, or waiting for specific
  80. events. Tasks also could be controlling specific hardware, or
  81. triggered by hardware interrupts.
  82. </P>
  83. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  84. </P>
  85. <P CLASS="western" STYLE="margin-bottom: 0in">For execution purposes
  86. <B>Tasks</B> are linked into execution <B>chains</B>, which are
  87. processed by the <B>Scheduler</B> in the order they were added
  88. (linked together).</P>
  89. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  90. </P>
  91. <P CLASS="western" STYLE="margin-bottom: 0in">Each task performs its
  92. function via a callback method. Scheduler calls Task’s callback
  93. method periodically until task is disabled or runs out of iterations.
  94. In addition to “regular” callback method, two additional methods
  95. could be utilized for each task: a callback method invoked every time
  96. the task is enabled, and a callback method invoked once when the task
  97. is disabled. Those two special methods allow tasks to properly
  98. initiate themselves for execution, and clean-up after execution is
  99. over (E.g., setup pin modes on enable, and always bring pin level to
  100. LOW at the end).
  101. </P>
  102. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  103. </P>
  104. <P CLASS="western" STYLE="margin-bottom: 0in">Tasks are responsible
  105. for supporting <B>cooperative</B> <B>multitasking</B> by being “good
  106. neighbors”, i.e., running their callback methods quickly in a
  107. non-blocking way, and releasing control back to scheduler as soon as
  108. possible.
  109. </P>
  110. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  111. </P>
  112. <P CLASS="western" STYLE="margin-bottom: 0in"><B>Scheduler</B> is
  113. executing Tasks' callback methods in the order the tasks were added
  114. to the chain, from first to last. Scheduler stops and exists after
  115. processing the chain once in order to allow other statements in the
  116. main code of <B>loop()</B> method to run. This is referred to as a
  117. <B>“scheduling pass”.</B></P>
  118. <P CLASS="western" STYLE="margin-bottom: 0in">(Normally, there is no
  119. need to have any other statements in the <B>loop</B>() method other
  120. than the Scheduler's <B>execute</B>() method).</P>
  121. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  122. </P>
  123. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  124. <B>Below is the flowchart of a Task lifecycle:</B></P>
  125. <P CLASS="western" STYLE="margin-bottom: 0in"><IMG SRC="TaskScheduler_html_m68472eb8.png" NAME="graphics1" ALIGN=BOTTOM WIDTH=664 HEIGHT=655 BORDER=0></P>
  126. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  127. </P>
  128. <P CLASS="western" STYLE="margin-bottom: 0in"><B>TaskScheduler</B>
  129. library maybe compiled with different compilation controls
  130. enabled/disabled. This is a way to limit TaskScheduler functionality
  131. (and size) for specific purpose (sketch). This is achieved by
  132. defining specific #<B>define</B> paramenters <I>before</I>
  133. TaskScheduler.h header file. Specifically:</P>
  134. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  135. </P>
  136. <P CLASS="western" STYLE="margin-bottom: 0in">If compiled with
  137. <FONT FACE="Courier New, monospace">_TASK_SLEEP_ON_IDLE_RUN</FONT>
  138. enabled, the scheduler will place processor into IDLE sleep mode (for
  139. approximately 1 ms, as the timer interrupt will wake it up), after
  140. what is determined to be an “idle” pass. An Idle Pass is a pass
  141. through the task chain when no Tasks were scheduled to run their
  142. callback methods. This is done to avoid repetitive idle passes
  143. through the chain when no tasks need to be executed. If any of the
  144. tasks in the chain always requires immediate execution (aInterval =
  145. 0), then there will be no IDLE sleep between task's callback method
  146. execution.</P>
  147. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  148. </P>
  149. <P CLASS="western" STYLE="margin-bottom: 0in"><B>Note: </B>Task
  150. Scheduler uses <B>millis()</B> to determine if tasks are ready to be
  151. invoked. Therefore, if you put your device to any “deep” sleep
  152. mode disabling timer interrupts, the <B>millis()</B> count will be
  153. suspended, leading to effective suspension of scheduling. Upon wake
  154. up, active tasks need to be re-enabled, which will effectively reset
  155. their internal time scheduling variables to the new value of
  156. <B>millis(). </B>Time spent in deep sleep mode should be considered
  157. “frozen”, i.e., if a task was scheduled to run in 1 second from
  158. now, and device was put to sleep for 5 minutes, upon wake up, the
  159. task will still be scheduled 1 second from the time of wake up.
  160. Executing <B>enable() </B>method on this tasks will make it run as
  161. soon as possible. This is a concern only for tasks which are required
  162. to run in a truly periodical manner (in absolute time terms).
  163. </P>
  164. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  165. </P>
  166. <P CLASS="western" STYLE="margin-bottom: 0in">In addition to
  167. time-only (<B>millis</B>() only) invocation, tasks can be scheduled
  168. to wait on an event employing StatusRequest objects (more about
  169. Status Requests later).</P>
  170. <P CLASS="western" STYLE="margin-bottom: 0in">Consider a scenario
  171. when one task (t1) is performing a function which affects execution
  172. of many tasks (t2, t3). In this case the task t1 will “signal”
  173. completion of its function via Status Request object. Tasks t2 and t3
  174. are “waiting” on the same Status Request object. As soon as
  175. status request completes, t2 and t3 are activated.</P>
  176. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  177. </P>
  178. <P CLASS="western" STYLE="margin-bottom: 0in">Alternative scenario is
  179. the ne task (t1) and waiting for the completion of a number of tasks
  180. (t2, t3). When done, t2 and t3 signal completion of their functions,
  181. t1 is invoked.
  182. </P>
  183. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  184. </P>
  185. <P CLASS="western" STYLE="margin-bottom: 0in">Please see the code
  186. examples at the end of this document, and included with the library
  187. package for details.</P>
  188. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  189. </P>
  190. <P CLASS="western" STYLE="margin-bottom: 0in"><B>COMPILE PARAMETERS:</B></P>
  191. <P CLASS="western" STYLE="margin-bottom: 0in">This library could be
  192. compiled in several configurations.</P>
  193. <P CLASS="western" STYLE="margin-bottom: 0in">Parameters (<B>#define</B>s)
  194. defining what functionality should or should not be included need be
  195. defined before the library header file in the body of arduino sketch.</P>
  196. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  197. </P>
  198. <P CLASS="western" STYLE="margin-bottom: 0in">#define
  199. <B>_TASK_TIMECRITICAL</B></P>
  200. <P CLASS="western" STYLE="margin-bottom: 0in">...will compile the
  201. library with time critical tracking option enabled.</P>
  202. <P CLASS="western" STYLE="margin-bottom: 0in">Time critical option
  203. keeps track where next execution time of the task falls, and makes it
  204. available via API through <B>Task::getOverrun()</B> method. If
  205. <B>getOverrun </B>returns a negative value, this Task’s next
  206. execution time point is <I>already</I> in the past, and task is
  207. behind schedule. This most probably means that either task’s
  208. callback method's runtime is too long, or the execution interval is
  209. too short (and therefore schedule is too aggressive).</P>
  210. <P CLASS="western" STYLE="margin-bottom: 0in">A positive value
  211. indicates that task is on schedule, and callback methods have enough
  212. time to finish before the next scheduled pass.
  213. </P>
  214. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  215. </P>
  216. <P CLASS="western" STYLE="margin-bottom: 0in">#define
  217. <B>_TASK_SLEEP_ON_IDLE_RUN</B></P>
  218. <P CLASS="western" STYLE="margin-bottom: 0in">...will compile the
  219. library with the <B>sleep</B> option enabled (AVR boards only).</P>
  220. <P CLASS="western" STYLE="margin-bottom: 0in">When enabled, scheduler
  221. will put the microcontroller into <B>SLEEP_MODE_IDLE</B> state if
  222. none of the tasks’ callback methods were activated during execution
  223. pass. <B>IDLE</B> state is interrupted by timers once every 1 ms.
  224. Putting microcontroller to IDLE state helps conserve power. Device
  225. in SLEEP_MODE_IDLE wakes up to all hardware and timer interrupts, so
  226. scheduling is kept current.</P>
  227. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  228. </P>
  229. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  230. #define <B>_TASK_STATUS_REQUEST</B></P>
  231. <P CLASS="western" STYLE="margin-bottom: 0in">…will compile
  232. TaskScheduler with support for StatusRequest object. Status Requests
  233. are objects allowing tasks to wait on an event, and signal event
  234. completion to each other.
  235. </P>
  236. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  237. </P>
  238. <P CLASS="western" STYLE="margin-bottom: 0in">#define <B>_TASK_WDT_IDS</B></P>
  239. <P CLASS="western" STYLE="margin-bottom: 0in">…will compile
  240. TaskScheduler with support for Task IDs and Control Points. Each task
  241. can be (and is by default) assigned an ID, which could be used to
  242. identify the task in case there is a problem with it. Furthermore
  243. within the task, Control Points could be defined to further help with
  244. pinpointing potential problem areas. For instance, the tasks which
  245. deal with external resources (sensors, serial communications,
  246. anything hardware dependent) can be blocked (or hung), by failed
  247. hardware. In this case, a watchdog timer could be employed to trap
  248. such a failed task, and identify which one (by task id) and where in
  249. the task (by a control point) the problem is likely located.</P>
  250. <P CLASS="western" STYLE="margin-bottom: 0in">
  251. </P>
  252. <P CLASS="western" STYLE="margin-bottom: 0in"><B>Note: </B>by
  253. default, talk IDs are assigned sequentially (1, 2, 3, …) to the
  254. tasks as they are being created. Programmer can assign a specific
  255. task id. <B>Task ids are unsigned integers.</B></P>
  256. <P CLASS="western" STYLE="margin-bottom: 0in">Control points provide
  257. a way to identify potential problem points within a task. Control
  258. points are <B>unsigned integers </B>as well. Please note that there
  259. is only one control point per task, and it is set to zero when the
  260. task’s callback method is invoked (this is done to prevent “stray”
  261. control point from previous task(s) confusing the matters.</P>
  262. <P CLASS="western" STYLE="margin-bottom: 0in">Example #7 contains a
  263. test of task ID and control points functionality.
  264. </P>
  265. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  266. </P>
  267. <P CLASS="western" STYLE="margin-bottom: 0in">#define
  268. <B>_TASK_LTS_POINTER</B></P>
  269. <P CLASS="western" STYLE="margin-bottom: 0in">…will compile
  270. TaskScheduler with support for Local Task Storage pointer (LTS). LTS
  271. is a generic (void*) pointer which could be set to reference a
  272. variable or a structure specific to a particular task. A callback
  273. method can get access to specific variables by getting reference to a
  274. currently running task from the scheduler, and then casting (void*)
  275. LTS pointer to the appropriate pointer type.
  276. </P>
  277. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  278. </P>
  279. <P CLASS="western" STYLE="margin-bottom: 0in">#define
  280. <B>_TASK_ROLLOVER_FIX</B></P>
  281. <P CLASS="western" STYLE="margin-bottom: 0in">…will compile
  282. TaskScheduler with support for millis() rollover approximately every
  283. 47 days. Only required if you plan for your sketch to run for
  284. extended amount of time (over 47 days).</P>
  285. <P CLASS="western" STYLE="margin-bottom: 0in">The problem here is
  286. when scheduler calculates next execution time for a task, a rollover
  287. may place next execution time “in the past” and cause task to be
  288. invoked incorrectly once every 47 days.
  289. </P>
  290. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  291. </P>
  292. <P CLASS="western" STYLE="margin-bottom: 0in"><B>NOTE: </B>above
  293. parameters are<B> DISABLED </B>by default, and need to be explicitly
  294. enabled by placing appropriate #define statements in front of the
  295. #include statement for the TaskScheduler header file.</P>
  296. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  297. </P>
  298. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  299. </P>
  300. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  301. </P>
  302. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  303. <FONT SIZE=4><B>API DOCUMENTATION:</B></FONT></P>
  304. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  305. <BR>
  306. </P>
  307. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  308. <FONT SIZE=4><B>TASKS:</B></FONT></P>
  309. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  310. <BR>
  311. </P>
  312. <P CLASS="western" STYLE="margin-bottom: 0in">CREATION:</P>
  313. <P CLASS="western" STYLE="margin-bottom: 0in"><B>Task();</B></P>
  314. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  315. </P>
  316. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Default
  317. constructor.
  318. </P>
  319. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Takes
  320. no parameters and creates a task that could be scheduled to run at
  321. every scheduling pass indefinitely, but does not have a callback
  322. method defined, so no code execution will actually take place.
  323. </P>
  324. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">All
  325. tasks are created <B>disabled</B> by default.</P>
  326. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  327. </P>
  328. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  329. <B>Task(unsigned long aInterval, long aIterations, void
  330. (*aCallback)(), Scheduler* aScheduler, bool aEnable, bool
  331. (*aOnEnable)(), void (*aOnDisable)())</B></P>
  332. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  333. </P>
  334. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Constructor
  335. with parameters.
  336. </P>
  337. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Creates
  338. a task that is scheduled to run every &lt;aInterval&gt; milliseconds,
  339. &lt;aIterations&gt; times, executing &lt;aCallback&gt; method on
  340. every pass.
  341. </P>
  342. <OL>
  343. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aInterval is in
  344. milliseconds (<B>default = 0)</B></P>
  345. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aIteration in
  346. number of times, -1 for indefinite execution (<B>default = -1)</B><BR><B>Note:
  347. </B>Tasks do not remember the number of iteration set initially.
  348. After the iterations are done, internal iteration counter is 0. If
  349. you need to perform another set of iterations, you need to set the
  350. number of iterations again. <BR><B>Note: </B>Tasks which performed
  351. all their iterations remain active.
  352. </P>
  353. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aCallback is a
  354. pointer to a void callback method without parameters (<B>default =
  355. NULL)</B></P>
  356. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aScheduler –
  357. <B>optional</B> reference to existing scheduler. If supplied (not
  358. NULL) this task will be appended to the task chain of the current
  359. scheduler). (<B>default = NULL)</B></P>
  360. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aEnable –
  361. <B>optional</B>. Value of <B>true </B>will create task enabled.
  362. (<B>default = false)</B></P>
  363. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aOnEnable is a
  364. pointer to a bool callback method without parameters, invoked when
  365. task is enabled. If OnEnable method returns <B>true</B>, task is
  366. enabled. If <B>OnEnable</B> method return <B>false</B>, task remains
  367. disabled (<B>default = NULL)</B></P>
  368. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aOnDisable is a
  369. pointer to a void callback method without parameters, invoked when
  370. task is disabled (<B>default = NULL)</B></P>
  371. </OL>
  372. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  373. </P>
  374. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">All
  375. tasks are created <B>disabled</B> by default (unless <B>aEnable</B> =
  376. true). You have to explicitly enable the task for execution.</P>
  377. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  378. </P>
  379. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:
  380. </B>OnEnable callback method is called immediately when task is
  381. enabled, which could be well ahead of the scheduled execution time of
  382. the task. Please bear that in mind – other tasks, hardware, serial
  383. interface may not even be initialized yet. It is always advisable to
  384. explicitly enable tasks with OnEnable methods after all
  385. initialization methods completed (e.g., at the end of <B>setup</B>()
  386. method)
  387. </P>
  388. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  389. </P>
  390. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enabled
  391. task is scheduled for execution as soon as the Scheduler's <B>execute</B>()
  392. methods gets control. In order to delay first run of the task, use
  393. <B>enableDelayed</B> or <B>delay</B> method (for enabled tasks)
  394. method.</P>
  395. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  396. </P>
  397. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  398. <B>Task(void (*aCallback)(), Scheduler* aScheduler, bool
  399. (*aOnEnable)(), void (*aOnDisable)())</B></P>
  400. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  401. </P>
  402. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  403. compiled with support for Status Request objects, this constructor
  404. creates a Task for activation on event (since such tasks must run
  405. <B>waitFor() </B>method, their <I>interval</I>, <I>iteration</I> and
  406. <I>enabled</I> status will be set by that method (<I>to 0, 1 and
  407. false</I> respectively).</P>
  408. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  409. </P>
  410. <P CLASS="western" STYLE="margin-bottom: 0in"><B>INFORMATION</B></P>
  411. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">The
  412. following 3 “getter” methods return task status
  413. (enabled/disabled), execution interval in milliseconds, number of
  414. <I><B>remaining</B></I> iterations.
  415. </P>
  416. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  417. </P>
  418. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>bool
  419. isEnabled() </B>
  420. </P>
  421. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>unsigned
  422. long getInterval()</B></P>
  423. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>long
  424. getIterations() </B>
  425. </P>
  426. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  427. </P>
  428. <P CLASS="western" STYLE="margin-bottom: 0in"><B>long getOverrun()</B></P>
  429. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  430. library is compiled with <FONT FACE="Courier New, monospace">_TASK_TIMECRITICAL</FONT>
  431. enabled, tasks are monitored for “long running” scenario. A “long
  432. running” task is a task that does not finish processing its
  433. callback methods quickly, and thus creates a situation for itself and
  434. other tasks where they don't run on a scheduled interval, but rather
  435. “catch up” and are behind. When task scheduler sets the next
  436. execution target time, it adds Task's execution interval to the
  437. previously scheduled execution time:</P>
  438. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <B>next
  439. execution time = previous execution time + task execution interval</B></P>
  440. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  441. </P>
  442. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  443. <B>next execution time</B> happens to be already in the past (<B>next
  444. execution time</B> &lt; <B>millis()</B>), then task is considered
  445. <I><B>overrun</B></I>. <B>GetOverrun</B> method returns number of
  446. milliseconds between next execution time and current time. If the
  447. <B>value is negative</B>, the task has overrun (cut into the) next
  448. execution interval by that many milliseconds.
  449. </P>
  450. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Positive
  451. value indicate number of milliseconds of slack this task has for
  452. execution purposes.
  453. </P>
  454. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  455. </P>
  456. <P CLASS="western" STYLE="margin-bottom: 0in"><B>unsigned long
  457. getRunCounter()</B></P>
  458. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  459. the number of the current run. “Current run” is the number of
  460. times a callback method has been invoked since the last time a task
  461. was enabled. <BR><BR>
  462. </P>
  463. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:
  464. </B>The <B>runCounter</B> value is incremented <I>before</I> callback
  465. method is invoked. If a task is checking the <B>runCounter</B> value
  466. within its callback method, then the first run value is 1.
  467. </P>
  468. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  469. task T1 is checking the <B>runCounter</B> value of another task (T2)
  470. , then value = 0 indicates that T2 has not been invoked yet, and
  471. value = 1 indicates that T2 has run once.
  472. </P>
  473. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  474. </P>
  475. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool
  476. isFirstIteration()</B></P>
  477. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Indicates
  478. whether current pass is (or will be) a first iteration of the task.
  479. </P>
  480. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool
  481. isLastIteration()</B></P>
  482. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">For
  483. tasks with a l<I>imited number of iterations only</I>, indicates
  484. whether current pass is the last iteration.
  485. </P>
  486. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  487. </P>
  488. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  489. <B>CONTROL:</B></P>
  490. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  491. <BR>
  492. </P>
  493. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void enable();</B></P>
  494. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  495. </P>
  496. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
  497. the task, and schedules it for immediate execution (without delay) at
  498. this or next scheduling pass depending on when the task was enabled.
  499. Scheduler will execute the next pass without any delay because there
  500. is a task which was enabled and requires execution.
  501. </P>
  502. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  503. </P>
  504. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:
  505. </B><SPAN STYLE="font-weight: normal">if task</SPAN> being enabled is
  506. not assigned to a scheduler and is not part of execution chain, then
  507. task <B>will not</B> be enabled.</P>
  508. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  509. </P>
  510. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:
  511. </B>enable() invokes task’s <B>OnEnable</B> method (if not NULL)
  512. <B>immediately</B>, which can prepare task for execution. <B>OnEnable</B>
  513. must return a value of <B>true</B> for task to be enabled. If
  514. <B>OnEnable</B> returns <B>false</B>, task remains disabled.
  515. <B>OnEnable</B> is invoked every time <B>enable</B> is called,
  516. regardless if task is already enabled or not.
  517. </P>
  518. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  519. </P>
  520. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:</B>
  521. in the event enable() method is called inside the OnEnable callback
  522. method (thus basically creating indefinte loop), TaskScheduler will
  523. only call OnEnable once (thus protecting the Task against OnEnable
  524. infinite loop).
  525. </P>
  526. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  527. </P>
  528. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool enableIfNot();</B></P>
  529. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  530. </P>
  531. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
  532. the task only if it was previously disabled. Returns previous enable
  533. state: <B>true</B> if task was already enabled, and <B>false</B> if
  534. task was disabled. Since <B>enable() </B>schedules Task for execution
  535. immediately, this method provides a way to activate tasks and
  536. schedule them for immediate execution only if they are not active
  537. already.</P>
  538. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  539. </P>
  540. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void delay();</B></P>
  541. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  542. </P>
  543. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Schedules
  544. the task for execution after a delay (aInterval), but does not change
  545. the enabled/disabled status of the task.
  546. </P>
  547. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  548. </P>
  549. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:
  550. </B>a delay of 0 (zero) will delay task for current execution
  551. interval. Use <B>forceNextIteration() </B>method to force execution
  552. of the task’s callback during immediate next scheduling pass.
  553. </P>
  554. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  555. </P>
  556. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void
  557. forceNextIteration();</B></P>
  558. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  559. </P>
  560. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Schedules
  561. the task for execution during immediate next scheduling pass.</P>
  562. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  563. </P>
  564. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  565. </B>Task’s schedule is adjusted to run from this moment in time.
  566. For instance: if a task was running every 10 seconds: 10, 20, 30, ..,
  567. calling <B>forceNextIteration </B>at 44th second of task execution
  568. will make subsequent schedule look like: 44, 54, 64, 74, ..</P>
  569. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  570. </P>
  571. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void
  572. enableDelayed();</B></P>
  573. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  574. </P>
  575. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
  576. the task, and schedules it for execution after task's current
  577. scheduling interval (aInterval).
  578. </P>
  579. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  580. </P>
  581. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void enableDelayed
  582. (unsigned long aDelay);</B></P>
  583. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  584. </P>
  585. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
  586. the task, and schedules it for execution after a specific delay
  587. (aDelay, which maybe different from aInterval).
  588. </P>
  589. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  590. </P>
  591. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void restart();</B></P>
  592. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  593. </P>
  594. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">For
  595. tasks with limited number of iterations only, <B>restart</B> method
  596. will re-enable the task, set the number of iterations back to last
  597. set value, and schedule task for execution as soon as possible.</P>
  598. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  599. </P>
  600. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void restartDelayed
  601. (unsigned long aDelay);</B></P>
  602. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  603. </P>
  604. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Same
  605. as <B>restart()</B> method, with the only difference being that Task
  606. is scheduled to run first iteration after a delay = <B>aDelay</B>
  607. milliseconds.</P>
  608. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  609. </P>
  610. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool disable();</B></P>
  611. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  612. </P>
  613. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Disables
  614. the task. Scheduler will not execute this task any longer, even if it
  615. remains in the chain. Task <B>can</B> be later re-enabled for
  616. execution.
  617. </P>
  618. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Return
  619. previous enabled state: <B>true</B> if task was enabled prior to
  620. calling disable, and <B>false</B> otherwise.</P>
  621. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  622. not NULL, task’s <B>OnDisable</B> method is invoked <B>immediately</B>.
  623. <B>OnDisable</B> is invoked only if task was in enabled state.
  624. Calling <B>disable</B> 3 times for instance will invoke <B>OnDisable</B>
  625. only once.</P>
  626. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  627. </P>
  628. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void set(unsigned
  629. long aInterval, long aIterations, void (*aCallback)() , bool
  630. (*aOnEnable)() , void (*aOnDisable)());</B></P>
  631. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  632. </P>
  633. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Allows
  634. dynamic control of task execution parameters in one method call.
  635. </P>
  636. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note</B><B>:
  637. </B>OnEnable and OnDisable parameters can be omitted. In that case
  638. they will be assigned to NULL and respective methods will no longer
  639. be called.
  640. </P>
  641. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  642. </P>
  643. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  644. Next five “setter” methods allow changes of individual task
  645. execution control parameters.
  646. </P>
  647. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
  648. setInterval (unsigned long aInterval) </B>
  649. </P>
  650. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
  651. setIterations (long aIterations) </B>
  652. </P>
  653. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
  654. setCallback (void (*aCallback)()) </B>
  655. </P>
  656. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
  657. setOnEnable (bool (*aCallback)()) </B>
  658. </P>
  659. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
  660. setOnDisable (void (*aCallback)()) </B>
  661. </P>
  662. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  663. </P>
  664. <P CLASS="western" STYLE="margin-bottom: 0in"><B>Note: </B>Next
  665. execution time calculation takes place <B>after</B> the callback
  666. method is called, so new interval will be used immediately by the
  667. scheduler. For the situations when one task is changing the interval
  668. parameter for the other, <B>setInterval</B> method calls <B>delay
  669. </B>explicitly to guarantee schedule change, however it <B>does not
  670. </B>enable the task if task is disabled.</P>
  671. <P CLASS="western" STYLE="margin-bottom: 0in"><B>Note: </B>Tasks that
  672. ran through all their allocated iterations are disabled.
  673. <B>SetIterations()</B> method <B>DOES NOT</B> enable the task. Either
  674. <B>enable</B> explicitly, or use <B>restart</B> methods.
  675. </P>
  676. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  677. </P>
  678. <P CLASS="western" STYLE="margin-bottom: 0in"><B>STATUS REQUEST
  679. METHODS:</B></P>
  680. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  681. <BR>
  682. </P>
  683. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  684. <B>void waitFor(StatusRequest* aStatusRequest, unsigned long
  685. aInterval = 0, long aIterations = 1)</B></P>
  686. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  687. <B>void waitForDelayed(StatusRequest* aStatusRequest, unsigned long
  688. aInterval = 0, long aIterations = 1)</B></P>
  689. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  690. <BR>
  691. </P>
  692. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  693. compiled with support for Status Requests, these methods make task
  694. wait for the completion of <B>aStatusRequest</B> event.
  695. </P>
  696. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">By
  697. default <B>waitFor() </B>sets tasks interval to <B>0 (zero)</B> for
  698. immediate execution when event happens, and also sets the number of
  699. <B>iterations to 1</B>. However, you can specify different interval
  700. and number of iterations.
  701. </P>
  702. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">By
  703. default <B>waitForDelayed() </B>sets tasks interval to a supplied
  704. value or (if omitted or zero) keeps the current interval, so delayed
  705. execution will take place when the event happens. It also sets the
  706. number of <B>iterations to 1</B><SPAN STYLE="font-weight: normal"> by
  707. default if not supplied</SPAN>.
  708. </P>
  709. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">When
  710. Status Request object completes, all tasks waiting on it are executed
  711. during next scheduling pass. Tasks waiting via <B>waitFor</B>()
  712. method are executed immediately. Tasks waiting via <B>waitForDelayed</B>()
  713. method are activated, but executed after current or supplied interval
  714. delay.
  715. </P>
  716. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  717. aStatusRequest</B> should be “activated” by calling <B>setWaiting()
  718. </B>method before making a task wait on it. Otherwise, the task will
  719. execute immediately.
  720. </P>
  721. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">The
  722. sequence of events to use Status Request object is as follows:</P>
  723. <OL>
  724. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Create a status
  725. request object</P>
  726. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Activate status
  727. request object (calling its <B>setWaiting</B>() method)</P>
  728. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Set up tasks to
  729. wait of the event completion</P>
  730. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Signal completion
  731. of event(s)</P>
  732. </OL>
  733. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  734. </P>
  735. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  736. </P>
  737. <P CLASS="western" STYLE="margin-bottom: 0in"><B>StatusRequest*
  738. getStatusRequest()</B></P>
  739. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  740. a StatusReqeust object this Task was waiting on.
  741. </P>
  742. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  743. </P>
  744. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  745. </P>
  746. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  747. <B>TASK ID, CONTROL POINTS METHODS:</B></P>
  748. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  749. <BR>
  750. </P>
  751. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  752. <B>void setId(unsigned int aID);</B></P>
  753. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  754. <BR>
  755. </P>
  756. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  757. compiled with support for Task IDs, this method will set the task ID
  758. explicitly.
  759. </P>
  760. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Calling
  761. this method is not necessary as task IDs are assigned automatically
  762. during task creation: 1, 2, 3, …</P>
  763. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  764. </P>
  765. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  766. <B>unsigned int getId();</B></P>
  767. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  768. <BR>
  769. </P>
  770. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  771. compiled with support for Task IDs, this method return current task’s
  772. ID.</P>
  773. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  774. </P>
  775. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  776. <B>void setControlPoint (unsigned int aPoint);</B></P>
  777. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  778. <BR>
  779. </P>
  780. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  781. compiled with support for Task IDs, this method will set a control
  782. point in the task’s code. Control points are similar to “try…catch”
  783. blocks, with control point ID specifying where in the code the “try”
  784. part started, and a mechanism like watchdog timer providing the
  785. “catch” functionality.</P>
  786. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  787. </P>
  788. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  789. <B>unsigned int getControlPoint()</B></P>
  790. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  791. <BR>
  792. </P>
  793. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  794. If compiled with support for Task IDs, this method will return
  795. currently set control point for this task.</P>
  796. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  797. <BR>
  798. </P>
  799. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  800. <BR>
  801. </P>
  802. <P CLASS="western" STYLE="margin-bottom: 0in"><B>LOCAL TASK STORAGE
  803. METHODS:</B></P>
  804. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  805. <BR>
  806. </P>
  807. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  808. <B>void setLtsPointer(void *aPtr);</B></P>
  809. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  810. <BR>
  811. </P>
  812. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  813. compiled with support for LTS, this method will set the task's local
  814. storage pointer.</P>
  815. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  816. </P>
  817. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  818. <B>void *getLtsPointer();</B></P>
  819. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  820. <BR>
  821. </P>
  822. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  823. compiled with support for LTS, this method will return reference to
  824. the task's local storage.</P>
  825. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  826. </B>the value returned has type (void *), and needs to be re-cast
  827. into appropriate pointer type. Please refer to example sketches for
  828. implementation options.
  829. </P>
  830. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  831. </P>
  832. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  833. </P>
  834. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  835. <BR>
  836. </P>
  837. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-inside: avoid; page-break-after: avoid">
  838. <FONT SIZE=4><B>TASK SCHEDULER:</B></FONT></P>
  839. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-inside: avoid; page-break-after: avoid">
  840. <BR>
  841. </P>
  842. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-inside: avoid; page-break-after: avoid">
  843. <B>CREATION:</B></P>
  844. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-inside: avoid; page-break-after: avoid">
  845. <BR>
  846. </P>
  847. <P CLASS="western" STYLE="margin-bottom: 0in"><B>Scheduler()</B><BR><BR>
  848. </P>
  849. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Default
  850. constructor.
  851. </P>
  852. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Takes
  853. no parameters. Creates task scheduler with default parameters and an
  854. empty task queue.
  855. </P>
  856. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  857. </P>
  858. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void init()</B></P>
  859. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  860. </P>
  861. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Initializes
  862. the task queue and scheduler parameters, Executed as part of
  863. constructor, so don't need to be explicitly called after creation.</P>
  864. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  865. </B>be default (if compiled with <FONT FACE="Courier New, monospace">_TASK_TIMECRITICAL</FONT>
  866. enabled) scheduler is allowed to put processor to IDLE sleep mode. If
  867. this behavior was changed via <B>allowSleep()</B> method, <B>inti()
  868. </B>will <B>NOT</B> reset allow sleep particular parameter.
  869. </P>
  870. <P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void
  871. addTask(Task&amp; aTask)</B></P>
  872. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  873. </P>
  874. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Adds
  875. task aTask to the execution queue (or chain) of tasks by appending it
  876. to the end of the chain. If two tasks are scheduled for execution,
  877. the sequence will match the order in which tasks were appended to the
  878. chain. However, in reality, due to different timing of task
  879. execution, the actual order may be different.
  880. </P>
  881. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  882. </B>Currently, changing the execution sequence in a chain dynamically
  883. is not supported.
  884. </P>
  885. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  886. you need to reorder the chain sequence – initialize the scheduler
  887. and re-add the tasks in a different order.
  888. </P>
  889. <P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void
  890. deleteTask(Task&amp; aTask)</B></P>
  891. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  892. </P>
  893. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Deletes
  894. task aTask from the execution chain. The chain of remaining tasks is
  895. linked together (i.e</P>
  896. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">if
  897. original task chain is 1 → 2 → 3 → 4, deleting 3 will result in
  898. 1 → 2 → 4).</P>
  899. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  900. </B>it is not required to delete a task from the chain. A disabled
  901. task will not be executed anyway, but you save a few microseconds per
  902. scheduling pass by deleting it, since it is not even considered for
  903. execution.
  904. </P>
  905. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">An
  906. example of proper use of this method would be running some sort of
  907. <B>initialize</B> task in the chain, and then deleting it from the
  908. chain since it only needs to run once.
  909. </P>
  910. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  911. </P>
  912. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void allowSleep(bool
  913. aState) </B>
  914. </P>
  915. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  916. </P>
  917. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Available
  918. in API only if compiled with <FONT FACE="Courier New, monospace">_TASK_TIMECRITICAL</FONT>
  919. enabled. Controls whether scheduler is allowed (<B>aState =true</B>),
  920. or not (<B>aState =false</B>) to put processor into IDLE sleep mode
  921. in case not tasks are scheduled to run.</P>
  922. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">The
  923. <B>default</B> behavior of scheduler upon creation is to allow sleep
  924. mode.
  925. </P>
  926. <P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void enableAll()</B></P>
  927. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void disableAll()</B></P>
  928. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  929. </P>
  930. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">enables
  931. and disables (respectively) all tasks in the chain. Convenient if
  932. your need to enable/disable majority of the tasks (i.e. disable all
  933. and then enable one).
  934. </P>
  935. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  936. <BR><B>Task&amp; currentTask()<BR></B><BR>
  937. </P>
  938. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  939. reference to the task, currently executing via <B>execute()</B> loop
  940. <B>OR </B>for OnEnable and OnDisable methods, reference to the task
  941. being enabled or disabled.
  942. </P>
  943. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">This
  944. distinction is important because one task can activate the other, and
  945. OnEnable should be referring to the task being enabled, not being
  946. executed.
  947. </P>
  948. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Could
  949. be used by callback methods to identify which Task actually invoked
  950. this callback method.</P>
  951. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  952. </P>
  953. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  954. <B>void* currentLts()<BR></B><BR>
  955. </P>
  956. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  957. pointer to Local Task Storage of the task, currently executing via
  958. <B>execute()</B> loop <B>OR </B>for OnEnable and OnDisable methods,
  959. task being enabled or disabled.
  960. </P>
  961. <P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void execute()</B></P>
  962. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  963. </P>
  964. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Executes
  965. one scheduling pass, including end-of-pass sleep. This method is
  966. typically placed inside the <B>loop()</B> method of the sketch. Since
  967. <B>execute</B> exits after every pass, you can put additional
  968. statements after <B>execute</B> inside the <B>loop()</B>
  969. </P>
  970. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  971. </P>
  972. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool isOverrun()</B></P>
  973. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  974. library is compiled with <FONT FACE="Courier New, monospace">_TASK_TIMECRITICAL</FONT>
  975. enabled, this method returns <B>true</B><SPAN STYLE="font-weight: normal">
  976. if currently invoked task has overrun its scheduled start time when
  977. it was invoked. Returns </SPAN><B>false</B><SPAN STYLE="font-weight: normal">
  978. if task has been invoked according to schedule.</SPAN></P>
  979. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  980. </P>
  981. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  982. </P>
  983. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  984. <B>STATUS REQUEST:</B></P>
  985. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  986. <BR>
  987. </P>
  988. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  989. <B>CREATION:</B></P>
  990. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  991. <BR>
  992. </P>
  993. <P CLASS="western" STYLE="margin-bottom: 0in"><B>StatusRequest()</B><BR><BR>
  994. </P>
  995. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Default
  996. constructor.
  997. </P>
  998. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Takes
  999. no parameters. Creates Status Request object, which is assigned a
  1000. status of “completed” on creation.
  1001. </P>
  1002. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1003. </P>
  1004. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void
  1005. setWaiting(unsigned int aCount = 1)</B></P>
  1006. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Activates
  1007. Status Request object. By default each object is set to wait on one
  1008. event only, however, if <B>aCount</B> is supplied, Status Request can
  1009. wait on multiple events. For instance, <B>setWaiting(3)</B> will wait
  1010. on three signals. An example could be waiting for completion of
  1011. measurements from 3 sensors.
  1012. </P>
  1013. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1014. </P>
  1015. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool signal(int
  1016. aStatus)</B></P>
  1017. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Signals
  1018. completion of the event to the Status Request object, and passes a
  1019. completion code, which could be interrogated later.
  1020. </P>
  1021. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  1022. </B> passing a <B>negative</B> status code to the status request
  1023. object is considered reporting an error condition, and will complete
  1024. the status request regardless of how many outstanding signals it is
  1025. still waiting for.
  1026. </P>
  1027. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note</B>:
  1028. only the latest status code is kept.</P>
  1029. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1030. </P>
  1031. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool signalComplete
  1032. (int aStatus)</B></P>
  1033. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Signals
  1034. completion of <B>ALL</B> events to the Status Request object, and
  1035. passes a completion code, which could be interrogated later. The
  1036. status request completes regardless of how many events it is still
  1037. waiting on.
  1038. </P>
  1039. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1040. </P>
  1041. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool pending() </B>
  1042. </P>
  1043. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  1044. <B>true</B> if status request is still waiting for event or events to
  1045. happen.</P>
  1046. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1047. </P>
  1048. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool completed () </B>
  1049. </P>
  1050. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  1051. <B>true</B> if status has completed.</P>
  1052. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1053. </P>
  1054. <P CLASS="western" STYLE="margin-bottom: 0in"><B>int getStatus()</B></P>
  1055. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  1056. the status code passed to the status request object by the <B>signal()
  1057. </B>and <B>signalComplete() </B>methods.
  1058. </P>
  1059. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Any
  1060. <B>positive</B> number is considered a successful completion status.</P>
  1061. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">A
  1062. 0 (zero) is considered a default successful completion status.</P>
  1063. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Any
  1064. <B>negative</B> number is considered an error code and unsuccessful
  1065. completion of a request.</P>
  1066. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1067. </P>
  1068. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1069. </P>
  1070. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1071. </P>
  1072. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1073. <B>CONSTANTS:</B></P>
  1074. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1075. </P>
  1076. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1077. <B>TASK_SECOND (1000)</B></P>
  1078. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1079. interval of 1 second</P>
  1080. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  1081. </P>
  1082. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1083. <B>TASK_MINUTE (60000)</B></P>
  1084. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1085. interval of 1 minute</P>
  1086. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  1087. </P>
  1088. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1089. <B>TASK_HOUR (3600000)</B></P>
  1090. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1091. interval of 1 hour</P>
  1092. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1093. <B>TASK_FOREVER (-1)</B></P>
  1094. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1095. number of iterations for infinite number of iterations</P>
  1096. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  1097. </P>
  1098. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1099. <B>TASK_ONCE (1)</B></P>
  1100. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1101. single iteration</P>
  1102. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  1103. </P>
  1104. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1105. <B>TASK_IMMEDIATE (0)</B></P>
  1106. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1107. interval for immediate execution</P>
  1108. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1109. </P>
  1110. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1111. </P>
  1112. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-before: always; page-break-after: avoid">
  1113. <B>IMPLEMENTATION SCENARIOS AND IDEAS:</B></P>
  1114. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1115. </P>
  1116. <OL>
  1117. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><B>EVENT DRIVEN
  1118. PROGRAMMING</B></P>
  1119. </OL>
  1120. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1121. </P>
  1122. <P CLASS="western" STYLE="margin-bottom: 0in">Each of the processes
  1123. of your application becomes a separate and distinct programming area,
  1124. which may or may not interact and control each other.
  1125. </P>
  1126. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1127. </P>
  1128. <P CLASS="western" STYLE="margin-bottom: 0in">Example:
  1129. </P>
  1130. <P CLASS="western" STYLE="margin-bottom: 0in">In a plant watering
  1131. system you need to measure soil humidity, control pump and display
  1132. the results</P>
  1133. <P CLASS="western" STYLE="margin-bottom: 0in">Each of the areas
  1134. becomes a task:</P>
  1135. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1136. </P>
  1137. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1138. <B>tMeasure</B> (TMEASURE_INTERVAL*SECOND, TASK_FOREVER,
  1139. &amp;measureCallback);<BR>Task <B>tWater</B>
  1140. (TWATER_INTERVAL*SECOND, RETRIES, &amp;waterCallback);<BR>Task
  1141. <B>tDisplay</B> (TDISPLAY_INTERVAL*SECOND, TASK_FOREVER,
  1142. &amp;displayCallback); <BR></FONT></FONT><BR>
  1143. </P>
  1144. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler
  1145. </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>
  1146. </P>
  1147. <P CLASS="western" STYLE="margin-bottom: 0in">Further, once you turn
  1148. on the pump, you keep it running for TWATER_INTERVAL interval and
  1149. then turn it off. Turning off a pump is also a task which only needs
  1150. to run once for every time the pump is turned on:</P>
  1151. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1152. </P>
  1153. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1154. </FONT></FONT><FONT FACE="Courier New, monospace"><FONT SIZE=2><B>tWaterOff</B></FONT></FONT><FONT FACE="Courier New, monospace"><FONT SIZE=2>
  1155. (WATERTIME*SECOND, TASK_ONCE,</FONT></FONT><FONT FACE="Courier New, monospace">
  1156. &amp;waterOffCallback);</FONT><BR><BR>
  1157. </P>
  1158. <P CLASS="western" STYLE="margin-bottom: 0in">Example of the callback
  1159. method:</P>
  1160. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1161. </P>
  1162. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1163. waterOffCallback() {<BR> motorOff();<BR>
  1164. tWater.enableDelayed();<BR>}</FONT></FONT></P>
  1165. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1166. </P>
  1167. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>or</FONT></FONT></P>
  1168. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1169. </P>
  1170. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1171. waterCallback() {<BR> if (tWater.getIterations()) {</FONT></FONT></P>
  1172. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1173. If this is not the last iteration = turn the pump on<BR>
  1174. motorOn();<BR> tWaterOff.set(parameters.watertime * TASK_SECOND,
  1175. TASK_ONCE, &amp;waterOffCallback);<BR>
  1176. tWaterOff.enableDelayed();<BR> return;<BR> }</FONT></FONT></P>
  1177. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1178. We could not reach target humidity – something is wrong<BR>
  1179. motorOff;<BR> taskManager.disableAll();<BR> tError.enable();<BR>}</FONT></FONT></P>
  1180. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1181. </P>
  1182. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Your
  1183. sample <B>setup</B>() and <B>loop</B>() (partially) are as follows. </FONT>
  1184. </P>
  1185. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>Note:
  1186. </B>please note that tWater is <B>not</B> activated during setup().
  1187. It is activated by tMeasure callback once the watering conditions are
  1188. met. </FONT>
  1189. </P>
  1190. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1191. </P>
  1192. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT SIZE=2><FONT FACE="Times New Roman, serif"> void
  1193. </FONT><FONT FACE="Courier New, monospace">setup()</FONT></FONT></P>
  1194. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>
  1195. ...</FONT></FONT></P>
  1196. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1197. <FONT SIZE=2><FONT FACE="Courier New, monospace">tWater.setIterations(parameters.retries);<BR>
  1198. tWaterOff.setInterval(parameters.watertime * SECOND);<BR><BR><BR>
  1199. taskManager.init();<BR> taskManager.addTask(tMeasure);<BR>
  1200. taskManager.addTask(tDisplay);<BR> taskManager.addTask(tWater);<BR>
  1201. taskManager.addTask(tWaterOff);<BR> <BR> tMeasure.enable();<BR>
  1202. tDisplay.enable();<BR><BR> currentHumidity =
  1203. measureHumidity();<BR>}<BR><BR><BR>void loop ()<BR>{<BR>
  1204. taskManager.execute();<BR>}</FONT><FONT FACE="Times New Roman, serif"><BR></FONT></FONT><BR>
  1205. </P>
  1206. <OL START=2>
  1207. <LI><P CLASS="western" STYLE="margin-bottom: 0in">“<FONT FACE="Times New Roman, serif"><B>NATIVE”
  1208. SUPPORT FOR FINITE STATE MACHINE</B></FONT></P>
  1209. </OL>
  1210. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1211. </P>
  1212. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Define
  1213. “states” as callback method or methods. Each callback method
  1214. executes activities specific to a “state” and then “transitions”
  1215. to the next state by assigning next callback method to the task. </FONT>
  1216. </P>
  1217. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Transition
  1218. from one state to the next is achieved by setting next callback
  1219. method at the end of preceding one. </FONT>
  1220. </P>
  1221. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>Note:
  1222. </B>do not call the next callback method explicitly. Yield to the
  1223. scheduler, and let the scheduler take care of next iteration during
  1224. the next pass. (Thus giving other tasks change to run their callback
  1225. methods). </FONT>
  1226. </P>
  1227. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1228. </P>
  1229. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Example:
  1230. Blinking LED 2 times a second could be achieved this way</FONT></P>
  1231. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1232. </P>
  1233. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler
  1234. ts;</FONT></FONT></P>
  1235. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1236. tLedBlinker (500, TASK_FOREVER, &amp;ledOnCallback, &amp;ts, true);</FONT></FONT></P>
  1237. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1238. </P>
  1239. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1240. ledOnCallback() {</FONT></FONT></P>
  1241. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> turnLedOn();</FONT></FONT></P>
  1242. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> tLedBlinker.setCallback(&amp;ledOffCallback);</FONT></FONT></P>
  1243. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1244. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1245. </P>
  1246. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1247. ledOffCallback() {</FONT></FONT></P>
  1248. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> turnLedOff();</FONT></FONT></P>
  1249. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> tLedBlinker.setCallback(&amp;ledOnCallback);</FONT></FONT></P>
  1250. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1251. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1252. </P>
  1253. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>setup()
  1254. {</FONT></FONT></P>
  1255. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1256. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1257. </P>
  1258. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>loop
  1259. () {</FONT></FONT></P>
  1260. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> ts.execute();</FONT></FONT></P>
  1261. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT><FONT FACE="Times New Roman, serif"><BR></FONT><BR>
  1262. </P>
  1263. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Obviously
  1264. the example is simple, but gives the idea of how the tasks could be
  1265. used to go through states.</FONT></P>
  1266. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1267. </P>
  1268. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1269. </P>
  1270. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1271. </P>
  1272. <OL START=3>
  1273. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>MULTIPLE
  1274. POSSIBLE CALLBACKS FOR TASK</B></FONT></P>
  1275. </OL>
  1276. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1277. </P>
  1278. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">There
  1279. may be a need to select an option for callback method based on
  1280. certain criteria, or randomly. </FONT>
  1281. </P>
  1282. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">You
  1283. can achieve that by defining an array of callback method pointers and
  1284. selecting one based on the criteria you need. </FONT>
  1285. </P>
  1286. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Example:
  1287. when a robot detects an obstacle, it may go left, right backwards,
  1288. etc. Each of the “directions” or “behaviors” are represented
  1289. by a different callback methods. </FONT>
  1290. </P>
  1291. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1292. </P>
  1293. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Another
  1294. example of using multiple callbacks:</FONT></P>
  1295. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">You
  1296. may need to “initialize” variables for a particular task.</FONT></P>
  1297. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">In
  1298. this case, define a tasks with two callbacks:</FONT></P>
  1299. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1300. </P>
  1301. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1302. <B>tWork</B> (T_INTERVAL, TASK_FOREVER, &amp;workCallbackInit);</FONT></FONT></P>
  1303. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">…</P>
  1304. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1305. </P>
  1306. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1307. workCallbackInit() {</FONT></FONT></P>
  1308. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
  1309. do your initializationstuff here</FONT></FONT></P>
  1310. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> </FONT></FONT></P>
  1311. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
  1312. finally assigne the main callback method </FONT></FONT>
  1313. </P>
  1314. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> tWork.setCallback(&amp;workCallback);</FONT></FONT></P>
  1315. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1316. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1317. </P>
  1318. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1319. workCallback() {</FONT></FONT></P>
  1320. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
  1321. main callback method</FONT></FONT></P>
  1322. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> …</FONT></FONT></P>
  1323. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1324. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1325. </P>
  1326. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">The
  1327. task will initialize during first execution pass and switch to
  1328. “regular” callback execution starting with second pass. There is
  1329. a delay between first and second passes of the task (scheduling
  1330. period, if defined). In order to execute the second pass immediately
  1331. after initialization first pass, change the above code like this:</FONT></P>
  1332. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1333. </P>
  1334. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1335. workCallbackInit() {</FONT></FONT></P>
  1336. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
  1337. do your initializationstuff here</FONT></FONT></P>
  1338. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> </FONT></FONT></P>
  1339. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
  1340. finally assigne the main callback method </FONT></FONT>
  1341. </P>
  1342. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> tWork.setCallback(&amp;workCallback);</FONT></FONT></P>
  1343. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> tWork.enable();</FONT></FONT></P>
  1344. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1345. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1346. </P>
  1347. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">The
  1348. task will run initialization first, then immediately second pass, and
  1349. then switch to processing at regular intervals starting with a third
  1350. pass. </FONT>
  1351. </P>
  1352. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1353. </P>
  1354. <OL START=3>
  1355. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>INTERRUP-DRIVEN
  1356. EXECUTION SUPPORT </B></FONT>
  1357. </P>
  1358. </OL>
  1359. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1360. </P>
  1361. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">In
  1362. case of interrupt-driven program flow, tasks could be scheduled to
  1363. run once to request asynchronous execution (request), and then
  1364. re-enabled (restarted) again with a different callback method to
  1365. process the results. </FONT>
  1366. </P>
  1367. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1368. </P>
  1369. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>Example</B>:
  1370. event driven distance calculation for ultrasonic pulses. EchoPin #6
  1371. triggers pin change interrupts on rising and falling edges to
  1372. determine the length of ultrasonic pulse.</FONT></P>
  1373. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1374. </P>
  1375. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#include
  1376. &lt;DirectIO.h&gt;<BR>#include &lt;TaskScheduler.h&gt;<BR>#include
  1377. &lt;PinChangeInt.h&gt;<BR><BR><BR>#define TRIGGERPIN 5<BR>#define
  1378. ECHOPIN 6<BR><BR>Output&lt;TRIGGERPIN&gt;
  1379. pTrigger;<BR>Input&lt;ECHOPIN&gt; pEcho;<BR><BR>Scheduler
  1380. r;<BR><BR>Task tMeasure(TASK_SECOND, TASK_FOREVER, &amp;measureCallback,
  1381. &amp;r, true);<BR>Task tDisplay(TASK_SECOND, TASK_FOREVER,
  1382. &amp;displayCallback, &amp;r, true);<BR>Task tPing(TASK_IMMEDIATE,
  1383. TASK_ONCE, &amp;pingCalcCallback, &amp;r, false);<BR><BR><BR>volatile
  1384. bool pulseBusy = false;<BR>volatile bool pulseTimeout =
  1385. false;<BR>volatile unsigned long pulseStart = 0;<BR>volatile unsigned
  1386. long pulseStop = 0;<BR>volatile unsigned long pingDistance =
  1387. 0;<BR><BR><BR>void pingTrigger(unsigned long aTimeout) {<BR> if
  1388. (pulseBusy) return; // do not trigger if in the middle of a pulse<BR>
  1389. if (pEcho == HIGH) return; // do not trigger if ECHO pin is high<BR>
  1390. <BR> pulseBusy = true;<BR> pulseTimeout = false;</FONT></FONT></P>
  1391. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2><BR>
  1392. pTrigger = LOW;<BR> delayMicroseconds(4);<BR> pTrigger = HIGH;<BR><BR>
  1393. tPing.setInterval (aTimeout);<BR><BR> delayMicroseconds(10);<BR>
  1394. pTrigger = LOW; <BR></FONT></FONT><BR>
  1395. </P>
  1396. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1397. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tPing.restartDelayed();
  1398. // timeout countdown starts now<BR></FONT></FONT><BR>
  1399. </P>
  1400. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1401. will start the pulse clock on the rising edge of ECHO pin</FONT></FONT></P>
  1402. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1403. <FONT FACE="Courier New, monospace"><FONT SIZE=2>PCintPort::attachInterrupt(ECHOPIN,
  1404. &amp;pingStartClock, RISING); <BR>}<BR><BR></FONT></FONT><BR>
  1405. </P>
  1406. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1407. Start clock on the <B>rising</B> edge of the ultrasonic pulse<BR>void
  1408. pingStartClock() {<BR> pulseStart = micros();<BR>
  1409. PCintPort::detachInterrupt(ECHOPIN); // not sure this is necessary<BR>
  1410. PCintPort::attachInterrupt(ECHOPIN, &amp;pingStopClock, FALLING); <BR>
  1411. tPing.restartDelayed();<BR>}<BR><BR>// Stop clock on the <B>falling</B>
  1412. edge of the ultrasonic pulse<BR>void pingStopClock() {<BR> pulseStop
  1413. = micros();<BR> PcintPort::detachInterrupt(ECHOPIN);</FONT></FONT></P>
  1414. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1415. <FONT FACE="Courier New, monospace"><FONT SIZE=2>pingDistance =
  1416. pulseStop - pulseStart;<BR> pulseBusy = false;<BR> tPing.disable();
  1417. // disable timeout<BR>}<BR></FONT></FONT><BR>
  1418. </P>
  1419. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1420. Stop clock because of the timeout – the wave did not return<BR>void
  1421. pingCalcCallback() {<BR> if (pulseBusy) {<BR> pingStopClock();<BR>
  1422. }<BR> pulseTimeout = true;<BR>}<BR><BR><BR><BR>// Initial measure
  1423. callback sets the trigger<BR>void measureCallback() {<BR> if
  1424. (pulseBusy) { // already measuring, try again<BR>
  1425. tMeasure.enable();<BR> return;<BR> }<BR> pingTrigger(30); // 30
  1426. milliseconds or max range of ~5.1 meters<BR>
  1427. tMeasure.setCallback(&amp;measureCallbackWait);<BR>}<BR></FONT></FONT><BR>
  1428. </P>
  1429. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1430. Wait for the measurement to <BR>void measureCallbackWait() {<BR> if
  1431. (pulseBusy) return;<BR> tMeasure.setCallback(&amp;measureCallback);
  1432. <BR>}<BR><BR><BR>bool state = true;<BR><BR>void displayCallback() {<BR>
  1433. char d[256];<BR> <BR> unsigned long cm = pingDistance * 17 / 100;
  1434. // cm<BR> <BR> snprintf(d, 256, &quot;pulseStart =
  1435. %8lu\tpulseStop=%8lu\tdistance, cm=%8lu&quot;, pulseStart, pulseStop,
  1436. cm);<BR> Serial.println(d);<BR> <BR>}<BR><BR>void setup() {<BR> //
  1437. put your setup code here, to run once:<BR> <BR>
  1438. Serial.begin(115200);<BR> <BR><BR> pTrigger = LOW;<BR> pEcho =
  1439. LOW;<BR> <BR>}<BR><BR>void loop() {<BR> // put your main code here,
  1440. to run repeatedly:<BR> r.execute();<BR>}<BR></FONT></FONT><BR>
  1441. </P>
  1442. <OL START=3>
  1443. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>USING
  1444. ONENABLE AND ONDISBALE METHODS </B></FONT>
  1445. </P>
  1446. </OL>
  1447. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1448. </P>
  1449. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Consider
  1450. a task to flash onboard LED for 5 seconds with random frequency. Task
  1451. should be repeated every 30 seconds indefinitely. Since frequency is
  1452. random, there are two challenges:</FONT></P>
  1453. <OL START=3>
  1454. <OL>
  1455. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">We
  1456. need to make sure LED is turned OFF at the last iteration</FONT></P>
  1457. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">We
  1458. need to calculate random frequency every time</FONT></P>
  1459. </OL>
  1460. </OL>
  1461. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1462. </P>
  1463. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Below
  1464. is the implementation using TaskScheduler </FONT>
  1465. </P>
  1466. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1467. </P>
  1468. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2><BR></FONT></FONT><BR>
  1469. </P>
  1470. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
  1471. _TASK_SLEEP_ON_IDLE_RUN</FONT></FONT></P>
  1472. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#include
  1473. &lt;TaskScheduler.h&gt;</FONT></FONT></P>
  1474. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1475. </P>
  1476. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
  1477. LEDPIN 13</FONT></FONT></P>
  1478. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1479. </P>
  1480. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1481. </P>
  1482. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler
  1483. ts;</FONT></FONT></P>
  1484. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1485. </P>
  1486. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1487. tWrapper(30000, TASK_FOREVER, &amp;WrapperCallback, &amp;ts, true);</FONT></FONT></P>
  1488. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1489. tBlink(5000, TASK_ONCE, NULL, &amp;ts, false, &amp;BlinkOnEnable,
  1490. &amp;BlinkOnDisable);</FONT></FONT></P>
  1491. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1492. tLED(TASK_IMMEDIATE, TASK_FOREVER, NULL, &amp;ts, false, NULL,
  1493. &amp;LEDOff);</FONT></FONT></P>
  1494. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1495. </P>
  1496. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1497. WrapperCallback() {</FONT></FONT></P>
  1498. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1499. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;In
  1500. WrapperCallback&quot;);</FONT></FONT></P>
  1501. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1502. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tBlink.restartDelayed();
  1503. // LED blinking is initiated </FONT></FONT>
  1504. </P>
  1505. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1506. <FONT FACE="Courier New, monospace"><FONT SIZE=2>//every
  1507. 30 seconds for 5 seconds</FONT></FONT></P>
  1508. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1509. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1510. </P>
  1511. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1512. </P>
  1513. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1514. Upon being enabled, tBlink will define the parameters</FONT></FONT></P>
  1515. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1516. and enable LED blinking task, which actually controls</FONT></FONT></P>
  1517. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1518. the hardware (LED in this example)</FONT></FONT></P>
  1519. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
  1520. BlinkOnEnable() {</FONT></FONT></P>
  1521. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1522. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;In
  1523. BlinkOnEnable&quot;);</FONT></FONT></P>
  1524. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1525. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setInterval(
  1526. 500 + random(501) );</FONT></FONT></P>
  1527. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1528. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setCallback(
  1529. &amp;LEDOn);</FONT></FONT></P>
  1530. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1531. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.enable();</FONT></FONT></P>
  1532. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1533. </P>
  1534. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1535. <FONT FACE="Courier New, monospace"><FONT SIZE=2>return true; //
  1536. Task should be enabled</FONT></FONT></P>
  1537. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1538. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1539. </P>
  1540. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1541. tBlink does not really need a callback method</FONT></FONT></P>
  1542. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1543. since it just waits for 5 seconds for the first </FONT></FONT>
  1544. </P>
  1545. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1546. and only iteration to occur. Once the iteration</FONT></FONT></P>
  1547. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1548. takes place, tBlink is disabled by the Scheduler, </FONT></FONT>
  1549. </P>
  1550. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1551. thus executing its OnDisable method below.</FONT></FONT></P>
  1552. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1553. </P>
  1554. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1555. BlinkOnDisable() {</FONT></FONT></P>
  1556. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1557. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;In
  1558. BlinkOnDisable&quot;);</FONT></FONT></P>
  1559. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1560. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.disable();</FONT></FONT></P>
  1561. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1562. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1563. </P>
  1564. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1565. LEDOn () {</FONT></FONT></P>
  1566. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1567. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;In
  1568. LEDOn&quot;);</FONT></FONT></P>
  1569. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1570. <FONT FACE="Courier New, monospace"><FONT SIZE=2>digitalWrite(LEDPIN,
  1571. HIGH);</FONT></FONT></P>
  1572. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1573. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setCallback(
  1574. &amp;LEDOff);</FONT></FONT></P>
  1575. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1576. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1577. </P>
  1578. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1579. LEDOff () {</FONT></FONT></P>
  1580. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1581. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;In
  1582. LEDOff&quot;);</FONT></FONT></P>
  1583. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1584. <FONT FACE="Courier New, monospace"><FONT SIZE=2>digitalWrite(LEDPIN,
  1585. LOW);</FONT></FONT></P>
  1586. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1587. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setCallback(
  1588. &amp;LEDOn);</FONT></FONT></P>
  1589. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1590. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1591. </P>
  1592. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1593. Note that LEDOff method serves as OnDisable method</FONT></FONT></P>
  1594. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1595. to make sure the LED is turned off when the tBlink</FONT></FONT></P>
  1596. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1597. task finishes (or disabled ahead of time)</FONT></FONT></P>
  1598. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1599. </P>
  1600. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1601. setup() {</FONT></FONT></P>
  1602. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1603. </P>
  1604. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1605. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.begin(115200);
  1606. </FONT></FONT>
  1607. </P>
  1608. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1609. <FONT FACE="Courier New, monospace"><FONT SIZE=2>pinMode(LEDPIN,
  1610. OUTPUT); </FONT></FONT>
  1611. </P>
  1612. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1613. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1614. </P>
  1615. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1616. loop() {</FONT></FONT></P>
  1617. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1618. <FONT FACE="Courier New, monospace"><FONT SIZE=2>// put your main
  1619. code here, to run repeatedly:</FONT></FONT></P>
  1620. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1621. </P>
  1622. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1623. <FONT FACE="Courier New, monospace"><FONT SIZE=2>ts.execute();</FONT></FONT></P>
  1624. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1625. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1626. </P>
  1627. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1628. </P>
  1629. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1630. </P>
  1631. <OL START=3>
  1632. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>USING
  1633. STATUS REQUEST OBJECTS </B></FONT>
  1634. </P>
  1635. </OL>
  1636. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1637. </P>
  1638. <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
  1639. test emulates querying 3 sensors once every 10 seconds, each could
  1640. respond with a different delay (ultrasonic sensors for instance) and
  1641. printing a min value of the three when all three have reported their
  1642. values.</FONT></FONT></P>
  1643. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1644. </P>
  1645. <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
  1646. overall timeout of 1 second is setup as well.</FONT></FONT></P>
  1647. <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
  1648. error message needs to be printed if a timeout occurred instead of a
  1649. value.</FONT></FONT></P>
  1650. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1651. </P>
  1652. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1653. </P>
  1654. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
  1655. _TASK_SLEEP_ON_IDLE_RUN</FONT></FONT></P>
  1656. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
  1657. _TASK_STATUS_REQUEST</FONT></FONT></P>
  1658. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1659. </P>
  1660. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#include
  1661. &lt;TaskScheduler.h&gt;</FONT></FONT></P>
  1662. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1663. </P>
  1664. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>StatusRequest
  1665. measure;</FONT></FONT></P>
  1666. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1667. </P>
  1668. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler
  1669. ts; </FONT></FONT>
  1670. </P>
  1671. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1672. </P>
  1673. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1674. tCycle(10000, TASK_FOREVER, &amp;CycleCallback, &amp;ts, true);</FONT></FONT></P>
  1675. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1676. tMeasure(TASK_SECOND, TASK_ONCE, &amp;MeasureCallback, &amp;ts,
  1677. false, &amp;MeasureEnable, &amp;MeasureDisable);</FONT></FONT></P>
  1678. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1679. tCalculate(&amp;CalcCallback, &amp;ts);</FONT></FONT></P>
  1680. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1681. tSensor1(TASK_IMMEDIATE, TASK_ONCE, &amp;S1Callback, &amp;ts, false,
  1682. &amp;S1Enable);</FONT></FONT></P>
  1683. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1684. tSensor2(TASK_IMMEDIATE, TASK_ONCE, &amp;S2Callback, &amp;ts, false,
  1685. &amp;S2Enable);</FONT></FONT></P>
  1686. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1687. tSensor3(TASK_IMMEDIATE, TASK_ONCE, &amp;S3Callback, &amp;ts, false,
  1688. &amp;S3Enable);</FONT></FONT></P>
  1689. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1690. </P>
  1691. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1692. </P>
  1693. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>long
  1694. distance, d1, d2, d3;</FONT></FONT></P>
  1695. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1696. </P>
  1697. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1698. CycleCallback() {</FONT></FONT></P>
  1699. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1700. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;CycleCallback:
  1701. Initiating measurement cycle every 10 seconds&quot;);</FONT></FONT></P>
  1702. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1703. </P>
  1704. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1705. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tMeasure.restartDelayed();</FONT></FONT></P>
  1706. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1707. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1708. </P>
  1709. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1710. </P>
  1711. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1712. </P>
  1713. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
  1714. MeasureEnable() {</FONT></FONT></P>
  1715. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1716. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;MeasureEnable:
  1717. Activating sensors&quot;); </FONT></FONT>
  1718. </P>
  1719. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1720. </P>
  1721. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1722. <FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = 0;</FONT></FONT></P>
  1723. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1724. <FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.setWaiting(3);
  1725. // Set the StatusRequest to wait for 3 signals. </FONT></FONT>
  1726. </P>
  1727. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1728. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tCalculate.waitFor(&amp;measure);</FONT></FONT></P>
  1729. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1730. </P>
  1731. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1732. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor1.restart();</FONT></FONT></P>
  1733. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1734. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor2.restart();</FONT></FONT></P>
  1735. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1736. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor3.restart();</FONT></FONT></P>
  1737. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1738. </P>
  1739. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1740. <FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
  1741. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1742. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1743. </P>
  1744. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1745. MeasureCallback() {</FONT></FONT></P>
  1746. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1747. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;MeasureCallback:
  1748. Invoked by calculate task or one second later&quot;); </FONT></FONT>
  1749. </P>
  1750. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1751. </P>
  1752. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1753. <FONT FACE="Courier New, monospace"><FONT SIZE=2>if
  1754. (measure.pending()) {</FONT></FONT></P>
  1755. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1756. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tCalculate.disable();</FONT></FONT></P>
  1757. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1758. <FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signalComplete(-1);
  1759. // signal error</FONT></FONT></P>
  1760. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1761. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;MeasureCallback:
  1762. Timeout!&quot;);</FONT></FONT></P>
  1763. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1764. <FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1765. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1766. <FONT FACE="Courier New, monospace"><FONT SIZE=2>else {</FONT></FONT></P>
  1767. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1768. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;MeasureCallback:
  1769. Min distance=&quot;);Serial.println(distance);</FONT></FONT></P>
  1770. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1771. <FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1772. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1773. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1774. </P>
  1775. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1776. MeasureDisable() {</FONT></FONT></P>
  1777. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1778. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;MeasureDisable:
  1779. Cleaning up&quot;); </FONT></FONT>
  1780. </P>
  1781. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1782. </P>
  1783. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1784. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor1.disable();</FONT></FONT></P>
  1785. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1786. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor2.disable();</FONT></FONT></P>
  1787. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1788. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor3.disable();</FONT></FONT></P>
  1789. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1790. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1791. </P>
  1792. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1793. </P>
  1794. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1795. CalcCallback() {</FONT></FONT></P>
  1796. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1797. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;CalcCallback:
  1798. calculating&quot;); </FONT></FONT>
  1799. </P>
  1800. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1801. <FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = -1;</FONT></FONT></P>
  1802. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1803. <FONT FACE="Courier New, monospace"><FONT SIZE=2>if (
  1804. measure.getStatus() &gt;= 0) { // only calculate if statusrequest
  1805. ended successfully</FONT></FONT></P>
  1806. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1807. <FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = d1 &lt;
  1808. d2 ? d1 : d2;</FONT></FONT></P>
  1809. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1810. <FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = d3 &lt;
  1811. distance ? d3 : distance;</FONT></FONT></P>
  1812. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1813. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tMeasure.forceNextIteration();</FONT></FONT></P>
  1814. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1815. <FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1816. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1817. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1818. </P>
  1819. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1820. </P>
  1821. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
  1822. Simulation code for sensor 1</FONT></FONT></P>
  1823. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
  1824. ----------------------------</FONT></FONT></P>
  1825. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1826. <FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
  1827. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
  1828. S1Enable() {</FONT></FONT></P>
  1829. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1830. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S1Enable:
  1831. Triggering sensor1. Delay=&quot;); </FONT></FONT>
  1832. </P>
  1833. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1834. </P>
  1835. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1836. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor1.setInterval(
  1837. random(1200) ); // Simulating sensor delay, which could go over 1
  1838. second and cause timeout</FONT></FONT></P>
  1839. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1840. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d1 = 0;</FONT></FONT></P>
  1841. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1842. </P>
  1843. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1844. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(
  1845. tSensor1.getInterval() );</FONT></FONT></P>
  1846. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1847. <FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
  1848. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1849. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1850. </P>
  1851. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1852. S1Callback() {</FONT></FONT></P>
  1853. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1854. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S1Callback:
  1855. Emulating measurement. d1=&quot;); </FONT></FONT>
  1856. </P>
  1857. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1858. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d1 = random(501); //
  1859. pick a value from 0 to 500 &quot;centimeters&quot; simulating a
  1860. measurement </FONT></FONT>
  1861. </P>
  1862. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1863. <FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signal();</FONT></FONT></P>
  1864. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1865. </P>
  1866. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1867. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(d1); </FONT></FONT>
  1868. </P>
  1869. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1870. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1871. </P>
  1872. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1873. </P>
  1874. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
  1875. Simulation code for sensor 2</FONT></FONT></P>
  1876. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
  1877. ----------------------------</FONT></FONT></P>
  1878. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1879. <FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
  1880. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
  1881. S2Enable() {</FONT></FONT></P>
  1882. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1883. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S2Enable:
  1884. Triggering sensor2. Delay=&quot;); </FONT></FONT>
  1885. </P>
  1886. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1887. </P>
  1888. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1889. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor2.setInterval(
  1890. random(1200) ); // Simulating sensor delay, which could go over 1
  1891. second and cause timeout</FONT></FONT></P>
  1892. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1893. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d2 = 0;</FONT></FONT></P>
  1894. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1895. </P>
  1896. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1897. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(
  1898. tSensor2.getInterval() );</FONT></FONT></P>
  1899. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1900. <FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
  1901. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1902. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1903. </P>
  1904. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1905. S2Callback() {</FONT></FONT></P>
  1906. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1907. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S2Callback:
  1908. Emulating measurement. d2=&quot;); </FONT></FONT>
  1909. </P>
  1910. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1911. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d2 = random(501); //
  1912. pick a value from 0 to 500 &quot;centimeters&quot; simulating a
  1913. measurement</FONT></FONT></P>
  1914. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1915. <FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signal();</FONT></FONT></P>
  1916. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1917. </P>
  1918. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1919. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(d2);
  1920. </FONT></FONT>
  1921. </P>
  1922. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1923. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1924. </P>
  1925. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1926. </P>
  1927. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
  1928. Simulation code for sensor 3</FONT></FONT></P>
  1929. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
  1930. ----------------------------</FONT></FONT></P>
  1931. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1932. <FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
  1933. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
  1934. S3Enable() {</FONT></FONT></P>
  1935. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1936. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S3Enable:
  1937. Triggering sensor3. Delay=&quot;); </FONT></FONT>
  1938. </P>
  1939. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1940. </P>
  1941. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1942. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor3.setInterval(
  1943. random(1200) ); // Simulating sensor delay, which could go over 1
  1944. second and cause timeout</FONT></FONT></P>
  1945. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1946. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d3 = 0;</FONT></FONT></P>
  1947. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1948. </P>
  1949. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1950. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(
  1951. tSensor3.getInterval() );</FONT></FONT></P>
  1952. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1953. <FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
  1954. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1955. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1956. </P>
  1957. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1958. S3Callback() {</FONT></FONT></P>
  1959. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1960. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S3Callback:
  1961. Emulating measurement. d3=&quot;); </FONT></FONT>
  1962. </P>
  1963. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1964. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d3 = random(501); //
  1965. pick a value from 0 to 500 &quot;centimeters&quot; simulating a
  1966. measurement</FONT></FONT></P>
  1967. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1968. <FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signal();</FONT></FONT></P>
  1969. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1970. </P>
  1971. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1972. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(d3); </FONT></FONT>
  1973. </P>
  1974. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1975. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1976. </P>
  1977. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1978. </P>
  1979. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
  1980. Main Arduino code</FONT></FONT></P>
  1981. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
  1982. Not much is left here - everything is taken care of by the framework</FONT></FONT></P>
  1983. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1984. <FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
  1985. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1986. setup() {</FONT></FONT></P>
  1987. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1988. </P>
  1989. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1990. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.begin(115200);</FONT></FONT></P>
  1991. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1992. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;TaskScheduler
  1993. StatusRequest Sensor Emulation Test. Complex Test.&quot;); </FONT></FONT>
  1994. </P>
  1995. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1996. <FONT FACE="Courier New, monospace"><FONT SIZE=2>randomSeed(analogRead(A1)+millis());</FONT></FONT></P>
  1997. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1998. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1999. </P>
  2000. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2001. loop() {</FONT></FONT></P>
  2002. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2003. <FONT FACE="Courier New, monospace"><FONT SIZE=2>ts.execute();</FONT></FONT></P>
  2004. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2005. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2006. </P>
  2007. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2008. </P>
  2009. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2010. </P>
  2011. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2012. </P>
  2013. <OL START=3>
  2014. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>USING
  2015. LOCAL TASK STORAGE POINTER </B></FONT>
  2016. </P>
  2017. </OL>
  2018. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2019. </P>
  2020. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=2 STYLE="font-size: 11pt">Tasks
  2021. can store a pointer to specific variable, structure or array, which
  2022. represents variables specific for a particular task. This may be
  2023. needed if you plan to use same callback method for multiple tasks.</FONT></FONT></P>
  2024. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=2 STYLE="font-size: 11pt">Consider
  2025. a scenario where you have several sensors of the same type. The
  2026. actual process of triggering measurement and collecting information
  2027. is identical. The only difference is the sensor address and a
  2028. variable for storing the results. </FONT></FONT>
  2029. </P>
  2030. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=2 STYLE="font-size: 11pt">In
  2031. this case each of the tasks, which performs measurement will utilize
  2032. the same callback methods. The only difference will be the variables
  2033. (specific for each of the sensor). </FONT></FONT>
  2034. </P>
  2035. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2036. </P>
  2037. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=2 STYLE="font-size: 11pt">Let's
  2038. define a sensor data structure and declare a couple of variables (for
  2039. 2 sensors for instance)</FONT></FONT></P>
  2040. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2041. </P>
  2042. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2043. <FONT FACE="Courier New, monospace"><FONT SIZE=2>typedef struct {</FONT></FONT></P>
  2044. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2045. <FONT FACE="Courier New, monospace"><FONT SIZE=2> unsigned int
  2046. address;</FONT></FONT></P>
  2047. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2048. <FONT FACE="Courier New, monospace"><FONT SIZE=2> unsigned
  2049. long distance;</FONT></FONT></P>
  2050. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2051. <FONT FACE="Courier New, monospace"><FONT SIZE=2>} sensor_data;</FONT></FONT></P>
  2052. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2053. <BR>
  2054. </P>
  2055. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2056. <FONT FACE="Courier New, monospace"><FONT SIZE=2>sensor_data s1, s2; </FONT></FONT>
  2057. </P>
  2058. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2059. </P>
  2060. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=2 STYLE="font-size: 11pt">Two
  2061. separate tasks are running to collect sensor data.</FONT></FONT></P>
  2062. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=2 STYLE="font-size: 11pt">(Note
  2063. that both tasks refer to the same callback methods)</FONT></FONT></P>
  2064. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2065. </P>
  2066. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2067. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler ts;</FONT></FONT></P>
  2068. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2><SPAN LANG="">Task
  2069. t1(100, </SPAN>TASK_FOREVER<SPAN LANG="">, &amp;Measure, &amp;ts,
  2070. false, &amp;MeasureOn); </SPAN></FONT></FONT>
  2071. </P>
  2072. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2><SPAN LANG="">Task
  2073. t2(100, </SPAN>TASK_FOREVER<SPAN LANG="">, &amp;Measure, &amp;ts,
  2074. false, &amp;MeasureOn); </SPAN></FONT></FONT>
  2075. </P>
  2076. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2077. </P>
  2078. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=2 STYLE="font-size: 11pt">Assign
  2079. pointers to the respective variables in the <B>setup</B>() method:</FONT></FONT></P>
  2080. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2081. </P>
  2082. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2083. setup() {</FONT></FONT></P>
  2084. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2085. </P>
  2086. <P CLASS="western" STYLE="margin-left: 1.48in; margin-bottom: 0in">…</P>
  2087. <P CLASS="western" STYLE="margin-left: 1.48in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>t1.setLtsPointer(&amp;s1);</FONT></FONT></P>
  2088. <P CLASS="western" STYLE="margin-left: 1.48in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>t2.setLtsPointer(&amp;s2);</FONT></FONT></P>
  2089. <P CLASS="western" STYLE="margin-left: 1.48in; margin-bottom: 0in">…</P>
  2090. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2091. </P>
  2092. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2093. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2094. </P>
  2095. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=2 STYLE="font-size: 11pt">Obtain
  2096. reference to specific <B>sensor_data</B> structure inside the common
  2097. callback method:</FONT></FONT></P>
  2098. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2099. </P>
  2100. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2101. Measure() {</FONT></FONT></P>
  2102. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> Task&amp;
  2103. T = ts.currentTask(); </FONT></FONT>
  2104. </P>
  2105. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> Sensor_data&amp;
  2106. V = *((sensor_data*) T.getLtsPointer());</FONT></FONT></P>
  2107. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2108. For t1, V will be pointing at s1</FONT></FONT></P>
  2109. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2110. For t2, V will be pointing at s2</FONT></FONT></P>
  2111. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2112. </P>
  2113. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2114. Alternatively use the Scheduler method:</FONT></FONT></P>
  2115. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> Sensor_data&amp;
  2116. V1 = *((sensor_data*) ts.currentLts());</FONT></FONT></P>
  2117. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2118. </P>
  2119. <P CLASS="western" STYLE="margin-left: 0.98in; text-indent: 0.49in; margin-bottom: 0in">
  2120. …</P>
  2121. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> V.distance
  2122. = &lt;calculate your values here&gt;;</FONT></FONT></P>
  2123. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2124. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2125. </P>
  2126. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2127. </P>
  2128. <OL START=3>
  2129. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>FUTHER
  2130. INFROMATION</B></FONT></P>
  2131. </OL>
  2132. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2133. </P>
  2134. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Please
  2135. refer to examples, provided with TaskScheduler package for further
  2136. information and implementation options.</FONT></FONT></P>
  2137. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2138. </P>
  2139. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Real
  2140. time examples of TaskScheduler are available here:</FONT></FONT></P>
  2141. <OL>
  2142. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT COLOR="#0000ff"><U><A HREF="http://www.instructables.com/id/APIS-Automated-Plant-Irrigation-System/"><FONT FACE="Courier New, monospace">http://www.instructables.com/id/APIS-Automated-Plant-Irrigation-System/</FONT></A></U></FONT></P>
  2143. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT COLOR="#0000ff"><U><A HREF="http://www.instructables.com/id/Wave-your-hand-to-control-OWI-Robotic-Arm-no-strin/"><FONT FACE="Courier New, monospace">http://www.instructables.com/id/Wave-your-hand-to-control-OWI-Robotic-Arm-no-strin/</FONT></A></U></FONT></P>
  2144. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT COLOR="#0000ff"><U><A HREF="http://www.instructables.com/id/Arduino-Nano-based-Hexbug-Scarab-Robotic-Spider"><FONT FACE="Courier New, monospace">http://www.instructables.com/id/Arduino-Nano-based-Hexbug-Scarab-Robotic-Spider</FONT></A></U></FONT></P>
  2145. </OL>
  2146. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  2147. </P>
  2148. </BODY>
  2149. </HTML>