/
Blink_AnalogRead.ino
90 lines (74 loc) · 1.97 KB
/
Blink_AnalogRead.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <frt.h>
namespace
{
// An anonymous namespace tells the linker that this code is only
// referenced in this very compilation unit. That's good for larger
// projects but not necessary for this sketch.
// Define the blink task and implement run().
// - final is optional but good practice
// - run() must be public
// - sleeping with remainder is optional
// - inlining run() is optional
class BlinkTask final :
public frt::Task<BlinkTask>
{
public:
bool run()
{
digitalWrite(LED_BUILTIN, true);
msleep(400, remainder);
digitalWrite(LED_BUILTIN, false);
msleep(400, remainder);
return true;
}
private:
unsigned int remainder = 0;
};
// Define the analog read task and implement run().
// - 84 is the stack size in bytes
class AnalogReadTask final :
public frt::Task<AnalogReadTask, 84>
{
public:
bool run()
{
Serial.println(analogRead(A0));
msleep(100, remainder);
return true;
}
private:
unsigned int remainder = 0;
};
// Create the static BlinkTask instance
BlinkTask blink_task;
// Create the static AnalogReadTask instance
AnalogReadTask analog_read_task;
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
while (!Serial);
// Start the blink task with priority 1
blink_task.start(1);
// Start the analog read task with higher priority 2
analog_read_task.start(2);
}
// This is called by the idle task (at the lowest priority 0)
void loop()
{
static bool stopped = false;
// Stop analog read task after 5s
if (!stopped && millis() > 5000) {
// Get the stack size used this time. This varies from run to
// run due to interrupts being served while the analog read task
// was running. Don't be too conservative!
Serial.print(F("AnalogReadTask used "));
Serial.print(analog_read_task.getUsedStackSize());
Serial.println(F(" bytes of stack this time."));
Serial.println(F("Stopping"));
analog_read_task.stopFromIdleTask();
Serial.println(F("Stopped"));
stopped = true;
}
}