TaskScheduler.html 149 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800
  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="20151128;23410000">
  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", "MS PMincho", 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.9.1: 2015-11-28</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.png" NAME="graphics1" ALIGN=BOTTOM WIDTH=664 HEIGHT=570 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"><B>NOTE: </B>above
  280. parameters are<B> DISABLED </B>by default, and need to be explicitly
  281. enabled by placing appropriate #define statements in front of the
  282. #include statement for the TaskScheduler header file.</P>
  283. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  284. </P>
  285. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  286. </P>
  287. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  288. </P>
  289. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  290. </P>
  291. <P CLASS="western" STYLE="margin-bottom: 0in"><B>TASK PRIORITY AND
  292. COOPERATIVE MULTITASKING:</B></P>
  293. <P CLASS="western" STYLE="margin-bottom: 0in">TaskScheduler <B>does
  294. not support</B> task priority functionality. I have been thinking a
  295. lot about it (especially since Symbian's Active Objects, which
  296. TaskScheduler is inspired by, support it), but decided against it.
  297. </P>
  298. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  299. </P>
  300. <P CLASS="western" STYLE="margin-bottom: 0in">This is why:</P>
  301. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  302. </P>
  303. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  304. </P>
  305. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  306. </P>
  307. <P CLASS="western" STYLE="margin-bottom: 0in">1. Execution chains are
  308. simple and efficient. The main idea is to minimize scheduling
  309. overhead by Scheduler going through the chain. Implementing true
  310. priority would require looking ahead through the entire chain,
  311. ranking and aging tasks by priorities. It would slow the <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">execute()
  312. </FONT></FONT>loop significantly.</P>
  313. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  314. </P>
  315. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  316. </P>
  317. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  318. </P>
  319. <P CLASS="western" STYLE="margin-bottom: 0in">2. TaskScheduler is <B>NOT</B>
  320. a pre-emptive multi-tasking library. Nor is it a Real-Time OS. There
  321. is no way to break execution of one task in favor of another.
  322. Therefore callback methods require careful programming for
  323. cooperative behavior.</P>
  324. <P CLASS="western" STYLE="margin-bottom: 0in">This has, however,
  325. significant benefits: you don't need to worry about concurrency
  326. inside the callback method, since only one callback method runs at a
  327. time, and could not be interrupted. All resources are yours for that
  328. period of time, noone can switch the value of variables (except
  329. interrupt functions of course...), etc. It is a stable and
  330. predictable environment, and it helps a lot with writing stable code.
  331. </P>
  332. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  333. </P>
  334. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  335. </P>
  336. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  337. </P>
  338. <P CLASS="western" STYLE="margin-bottom: 0in">A number of things
  339. could be done instead of priorities:</P>
  340. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  341. </P>
  342. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  343. </P>
  344. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  345. </P>
  346. <P CLASS="western" STYLE="margin-bottom: 0in">1. Schedule your
  347. critical tasks to run more frequently than the other tasks</P>
  348. <P CLASS="western" STYLE="margin-bottom: 0in">. <BR>(Since you can
  349. control the interval, you could also change the task to run more or
  350. less frequently as the situation demands).</P>
  351. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  352. </P>
  353. <P CLASS="western" STYLE="margin-bottom: 0in">2. If one particular
  354. callback routine is critical, create a couple of tasks referring to
  355. the same callback and &quot;sprinkle&quot; them around the chain:</P>
  356. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  357. </P>
  358. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  359. </P>
  360. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  361. </P>
  362. <P CLASS="western" STYLE="margin-bottom: 0in"> <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">Scheduler
  363. ts;</FONT></FONT></P>
  364. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  365. </P>
  366. <P CLASS="western" STYLE="margin-bottom: 0in"> <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">Task
  367. t1(20, TASK_FOREVER, &amp;callback1, &amp;ts);</FONT></FONT></P>
  368. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  369. </P>
  370. <P CLASS="western" STYLE="margin-bottom: 0in"> <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">Task
  371. t2(1000, TASK_FOREVER, &amp;callback2, &amp;ts);</FONT></FONT></P>
  372. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  373. </P>
  374. <P CLASS="western" STYLE="margin-bottom: 0in"> <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">Task
  375. t3(20, TASK_FOREVER, &amp;callback1, &amp;ts);</FONT></FONT></P>
  376. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  377. </P>
  378. <P CLASS="western" STYLE="margin-bottom: 0in"> <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">Task
  379. t4(1000, TASK_FOREVER, &amp;callback4, &amp;ts);</FONT></FONT></P>
  380. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  381. </P>
  382. <P CLASS="western" STYLE="margin-bottom: 0in"> <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t3.delay(10);</FONT></FONT></P>
  383. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  384. </P>
  385. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  386. </P>
  387. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  388. </P>
  389. <P CLASS="western" STYLE="margin-bottom: 0in">Note that t1 and t3
  390. call the same callback method, and are shifted in time by 10 millis.
  391. So effectively callback1 will be called every 10 millis, but would be
  392. &quot;sandwiched&quot; between t2 and t4.
  393. </P>
  394. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  395. </P>
  396. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  397. </P>
  398. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  399. </P>
  400. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  401. </P>
  402. <P CLASS="western" STYLE="margin-bottom: 0in">3. Use short efficient
  403. callback methods written for cooperative multitasking.</P>
  404. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  405. </P>
  406. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  407. </P>
  408. <P CLASS="western" STYLE="margin-bottom: 0in">What that means is:</P>
  409. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  410. </P>
  411. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  412. </P>
  413. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  414. </P>
  415. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">a)
  416. <B>DO NOT</B> use Arduino's <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">delay()</FONT></FONT><FONT FACE="Andale Mono, MS Mincho">
  417. </FONT>function. It is blocking and will hold the entire chain.
  418. Instead break the callback method into two, switch the callback
  419. method of the task where delay is necessary and delay the task by
  420. that number of millis. You get your delay, and other tasks get a
  421. chance to run:</P>
  422. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  423. </P>
  424. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  425. </P>
  426. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  427. </P>
  428. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">instead
  429. of:</P>
  430. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  431. </P>
  432. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  433. </P>
  434. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  435. </P>
  436. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  437. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  438. callback() {</FONT></FONT></P>
  439. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  440. </P>
  441. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  442. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  443. stuff</FONT></FONT></P>
  444. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  445. </P>
  446. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  447. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">delay(1000);</FONT></FONT></P>
  448. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  449. </P>
  450. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  451. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  452. more stuff</FONT></FONT></P>
  453. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  454. </P>
  455. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  456. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  457. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  458. </P>
  459. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  460. </P>
  461. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  462. </P>
  463. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">do
  464. this:</P>
  465. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  466. </P>
  467. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  468. </P>
  469. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  470. </P>
  471. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  472. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  473. callback1() {</FONT></FONT></P>
  474. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  475. </P>
  476. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  477. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  478. stuff</FONT></FONT></P>
  479. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  480. </P>
  481. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  482. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.setCallback(&amp;callback2);</FONT></FONT></P>
  483. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  484. </P>
  485. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  486. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.delay(1000);</FONT></FONT></P>
  487. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  488. </P>
  489. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  490. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  491. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  492. </P>
  493. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  494. </P>
  495. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  496. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  497. callback2() {</FONT></FONT></P>
  498. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  499. </P>
  500. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  501. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  502. more stuff</FONT></FONT></P>
  503. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  504. </P>
  505. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  506. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.setCallback(&amp;callback1);</FONT></FONT></P>
  507. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  508. </P>
  509. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  510. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  511. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  512. </P>
  513. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  514. </P>
  515. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  516. </P>
  517. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  518. </P>
  519. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  520. </P>
  521. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">b)
  522. Same goes to <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">pulseIn()</FONT></FONT>
  523. function. If you have to use it, set the timeout parameter such that
  524. it is not a default 1 second. PulseIn functionality could be achieved
  525. via pin interrupts, and that solution is non-blocking.</P>
  526. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  527. </P>
  528. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  529. </P>
  530. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  531. </P>
  532. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">c)
  533. Do don run long loops (for or do/while) in you callback methods. Make
  534. the main arduino loop be the loop driver for you:</P>
  535. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  536. </P>
  537. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  538. </P>
  539. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  540. </P>
  541. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">instead
  542. of:</P>
  543. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  544. </P>
  545. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  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">
  550. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  551. callback() {</FONT></FONT></P>
  552. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  553. </P>
  554. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  555. </P>
  556. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  557. </P>
  558. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  559. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">for(int
  560. i=0; i&lt;1000; i++) {</FONT></FONT></P>
  561. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  562. </P>
  563. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  564. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  565. stuff // one loop action</FONT></FONT></P>
  566. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  567. </P>
  568. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  569. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  570. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  571. </P>
  572. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  573. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  574. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  575. </P>
  576. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  577. </P>
  578. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  579. </P>
  580. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">do
  581. this:</P>
  582. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  583. </P>
  584. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  585. </P>
  586. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  587. </P>
  588. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  589. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">Task
  590. t1(TASK_IMMEDIATE, 1000, &amp;callback);</FONT></FONT></P>
  591. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  592. </P>
  593. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  594. </P>
  595. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  596. </P>
  597. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  598. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  599. callback() {</FONT></FONT></P>
  600. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  601. </P>
  602. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  603. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">int
  604. i = t1.getRunCounter() -1;</FONT></FONT></P>
  605. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  606. </P>
  607. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  608. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  609. stuff // one loop action</FONT></FONT></P>
  610. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  611. </P>
  612. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  613. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  614. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  615. </P>
  616. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  617. </P>
  618. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  619. </P>
  620. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">or
  621. this:</P>
  622. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  623. </P>
  624. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  625. </P>
  626. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  627. </P>
  628. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  629. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">Task
  630. t1(TASK_IMMEDIATE, 1000, &amp;callback, true, &amp;t1On);</FONT></FONT></P>
  631. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  632. </P>
  633. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  634. </P>
  635. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  636. </P>
  637. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  638. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">int
  639. i;</FONT></FONT></P>
  640. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  641. </P>
  642. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  643. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">bool
  644. t1On() {</FONT></FONT></P>
  645. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  646. </P>
  647. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  648. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">i
  649. = 0;</FONT></FONT></P>
  650. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  651. </P>
  652. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  653. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">return
  654. true;</FONT></FONT></P>
  655. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  656. </P>
  657. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  658. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  659. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  660. </P>
  661. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  662. </P>
  663. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  664. </P>
  665. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  666. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  667. callback() {</FONT></FONT></P>
  668. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  669. </P>
  670. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  671. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  672. stuff // one loop action</FONT></FONT></P>
  673. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  674. </P>
  675. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  676. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">i++;</FONT></FONT></P>
  677. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  678. </P>
  679. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  680. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  681. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  682. </P>
  683. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  684. </P>
  685. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  686. </P>
  687. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  688. </P>
  689. <P CLASS="western" ALIGN=CENTER STYLE="margin-left: 0.49in; margin-bottom: 0in">
  690. <B>REMEMBER: you are already inside the loop - take advantage of it. </B>
  691. </P>
  692. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  693. </P>
  694. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">d)
  695. Break long running callback methods into several shorter ones, and
  696. pass control from one to the other via <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">setCallback()
  697. </FONT></FONT>method:</P>
  698. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  699. </P>
  700. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  701. </P>
  702. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  703. </P>
  704. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  705. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">Task
  706. t1(TASK_IMMEDIATE, TASK_FAREVER, &amp;callback);</FONT></FONT></P>
  707. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  708. </P>
  709. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  710. </P>
  711. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  712. </P>
  713. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  714. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  715. callback() {</FONT></FONT></P>
  716. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  717. </P>
  718. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  719. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  720. do some stuff</FONT></FONT></P>
  721. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  722. </P>
  723. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  724. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.setCallback(&amp;callback_step2);</FONT></FONT></P>
  725. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  726. </P>
  727. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  728. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  729. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  730. </P>
  731. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  732. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  733. callback_step2() {</FONT></FONT></P>
  734. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  735. </P>
  736. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  737. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  738. do more stuff</FONT></FONT></P>
  739. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  740. </P>
  741. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  742. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.setCallback(&amp;callback_step3);</FONT></FONT></P>
  743. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  744. </P>
  745. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  746. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  747. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  748. </P>
  749. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  750. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  751. callback_step3() {</FONT></FONT></P>
  752. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  753. </P>
  754. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  755. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  756. do last part of the stuff</FONT></FONT></P>
  757. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  758. </P>
  759. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  760. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.setCallback(&amp;callback);</FONT></FONT></P>
  761. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  762. </P>
  763. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  764. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.delay(1000);</FONT></FONT></P>
  765. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  766. </P>
  767. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  768. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  769. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  770. </P>
  771. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  772. </P>
  773. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  774. </P>
  775. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">This
  776. will execute all parts of the callback function in three successive
  777. steps, sheduled immediately, but allowing other tasks in the cahin to
  778. run. Notince that task is scheduled to run immediately, and 1 second
  779. period is achieved by delaying the task for 1000 millis at the last
  780. step.
  781. </P>
  782. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  783. </P>
  784. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Alternatively
  785. you could schedule the task to run every 1000 millis and use
  786. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">forceNextIteration()</FONT></FONT>
  787. method in steps 1 and 2 (but not 3!)</P>
  788. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  789. </P>
  790. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  791. </P>
  792. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  793. </P>
  794. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  795. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">Task
  796. t1(1000, TASK_FOREVER, &amp;callback);</FONT></FONT></P>
  797. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  798. </P>
  799. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  800. </P>
  801. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  802. </P>
  803. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  804. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  805. callback() {</FONT></FONT></P>
  806. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  807. </P>
  808. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  809. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  810. do some stuff</FONT></FONT></P>
  811. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  812. </P>
  813. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  814. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.setCallback(&amp;callback_step2);</FONT></FONT></P>
  815. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  816. </P>
  817. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  818. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.forceNextIteration();</FONT></FONT></P>
  819. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  820. </P>
  821. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  822. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  823. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  824. </P>
  825. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  826. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  827. callback_step2() {</FONT></FONT></P>
  828. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  829. </P>
  830. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  831. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  832. do more stuff</FONT></FONT></P>
  833. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  834. </P>
  835. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  836. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.setCallback(&amp;callback_step3);</FONT></FONT></P>
  837. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  838. </P>
  839. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  840. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.forceNextIteration();</FONT></FONT></P>
  841. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  842. </P>
  843. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  844. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  845. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  846. </P>
  847. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  848. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">void
  849. callback_step3() {</FONT></FONT></P>
  850. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  851. </P>
  852. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  853. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">...
  854. do last part of the stuff</FONT></FONT></P>
  855. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  856. </P>
  857. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  858. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">t1.setCallback(&amp;callback);</FONT></FONT></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">
  862. <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt">}</FONT></FONT></P>
  863. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  864. </P>
  865. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  866. </P>
  867. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  868. </P>
  869. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  870. </P>
  871. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  872. </P>
  873. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">e)
  874. Compile the library with <FONT FACE="FreeMono, MS Mincho, monospace"><FONT SIZE=2 STYLE="font-size: 10pt"><B>_TASK_TIMECRITICAL</B></FONT></FONT>
  875. enabled and check if your tasks are falling behind schedule. If they
  876. are - you need to optimize your code further (or maybe re-evaluate
  877. your schedule). If they are not - all is well and you don't need to
  878. do anything. E.g., I have a spider robot which needs to measure
  879. distance, control motors, and keep track of the angle via querying
  880. gyroscope and accelerometer every 10 ms. The idea was to flash
  881. onboard LED if any of the tasks fall behind. At 10 ms interval for
  882. the gyro the LED does not flash, which means none of the tasks are
  883. blocking the others from starting on time.
  884. </P>
  885. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  886. </P>
  887. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  888. </P>
  889. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  890. </P>
  891. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  892. <FONT SIZE=4><B>API DOCUMENTATION:</B></FONT></P>
  893. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  894. <BR>
  895. </P>
  896. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  897. <FONT SIZE=4><B>TASKS:</B></FONT></P>
  898. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  899. <BR>
  900. </P>
  901. <P CLASS="western" STYLE="margin-bottom: 0in">CREATION:</P>
  902. <P CLASS="western" STYLE="margin-bottom: 0in"><B>Task();</B></P>
  903. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  904. </P>
  905. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Default
  906. constructor.
  907. </P>
  908. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Takes
  909. no parameters and creates a task that could be scheduled to run at
  910. every scheduling pass indefinitely, but does not have a callback
  911. method defined, so no code execution will actually take place.
  912. </P>
  913. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">All
  914. tasks are created <B>disabled</B> by default.</P>
  915. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  916. </P>
  917. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  918. <B>Task(unsigned long aInterval, long aIterations, void
  919. (*aCallback)(), Scheduler* aScheduler, bool aEnable, bool
  920. (*aOnEnable)(), void (*aOnDisable)())</B></P>
  921. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  922. </P>
  923. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Constructor
  924. with parameters.
  925. </P>
  926. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Creates
  927. a task that is scheduled to run every &lt;aInterval&gt; milliseconds,
  928. &lt;aIterations&gt; times, executing &lt;aCallback&gt; method on
  929. every pass.
  930. </P>
  931. <OL>
  932. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aInterval is in
  933. milliseconds (<B>default = 0)</B></P>
  934. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aIteration in
  935. number of times, -1 for indefinite execution (<B>default = -1)</B><BR><B>Note:
  936. </B>Tasks do not remember the number of iteration set initially.
  937. After the iterations are done, internal iteration counter is 0. If
  938. you need to perform another set of iterations, you need to set the
  939. number of iterations again. <BR><B>Note: </B>Tasks which performed
  940. all their iterations remain active.
  941. </P>
  942. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aCallback is a
  943. pointer to a void callback method without parameters (<B>default =
  944. NULL)</B></P>
  945. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aScheduler –
  946. <B>optional</B> reference to existing scheduler. If supplied (not
  947. NULL) this task will be appended to the task chain of the current
  948. scheduler). (<B>default = NULL)</B></P>
  949. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aEnable –
  950. <B>optional</B>. Value of <B>true </B>will create task enabled.
  951. (<B>default = false)</B></P>
  952. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aOnEnable is a
  953. pointer to a bool callback method without parameters, invoked when
  954. task is enabled. If OnEnable method returns <B>true</B>, task is
  955. enabled. If <B>OnEnable</B> method return <B>false</B>, task remains
  956. disabled (<B>default = NULL)</B></P>
  957. <LI><P CLASS="western" STYLE="margin-bottom: 0in">aOnDisable is a
  958. pointer to a void callback method without parameters, invoked when
  959. task is disabled (<B>default = NULL)</B></P>
  960. </OL>
  961. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  962. </P>
  963. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">All
  964. tasks are created <B>disabled</B> by default (unless <B>aEnable</B> =
  965. true). You have to explicitly enable the task for execution.</P>
  966. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  967. </P>
  968. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:
  969. </B>OnEnable callback method is called immediately when task is
  970. enabled, which could be well ahead of the scheduled execution time of
  971. the task. Please bear that in mind – other tasks, hardware, serial
  972. interface may not even be initialized yet. It is always advisable to
  973. explicitly enable tasks with OnEnable methods after all
  974. initialization methods completed (e.g., at the end of <B>setup</B>()
  975. method)
  976. </P>
  977. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  978. </P>
  979. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enabled
  980. task is scheduled for execution as soon as the Scheduler's <B>execute</B>()
  981. methods gets control. In order to delay first run of the task, use
  982. <B>enableDelayed</B> or <B>delay</B> method (for enabled tasks)
  983. method.</P>
  984. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  985. </P>
  986. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  987. <B>Task(void (*aCallback)(), Scheduler* aScheduler, bool
  988. (*aOnEnable)(), void (*aOnDisable)())</B></P>
  989. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  990. </P>
  991. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  992. compiled with support for Status Request objects, this constructor
  993. creates a Task for activation on event (since such tasks must run
  994. <B>waitFor() </B>method, their <I>interval</I>, <I>iteration</I> and
  995. <I>enabled</I> status will be set by that method (<I>to 0, 1 and
  996. false</I> respectively).</P>
  997. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  998. </P>
  999. <P CLASS="western" STYLE="margin-bottom: 0in"><B>INFORMATION</B></P>
  1000. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">The
  1001. following 3 “getter” methods return task status
  1002. (enabled/disabled), execution interval in milliseconds, number of
  1003. <I><B>remaining</B></I> iterations.
  1004. </P>
  1005. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1006. </P>
  1007. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>bool
  1008. isEnabled() </B>
  1009. </P>
  1010. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>unsigned
  1011. long getInterval()</B></P>
  1012. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>long
  1013. getIterations() </B>
  1014. </P>
  1015. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1016. </P>
  1017. <P CLASS="western" STYLE="margin-bottom: 0in"><B>long getOverrun()</B></P>
  1018. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1019. library is compiled with <FONT FACE="Courier New, monospace">_TASK_TIMECRITICAL</FONT>
  1020. enabled, tasks are monitored for “long running” scenario. A “long
  1021. running” task is a task that does not finish processing its
  1022. callback methods quickly, and thus creates a situation for itself and
  1023. other tasks where they don't run on a scheduled interval, but rather
  1024. “catch up” and are behind. When task scheduler sets the next
  1025. execution target time, it adds Task's execution interval to the
  1026. previously scheduled execution time:</P>
  1027. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <B>next
  1028. execution time = previous execution time + task execution interval</B></P>
  1029. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1030. </P>
  1031. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1032. <B>next execution time</B> happens to be already in the past (<B>next
  1033. execution time</B> &lt; <B>millis()</B>), then task is considered
  1034. <I><B>overrun</B></I>. <B>GetOverrun</B> method returns number of
  1035. milliseconds between next execution time and current time. If the
  1036. <B>value is negative</B>, the task has overrun (cut into the) next
  1037. execution interval by that many milliseconds.
  1038. </P>
  1039. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Positive
  1040. value indicate number of milliseconds of slack this task has for
  1041. execution purposes.
  1042. </P>
  1043. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1044. </P>
  1045. <P CLASS="western" STYLE="margin-bottom: 0in"><B>unsigned long
  1046. getRunCounter()</B></P>
  1047. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  1048. the number of the current run. “Current run” is the number of
  1049. times a callback method has been invoked since the last time a task
  1050. was enabled. <BR><BR>
  1051. </P>
  1052. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:
  1053. </B>The <B>runCounter</B> value is incremented <I>before</I> callback
  1054. method is invoked. If a task is checking the <B>runCounter</B> value
  1055. within its callback method, then the first run value is 1.
  1056. </P>
  1057. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1058. task T1 is checking the <B>runCounter</B> value of another task (T2)
  1059. , then value = 0 indicates that T2 has not been invoked yet, and
  1060. value = 1 indicates that T2 has run once.
  1061. </P>
  1062. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1063. </P>
  1064. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool
  1065. isFirstIteration()</B></P>
  1066. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Indicates
  1067. whether current pass is (or will be) a first iteration of the task.
  1068. </P>
  1069. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool
  1070. isLastIteration()</B></P>
  1071. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">For
  1072. tasks with a l<I>imited number of iterations only</I>, indicates
  1073. whether current pass is the last iteration.
  1074. </P>
  1075. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1076. </P>
  1077. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1078. <B>CONTROL:</B></P>
  1079. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1080. <BR>
  1081. </P>
  1082. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void enable();</B></P>
  1083. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1084. </P>
  1085. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
  1086. the task, and schedules it for immediate execution (without delay) at
  1087. this or next scheduling pass depending on when the task was enabled.
  1088. Scheduler will execute the next pass without any delay because there
  1089. is a task which was enabled and requires execution.
  1090. </P>
  1091. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1092. </P>
  1093. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:
  1094. </B>if task being enabled is not assigned to a scheduler and is not
  1095. part of execution chain, then task <B>will not</B> be enabled.</P>
  1096. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1097. </P>
  1098. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:
  1099. </B>enable() invokes task’s <B>OnEnable</B> method (if not NULL)
  1100. <B>immediately</B>, which can prepare task for execution. <B>OnEnable</B>
  1101. must return a value of <B>true</B> for task to be enabled. If
  1102. <B>OnEnable</B> returns <B>false</B>, task remains disabled.
  1103. <B>OnEnable</B> is invoked every time <B>enable</B> is called,
  1104. regardless if task is already enabled or not. Alignment to current
  1105. millis() is performed after <B>OnEnable</B> exits, so any changes to
  1106. the interval inside <B>OnEnable</B> is taken into consideration.</P>
  1107. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">TaskScheduler
  1108. allows tasks to be added to the a Scheduler and enabled at the time
  1109. of creation. <B>Be very careful</B> with such tasks – the <B>OnEnable
  1110. </B>method will be executed immediately, while certain objects (i.e.,
  1111. other Tasks, libraries) are not yet ready (e.g., <B>Wire.begin()</B>
  1112. was not yet called), or hardware not yet activated (pins not set to
  1113. INPUT or OUTPUT).
  1114. </P>
  1115. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">It
  1116. is very much recommended to to enable all tasks at the end of <B>setup()</B>
  1117. method after all initializations are done.
  1118. </P>
  1119. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1120. you require immediate execution of already enabled task, use
  1121. <B>forceNextIteratoin</B>() method instead of <B>enable</B>(): it
  1122. achieves the result, but does not call <B>OnEnable</B> method.
  1123. </P>
  1124. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1125. </P>
  1126. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:</B>
  1127. in the event <B>enable</B>() method is called inside the <B>OnEnable</B>
  1128. callback method (thus basically creating indefinte loop),
  1129. TaskScheduler will only call <B>OnEnable</B> once (thus protecting
  1130. the Task against <B>OnEnable</B> infinite loop).
  1131. </P>
  1132. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1133. </P>
  1134. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1135. <B>bool enableIfNot();</B></P>
  1136. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1137. <BR>
  1138. </P>
  1139. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
  1140. the task only if it was previously disabled. Returns previous enable
  1141. state: <B>true</B> if task was already enabled, and <B>false</B> if
  1142. task was disabled. Since <B>enable() </B>schedules Task for execution
  1143. immediately, this method provides a way to activate tasks and
  1144. schedule them for immediate execution only if they are not active
  1145. already.</P>
  1146. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1147. </P>
  1148. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void delay();</B></P>
  1149. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1150. </P>
  1151. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Schedules
  1152. the task for execution after a delay (aInterval), but does not change
  1153. the enabled/disabled status of the task.
  1154. </P>
  1155. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1156. </P>
  1157. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>NOTE:
  1158. </B>a delay of 0 (zero) will delay task for current execution
  1159. interval. Use <B>forceNextIteration() </B>method to force execution
  1160. of the task’s callback during immediate next scheduling pass.
  1161. </P>
  1162. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1163. </P>
  1164. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void
  1165. forceNextIteration();</B></P>
  1166. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1167. </P>
  1168. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Schedules
  1169. the task for execution during immediate next scheduling pass.</P>
  1170. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">The
  1171. Task must be already <I>enabled</I> prior to this method.
  1172. </P>
  1173. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1174. </P>
  1175. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  1176. </B>Task’s schedule is adjusted to run from this moment in time.
  1177. For instance: if a task was running every 10 seconds: 10, 20, 30, ..,
  1178. calling <B>forceNextIteration </B>at 44th second of task execution
  1179. will make subsequent schedule look like: 44, 54, 64, 74, ..</P>
  1180. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1181. </P>
  1182. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void
  1183. enableDelayed();</B></P>
  1184. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1185. </P>
  1186. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
  1187. the task, and schedules it for execution after task's current
  1188. scheduling interval (aInterval).
  1189. </P>
  1190. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1191. </P>
  1192. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void enableDelayed
  1193. (unsigned long aDelay);</B></P>
  1194. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1195. </P>
  1196. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Enables
  1197. the task, and schedules it for execution after a specific delay
  1198. (aDelay, which maybe different from aInterval).
  1199. </P>
  1200. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1201. </P>
  1202. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void restart();</B></P>
  1203. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1204. </P>
  1205. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">For
  1206. tasks with limited number of iterations only, <B>restart</B> method
  1207. will re-enable the task, set the number of iterations back to last
  1208. set value, and schedule task for execution as soon as possible.</P>
  1209. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1210. </P>
  1211. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void restartDelayed
  1212. (unsigned long aDelay);</B></P>
  1213. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1214. </P>
  1215. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Same
  1216. as <B>restart()</B> method, with the only difference being that Task
  1217. is scheduled to run first iteration after a delay = <B>aDelay</B>
  1218. milliseconds.</P>
  1219. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1220. </P>
  1221. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool disable();</B></P>
  1222. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1223. </P>
  1224. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Disables
  1225. the task. Scheduler will not execute this task any longer, even if it
  1226. remains in the chain. Task <B>can</B> be later re-enabled for
  1227. execution.
  1228. </P>
  1229. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Return
  1230. previous enabled state: <B>true</B> if task was enabled prior to
  1231. calling disable, and <B>false</B> otherwise.</P>
  1232. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1233. not NULL, task’s <B>OnDisable</B> method is invoked <B>immediately</B>.
  1234. <B>OnDisable</B> is invoked only if task was in enabled state.
  1235. Calling <B>disable</B> 3 times for instance will invoke <B>OnDisable</B>
  1236. only once.</P>
  1237. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1238. </P>
  1239. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void set(unsigned
  1240. long aInterval, long aIterations, void (*aCallback)() , bool
  1241. (*aOnEnable)() , void (*aOnDisable)());</B></P>
  1242. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1243. </P>
  1244. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Allows
  1245. dynamic control of task execution parameters in one method call.
  1246. </P>
  1247. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note</B><B>:
  1248. </B>OnEnable and OnDisable parameters can be omitted. In that case
  1249. they will be assigned to <B>NULL</B> and respective methods will no
  1250. longer be called. Therefore it is advisable to use either all five
  1251. parameters explicitly, or employ individual “setter” methods
  1252. below instead.
  1253. </P>
  1254. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1255. </P>
  1256. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1257. Next five “setter” methods allow changes of individual task
  1258. execution control parameters.
  1259. </P>
  1260. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
  1261. setInterval (unsigned long aInterval) </B>
  1262. </P>
  1263. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
  1264. setIterations (long aIterations) </B>
  1265. </P>
  1266. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
  1267. setCallback (void (*aCallback)()) </B>
  1268. </P>
  1269. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
  1270. setOnEnable (bool (*aCallback)()) </B>
  1271. </P>
  1272. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>void
  1273. setOnDisable (void (*aCallback)()) </B>
  1274. </P>
  1275. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1276. </P>
  1277. <P CLASS="western" STYLE="margin-bottom: 0in"><B>NOTE: </B>Next
  1278. execution time calculation takes place <B>after</B> the callback
  1279. method is called, so new interval will be used immediately by the
  1280. scheduler. For the situations when one task is changing the interval
  1281. parameter for the other, <B>setInterval</B> method calls <B>delay
  1282. </B>explicitly to guarantee schedule change, however it <B>does not
  1283. </B>enable the task if task is disabled.</P>
  1284. <P CLASS="western" STYLE="margin-bottom: 0in"><B>NOTE: </B>Tasks that
  1285. ran through all their allocated iterations are disabled.
  1286. <B>SetIterations()</B> method <B>DOES NOT</B> enable the task. Either
  1287. <B>enable</B> explicitly, or use <B>restart</B> methods.
  1288. </P>
  1289. <P CLASS="western" STYLE="margin-bottom: 0in">Please note that as a
  1290. result execution of the taks is <B>delayed</B> by the provided
  1291. interval. If immediate invocation is required, call
  1292. <B>forceNextIteration</B>() method after setting a new interval.
  1293. </P>
  1294. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1295. </P>
  1296. <P CLASS="western" STYLE="margin-bottom: 0in"><B>STATUS REQUEST
  1297. METHODS:</B></P>
  1298. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1299. <BR>
  1300. </P>
  1301. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1302. <B>void waitFor(StatusRequest* aStatusRequest, unsigned long
  1303. aInterval = 0, long aIterations = 1)</B></P>
  1304. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1305. <B>void waitForDelayed(StatusRequest* aStatusRequest, unsigned long
  1306. aInterval = 0, long aIterations = 1)</B></P>
  1307. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1308. <BR>
  1309. </P>
  1310. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1311. compiled with support for Status Requests, these methods make task
  1312. wait for the completion of <B>aStatusRequest</B> event.
  1313. </P>
  1314. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">By
  1315. default <B>waitFor() </B>sets tasks interval to <B>0 (zero)</B> for
  1316. immediate execution when event happens, and also sets the number of
  1317. <B>iterations to 1</B>. However, you can specify different interval
  1318. and number of iterations.
  1319. </P>
  1320. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">By
  1321. default <B>waitForDelayed() </B>sets tasks interval to a supplied
  1322. value or (if omitted or zero) keeps the current interval, so delayed
  1323. execution will take place when the event happens. It also sets the
  1324. number of <B>iterations to 1</B> by default if not supplied.
  1325. </P>
  1326. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">When
  1327. Status Request object completes, all tasks waiting on it are executed
  1328. during next scheduling pass. Tasks waiting via <B>waitFor</B>()
  1329. method are executed immediately. Tasks waiting via <B>waitForDelayed</B>()
  1330. method are activated, but executed after current or supplied interval
  1331. delay.
  1332. </P>
  1333. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  1334. aStatusRequest</B> should be “activated” by calling <B>setWaiting()
  1335. </B>method before making a task wait on it. Otherwise, the task will
  1336. execute immediately.
  1337. </P>
  1338. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">The
  1339. sequence of events to use Status Request object is as follows:</P>
  1340. <OL>
  1341. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Create a status
  1342. request object</P>
  1343. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Activate status
  1344. request object (calling its <B>setWaiting</B>() method)</P>
  1345. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Set up tasks to
  1346. wait of the event completion</P>
  1347. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Signal completion
  1348. of event(s)</P>
  1349. </OL>
  1350. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1351. </P>
  1352. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1353. </P>
  1354. <P CLASS="western" STYLE="margin-bottom: 0in"><B>StatusRequest*
  1355. getStatusRequest()</B></P>
  1356. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  1357. a StatusReqeust object this Task was waiting on.
  1358. </P>
  1359. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1360. </P>
  1361. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1362. </P>
  1363. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1364. <B>TASK ID, CONTROL POINTS METHODS:</B></P>
  1365. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1366. <BR>
  1367. </P>
  1368. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1369. <B>void setId(unsigned int aID);</B></P>
  1370. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1371. <BR>
  1372. </P>
  1373. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1374. compiled with support for Task IDs, this method will set the task ID
  1375. explicitly.
  1376. </P>
  1377. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Calling
  1378. this method is not necessary as task IDs are assigned automatically
  1379. during task creation: 1, 2, 3, …</P>
  1380. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1381. </P>
  1382. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1383. <B>unsigned int getId();</B></P>
  1384. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1385. <BR>
  1386. </P>
  1387. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1388. compiled with support for Task IDs, this method return current task’s
  1389. ID.</P>
  1390. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1391. </P>
  1392. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1393. <B>void setControlPoint (unsigned int aPoint);</B></P>
  1394. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1395. <BR>
  1396. </P>
  1397. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1398. compiled with support for Task IDs, this method will set a control
  1399. point in the task’s code. Control points are similar to “try…catch”
  1400. blocks, with control point ID specifying where in the code the “try”
  1401. part started, and a mechanism like watchdog timer providing the
  1402. “catch” functionality.</P>
  1403. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1404. </P>
  1405. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1406. <B>unsigned int getControlPoint()</B></P>
  1407. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1408. <BR>
  1409. </P>
  1410. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1411. If compiled with support for Task IDs, this method will return
  1412. currently set control point for this task.</P>
  1413. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1414. <BR>
  1415. </P>
  1416. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1417. <BR>
  1418. </P>
  1419. <P CLASS="western" STYLE="margin-bottom: 0in"><B>LOCAL TASK STORAGE
  1420. METHODS:</B></P>
  1421. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1422. <BR>
  1423. </P>
  1424. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1425. <B>void setLtsPointer(void *aPtr);</B></P>
  1426. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1427. <BR>
  1428. </P>
  1429. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1430. compiled with support for LTS, this method will set the task's local
  1431. storage pointer.</P>
  1432. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1433. </P>
  1434. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1435. <B>void *getLtsPointer();</B></P>
  1436. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1437. <BR>
  1438. </P>
  1439. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1440. compiled with support for LTS, this method will return reference to
  1441. the task's local storage.</P>
  1442. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  1443. </B>the value returned has type (void *), and needs to be re-cast
  1444. into appropriate pointer type. Please refer to example sketches for
  1445. implementation options.
  1446. </P>
  1447. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1448. </P>
  1449. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1450. </P>
  1451. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1452. <BR>
  1453. </P>
  1454. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-inside: avoid; page-break-after: avoid">
  1455. <FONT SIZE=4><B>TASK SCHEDULER:</B></FONT></P>
  1456. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-inside: avoid; page-break-after: avoid">
  1457. <BR>
  1458. </P>
  1459. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-inside: avoid; page-break-after: avoid">
  1460. <B>CREATION:</B></P>
  1461. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-inside: avoid; page-break-after: avoid">
  1462. <BR>
  1463. </P>
  1464. <P CLASS="western" STYLE="margin-bottom: 0in"><B>Scheduler()</B><BR><BR>
  1465. </P>
  1466. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Default
  1467. constructor.
  1468. </P>
  1469. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Takes
  1470. no parameters. Creates task scheduler with default parameters and an
  1471. empty task queue.
  1472. </P>
  1473. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1474. </P>
  1475. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void init()</B></P>
  1476. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1477. </P>
  1478. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Initializes
  1479. the task queue and scheduler parameters, Executed as part of
  1480. constructor, so don't need to be explicitly called after creation.</P>
  1481. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  1482. </B>be default (if compiled with <FONT FACE="Courier New, monospace">_TASK_TIMECRITICAL</FONT>
  1483. enabled) scheduler is allowed to put processor to IDLE sleep mode. If
  1484. this behavior was changed via <B>allowSleep()</B> method, <B>inti()
  1485. </B>will <B>NOT</B> reset allow sleep particular parameter.
  1486. </P>
  1487. <P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void
  1488. addTask(Task&amp; aTask)</B></P>
  1489. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1490. </P>
  1491. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Adds
  1492. task aTask to the execution queue (or chain) of tasks by appending it
  1493. to the end of the chain. If two tasks are scheduled for execution,
  1494. the sequence will match the order in which tasks were appended to the
  1495. chain. However, in reality, due to different timing of task
  1496. execution, the actual order may be different.
  1497. </P>
  1498. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  1499. </B>Currently, changing the execution sequence in a chain dynamically
  1500. is not supported.
  1501. </P>
  1502. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1503. you need to reorder the chain sequence – initialize the scheduler
  1504. and re-add the tasks in a different order.
  1505. </P>
  1506. <P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void
  1507. deleteTask(Task&amp; aTask)</B></P>
  1508. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1509. </P>
  1510. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Deletes
  1511. task aTask from the execution chain. The chain of remaining tasks is
  1512. linked together (i.e</P>
  1513. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">if
  1514. original task chain is 1 → 2 → 3 → 4, deleting 3 will result in
  1515. 1 → 2 → 4).</P>
  1516. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  1517. </B>it is not required to delete a task from the chain. A disabled
  1518. task will not be executed anyway, but you save a few microseconds per
  1519. scheduling pass by deleting it, since it is not even considered for
  1520. execution.
  1521. </P>
  1522. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">An
  1523. example of proper use of this method would be running some sort of
  1524. <B>initialize</B> task in the chain, and then deleting it from the
  1525. chain since it only needs to run once.
  1526. </P>
  1527. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1528. </P>
  1529. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void allowSleep(bool
  1530. aState) </B>
  1531. </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">Available
  1535. in API only if compiled with <FONT FACE="Courier New, monospace">_TASK_TIMECRITICAL</FONT>
  1536. enabled. Controls whether scheduler is allowed (<B>aState =true</B>),
  1537. or not (<B>aState =false</B>) to put processor into IDLE sleep mode
  1538. in case not tasks are scheduled to run.</P>
  1539. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">The
  1540. <B>default</B> behavior of scheduler upon creation is to allow sleep
  1541. mode.
  1542. </P>
  1543. <P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void enableAll()</B></P>
  1544. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void disableAll()</B></P>
  1545. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1546. </P>
  1547. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">enables
  1548. and disables (respectively) all tasks in the chain. Convenient if
  1549. your need to enable/disable majority of the tasks (i.e. disable all
  1550. and then enable one).
  1551. </P>
  1552. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1553. <BR><B>Task&amp; currentTask()<BR></B><BR>
  1554. </P>
  1555. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  1556. reference to the task, currently executing via <B>execute()</B> loop
  1557. <B>OR </B>for OnEnable and OnDisable methods, reference to the task
  1558. being enabled or disabled.
  1559. </P>
  1560. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">This
  1561. distinction is important because one task can activate the other, and
  1562. OnEnable should be referring to the task being enabled, not being
  1563. executed.
  1564. </P>
  1565. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Could
  1566. be used by callback methods to identify which Task actually invoked
  1567. this callback method.</P>
  1568. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1569. </P>
  1570. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1571. <B>void* currentLts()<BR></B><BR>
  1572. </P>
  1573. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  1574. pointer to Local Task Storage of the task, currently executing via
  1575. <B>execute()</B> loop <B>OR </B>for OnEnable and OnDisable methods,
  1576. task being enabled or disabled.
  1577. </P>
  1578. <P CLASS="western" STYLE="margin-bottom: 0in"><BR><B>void execute()</B></P>
  1579. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1580. </P>
  1581. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Executes
  1582. one scheduling pass, including end-of-pass sleep. This method is
  1583. typically placed inside the <B>loop()</B> method of the sketch. Since
  1584. <B>execute</B> exits after every pass, you can put additional
  1585. statements after <B>execute</B> inside the <B>loop().</B></P>
  1586. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Generally,
  1587. execute will perform the following steps:</P>
  1588. <OL>
  1589. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Ignore task
  1590. completely if it is disabled.</P>
  1591. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Disable task if it
  1592. ran out of iterations (calling OnDesable, if necessary).</P>
  1593. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Check if task is
  1594. waiting on a StatusRequest object, and make appropriate scheduling
  1595. arrangements</P>
  1596. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Perform necessary
  1597. timing calculations</P>
  1598. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Invoke task's
  1599. callback method, if it is time to do so, and one is provided.
  1600. </P>
  1601. <LI><P CLASS="western" STYLE="margin-bottom: 0in">Put
  1602. microcontroller to sleep (if requested and supported) if none of the
  1603. tasks were invoked.
  1604. </P>
  1605. </OL>
  1606. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1607. </P>
  1608. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Please
  1609. NOTE:</B> schedule-related calculations are performed prior to task's
  1610. callback method invocation. This allows tasks to manipulate their
  1611. runtime parameters (like execution interval) directly.</P>
  1612. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1613. </P>
  1614. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool isOverrun()</B></P>
  1615. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">If
  1616. library is compiled with <FONT FACE="Courier New, monospace">_TASK_TIMECRITICAL</FONT>
  1617. enabled, this method returns <B>true</B> if currently invoked task
  1618. has overrun its scheduled start time when it was invoked. Returns
  1619. <B>false</B> if task has been invoked according to schedule.</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"><BR>
  1623. </P>
  1624. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1625. <B>STATUS REQUEST:</B></P>
  1626. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1627. <BR>
  1628. </P>
  1629. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1630. <B>CREATION:</B></P>
  1631. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-after: avoid">
  1632. <BR>
  1633. </P>
  1634. <P CLASS="western" STYLE="margin-bottom: 0in"><B>StatusRequest()</B><BR><BR>
  1635. </P>
  1636. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Default
  1637. constructor.
  1638. </P>
  1639. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Takes
  1640. no parameters. Creates Status Request object, which is assigned a
  1641. status of “completed” on creation.
  1642. </P>
  1643. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1644. </P>
  1645. <P CLASS="western" STYLE="margin-bottom: 0in"><B>void
  1646. setWaiting(unsigned int aCount = 1)</B></P>
  1647. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Activates
  1648. Status Request object. By default each object is set to wait on one
  1649. event only, however, if <B>aCount</B> is supplied, Status Request can
  1650. wait on multiple events. For instance, <B>setWaiting(3)</B> will wait
  1651. on three signals. An example could be waiting for completion of
  1652. measurements from 3 sensors.
  1653. </P>
  1654. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1655. </P>
  1656. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool signal(int
  1657. aStatus)</B></P>
  1658. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Signals
  1659. completion of the event to the Status Request object, and passes a
  1660. completion code, which could be interrogated later.
  1661. </P>
  1662. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note:
  1663. </B> passing a <B>negative</B> status code to the status request
  1664. object is considered reporting an error condition, and will complete
  1665. the status request regardless of how many outstanding signals it is
  1666. still waiting for.
  1667. </P>
  1668. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><B>Note</B>:
  1669. only the latest status code is kept.</P>
  1670. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1671. </P>
  1672. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool signalComplete
  1673. (int aStatus)</B></P>
  1674. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Signals
  1675. completion of <B>ALL</B> events to the Status Request object, and
  1676. passes a completion code, which could be interrogated later. The
  1677. status request completes regardless of how many events it is still
  1678. waiting on.
  1679. </P>
  1680. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1681. </P>
  1682. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool pending() </B>
  1683. </P>
  1684. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  1685. <B>true</B> if status request is still waiting for event or events to
  1686. happen.</P>
  1687. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1688. </P>
  1689. <P CLASS="western" STYLE="margin-bottom: 0in"><B>bool completed () </B>
  1690. </P>
  1691. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  1692. <B>true</B> if status has completed.</P>
  1693. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1694. </P>
  1695. <P CLASS="western" STYLE="margin-bottom: 0in"><B>int getStatus()</B></P>
  1696. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Returns
  1697. the status code passed to the status request object by the <B>signal()
  1698. </B>and <B>signalComplete() </B>methods.
  1699. </P>
  1700. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Any
  1701. <B>positive</B> number is considered a successful completion status.</P>
  1702. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">A
  1703. 0 (zero) is considered a default successful completion status.</P>
  1704. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">Any
  1705. <B>negative</B> number is considered an error code and unsuccessful
  1706. completion of a request.</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-bottom: 0in; page-break-after: avoid">
  1714. <B>CONSTANTS:</B></P>
  1715. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1716. </P>
  1717. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1718. <B>TASK_SECOND (1000)</B></P>
  1719. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1720. interval of 1 second</P>
  1721. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  1722. </P>
  1723. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1724. <B>TASK_MINUTE (60000)</B></P>
  1725. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1726. interval of 1 minute</P>
  1727. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  1728. </P>
  1729. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1730. <B>TASK_HOUR (3600000)</B></P>
  1731. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1732. interval of 1 hour</P>
  1733. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1734. <B>TASK_FOREVER (-1)</B></P>
  1735. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1736. number of iterations for infinite number of iterations</P>
  1737. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  1738. </P>
  1739. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1740. <B>TASK_ONCE (1)</B></P>
  1741. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1742. single iteration</P>
  1743. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  1744. </P>
  1745. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in; page-break-after: avoid">
  1746. <B>TASK_IMMEDIATE (0)</B></P>
  1747. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">Task
  1748. interval for immediate execution</P>
  1749. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1750. </P>
  1751. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1752. </P>
  1753. <P CLASS="western" STYLE="margin-bottom: 0in; page-break-before: always; page-break-after: avoid">
  1754. <B>IMPLEMENTATION SCENARIOS AND IDEAS:</B></P>
  1755. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1756. </P>
  1757. <OL>
  1758. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><B>EVENT DRIVEN
  1759. PROGRAMMING</B></P>
  1760. </OL>
  1761. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1762. </P>
  1763. <P CLASS="western" STYLE="margin-bottom: 0in">Each of the processes
  1764. of your application becomes a separate and distinct programming area,
  1765. which may or may not interact and control each other.
  1766. </P>
  1767. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1768. </P>
  1769. <P CLASS="western" STYLE="margin-bottom: 0in">Example:
  1770. </P>
  1771. <P CLASS="western" STYLE="margin-bottom: 0in">In a plant watering
  1772. system you need to measure soil humidity, control pump and display
  1773. the results</P>
  1774. <P CLASS="western" STYLE="margin-bottom: 0in">Each of the areas
  1775. becomes a task:</P>
  1776. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1777. </P>
  1778. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1779. <B>tMeasure</B> (TMEASURE_INTERVAL*SECOND, TASK_FOREVER,
  1780. &amp;measureCallback);<BR>Task <B>tWater</B>
  1781. (TWATER_INTERVAL*SECOND, RETRIES, &amp;waterCallback);<BR>Task
  1782. <B>tDisplay</B> (TDISPLAY_INTERVAL*SECOND, TASK_FOREVER,
  1783. &amp;displayCallback); <BR></FONT></FONT><BR>
  1784. </P>
  1785. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler
  1786. </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>
  1787. </P>
  1788. <P CLASS="western" STYLE="margin-bottom: 0in">Further, once you turn
  1789. on the pump, you keep it running for TWATER_INTERVAL interval and
  1790. then turn it off. Turning off a pump is also a task which only needs
  1791. to run once for every time the pump is turned on:</P>
  1792. <P CLASS="western" STYLE="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>Task
  1795. </FONT></FONT><FONT FACE="Courier New, monospace"><FONT SIZE=2><B>tWaterOff</B></FONT></FONT><FONT FACE="Courier New, monospace"><FONT SIZE=2>
  1796. (WATERTIME*SECOND, TASK_ONCE,</FONT></FONT><FONT FACE="Courier New, monospace">
  1797. &amp;waterOffCallback);</FONT><BR><BR>
  1798. </P>
  1799. <P CLASS="western" STYLE="margin-bottom: 0in">Example of the callback
  1800. method:</P>
  1801. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1802. </P>
  1803. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1804. waterOffCallback() {<BR> motorOff();<BR>
  1805. tWater.enableDelayed();<BR>}</FONT></FONT></P>
  1806. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1807. </P>
  1808. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>or</FONT></FONT></P>
  1809. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1810. </P>
  1811. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1812. waterCallback() {<BR> if (tWater.getIterations()) {</FONT></FONT></P>
  1813. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1814. If this is not the last iteration = turn the pump on<BR>
  1815. motorOn();<BR> tWaterOff.set(parameters.watertime * TASK_SECOND,
  1816. TASK_ONCE, &amp;waterOffCallback);<BR>
  1817. tWaterOff.enableDelayed();<BR> return;<BR> }</FONT></FONT></P>
  1818. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  1819. We could not reach target humidity – something is wrong<BR>
  1820. motorOff;<BR> taskManager.disableAll();<BR> tError.enable();<BR>}</FONT></FONT></P>
  1821. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1822. </P>
  1823. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Your
  1824. sample <B>setup</B>() and <B>loop</B>() (partially) are as follows. </FONT>
  1825. </P>
  1826. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>Note:
  1827. </B>please note that tWater is <B>not</B> activated during setup().
  1828. It is activated by tMeasure callback once the watering conditions are
  1829. met. </FONT>
  1830. </P>
  1831. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1832. </P>
  1833. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT SIZE=2><FONT FACE="Times New Roman, serif"> void
  1834. </FONT><FONT FACE="Courier New, monospace">setup()</FONT></FONT></P>
  1835. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>
  1836. ...</FONT></FONT></P>
  1837. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  1838. <FONT SIZE=2><FONT FACE="Courier New, monospace">tWater.setIterations(parameters.retries);<BR>
  1839. tWaterOff.setInterval(parameters.watertime * SECOND);<BR><BR><BR>
  1840. taskManager.init();<BR> taskManager.addTask(tMeasure);<BR>
  1841. taskManager.addTask(tDisplay);<BR> taskManager.addTask(tWater);<BR>
  1842. taskManager.addTask(tWaterOff);<BR> <BR> tMeasure.enable();<BR>
  1843. tDisplay.enable();<BR><BR> currentHumidity =
  1844. measureHumidity();<BR>}<BR><BR><BR>void loop ()<BR>{<BR>
  1845. taskManager.execute();<BR>}</FONT><FONT FACE="Times New Roman, serif"><BR></FONT></FONT><BR>
  1846. </P>
  1847. <OL START=2>
  1848. <LI><P CLASS="western" STYLE="margin-bottom: 0in">“<FONT FACE="Times New Roman, serif"><B>NATIVE”
  1849. SUPPORT FOR FINITE STATE MACHINE</B></FONT></P>
  1850. </OL>
  1851. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1852. </P>
  1853. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Define
  1854. “states” as callback method or methods. Each callback method
  1855. executes activities specific to a “state” and then “transitions”
  1856. to the next state by assigning next callback method to the task. </FONT>
  1857. </P>
  1858. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Transition
  1859. from one state to the next is achieved by setting next callback
  1860. method at the end of preceding one. </FONT>
  1861. </P>
  1862. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>Note:
  1863. </B>do not call the next callback method explicitly. Yield to the
  1864. scheduler, and let the scheduler take care of next iteration during
  1865. the next pass. (Thus giving other tasks change to run their callback
  1866. methods). </FONT>
  1867. </P>
  1868. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1869. </P>
  1870. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Example:
  1871. Blinking LED 2 times a second could be achieved this way</FONT></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>Scheduler
  1875. ts;</FONT></FONT></P>
  1876. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1877. tLedBlinker (500, TASK_FOREVER, &amp;ledOnCallback, &amp;ts, true);</FONT></FONT></P>
  1878. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1879. </P>
  1880. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1881. ledOnCallback() {</FONT></FONT></P>
  1882. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> turnLedOn();</FONT></FONT></P>
  1883. <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>
  1884. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1885. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1886. </P>
  1887. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1888. ledOffCallback() {</FONT></FONT></P>
  1889. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> turnLedOff();</FONT></FONT></P>
  1890. <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>
  1891. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1892. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1893. </P>
  1894. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>setup()
  1895. {</FONT></FONT></P>
  1896. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1897. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1898. </P>
  1899. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>loop
  1900. () {</FONT></FONT></P>
  1901. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> ts.execute();</FONT></FONT></P>
  1902. <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>
  1903. </P>
  1904. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Obviously
  1905. the example is simple, but gives the idea of how the tasks could be
  1906. used to go through states.</FONT></P>
  1907. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1908. </P>
  1909. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1910. </P>
  1911. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1912. </P>
  1913. <OL START=3>
  1914. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>MULTIPLE
  1915. POSSIBLE CALLBACKS FOR TASK</B></FONT></P>
  1916. </OL>
  1917. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1918. </P>
  1919. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">There
  1920. may be a need to select an option for callback method based on
  1921. certain criteria, or randomly. </FONT>
  1922. </P>
  1923. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">You
  1924. can achieve that by defining an array of callback method pointers and
  1925. selecting one based on the criteria you need. </FONT>
  1926. </P>
  1927. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Example:
  1928. when a robot detects an obstacle, it may go left, right backwards,
  1929. etc. Each of the “directions” or “behaviors” are represented
  1930. by a different callback methods. </FONT>
  1931. </P>
  1932. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1933. </P>
  1934. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Another
  1935. example of using multiple callbacks:</FONT></P>
  1936. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">You
  1937. may need to “initialize” variables for a particular task.</FONT></P>
  1938. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">In
  1939. this case, define a tasks with two callbacks:</FONT></P>
  1940. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1941. </P>
  1942. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  1943. <B>tWork</B> (T_INTERVAL, TASK_FOREVER, &amp;workCallbackInit);</FONT></FONT></P>
  1944. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">…</P>
  1945. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1946. </P>
  1947. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1948. workCallbackInit() {</FONT></FONT></P>
  1949. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
  1950. do your initializationstuff here</FONT></FONT></P>
  1951. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> </FONT></FONT></P>
  1952. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
  1953. finally assigne the main callback method </FONT></FONT>
  1954. </P>
  1955. <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>
  1956. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1957. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  1958. </P>
  1959. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1960. workCallback() {</FONT></FONT></P>
  1961. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
  1962. main callback method</FONT></FONT></P>
  1963. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> …</FONT></FONT></P>
  1964. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1965. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1966. </P>
  1967. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">The
  1968. task will initialize during first execution pass and switch to
  1969. “regular” callback execution starting with second pass. There is
  1970. a delay between first and second passes of the task (scheduling
  1971. period, if defined). In order to execute the second pass immediately
  1972. after initialization first pass, change the above code like this:</FONT></P>
  1973. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1974. </P>
  1975. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  1976. workCallbackInit() {</FONT></FONT></P>
  1977. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
  1978. do your initializationstuff here</FONT></FONT></P>
  1979. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> </FONT></FONT></P>
  1980. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> //
  1981. finally assigne the main callback method </FONT></FONT>
  1982. </P>
  1983. <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>
  1984. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> tWork.enable();</FONT></FONT></P>
  1985. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  1986. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1987. </P>
  1988. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">The
  1989. task will run initialization first, then immediately second pass, and
  1990. then switch to processing at regular intervals starting with a third
  1991. pass. </FONT>
  1992. </P>
  1993. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  1994. </P>
  1995. <OL START=3>
  1996. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>INTERRUP-DRIVEN
  1997. EXECUTION SUPPORT </B></FONT>
  1998. </P>
  1999. </OL>
  2000. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  2001. </P>
  2002. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">In
  2003. case of interrupt-driven program flow, tasks could be scheduled to
  2004. run once to request asynchronous execution (request), and then
  2005. re-enabled (restarted) again with a different callback method to
  2006. process the results. </FONT>
  2007. </P>
  2008. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  2009. </P>
  2010. <P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>Example</B>:
  2011. event driven distance calculation for ultrasonic pulses. EchoPin #6
  2012. triggers pin change interrupts on rising and falling edges to
  2013. determine the length of ultrasonic pulse.</FONT></P>
  2014. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  2015. </P>
  2016. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#include
  2017. &lt;DirectIO.h&gt;<BR>#include &lt;TaskScheduler.h&gt;<BR>#include
  2018. &lt;PinChangeInt.h&gt;<BR><BR><BR>#define TRIGGERPIN 5<BR>#define
  2019. ECHOPIN 6<BR><BR>Output&lt;TRIGGERPIN&gt;
  2020. pTrigger;<BR>Input&lt;ECHOPIN&gt; pEcho;<BR><BR>Scheduler
  2021. r;<BR><BR>Task tMeasure(TASK_SECOND, TASK_FOREVER, &amp;measureCallback,
  2022. &amp;r, true);<BR>Task tDisplay(TASK_SECOND, TASK_FOREVER,
  2023. &amp;displayCallback, &amp;r, true);<BR>Task tPing(TASK_IMMEDIATE,
  2024. TASK_ONCE, &amp;pingCalcCallback, &amp;r, false);<BR><BR><BR>volatile
  2025. bool pulseBusy = false;<BR>volatile bool pulseTimeout =
  2026. false;<BR>volatile unsigned long pulseStart = 0;<BR>volatile unsigned
  2027. long pulseStop = 0;<BR>volatile unsigned long pingDistance =
  2028. 0;<BR><BR><BR>void pingTrigger(unsigned long aTimeout) {<BR> if
  2029. (pulseBusy) return; // do not trigger if in the middle of a pulse<BR>
  2030. if (pEcho == HIGH) return; // do not trigger if ECHO pin is high<BR>
  2031. <BR> pulseBusy = true;<BR> pulseTimeout = false;</FONT></FONT></P>
  2032. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2><BR>
  2033. pTrigger = LOW;<BR> delayMicroseconds(4);<BR> pTrigger = HIGH;<BR><BR>
  2034. tPing.setInterval (aTimeout);<BR><BR> delayMicroseconds(10);<BR>
  2035. pTrigger = LOW; <BR></FONT></FONT><BR>
  2036. </P>
  2037. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2038. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tPing.restartDelayed();
  2039. // timeout countdown starts now<BR></FONT></FONT><BR>
  2040. </P>
  2041. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2042. will start the pulse clock on the rising edge of ECHO pin</FONT></FONT></P>
  2043. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2044. <FONT FACE="Courier New, monospace"><FONT SIZE=2>PCintPort::attachInterrupt(ECHOPIN,
  2045. &amp;pingStartClock, RISING); <BR>}<BR><BR></FONT></FONT><BR>
  2046. </P>
  2047. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2048. Start clock on the <B>rising</B> edge of the ultrasonic pulse<BR>void
  2049. pingStartClock() {<BR> pulseStart = micros();<BR>
  2050. PCintPort::detachInterrupt(ECHOPIN); // not sure this is necessary<BR>
  2051. PCintPort::attachInterrupt(ECHOPIN, &amp;pingStopClock, FALLING); <BR>
  2052. tPing.restartDelayed();<BR>}<BR><BR>// Stop clock on the <B>falling</B>
  2053. edge of the ultrasonic pulse<BR>void pingStopClock() {<BR> pulseStop
  2054. = micros();<BR> PcintPort::detachInterrupt(ECHOPIN);</FONT></FONT></P>
  2055. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2056. <FONT FACE="Courier New, monospace"><FONT SIZE=2>pingDistance =
  2057. pulseStop - pulseStart;<BR> pulseBusy = false;<BR> tPing.disable();
  2058. // disable timeout<BR>}<BR></FONT></FONT><BR>
  2059. </P>
  2060. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2061. Stop clock because of the timeout – the wave did not return<BR>void
  2062. pingCalcCallback() {<BR> if (pulseBusy) {<BR> pingStopClock();<BR>
  2063. }<BR> pulseTimeout = true;<BR>}<BR><BR><BR><BR>// Initial measure
  2064. callback sets the trigger<BR>void measureCallback() {<BR> if
  2065. (pulseBusy) { // already measuring, try again<BR>
  2066. tMeasure.enable();<BR> return;<BR> }<BR> pingTrigger(30); // 30
  2067. milliseconds or max range of ~5.1 meters<BR>
  2068. tMeasure.setCallback(&amp;measureCallbackWait);<BR>}<BR></FONT></FONT><BR>
  2069. </P>
  2070. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2071. Wait for the measurement to <BR>void measureCallbackWait() {<BR> if
  2072. (pulseBusy) return;<BR> tMeasure.setCallback(&amp;measureCallback);
  2073. <BR>}<BR><BR><BR>bool state = true;<BR><BR>void displayCallback() {<BR>
  2074. char d[256];<BR> <BR> unsigned long cm = pingDistance * 17 / 100;
  2075. // cm<BR> <BR> snprintf(d, 256, &quot;pulseStart =
  2076. %8lu\tpulseStop=%8lu\tdistance, cm=%8lu&quot;, pulseStart, pulseStop,
  2077. cm);<BR> Serial.println(d);<BR> <BR>}<BR><BR>void setup() {<BR> //
  2078. put your setup code here, to run once:<BR> <BR>
  2079. Serial.begin(115200);<BR> <BR><BR> pTrigger = LOW;<BR> pEcho =
  2080. LOW;<BR> <BR>}<BR><BR>void loop() {<BR> // put your main code here,
  2081. to run repeatedly:<BR> r.execute();<BR>}<BR></FONT></FONT><BR>
  2082. </P>
  2083. <OL START=3>
  2084. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>USING
  2085. ONENABLE AND ONDISBALE METHODS </B></FONT>
  2086. </P>
  2087. </OL>
  2088. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  2089. </P>
  2090. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Consider
  2091. a task to flash onboard LED for 5 seconds with random frequency. Task
  2092. should be repeated every 30 seconds indefinitely. Since frequency is
  2093. random, there are two challenges:</FONT></P>
  2094. <OL START=3>
  2095. <OL>
  2096. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">We
  2097. need to make sure LED is turned OFF at the last iteration</FONT></P>
  2098. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif">We
  2099. need to calculate random frequency every time</FONT></P>
  2100. </OL>
  2101. </OL>
  2102. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2103. </P>
  2104. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Times New Roman, serif">Below
  2105. is the implementation using TaskScheduler </FONT>
  2106. </P>
  2107. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2108. </P>
  2109. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2><BR></FONT></FONT><BR>
  2110. </P>
  2111. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
  2112. _TASK_SLEEP_ON_IDLE_RUN</FONT></FONT></P>
  2113. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#include
  2114. &lt;TaskScheduler.h&gt;</FONT></FONT></P>
  2115. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2116. </P>
  2117. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
  2118. LEDPIN 13</FONT></FONT></P>
  2119. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2120. </P>
  2121. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2122. </P>
  2123. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler
  2124. ts;</FONT></FONT></P>
  2125. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2126. </P>
  2127. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  2128. tWrapper(30000, TASK_FOREVER, &amp;WrapperCallback, &amp;ts, true);</FONT></FONT></P>
  2129. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  2130. tBlink(5000, TASK_ONCE, NULL, &amp;ts, false, &amp;BlinkOnEnable,
  2131. &amp;BlinkOnDisable);</FONT></FONT></P>
  2132. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  2133. tLED(TASK_IMMEDIATE, TASK_FOREVER, NULL, &amp;ts, false, NULL,
  2134. &amp;LEDOff);</FONT></FONT></P>
  2135. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2136. </P>
  2137. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2138. WrapperCallback() {</FONT></FONT></P>
  2139. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2140. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;In
  2141. WrapperCallback&quot;);</FONT></FONT></P>
  2142. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2143. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tBlink.restartDelayed();
  2144. // LED blinking is initiated </FONT></FONT>
  2145. </P>
  2146. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2147. <FONT FACE="Courier New, monospace"><FONT SIZE=2>//every
  2148. 30 seconds for 5 seconds</FONT></FONT></P>
  2149. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2150. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2151. </P>
  2152. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2153. </P>
  2154. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2155. Upon being enabled, tBlink will define the parameters</FONT></FONT></P>
  2156. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2157. and enable LED blinking task, which actually controls</FONT></FONT></P>
  2158. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2159. the hardware (LED in this example)</FONT></FONT></P>
  2160. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
  2161. BlinkOnEnable() {</FONT></FONT></P>
  2162. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2163. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;In
  2164. BlinkOnEnable&quot;);</FONT></FONT></P>
  2165. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2166. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setInterval(
  2167. 500 + random(501) );</FONT></FONT></P>
  2168. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2169. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setCallback(
  2170. &amp;LEDOn);</FONT></FONT></P>
  2171. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2172. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.enable();</FONT></FONT></P>
  2173. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2174. </P>
  2175. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2176. <FONT FACE="Courier New, monospace"><FONT SIZE=2>return true; //
  2177. Task should be enabled</FONT></FONT></P>
  2178. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2179. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2180. </P>
  2181. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2182. tBlink does not really need a callback method</FONT></FONT></P>
  2183. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2184. since it just waits for 5 seconds for the first </FONT></FONT>
  2185. </P>
  2186. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2187. and only iteration to occur. Once the iteration</FONT></FONT></P>
  2188. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2189. takes place, tBlink is disabled by the Scheduler, </FONT></FONT>
  2190. </P>
  2191. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2192. thus executing its OnDisable method below.</FONT></FONT></P>
  2193. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2194. </P>
  2195. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2196. BlinkOnDisable() {</FONT></FONT></P>
  2197. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2198. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;In
  2199. BlinkOnDisable&quot;);</FONT></FONT></P>
  2200. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2201. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.disable();</FONT></FONT></P>
  2202. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2203. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2204. </P>
  2205. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2206. LEDOn () {</FONT></FONT></P>
  2207. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2208. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;In
  2209. LEDOn&quot;);</FONT></FONT></P>
  2210. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2211. <FONT FACE="Courier New, monospace"><FONT SIZE=2>digitalWrite(LEDPIN,
  2212. HIGH);</FONT></FONT></P>
  2213. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2214. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setCallback(
  2215. &amp;LEDOff);</FONT></FONT></P>
  2216. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2217. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2218. </P>
  2219. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2220. LEDOff () {</FONT></FONT></P>
  2221. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2222. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;In
  2223. LEDOff&quot;);</FONT></FONT></P>
  2224. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2225. <FONT FACE="Courier New, monospace"><FONT SIZE=2>digitalWrite(LEDPIN,
  2226. LOW);</FONT></FONT></P>
  2227. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2228. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tLED.setCallback(
  2229. &amp;LEDOn);</FONT></FONT></P>
  2230. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2231. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2232. </P>
  2233. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2234. Note that LEDOff method serves as OnDisable method</FONT></FONT></P>
  2235. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2236. to make sure the LED is turned off when the tBlink</FONT></FONT></P>
  2237. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2238. task finishes (or disabled ahead of time)</FONT></FONT></P>
  2239. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2240. </P>
  2241. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2242. setup() {</FONT></FONT></P>
  2243. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2244. </P>
  2245. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2246. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.begin(115200);
  2247. </FONT></FONT>
  2248. </P>
  2249. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2250. <FONT FACE="Courier New, monospace"><FONT SIZE=2>pinMode(LEDPIN,
  2251. OUTPUT); </FONT></FONT>
  2252. </P>
  2253. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2254. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2255. </P>
  2256. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2257. loop() {</FONT></FONT></P>
  2258. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2259. <FONT FACE="Courier New, monospace"><FONT SIZE=2>// put your main
  2260. code here, to run repeatedly:</FONT></FONT></P>
  2261. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2262. </P>
  2263. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2264. <FONT FACE="Courier New, monospace"><FONT SIZE=2>ts.execute();</FONT></FONT></P>
  2265. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2266. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2267. </P>
  2268. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2269. </P>
  2270. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2271. </P>
  2272. <OL START=3>
  2273. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>USING
  2274. STATUS REQUEST OBJECTS </B></FONT>
  2275. </P>
  2276. </OL>
  2277. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2278. </P>
  2279. <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
  2280. test emulates querying 3 sensors once every 10 seconds, each could
  2281. respond with a different delay (ultrasonic sensors for instance) and
  2282. printing a min value of the three when all three have reported their
  2283. values.</FONT></FONT></P>
  2284. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2285. </P>
  2286. <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
  2287. overall timeout of 1 second is setup as well.</FONT></FONT></P>
  2288. <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
  2289. error message needs to be printed if a timeout occurred instead of a
  2290. value.</FONT></FONT></P>
  2291. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2292. </P>
  2293. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2294. </P>
  2295. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
  2296. _TASK_SLEEP_ON_IDLE_RUN</FONT></FONT></P>
  2297. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#define
  2298. _TASK_STATUS_REQUEST</FONT></FONT></P>
  2299. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2300. </P>
  2301. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>#include
  2302. &lt;TaskScheduler.h&gt;</FONT></FONT></P>
  2303. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2304. </P>
  2305. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>StatusRequest
  2306. measure;</FONT></FONT></P>
  2307. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2308. </P>
  2309. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler
  2310. ts; </FONT></FONT>
  2311. </P>
  2312. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2313. </P>
  2314. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  2315. tCycle(10000, TASK_FOREVER, &amp;CycleCallback, &amp;ts, true);</FONT></FONT></P>
  2316. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  2317. tMeasure(TASK_SECOND, TASK_ONCE, &amp;MeasureCallback, &amp;ts,
  2318. false, &amp;MeasureEnable, &amp;MeasureDisable);</FONT></FONT></P>
  2319. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  2320. tCalculate(&amp;CalcCallback, &amp;ts);</FONT></FONT></P>
  2321. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  2322. tSensor1(TASK_IMMEDIATE, TASK_ONCE, &amp;S1Callback, &amp;ts, false,
  2323. &amp;S1Enable);</FONT></FONT></P>
  2324. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  2325. tSensor2(TASK_IMMEDIATE, TASK_ONCE, &amp;S2Callback, &amp;ts, false,
  2326. &amp;S2Enable);</FONT></FONT></P>
  2327. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Task
  2328. tSensor3(TASK_IMMEDIATE, TASK_ONCE, &amp;S3Callback, &amp;ts, false,
  2329. &amp;S3Enable);</FONT></FONT></P>
  2330. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2331. </P>
  2332. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2333. </P>
  2334. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>long
  2335. distance, d1, d2, d3;</FONT></FONT></P>
  2336. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2337. </P>
  2338. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2339. CycleCallback() {</FONT></FONT></P>
  2340. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2341. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;CycleCallback:
  2342. Initiating measurement cycle every 10 seconds&quot;);</FONT></FONT></P>
  2343. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2344. </P>
  2345. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2346. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tMeasure.restartDelayed();</FONT></FONT></P>
  2347. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2348. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2349. </P>
  2350. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2351. </P>
  2352. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2353. </P>
  2354. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
  2355. MeasureEnable() {</FONT></FONT></P>
  2356. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2357. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;MeasureEnable:
  2358. Activating sensors&quot;); </FONT></FONT>
  2359. </P>
  2360. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2361. </P>
  2362. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2363. <FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = 0;</FONT></FONT></P>
  2364. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2365. <FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.setWaiting(3);
  2366. // Set the StatusRequest to wait for 3 signals. </FONT></FONT>
  2367. </P>
  2368. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2369. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tCalculate.waitFor(&amp;measure);</FONT></FONT></P>
  2370. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2371. </P>
  2372. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2373. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor1.restart();</FONT></FONT></P>
  2374. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2375. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor2.restart();</FONT></FONT></P>
  2376. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2377. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor3.restart();</FONT></FONT></P>
  2378. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2379. </P>
  2380. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2381. <FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
  2382. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2383. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2384. </P>
  2385. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2386. MeasureCallback() {</FONT></FONT></P>
  2387. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2388. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;MeasureCallback:
  2389. Invoked by calculate task or one second later&quot;); </FONT></FONT>
  2390. </P>
  2391. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2392. </P>
  2393. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2394. <FONT FACE="Courier New, monospace"><FONT SIZE=2>if
  2395. (measure.pending()) {</FONT></FONT></P>
  2396. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2397. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tCalculate.disable();</FONT></FONT></P>
  2398. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2399. <FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signalComplete(-1);
  2400. // signal error</FONT></FONT></P>
  2401. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2402. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;MeasureCallback:
  2403. Timeout!&quot;);</FONT></FONT></P>
  2404. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2405. <FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2406. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2407. <FONT FACE="Courier New, monospace"><FONT SIZE=2>else {</FONT></FONT></P>
  2408. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2409. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;MeasureCallback:
  2410. Min distance=&quot;);Serial.println(distance);</FONT></FONT></P>
  2411. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2412. <FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2413. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2414. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2415. </P>
  2416. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2417. MeasureDisable() {</FONT></FONT></P>
  2418. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2419. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;MeasureDisable:
  2420. Cleaning up&quot;); </FONT></FONT>
  2421. </P>
  2422. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2423. </P>
  2424. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2425. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor1.disable();</FONT></FONT></P>
  2426. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2427. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor2.disable();</FONT></FONT></P>
  2428. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2429. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor3.disable();</FONT></FONT></P>
  2430. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2431. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2432. </P>
  2433. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2434. </P>
  2435. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2436. CalcCallback() {</FONT></FONT></P>
  2437. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2438. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;CalcCallback:
  2439. calculating&quot;); </FONT></FONT>
  2440. </P>
  2441. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2442. <FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = -1;</FONT></FONT></P>
  2443. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2444. <FONT FACE="Courier New, monospace"><FONT SIZE=2>if (
  2445. measure.getStatus() &gt;= 0) { // only calculate if statusrequest
  2446. ended successfully</FONT></FONT></P>
  2447. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2448. <FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = d1 &lt;
  2449. d2 ? d1 : d2;</FONT></FONT></P>
  2450. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2451. <FONT FACE="Courier New, monospace"><FONT SIZE=2>distance = d3 &lt;
  2452. distance ? d3 : distance;</FONT></FONT></P>
  2453. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2454. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tMeasure.forceNextIteration();</FONT></FONT></P>
  2455. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2456. <FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2457. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2458. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2459. </P>
  2460. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2461. </P>
  2462. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
  2463. Simulation code for sensor 1</FONT></FONT></P>
  2464. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
  2465. ----------------------------</FONT></FONT></P>
  2466. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2467. <FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
  2468. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
  2469. S1Enable() {</FONT></FONT></P>
  2470. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2471. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S1Enable:
  2472. Triggering sensor1. Delay=&quot;); </FONT></FONT>
  2473. </P>
  2474. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2475. </P>
  2476. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2477. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor1.setInterval(
  2478. random(1200) ); // Simulating sensor delay, which could go over 1
  2479. second and cause timeout</FONT></FONT></P>
  2480. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2481. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d1 = 0;</FONT></FONT></P>
  2482. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2483. </P>
  2484. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2485. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(
  2486. tSensor1.getInterval() );</FONT></FONT></P>
  2487. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2488. <FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
  2489. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2490. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2491. </P>
  2492. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2493. S1Callback() {</FONT></FONT></P>
  2494. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2495. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S1Callback:
  2496. Emulating measurement. d1=&quot;); </FONT></FONT>
  2497. </P>
  2498. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2499. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d1 = random(501); //
  2500. pick a value from 0 to 500 &quot;centimeters&quot; simulating a
  2501. measurement </FONT></FONT>
  2502. </P>
  2503. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2504. <FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signal();</FONT></FONT></P>
  2505. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2506. </P>
  2507. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2508. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(d1); </FONT></FONT>
  2509. </P>
  2510. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2511. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2512. </P>
  2513. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2514. </P>
  2515. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
  2516. Simulation code for sensor 2</FONT></FONT></P>
  2517. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
  2518. ----------------------------</FONT></FONT></P>
  2519. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2520. <FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
  2521. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
  2522. S2Enable() {</FONT></FONT></P>
  2523. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2524. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S2Enable:
  2525. Triggering sensor2. Delay=&quot;); </FONT></FONT>
  2526. </P>
  2527. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2528. </P>
  2529. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2530. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor2.setInterval(
  2531. random(1200) ); // Simulating sensor delay, which could go over 1
  2532. second and cause timeout</FONT></FONT></P>
  2533. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2534. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d2 = 0;</FONT></FONT></P>
  2535. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2536. </P>
  2537. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2538. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(
  2539. tSensor2.getInterval() );</FONT></FONT></P>
  2540. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2541. <FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
  2542. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2543. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2544. </P>
  2545. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2546. S2Callback() {</FONT></FONT></P>
  2547. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2548. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S2Callback:
  2549. Emulating measurement. d2=&quot;); </FONT></FONT>
  2550. </P>
  2551. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2552. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d2 = random(501); //
  2553. pick a value from 0 to 500 &quot;centimeters&quot; simulating a
  2554. measurement</FONT></FONT></P>
  2555. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2556. <FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signal();</FONT></FONT></P>
  2557. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2558. </P>
  2559. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2560. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(d2);
  2561. </FONT></FONT>
  2562. </P>
  2563. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2564. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2565. </P>
  2566. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2567. </P>
  2568. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
  2569. Simulation code for sensor 3</FONT></FONT></P>
  2570. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
  2571. ----------------------------</FONT></FONT></P>
  2572. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2573. <FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
  2574. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>bool
  2575. S3Enable() {</FONT></FONT></P>
  2576. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2577. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S3Enable:
  2578. Triggering sensor3. Delay=&quot;); </FONT></FONT>
  2579. </P>
  2580. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2581. </P>
  2582. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2583. <FONT FACE="Courier New, monospace"><FONT SIZE=2>tSensor3.setInterval(
  2584. random(1200) ); // Simulating sensor delay, which could go over 1
  2585. second and cause timeout</FONT></FONT></P>
  2586. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2587. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d3 = 0;</FONT></FONT></P>
  2588. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2589. </P>
  2590. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2591. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(
  2592. tSensor3.getInterval() );</FONT></FONT></P>
  2593. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2594. <FONT FACE="Courier New, monospace"><FONT SIZE=2>return true;</FONT></FONT></P>
  2595. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2596. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2597. </P>
  2598. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2599. S3Callback() {</FONT></FONT></P>
  2600. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2601. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.print(&quot;S3Callback:
  2602. Emulating measurement. d3=&quot;); </FONT></FONT>
  2603. </P>
  2604. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2605. <FONT FACE="Courier New, monospace"><FONT SIZE=2>d3 = random(501); //
  2606. pick a value from 0 to 500 &quot;centimeters&quot; simulating a
  2607. measurement</FONT></FONT></P>
  2608. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2609. <FONT FACE="Courier New, monospace"><FONT SIZE=2>measure.signal();</FONT></FONT></P>
  2610. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2611. </P>
  2612. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2613. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(d3); </FONT></FONT>
  2614. </P>
  2615. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2616. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2617. </P>
  2618. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2619. </P>
  2620. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>/**
  2621. Main Arduino code</FONT></FONT></P>
  2622. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"> <FONT FACE="Courier New, monospace"><FONT SIZE=2>*
  2623. Not much is left here - everything is taken care of by the framework</FONT></FONT></P>
  2624. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2625. <FONT FACE="Courier New, monospace"><FONT SIZE=2>*/</FONT></FONT></P>
  2626. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2627. setup() {</FONT></FONT></P>
  2628. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2629. </P>
  2630. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2631. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.begin(115200);</FONT></FONT></P>
  2632. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2633. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Serial.println(&quot;TaskScheduler
  2634. StatusRequest Sensor Emulation Test. Complex Test.&quot;); </FONT></FONT>
  2635. </P>
  2636. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2637. <FONT FACE="Courier New, monospace"><FONT SIZE=2>randomSeed(analogRead(A1)+millis());</FONT></FONT></P>
  2638. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2639. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2640. </P>
  2641. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2642. loop() {</FONT></FONT></P>
  2643. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in">
  2644. <FONT FACE="Courier New, monospace"><FONT SIZE=2>ts.execute();</FONT></FONT></P>
  2645. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2646. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2647. </P>
  2648. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2649. </P>
  2650. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2651. </P>
  2652. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2653. </P>
  2654. <OL START=3>
  2655. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>USING
  2656. LOCAL TASK STORAGE POINTER </B></FONT>
  2657. </P>
  2658. </OL>
  2659. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2660. </P>
  2661. <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
  2662. can store a pointer to specific variable, structure or array, which
  2663. represents variables specific for a particular task. This may be
  2664. needed if you plan to use same callback method for multiple tasks.</FONT></FONT></P>
  2665. <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
  2666. a scenario where you have several sensors of the same type. The
  2667. actual process of triggering measurement and collecting information
  2668. is identical. The only difference is the sensor address and a
  2669. variable for storing the results. </FONT></FONT>
  2670. </P>
  2671. <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
  2672. this case each of the tasks, which performs measurement will utilize
  2673. the same callback methods. The only difference will be the variables
  2674. (specific for each of the sensor). </FONT></FONT>
  2675. </P>
  2676. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2677. </P>
  2678. <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
  2679. define a sensor data structure and declare a couple of variables (for
  2680. 2 sensors for instance)</FONT></FONT></P>
  2681. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2682. </P>
  2683. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2684. <FONT FACE="Courier New, monospace"><FONT SIZE=2>typedef struct {</FONT></FONT></P>
  2685. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2686. <FONT FACE="Courier New, monospace"><FONT SIZE=2> unsigned int
  2687. address;</FONT></FONT></P>
  2688. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2689. <FONT FACE="Courier New, monospace"><FONT SIZE=2> unsigned
  2690. long distance;</FONT></FONT></P>
  2691. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2692. <FONT FACE="Courier New, monospace"><FONT SIZE=2>} sensor_data;</FONT></FONT></P>
  2693. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2694. <BR>
  2695. </P>
  2696. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2697. <FONT FACE="Courier New, monospace"><FONT SIZE=2>sensor_data s1, s2; </FONT></FONT>
  2698. </P>
  2699. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2700. </P>
  2701. <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
  2702. separate tasks are running to collect sensor data.</FONT></FONT></P>
  2703. <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
  2704. that both tasks refer to the same callback methods)</FONT></FONT></P>
  2705. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2706. </P>
  2707. <P LANG="" CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in">
  2708. <FONT FACE="Courier New, monospace"><FONT SIZE=2>Scheduler ts;</FONT></FONT></P>
  2709. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2><SPAN LANG="">Task
  2710. t1(100, </SPAN>TASK_FOREVER<SPAN LANG="">, &amp;Measure, &amp;ts,
  2711. false, &amp;MeasureOn); </SPAN></FONT></FONT>
  2712. </P>
  2713. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2><SPAN LANG="">Task
  2714. t2(100, </SPAN>TASK_FOREVER<SPAN LANG="">, &amp;Measure, &amp;ts,
  2715. false, &amp;MeasureOn); </SPAN></FONT></FONT>
  2716. </P>
  2717. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2718. </P>
  2719. <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
  2720. pointers to the respective variables in the <B>setup</B>() method:</FONT></FONT></P>
  2721. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2722. </P>
  2723. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2724. setup() {</FONT></FONT></P>
  2725. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2726. </P>
  2727. <P CLASS="western" STYLE="margin-left: 1.48in; margin-bottom: 0in">…</P>
  2728. <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>
  2729. <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>
  2730. <P CLASS="western" STYLE="margin-left: 1.48in; margin-bottom: 0in">…</P>
  2731. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2732. </P>
  2733. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2734. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2735. </P>
  2736. <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
  2737. reference to specific <B>sensor_data</B> structure inside the common
  2738. callback method:</FONT></FONT></P>
  2739. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2740. </P>
  2741. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>void
  2742. Measure() {</FONT></FONT></P>
  2743. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> Task&amp;
  2744. T = ts.currentTask(); </FONT></FONT>
  2745. </P>
  2746. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> Sensor_data&amp;
  2747. V = *((sensor_data*) T.getLtsPointer());</FONT></FONT></P>
  2748. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2749. For t1, V will be pointing at s1</FONT></FONT></P>
  2750. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2751. For t2, V will be pointing at s2</FONT></FONT></P>
  2752. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2753. </P>
  2754. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>//
  2755. Alternatively use the Scheduler method:</FONT></FONT></P>
  2756. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> Sensor_data&amp;
  2757. V1 = *((sensor_data*) ts.currentLts());</FONT></FONT></P>
  2758. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2759. </P>
  2760. <P CLASS="western" STYLE="margin-left: 0.98in; text-indent: 0.49in; margin-bottom: 0in">
  2761. …</P>
  2762. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2> V.distance
  2763. = &lt;calculate your values here&gt;;</FONT></FONT></P>
  2764. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>}</FONT></FONT></P>
  2765. <P CLASS="western" STYLE="margin-left: 0.98in; margin-bottom: 0in"><BR>
  2766. </P>
  2767. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2768. </P>
  2769. <OL START=3>
  2770. <LI><P CLASS="western" STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><B>FUTHER
  2771. INFROMATION</B></FONT></P>
  2772. </OL>
  2773. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2774. </P>
  2775. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Please
  2776. refer to examples, provided with TaskScheduler package for further
  2777. information and implementation options.</FONT></FONT></P>
  2778. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><BR>
  2779. </P>
  2780. <P CLASS="western" STYLE="margin-left: 0.49in; margin-bottom: 0in"><FONT FACE="Courier New, monospace"><FONT SIZE=2>Real
  2781. time examples of TaskScheduler are available here:</FONT></FONT></P>
  2782. <OL>
  2783. <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>
  2784. <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>
  2785. <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>
  2786. </OL>
  2787. <P CLASS="western" STYLE="margin-bottom: 0in"><BR>
  2788. </P>
  2789. <DIV TYPE=FOOTER>
  2790. <P STYLE="margin-top: 0.35in; margin-bottom: 0in"> <SDFIELD TYPE=PAGE SUBTYPE=RANDOM FORMAT=ARABIC>4</SDFIELD></P>
  2791. </DIV>
  2792. </BODY>
  2793. </HTML>