-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchiconCube_v0_1a.ino
473 lines (404 loc) · 14.3 KB
/
chiconCube_v0_1a.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/***********************************************************
// chiconCube_v0_1a.ino
// version 0.1a - 12/08/2015
// Author : Vincent ANTOINE
//
// MAIN FILE of CHICON HDW Management
/**********************************************************/
#define WITH_SERIAL 1
#include <SPI.h>
#include <MemoryFree.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include "ledprocessing.h"
#include "chicon.h"
#include "wifiManager.h"
#include "config.h"
#define SS_SD_CARD 4
#define SS_ETHERNET 10
#define FIRST_CONFIG_FREQUENCY 5000
#define MAXLED 3
#define STATUS_LED_PIN 6
#define ERROR_LED_PIN 14
#define RCV_BUFF_SIZE 400
// You can choose the latch pin yourself.
const int ShiftPWM_latchPin=8;
// ** uncomment this part to NOT use the SPI port and change the pin numbers. This is 2.5x slower **
#define SHIFTPWM_NOSPI
const int ShiftPWM_dataPin = 3;
const int ShiftPWM_clockPin = 5;
// If your LED's turn on if the pin is low, set this to true, otherwise set it to false.
const bool ShiftPWM_invertOutputs = false;
// You can enable the option below to shift the PWM phase of each shift register by 8 compared to the previous.
// This will slightly increase the interrupt load, but will prevent all PWM signals from becoming high at the same time.
// This will be a bit easier on your power supply, because the current peaks are distributed.
const bool ShiftPWM_balanceLoad = false;
#include <ShiftPWM.h>
unsigned char maxBrightness = 255;
unsigned char pwmFrequency = 75;
byte numRegisters = 2;
//int numRGBleds = 3;
unsigned long lastConfigCheck = 0;
char buf[RCV_BUFF_SIZE];
boolean stringComplete = false;
//short strInd=0;
service* myServices;
led myLeds[MAXLED];
short nbrConfigService = -1;
boolean jsonStarted;
boolean enrollMe = false;
volatile boolean isALedSelected =false;
volatile boolean isALedSelectedConfirmed =false;
volatile short selectedLEDID = 0;
volatile short selectionTime = 4000;
volatile short preventDoubleClickTimer = 500;
volatile boolean doubleClickDetected = false;
volatile unsigned long last_selectedLed_time = 0;
/***********************************************************
// Interrupt function executed each time button is pressed
// if button is pressed before selectionTime value, a request is sent to the server for action
// if not, selectedLEDID is incremented (next LED selected).
// In LEDSelection process
// Updated global variable:
// selectedLEDID : Current selected LED (incr each time function is called
// last_selectedLed_time : Current time when button is pressed
// isALedSelected set to true to indicate to loop function button has been pressed and a LED is currently selected
// When a LED has been selected and selection confimed by a second button push with selectionTime
// Updated global variable:
// isALedSelected set to false to indicate to loop function selection process has ended
// last_selectedLed_time set to 0 to allow a next selection process
//
/**********************************************************/
void selectLED(){
if(!doubleClickDetected){
doubleClickDetected=true;
if(last_selectedLed_time == 0){
if(selectedLEDID==MAXLED){
selectedLEDID = 0;
}
selectedLEDID++;
#if defined(WITH_SERIAL)
Serial.print(F("Led Selected"));
Serial.println(selectedLEDID);
#endif
isALedSelected = true;
}else{
//On a rappuyer dans le temps de la selection on balance la requête de selection
isALedSelectedConfirmed = true;
manageBuiltinStatusLed(false); //on met le status led en vert pour indiquer qu'on est plus en mode sélection
last_selectedLed_time =0;
}
}
}
/***********************************************************
// processLEDValues
// This function goes through all configured services
// For each service it goes trough each LED and call the setLEDValues(led*) to light on the LED according to received values
/**********************************************************/
boolean processLEDValues(){
byte i;
byte j;
led * currLed;
for(i=0;i<nbrConfigService;i++){
for(j=0;j<myServices[i].nbrLed;j++){
currLed = myServices[i].ledTab[j];
setLEDValues(currLed);
}
}
return true;
}
/***********************************************************
// getLedByID
// This function goes through the myLeds array and return the led with id id_
// input : id of the searched led
// return : search led or NULL if not found
/**********************************************************/
led* getLedById(int id_){
byte i;
for(i=0;i<MAXLED;i++){
if(myLeds[i].id ==id_){
return &myLeds[i];
}
}
return NULL;
}
service* getServiceById(unsigned long id_){
byte i;
for(i=0;i<nbrConfigService;i++){
if(myServices[i].id ==id_){
return &myServices[i];
}
}
return NULL;
}
void manageBuiltinStatusLed(boolean error){
digitalWrite(STATUS_LED_PIN,!error);
digitalWrite(ERROR_LED_PIN,error);
}
void setup() {
//#if defined(WITH_SERIAL)
Serial.begin(9600);
Serial.print(F("Booting Firmware 0.1a\n"));
// #endif
//Shift PWM init
ShiftPWM.SetAmountOfRegisters(numRegisters);
//ShiftPWM.SetPinGrouping(1);
ShiftPWM.Start(pwmFrequency,maxBrightness);
//Led Init
initLED();
//Error & Status LED init
pinMode(STATUS_LED_PIN,OUTPUT);
pinMode(ERROR_LED_PIN,OUTPUT);
manageBuiltinStatusLed(true);
//Init select button
pinMode(2,INPUT);
attachInterrupt(0, selectLED, RISING);
//init shield
pinMode(SS_ETHERNET, OUTPUT);
digitalWrite(SS_ETHERNET, HIGH); // Wifi Not active
while(!connectToWifiNetwork()); //if no connection loop trying to connect
manageBuiltinStatusLed(false);
Serial.print(F("freeMemory()="));
Serial.println(freeMemory());
}
void checkServiceTimer(){
byte i=0;
for(i=0;i<nbrConfigService;i++){
if((millis() - myServices[i].lastCheck > myServices[i].frequency) ){
#if defined(WITH_SERIAL)
Serial.print(F("@Srv ID "));
Serial.println(myServices[i].id);
#endif
if(executeServiceOverWifi(magicNumber,myServices[i].id)){
myServices[i].lastCheck = millis();
processServerResponse();
}
}
}
}
boolean checkServiceConfig(){
if(nbrConfigService == -1 && (millis() - lastConfigCheck > FIRST_CONFIG_FREQUENCY)){
#if defined(WITH_SERIAL)
Serial.println(F("@First Config Request"));
#endif
configRequestOverWifi(magicNumber);
processServerResponse();
lastConfigCheck = millis();
}
//else if(millis() - lastConfigCheck > CONFIG_UPDATE_FREQUENCY){
// Serial.println("@Config Update Request");
//lastConfigCheck = millis();
//}
return true;
}
void loop() {
if(!isALedSelected && !isALedSelectedConfirmed){
checkServiceConfig();
checkServiceTimer();
processLEDValues();
updateSocketTimer();
}
checkLedSelection();
}
void checkLedSelection(){
if(isALedSelected){
manageBuiltinStatusLed(true); //On met le status LED en rouge afin d'indiquer qu'on est en mode sélection
if(millis() - last_selectedLed_time > preventDoubleClickTimer && last_selectedLed_time!=0 ){
doubleClickDetected = false;
}
if(last_selectedLed_time==0){ //On est en mode selection
ledSelected(getLedById(selectedLEDID));
last_selectedLed_time=millis();
}
if(isALedSelectedConfirmed){ //On a confirmer la selection de la LED
#if defined(WITH_SERIAL)
Serial.print("LS-confirmed");
Serial.println(selectedLEDID);
#endif
isALedSelected = false;
last_selectedLed_time = 0;
isALedSelectedConfirmed = false;
manageBuiltinStatusLed(false);
forceSocketClose();
selectLEDOverWifi(magicNumber,selectedLEDID);
delay(preventDoubleClickTimer);
processServerResponse();
doubleClickDetected = false;
}
if(millis() - last_selectedLed_time > selectionTime && last_selectedLed_time!=0){
//L'utilisateur n'a pas appuyé dans le temps imparti
isALedSelected = false;
last_selectedLed_time = 0;
manageBuiltinStatusLed(false); //On met le status LED à vert afin d'indiquer qu'on est plus en mode sélection
doubleClickDetected = false;
}
}
}
void processServerResponse(){
if (stringComplete) {
//Serial.print("JSON Processing ");
//Serial.println(buf);
if(buf[0]=='{'){
#if defined(WITH_SERIAL)
Serial.print(F("\nfreeMemory()="));
Serial.println(freeMemory());
Serial.print("JSON Processing ");
Serial.println(buf);
#endif
parseJson(buf);
}
stringComplete = false;
}
}
int parseJson(char * jsonString){
unsigned short i=0;
unsigned short j=0;
char* srvPtr;
char* objPtr;
char * eltPtr;
char * eltValuePtr;
char * ledPtr;
char * ledArrayPtr;
char * ledEltPtr;
char * ledEltObjPtr;
char * ledEltObjValuePtr;
char * ledValueArrayPtr;
char * ledValueEltPtr;
unsigned short srvNbr=0;
unsigned short ledNbr = 0;
unsigned short idLed;
unsigned long idSrv;
unsigned short srvTypeLed;
led* currLed;
service* currService;
int* ledValue;
int nbrValue;
int value;
srvPtr = getJsonObjectElement(jsonString,"srvconfig");
if(srvPtr !=NULL){
//We are receiving a new configuration string
if(nbrConfigService != -1){
for(i=0;i<nbrConfigService;i++){
free(myServices[i].ledTab);
}
free(myServices);
}
//Pause the checkService.
nbrConfigService = 0;
//Reset leds
resetLED();
srvPtr = getJsonObjectEltValue(srvPtr);
srvNbr = getJsonArrayNbrElt(srvPtr);
myServices = (service*)malloc(srvNbr*sizeof(service));
for(i=0;i<srvNbr;i++){
eltPtr = getJsonArrayElement(srvPtr,i);
if(eltPtr !=NULL){
objPtr = getJsonObjectElement(eltPtr,"id");
if(objPtr != NULL){
eltValuePtr = getJsonObjectEltValue(objPtr);
setJsonLongValue(eltValuePtr,&(myServices[i].id));
}
objPtr = getJsonObjectElement(eltPtr,"freq");
if(objPtr != NULL){
eltValuePtr = getJsonObjectEltValue(objPtr);
setJsonLongValue(eltValuePtr,&(myServices[i].frequency));
}
ledPtr = getJsonObjectElement(eltPtr,"led");
ledArrayPtr = getJsonObjectEltValue(ledPtr);
if(objPtr != NULL){
ledNbr = getJsonArrayNbrElt(ledArrayPtr);
Serial.print("LedNbr:");
Serial.println(ledNbr);
myServices[i].nbrLed = ledNbr;
myServices[i].ledTab = (led**)(malloc(ledNbr*sizeof(led*)));
for(j=0;j<ledNbr;j++){
ledEltPtr = getJsonArrayElement(ledArrayPtr,j);
if(ledEltPtr != NULL){
ledEltObjPtr = getJsonObjectElement(ledEltPtr,"id");
ledEltObjValuePtr = getJsonObjectEltValue(ledEltObjPtr);
setJsonLongValue(ledEltObjValuePtr,(unsigned long*)&idLed);
ledEltObjPtr = getJsonObjectElement(ledEltPtr,"type");
ledEltObjValuePtr = getJsonObjectEltValue(ledEltObjPtr);
setJsonLongValue(ledEltObjValuePtr,(unsigned long*)&srvTypeLed);
currLed = getLedById(idLed);
if(currLed!=NULL){
currLed->srv_type = srvTypeLed;
myServices[i].ledTab[j] = currLed;
}
}
}
}
#if defined(WITH_SERIAL)
Serial.print("New service configured ");
Serial.print("id ");
Serial.print(myServices[i].id);
Serial.print(" - freq ");
Serial.println(myServices[i].frequency);
Serial.print("with ");
Serial.print(myServices[i].nbrLed);
Serial.println(" LEDs configured");
for(j=0;j<myServices[i].nbrLed;j++){
Serial.print("\tLED id ");
Serial.print(myServices[i].ledTab[j]->id);
Serial.print(" - Type ");
Serial.println(myServices[i].ledTab[j]->srv_type);
}
#endif
}
}
nbrConfigService = srvNbr;
lastConfigCheck = millis();
return srvNbr;
}else{
srvPtr = getJsonObjectElement(jsonString,"srvset");
if(srvPtr !=NULL && nbrConfigService > 0){
//We are receiving a srv set string
//{"srvset":{"id":12300,"led":[{"id":1,"value":1},{"id":3,"value":[34,26,62]}]}}
objPtr = getJsonObjectEltValue(srvPtr);
eltPtr = getJsonObjectElement(objPtr,"id");
if(eltPtr != NULL){
eltValuePtr = getJsonObjectEltValue(eltPtr);
setJsonLongValue(eltValuePtr,&idSrv);
currService = getServiceById(idSrv);
if(currService != NULL){
ledPtr = getJsonObjectElement(objPtr,"led");
if(ledPtr != NULL){
ledArrayPtr = getJsonObjectEltValue(ledPtr);
for(i=0;i<currService->nbrLed;i++){
ledEltPtr = getJsonArrayElement(ledArrayPtr,i);
if(ledEltPtr != NULL){
ledEltObjPtr = getJsonObjectElement(ledEltPtr,"id");
if(ledEltObjPtr != NULL){
ledEltObjValuePtr = getJsonObjectEltValue(ledEltObjPtr);
setJsonLongValue(ledEltObjValuePtr,(unsigned long*)&idLed);
currLed = getLedById(idLed);
ledEltObjPtr = getJsonObjectElement(ledEltPtr,"value");
if(ledEltObjPtr != NULL){
ledValueArrayPtr = getJsonObjectEltValue(ledEltObjPtr);
nbrValue = getJsonArrayNbrElt(ledValueArrayPtr);
for(j=0;j<nbrValue;j++){
ledValueEltPtr = getJsonArrayElement(ledValueArrayPtr,j);
setJsonLongValue(ledValueEltPtr,(unsigned long*)&value);
currLed->value[j] = value;
}
}
}
}
}
/*
Serial.print("New service set ");
Serial.print("id ");
Serial.println(currService->id);
Serial.println(" LEDs Value Set");
for(j=0;j<currService->nbrLed;j++){
printLedDetails(currService->ledTab[j]);
}
*/
return 0;
}
}
}
}
}
return -1;
}