File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11#include < Taskfun.h>
22
3- // synchronize access to this shared global variable
3+ // shared variable to synchronize tasks
44SyncVar<bool > _on;
55
6- // function that turns LED on or off depending on argument
7- void OnOff ( bool isOn ) {
6+ // task to turn the LED on
7+ void On ( int & ) {
88 while (1 ) {
9- if (_on == isOn) {
10- digitalWrite (LED_BUILTIN , isOn);
9+ if (_on) {
10+ digitalWrite (LED_BUILTIN , HIGH );
11+ delay (1000 );
12+ _on = false ;
13+ }
14+ }
15+ }
16+
17+ // task to turn the LED off
18+ void Off (int &) {
19+ while (1 ) {
20+ if (!_on) {
21+ digitalWrite (LED_BUILTIN , LOW );
1122 delay (1000 );
12- _on = !_on ;
23+ _on = true ;
1324 }
1425 }
1526}
1627
1728void setup () {
1829 pinMode (LED_BUILTIN , OUTPUT );
19-
20- // initialize multitasking
2130 setupTasks ();
22-
23- // ensure no task switching while initializing
2431 noInterrupts ();
25- // add OnOff task as a task responsible for LED On state
26- runTask (OnOff, true );
27- // add OnOff task as a task responsible for LED Off state
28- runTask (OnOff, false );
29- // resume multitasking
32+ runTask (On, 0 /* unused argument */ );
33+ runTask (Off, 0 );
3034 interrupts ();
3135}
3236
3337void loop () {
3438 yield (); // give up CPU
35- }
39+ }
You can’t perform that action at this time.
0 commit comments