-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Tasks.h
374 lines (321 loc) · 9.63 KB
/
Tasks.h
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#ifndef _TASKS_H_
#define _TASKS_H_
//#include "Config.h"
// called from here but loaded later from UI.h
extern void checkButtons( void );
extern void initScroll( String text=" Meteo is loading" );
extern void handleScroll();
extern void displayRtcTime();
extern void displayRtcDate();
extern void displayData( void );
#ifdef DCF77_DO_WEATHER
extern void saveWeatherCache();
#endif
void IRAM_ATTR int0handler() {
DCFSignalState = digitalRead( DCF77PIN );
}
static void mainTask( void * param ) {
log_e( "Starting main DCF77 signal task" );
// Initialize DCF77 pulse interrupt on pin interrupt(DCF77PIN), looking for a change of the signal,
// so either rising or falling edge pulses will trigger the interrupt handler and
// execute the int0handler function.
attachInterrupt(digitalPinToInterrupt(DCF77PIN), int0handler, CHANGE);
while( true ) {
// check if pulse direction is changed (rising or falling)
vTaskDelay( 1 );
if ( DCFSignalState != previousSignalState ) {
previousSignalState = DCFSignalState; // 'reset' state of variable
scanSignal(); // evaluate incoming pulse
}
}
}
#ifdef USE_SPEAKER
static void soundTask( void * param ) {
log_e( "Starting sound task" );
while( true ) {
if(willBuzzNote!=0 && willBuzzDuration!=0) {
Speaker.tone( willBuzzNote, willBuzzDuration );
vTaskDelay( 1 );
willBuzzDuration = 0;
willBuzzNote = 0;
}
Speaker.update();
vTaskDelay( 1 );
}
}
#endif
#ifdef USE_BUTTONS
static void buttonsTask( void * param ) {
//UIModes currentMode = UIMode;
log_e("Starting buttons task");
while( true ) {
checkButtons();
vTaskDelay(50);
/*
if( currentMode != UIMode ) {
takeMuxSemaphore();
tft.fillScreen( TFT_BLACKISH );
giveMuxSemaphore();
currentMode = UIMode;
}*/
}
}
#endif
static void doSecondlyTask() {
displayRtcTime();
switch ( second() ) {
case 0:
if ( dayTime == 1 && minute() == 0 ) {
log_d( "new hour" );
}
if ( hour() == 00 && minute() == 00 ) {
// do something at midnight
log_d( "midnight" );
}
if( UIMode == DCF_CLOCK ) {
displayRtcDate();
}
break;
case 2:
// hourly chime output: DEACTIVATE
break;
case 30:
// display 'HI' on display for Hi temperature
break;
case 31:
case 32:
// display Max temperature on DCF display LED display
break;
case 33:
// display 'LO' on display for Low temperature
break;
case 34:
case 35:
// display Min temperature on DCF display LED display
break;
case 36:
// display 'CU' on display for Current temperature
break;
case 37:
// read temperature, store in min/max memory and display current temperature
//displayTemp();
break;
}// switch
}
/*
static void tasksEverySecond() {
if ( second() != previousSecond ) {
// 'reset' variable state
previousSecond = second();
doSecondlyTask();
}
}*/
static void doMinutelyTask() {
// display date, week LED's, week nr etc.
if ( dcfValidSignal == true ) {
if( UIMode == DCF_CLOCK ) {
displayData();
}
//#ifdef DCF77_DO_WEATHER
// saveWeatherCache();
//#endif
}
}
/*
void tasksEveryMinute() {
// display date, week LED's, week nr etc. if time is changed
if ( minute() != previousMinute ) {
// 'reset' state of variable
previousMinute = minute();
doMinutelyTask();
}
}*/
static void doHourlyTask() {
// reset error counter display
errorCounter = 0;
LedDisplay( DisplayBufferBitError, "R", errorCounter );
#ifdef DCF77_DO_WEATHER
saveWeatherCache();
#endif
}
void tasksEveryHour() {
if ( hour() != previousHour ) {
// 'reset' variable state
previousHour = hour();
doHourlyTask();
}
}
static void secondlyTask( void *param ) {
doSecondlyTask();
vTaskDelete( NULL );
}
static void minutelyTask( void *param ) {
doMinutelyTask();
vTaskDelete( NULL );
}
static void hourlyTask( void *param ) {
doHourlyTask();
vTaskDelete( NULL );
}
void finalizeBufferTask( void *param ) {
finalizeBuffer();
vTaskDelete( NULL );
}
static void timerTasks( void *param ) {
log_d( "Entering timers task" );
#ifdef DCF77_DO_WEATHER
//initScroll();
#endif
String decoderStatusStr = " Minute Marker ";
while(1) {
if ( second() != previousSecond ) {
previousSecond = second();
xTaskCreatePinnedToCore( secondlyTask, "secondlyTask", 2048, NULL, 1, NULL, 0 );
switch( previousSecond ) {
case 0: decoderStatusStr = " Minute Marker "; break;
case 1: decoderStatusStr = " Meteo Data "; break;
case 15: decoderStatusStr = " Antenna Check "; break;
case 16: decoderStatusStr = " Summertime "; break;
case 17: decoderStatusStr = " CEST Summertime "; break;
case 18: decoderStatusStr = " CET Wintertime "; break;
case 19: decoderStatusStr = " Leap Second "; break;
case 20: decoderStatusStr = "Encoded Time Start"; break;
case 21: decoderStatusStr = " Minute Data "; break;
case 28: decoderStatusStr = " Minute Parity "; break;
case 29: decoderStatusStr = " Hour Data "; break;
case 35: decoderStatusStr = " Hour Parity "; break;
case 36: decoderStatusStr = " Day of Month "; break;
case 42: decoderStatusStr = " Day of Week "; break;
case 45: decoderStatusStr = " Month Data "; break;
case 50: decoderStatusStr = " Year Data "; break;
case 58: decoderStatusStr = " Date Parity "; break;
}
if( tft.width() >=320 ) {
//TODO: sprite this
takeMuxSemaphore();
setFontStyle( &sprite, StatusFontStyle );
sprite.setTextColor(TFT_GREEN, TFT_BLACK);
sprite.drawString( decoderStatusStr, tft.width()/2, 95);
giveMuxSemaphore();
} else {
log_w( "%s", decoderStatusStr.c_str() );
}
vTaskDelay( 20 );
continue;
}
if ( minute() != previousMinute ) {
previousMinute = minute();
xTaskCreatePinnedToCore( minutelyTask, "minuteTask", 2048, NULL, 1, NULL, 0 );
vTaskDelay( 20 );
continue;
}
if (hour() != previousHour) {
previousHour = hour();
xTaskCreatePinnedToCore( hourlyTask, "hourlyTask", 2048, NULL, 1, NULL, 0 );
vTaskDelay( 20 );
continue;
}
switch( UIMode )
{
#ifdef USE_BUTTONS
case COOK_TIMER:
cookTimerloop();
break;
#endif
default:
#ifdef DCF77_DO_WEATHER
if( weatherReady ) {
showWeather();
weatherReady = false;
}
#endif
break;
}
#ifdef USE_BUTTONS
if( lastPushCounter != longPushCounter ) {
lastPushCounter = longPushCounter;
takeMuxSemaphore();
if( longPushCounter == 0 ) {
sprite.fillRect( sprite.width()-15, 30, 10, 31, TFT_BLACK );
} else {
sprite.fillRect( sprite.width()-15, 60-longPushCounter, 10, longPushCounter, TFT_BLUE );
}
giveMuxSemaphore();
}
#endif
// retrieve last buffer changes
/*
takeMuxSemaphore();
MainSprite.createSprite( tft.width(), tft.height() );
MainSprite.fillSprite( TFT_BLACK );
MainSprite.setSwapBytes( true );
MainSprite.pushImage( 0, 0, tft.width(), tft.height(), (const uint16_t*)sprite.frameBuffer(1) );
giveMuxSemaphore();
*/
#ifdef DCF77_DO_WEATHER
//handleScroll();
#endif
takeMuxSemaphore();
sprite.pushSprite(0, 0/*, TFT_BLACK*/ );
giveMuxSemaphore();
/*
takeMuxSemaphore();
MainSprite.pushSprite( 0, 0, TFT_BLACK ); // last = transparent color
MainSprite.deleteSprite();
giveMuxSemaphore();*/
vTaskDelay( 20 );
}
}
static uint8_t needrendering;// = 0;
static bool isrendering = false;
static void drawIcon( SpriteSheetIcon icon, uint16_t x, uint16_t y ) {
while( isrendering ) {
vTaskDelay( 10 );
}
isrendering = true;
log_d("Icon #%d will be rendererd at [%d,%d] (%d in queue)", icon, x, y, needrendering );
takeMuxSemaphore();
tft.fillRect( x, y, 32, 32, TFT_BLACK );
//bool swap = sprite.getSwapBytes();
//sprite.setSwapBytes( !swap ); // dafuq
sprite_drawSpriteSheet( sprite, icon, x, y );
giveMuxSemaphore();
needrendering--;
isrendering = false;
log_d("Icon #%d was rendererd !", icon );
}
static void drawIcon( void * param ) {
if( param == nullptr ) {
vTaskDelete( NULL );
return;
}
//*param = reinterpret_cast<UI_Icon*>(param);
UI_Icon *taskicon = reinterpret_cast<UI_Icon*>(param);
//UI_Icon *taskicon = (UI_Icon*) param;
drawIcon( taskicon->spriteSheetIcon, taskicon->x, taskicon->y );
vTaskDelete( NULL );
}
static void drawIconTask( SpriteSheetIcon icon, uint16_t x, uint16_t y ) {
UI_Icon *taskicon = new UI_Icon(x, y, icon);
xTaskCreatePinnedToCore( drawIcon, "drawIcon", 16000, (void*)taskicon, 8, NULL, 0 );
}
static void setupTasks( void* params=NULL ) {
mux = xSemaphoreCreateMutex();
#ifdef USE_BUTTONS
xTaskCreatePinnedToCore( buttonsTask, "buttonsTask", 4096, NULL, 1, NULL, 0 );
vTaskDelay( 10 );
#endif
#ifdef USE_SPEAKER
xTaskCreatePinnedToCore( soundTask, "soundTask", 2048, NULL, 4, NULL, 0 );
vTaskDelay( 10 );
#endif
xTaskCreatePinnedToCore( timerTasks, "timerTasks", 8192, NULL, 4, NULL, 0 );
vTaskDelay( 10 );
xTaskCreatePinnedToCore( mainTask, "mainTask", 2048, NULL, 8, NULL, 1 );
vTaskDelay( 10 );
vTaskDelete( NULL );
}
void initTasks() {
xTaskCreate( setupTasks, "setupTasks", 16000, NULL, 2, NULL );
}
#endif // _TASKS_H_