-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathStatusLED.cpp
More file actions
250 lines (204 loc) · 6.31 KB
/
StatusLED.cpp
File metadata and controls
250 lines (204 loc) · 6.31 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
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
/*
* Copyright (c) 2012 Yeelink.net by dapingliu <dapingliu@yeelink.net>
*
* This is the main project file for YeeBox. This version implements
* YeeBox Ethernet-Zigbee solution, so called YeeboxEZ.
* - Atmel Mega328P
* - WIZnet W5100
* - TI CC2530
*/
#include "StatusLED.h"
// after v1.0.2, global & local leds swapped
#define GLOBAL_LED 4
#define LOCAL_LED 3
#define TIMER2_INTERVAL 50
#define FAST_FLASH_INTERVAL 1 //1 timer2 intervals == 50ms
#define MIDDLE_FLASH_INTERVAL 5 //5 timer2 intervals == 250ms
#define SLOW_FLASH_INTERVAL 20 //20 timer2 intervals = 1s
class CommonData *_leds_cmdata;
unsigned short slowFlashCounter;
unsigned short middleFlashCounter;
unsigned short fastFlashCounter;
unsigned short localLEDOnOff = LOW;
unsigned short globalLEDOnOff = LOW;
void statusLedInit(CommonData *cmdata);
void timer2InterruptHandler();
//do setup here
void statusLedInit(CommonData *cmdata) {
_leds_cmdata = cmdata;
fastFlashCounter = 0;
middleFlashCounter = 0;
slowFlashCounter = 0;
pinMode(GLOBAL_LED, OUTPUT);
pinMode(LOCAL_LED, OUTPUT);
//every 100ms will trigger
MsTimer2::set(TIMER2_INTERVAL, timer2InterruptHandler);
MsTimer2::start();
}
void timer2InterruptHandler() {
fastFlashCounter++;
middleFlashCounter++;
slowFlashCounter++;
//fast flash
if(fastFlashCounter == FAST_FLASH_INTERVAL) {
fastFlashCounter = 0;
if(_leds_cmdata->leds->localLedStatus == LED_FAST_FLASH) {
localLEDOnOff = (localLEDOnOff == LOW ? HIGH : LOW);
digitalWrite(LOCAL_LED, localLEDOnOff);
}
if(_leds_cmdata->leds->globalLedStatus == LED_FAST_FLASH) {
globalLEDOnOff = (globalLEDOnOff == LOW ? HIGH : LOW);
digitalWrite(GLOBAL_LED, globalLEDOnOff);
}
}
//middle flash
if(middleFlashCounter == MIDDLE_FLASH_INTERVAL) {
middleFlashCounter = 0;
if(_leds_cmdata->leds->localLedStatus == LED_MIDDLE_FLASH) {
localLEDOnOff = (localLEDOnOff == LOW ? HIGH : LOW);
digitalWrite(LOCAL_LED, localLEDOnOff);
}
if(_leds_cmdata->leds->globalLedStatus == LED_MIDDLE_FLASH) {
globalLEDOnOff = (globalLEDOnOff == LOW ? HIGH : LOW);
digitalWrite(GLOBAL_LED, globalLEDOnOff);
}
}
//slow flash
if(slowFlashCounter == SLOW_FLASH_INTERVAL) {
slowFlashCounter = 0;
if(_leds_cmdata->leds->localLedStatus == LED_SLOW_FLASH) {
localLEDOnOff = (localLEDOnOff == LOW ? HIGH : LOW);
digitalWrite(LOCAL_LED, localLEDOnOff);
}
if(_leds_cmdata->leds->globalLedStatus == LED_SLOW_FLASH) {
globalLEDOnOff = (globalLEDOnOff == LOW ? HIGH : LOW);
digitalWrite(GLOBAL_LED, globalLEDOnOff);
}
}
//on or off
if(_leds_cmdata->leds->localLedStatus == LED_ON ||
_leds_cmdata->leds->localLedStatus == LED_OFF) {
digitalWrite(LOCAL_LED, _leds_cmdata->leds->localLedStatus);
}
if(_leds_cmdata->leds->globalLedStatus == LED_ON ||
_leds_cmdata->leds->globalLedStatus == LED_OFF) {
digitalWrite(GLOBAL_LED, _leds_cmdata->leds->globalLedStatus);
}
}
//--------------------------------------------------------------------------
void StatusLED::begin(CommonData *cmdata){
this->_cmdata = cmdata;
statusLedInit(cmdata);
}
void StatusLED::loop(){
unsigned long now = millis();
bool deviceOnline = false;
if(this->localLedStatus == LED_FAST_FLASH ||
this->localLedStatus == LED_MIDDLE_FLASH ||
this->localLedStatus == LED_SLOW_FLASH) {
if(now - this->_local_flash_start_time > this->_local_flash_timer) {
this->localLedStatus = this->preFlashLocalLedStatus;
}
}
if(this->globalLedStatus == LED_FAST_FLASH ||
this->globalLedStatus == LED_MIDDLE_FLASH ||
this->globalLedStatus == LED_SLOW_FLASH) {
if(now - this->_global_flash_start_time > this->_global_flash_timer) {
this->globalLedStatus = this->preFlashGlobalLedStatus;
}
}
//Global LED shows
for(int i=0; i<this->_cmdata->deviceCount; i++) {
deviceOnline = (deviceOnline || this->_cmdata->deviceList[i].online);
}
if(deviceOnline == false) {
this->setGlobalOff();
}
else {
this->setGlobalOn();
}
}
void StatusLED::setLocalOn() {
this->localLedStatus = LED_ON;
}
void StatusLED::setLocalOff() {
this->localLedStatus = LED_OFF;
}
void StatusLED::setLocalFastFlash(unsigned int time) {
if((time < 0) || (time > 65000)) {
return;
}
if(this->localLedStatus == LED_ON ||
this->localLedStatus == LED_OFF) {
this->preFlashLocalLedStatus = this->localLedStatus;
this->_local_flash_timer = time;
this->_local_flash_start_time = millis();
this->localLedStatus = LED_FAST_FLASH;
}
}
void StatusLED::setLocalMiddleFlash(unsigned int time) {
if((time < 0) || (time > 65000)) {
return;
}
if(this->localLedStatus == LED_ON ||
this->localLedStatus == LED_OFF) {
this->preFlashLocalLedStatus = this->localLedStatus;
this->_local_flash_timer = time;
this->_local_flash_start_time = millis();
this->localLedStatus = LED_MIDDLE_FLASH;
}
}
void StatusLED::setLocalSlowFlash(unsigned int time) {
if((time < 0) || (time > 65000)) {
return;
}
if(this->localLedStatus == LED_ON ||
this->localLedStatus == LED_OFF) {
this->preFlashLocalLedStatus = this->localLedStatus;
this->_local_flash_timer = time;
this->_local_flash_start_time = millis();
this->localLedStatus = LED_SLOW_FLASH;
}
}
void StatusLED::setGlobalOn() {
this->globalLedStatus = LED_ON;
}
void StatusLED::setGlobalOff() {
this->globalLedStatus = LED_OFF;
}
void StatusLED::setGlobalFastFlash(unsigned int time) {
if((time < 0) || (time > 65000)) {
return;
}
if(this->globalLedStatus == LED_ON ||
this->globalLedStatus == LED_OFF) {
this->preFlashGlobalLedStatus = this->globalLedStatus;
this->_global_flash_timer = time;
this->_global_flash_start_time = millis();
this->globalLedStatus = LED_FAST_FLASH;
}
}
void StatusLED::setGlobalMiddleFlash(unsigned int time) {
if((time < 0) || (time > 65000)) {
return;
}
if(this->globalLedStatus == LED_ON ||
this->globalLedStatus == LED_OFF) {
this->preFlashGlobalLedStatus = this->globalLedStatus;
this->_global_flash_timer = time;
this->_global_flash_start_time = millis();
this->globalLedStatus = LED_MIDDLE_FLASH;
}
}
void StatusLED::setGlobalSlowFlash(unsigned int time) {
if((time < 0) || (time > 65000)) {
return;
}
if(this->globalLedStatus == LED_ON ||
this->globalLedStatus == LED_OFF) {
this->preFlashGlobalLedStatus = this->globalLedStatus;
this->_global_flash_timer = time;
this->_global_flash_start_time = millis();
this->globalLedStatus = LED_SLOW_FLASH;
}
}