Arduino-Scheduler
Portable Collaborative Multi-Tasking Scheduler for Arduino
SchedulerDemo.ino
Go to the documentation of this file.
1 
28 #include <Scheduler.h>
29 
30 // Check for SparkFun SAMD21 Breakout
31 #if defined(ARDUINO_ARCH_SAMD) && (USB_PID == 0x8D21)
32 #define Serial SerialUSB
33 #endif
34 
35 void setup()
36 {
37  Serial.begin(57600);
38  while (!Serial);
39  Serial.println(F("SchedulerDemo: started"));
40 
41  Serial.print(millis());
42  Serial.println(F(":setup"));
43 
46 }
47 
48 void loop()
49 {
50  // Main loop iteraction count
51  static int i = 0;
52 
53  // Print main loop iterations
54  Serial.print(millis());
55  Serial.print(F(":loop::i="));
56  Serial.println(i++);
57  delay(500);
58 
59  Serial.print(millis());
60  Serial.print(F(":loop::stack="));
61  Serial.println(Scheduler.stack());
62 }
63 
64 const int LED = 13;
65 
66 void setup2()
67 {
68  Serial.print(millis());
69  Serial.println(F(":setup2"));
70  pinMode(LED, OUTPUT);
71 }
72 
73 void loop2()
74 {
75  // Turn LED off
76  Serial.print(millis());
77  Serial.println(F(":loop2::led off"));
78  digitalWrite(LED, LOW);
79  delay(1000);
80 
81  // Turn LED on
82  Serial.print(millis());
83  Serial.println(F(":loop2::led on"));
84  digitalWrite(LED, HIGH);
85  delay(1000);
86 
87  Serial.print(millis());
88  Serial.print(F(":loop2::stack="));
89  Serial.println(Scheduler.stack());
90 }
91 
92 void setup3()
93 {
94  Serial.print(millis());
95  Serial.println(F(":setup3"));
96 }
97 
98 void loop3()
99 {
100  static char* buf = NULL;
101 
102  // Check for buffer allocation
103  if (buf == NULL) {
104  buf = (char*) malloc(64);
105  Serial.print(millis());
106  Serial.print(F(":loop3:alloc:buf=0x"));
107  Serial.println((int) buf, HEX);
108  if (buf == NULL) {
109  delay(1000);
110  return;
111  }
112  }
113 
114  // Read line and yield while waiting for characters
115  // Capture wait time and number of yields
116  char* bp = buf;
117  unsigned long yields = 0;
118  unsigned long start = millis();
119  int c;
120  while ((c = Serial.read()) != '\n') {
121  if (c > 0)
122  *bp++ = c;
123  else {
124  yields += 1;
125  yield();
126  }
127  }
128  *bp = 0;
129 
130  // Print wait time, number of yields and line
131  unsigned long stop = millis();
132  unsigned long ms = stop - start;
133  Serial.print(millis());
134  Serial.print(F(":loop3:yields="));
135  Serial.print(yields);
136  Serial.print(F(",ms="));
137  Serial.print(ms);
138  Serial.print(F(",buf="));
139  Serial.println(buf);
140 
141  // Check for buffer free command
142  if (!strcmp_P(buf, (const char*) F("free"))) {
143  Serial.print(millis());
144  Serial.print(F(":loop3:free:buf=0x"));
145  Serial.println((int) buf, HEX);
146  free(buf);
147  delay(500);
148  buf = NULL;
149  }
150 
151  Serial.print(millis());
152  Serial.print(F(":loop3::stack="));
153  Serial.println(Scheduler.stack());
154 }
void loop3()
void setup()
void loop2()
static char * buf
Definition: ShellTask.h:31
void setup2()
static size_t stack()
Definition: Scheduler.cpp:127
void setup3()
SchedulerClass Scheduler
Definition: Scheduler.cpp:53
static bool start(func_t taskSetup, func_t taskLoop, size_t stackSize=DEFAULT_STACK_SIZE)
Definition: Scheduler.cpp:76
void yield(void)
Definition: Scheduler.cpp:153
const int LED
void loop()