forked from xvedra/TaskScheduler-custom-sleep-method
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test-Taskscheduler.ino
219 lines (178 loc) · 4.52 KB
/
Test-Taskscheduler.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
//#define USE_STM32F103
#define DEB_IDLE_CALL
#define USE_US_CORRECTION
#define _TASK_PRIORITY
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_EXTERNAL_TIME
uint32_t external_millis();
uint32_t external_micros();
#ifdef _TASK_SLEEP_ON_IDLE_RUN
#define myMillis() external_millis()
#else
#define myMillis() millis()
#endif
#ifdef USE_STM32F103
#include <STM32LowPower.h>
#endif
//#include <TaskScheduler.h>
#include ".\TaskScheduler\src\TaskScheduler.h"
Scheduler ts;
#ifdef _TASK_PRIORITY
Scheduler hpts;
#endif
#define WAKEUP_TIME_US 14 //STM32F103 deepSleep wakeup time = 14 usec
uint32_t micros_in_low_power = 0;
uint32_t millis_in_low_power = 0;
void myIdle(unsigned long aT);
void lowPowerMsec(uint32_t msec);
#define PERIOD1 1000
#define DURATION 10000
void blink1CB();
Task tBlink1 ( PERIOD1 * TASK_MILLISECOND, TASK_FOREVER, &blink1CB, &ts, true );
void handler();
#ifdef _TASK_PRIORITY
Task tHandler ( TASK_IMMEDIATE, TASK_ONCE, &handler, &hpts, false);
#else
Task tHandler ( TASK_IMMEDIATE, TASK_ONCE, &handler, &ts, false);
#endif
void slave1();
Task tSlave1 ( TASK_IMMEDIATE, TASK_ONCE, &slave1, &ts, false);
void slave2();
Task tSlave2 ( TASK_IMMEDIATE, TASK_ONCE, &slave2, &ts, false);
char event[64] = "";
// the setup function runs once when you press reset or power the board
void setup()
{
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println("Scheduler test");
#ifdef _TASK_SLEEP_ON_IDLE_RUN
ts.setSleepMethod( &myIdle );
#endif
#ifdef _TASK_PRIORITY
ts.setHighPriorityScheduler(&hpts);
ts.enableAll(true);
#endif
}
// the loop function runs over and over again forever
void loop()
{
ts.execute();
}
void putEvent(const char* p)
{
strncpy(event, p, 64);
tHandler.restart();
}
void handler()
{
if(strlen(event) != 0)
{
Serial.print(" >>> Event: ");
Serial.print(event);
Serial.println("");
strcpy(event, "");
}
}
void slave1()
{
Serial.print(millis());
Serial.println(" > SLV-1");
putEvent("SLAVE 1 done");
}
void slave2()
{
Serial.print(millis());
Serial.println(" > SLV-2");
putEvent("SLAVE 2 done");
}
void blink1CB()
{
static bool flag = 0;
flag = !flag;
if(flag)
{
digitalWrite(LED_BUILTIN, HIGH);
Serial.print(millis());
Serial.println(" > LEDS: ON");
putEvent("LED ON done");
}
else
{
digitalWrite(LED_BUILTIN, LOW);
Serial.print(millis());
Serial.println(" > LEDS: OFF");
putEvent("LED OFF done");
}
tSlave1.restart();
tSlave2.restart();
}
/*
* LOW POWER HANDLE
*/
uint32_t external_millis()
{
return(millis() + millis_in_low_power);
}
uint32_t external_micros()
{
return(micros() + micros_in_low_power);
}
void myIdle(unsigned long aT)
{
uint32_t t = 0;
#ifdef DEB_IDLE_CALL
t = millis();
Serial.print("myIdle:");
Serial.println(aT);
t = millis() - t; //Cálculo del tiempo perdido en enviar el mensaje:
#endif
if(aT > t)
{
#ifdef USE_STM32F103 //solo en caso de un verdadero Idle (ya que el timer queda suspendido)
lowPowerMsec(aT - t);
#else
delay(aT - t); //simula el Idle
#endif
}
}
void lowPowerMsec(uint32_t msec)
{
//Here your lowpower function...
#ifdef USE_US_CORRECTION
static uint32_t UsCounter = 0;
millis_in_low_power += msec;
micros_in_low_power += ((uint32_t)msec * 1000);
UsCounter += WAKEUP_TIME_US;
while(msec && UsCounter >= 1000)
{
UsCounter -= 1000;
msec--;
}
#else
millis_in_low_power += msec;
micros_in_low_power += ((uint32_t)msec * 1000);
#endif
#ifdef USE_STM32F103
LowPower.deepSleep(msec);
#endif
}