TaskScheduler.html 149 KB

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