-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternController.cpp
100 lines (85 loc) · 2.15 KB
/
PatternController.cpp
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
/*
* PatternController.cpp
*
* Created on: May 7, 2016
* Author: Adam
*/
#include "Led.h"
#include "PatternController.h"
#include "task.h"
void PatternController::cast(void* param) {
PatternController* ctrl = (PatternController*)param;
ctrl->taskHandle = xTaskGetCurrentTaskHandle();
ctrl->taskFunc();
}
void PatternController::setPattern(Pattern pattern, byte r, byte g, byte b) {
if (pattern != OFF) {
this->led->writeColor(r, g, b);
}
if (pattern != this->currentPattern) {
this->currentPattern = pattern;
this->r = r;
this->g = g;
this->b = b;
this->taskChanged = true;
xTaskNotifyGive(this->taskHandle);
}
}
PatternController::PatternController(Led* led, QueueHandle_t ledQueue) {
this->led = led;
this->ledQueue = ledQueue;
this->taskHandle = NULL;
this->r = 63;
this->g = 63;
this->b = 63;
}
void PatternController::taskFunc() {
LedMsg offMsg = {0, 0, 0, 1};
LedMsg onMsg = {255, 255, 255, 100};
while(1) {
// If we're here, the pattern changed
xQueueReset(ledQueue);
this->taskChanged = false;
onMsg.r = r;
onMsg.g = g;
onMsg.b = b;
switch(this->currentPattern) {
case OFF:
offMsg.duration = 1;
xQueueSendToFront(ledQueue, &offMsg, 0);
break;
case BLINK_ONCE:
onMsg.duration = 100;
offMsg.duration = 1;
xQueueSendToBack(ledQueue, &onMsg, 0);
xQueueSendToBack(ledQueue, &offMsg, 0);
this->currentPattern = OFF;
break;
case LONG_BLINK:
onMsg.duration = 1000;
offMsg.duration = 1;
xQueueSendToBack(ledQueue, &onMsg, 0);
xQueueSendToBack(ledQueue, &offMsg, 0);
this->currentPattern = OFF;
break;
case BLINK_TWICE:
onMsg.duration = 50;
offMsg.duration = 50;
xQueueSendToBack(ledQueue, &onMsg, 0);
xQueueSendToBack(ledQueue, &offMsg, 0);
xQueueSendToBack(ledQueue, &onMsg, 0);
xQueueSendToBack(ledQueue, &offMsg, 0);
this->currentPattern = OFF;
break;
case SIMPLE_BLINK:
onMsg.duration = 100;
offMsg.duration = 100;
while (!this->taskChanged) {
xQueueSendToBack(ledQueue, &onMsg, portMAX_DELAY);
xQueueSendToBack(ledQueue, &offMsg, portMAX_DELAY);
}
break;
}
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
} // Must never exit
}