-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathEx_03_SubProcesses.ino
More file actions
205 lines (179 loc) · 4.23 KB
/
Ex_03_SubProcesses.ino
File metadata and controls
205 lines (179 loc) · 4.23 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Example 03: Ex_03_SubProcesses.ino
* By: GitModu
*
* This example expands the Ex_02_MultiBlink.ino example, and demonstrates sub-instancing of Processes inside a class.
* This allows class definition with a custom Process without needing extra static declarations, sharing the same scheduler.
* BlinkProcess has 3 BlinkProcess objects that will blink at a diff. period and add them to the scheduler:
* - blink250 (pin 13)
* - blink500 (pin 12)
* - blink1000 (pin 11)
* Connect an LED to each of these pins and watch them blink.
* Also added a simple LogProcess to demonstrate the usage of the same scheduler.
*/
#include <ProcessScheduler.h>
// Create my custom Blink Process
class BlinkProcess : public Process
{
public:
// Call the Process constructor
BlinkProcess(Scheduler &manager, ProcPriority pr, unsigned int period, int pin)
: Process(manager, pr, period)
{
_pinState = LOW; // Set the default state
_pin = pin; // Store the pin number
}
protected:
//setup the pins
virtual void setup()
{
pinMode(_pin, OUTPUT);
_pinState = LOW;
digitalWrite(_pin, _pinState);
}
// Undo setup()
virtual void cleanup()
{
pinMode(_pin, INPUT);
_pinState = LOW;
}
//LEDs should be off when disabled
virtual void onDisable()
{
_pinState = LOW;
digitalWrite(_pin, _pinState);
}
//Start the LEDs on
virtual void onEnable()
{
_pinState = HIGH;
digitalWrite(_pin, _pinState);
}
// Create our service routine
virtual void service()
{
// If pin is on turn it off, otherwise turn it on
_pinState = !_pinState;
digitalWrite(_pin, _pinState);
}
private:
bool _pinState; //the Current state of the pin
int _pin; // The pin the LED is on
};
class BlinkProcessManager :public Process
{
private:
enum ProcessManagerStates
{
Step1,
Step2,
Step3
} ProcessManagerState = Step1;
public:
// Call the Processes constructor's
BlinkProcessManager(Scheduler &manager) :
Process(manager, LOW_PRIORITY, 1000)
, blink250(manager, HIGH_PRIORITY, 250, 13)
, blink500(manager, HIGH_PRIORITY, 500, 12)
, blink1000(manager, MEDIUM_PRIORITY, 1000, 11)
{
}
// Add our blink processes, without enabling them just yet
virtual void setup()
{
blink250.add();
blink500.add();
blink1000.add();
}
void start()
{
blink250.enable();
blink500.enable();
blink1000.enable();
}
void stop()
{
blink250.disable();
blink500.disable();
blink1000.disable();
}
//Disable sub-processes when disabled
virtual void onDisable()
{
stop();
}
//Enable sub-processes when enabled
virtual void onEnable()
{
start();
}
virtual void cleanup()
{
blink250.destroy();
blink500.destroy();
blink1000.destroy();
}
// Create our service routine
virtual void service()
{
switch (ProcessManagerState)
{
case BlinkProcessManager::Step1://Enable blinkers for 5 seconds
Serial.println(F("Blinkers enabled."));
start();
ProcessManagerState = BlinkProcessManager::Step2;
setPeriod(5000);
break;
case BlinkProcessManager::Step2://Disable blinkers for 5 seconds
Serial.println(F("Blinkers disabled."));
stop();
ProcessManagerState = BlinkProcessManager::Step3;
setPeriod(5000);
break;
case BlinkProcessManager::Step3://Sleep for 2 seconds
Serial.println(F("Blinker manager sleeping."));
ProcessManagerState = BlinkProcessManager::Step1;
setPeriod(2000);
break;
default:
break;
}
}
private:
// Create our blink processes
BlinkProcess blink250;// Blink 13 every 250 ms
BlinkProcess blink500; // Blink 12 every 500 ms
BlinkProcess blink1000; // Blink 11 every 1000 ms
};
class LogProcess : public Process
{
public:
LogProcess(Scheduler &manager, ProcPriority pr, unsigned int period)
: Process(manager, pr, period)
{
}
protected:
//setup the pins
virtual void setup()
{
Serial.begin(57600);
}
// Create our service routine
virtual void service()
{
Serial.println(F("Log Message."));
}
};
Scheduler sched; // Create a global Scheduler object
BlinkProcessManager BlinkManager(sched); // Has 3 independent BlinkProcesses.
LogProcess LogService(sched, LOW_PRIORITY, 10000); // Show a log message every 10000 ms
void setup()
{
// Add and enable our blink processes and custom process, sharing the scheduler.
LogService.add(true);
BlinkManager.add(true);
}
void loop()
{
sched.run();
}