-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFlowtoysConnectBridge.ino
423 lines (334 loc) · 8.94 KB
/
FlowtoysConnectBridge.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
#define VERSION 1
//#define VERSION 2
#include "Config.h"
Config conf;
#define USE_SERIAL 1
#if USE_SERIAL
#define SERIAL_DEBUG 1
#define USE_BLE 0
#endif
#define USE_RF 1
#define USE_WIFI 1
#if USE_WIFI
#define USE_OSC 1
#define USE_STREAMING 1
#endif
#define USE_BUTTONS 1
#define USE_LEDS 1
#define USE_FILES 0
#define USE_PLAYER 0
#if USE_SERIAL
#include "SerialManager.h"
SerialManager serialManager;
#if USE_BLE
#include "BLEManager.h"
BLEManager bleManager;
#endif //BLE
#endif //SERIAL
#if USE_RF
float globalBrightness = 1;
#include "RFManager.h"
RFManager rfManager;
#endif
#if USE_WIFI
#include "WifiManager.h"
WifiManager wifiManager;
#if USE_OSC
#include "OSCManager.h"
OSCManager oscManager;
#endif //OSC
#endif //WIFI
#if USE_BUTTONS
#include "ButtonManager.h"
ButtonManager btManager;
#endif
#if USE_LEDS
#include "LedManager.h"
LedManager ledManager;
#endif
#if USE_FILES
#include "FileManager.h"
FileManager fileManager;
#endif
#if USE_PLAYER
#include "Player.h"
Player player;
#endif
#if USE_STREAMING
#include "StreamManager.h"
StreamManager stream;
#endif
//LEDS
float timeAtLastBLEReceived;
float timeAtLastOSCReceived;
float timeAtLastRFReceived;
void patternCallback(String providerId, CommandProvider::PatternData data)
{
DBG("Set pattern ! " + String(data.page) + ":" + String(data.mode) + " / "+ String(data.actives));
if (providerId == "OSC") timeAtLastOSCReceived = millis() / 1000.0f;
else timeAtLastBLEReceived = millis() / 1000.0f;
#if USE_RF
rfManager.setPattern(data);
#endif
}
void commandCallback(String providerId, CommandProvider::CommandData data)
{
DBG("Got Command from " + providerId + " : " + data.type);
if (providerId == "OSC") timeAtLastOSCReceived = millis() / 1000.0f;
else timeAtLastBLEReceived = millis() / 1000.0f;
switch (data.type)
{
#if USE_RF
case CommandProvider::CommandType::GROUP_ADDED:
case CommandProvider::CommandType::RF_DATA:
timeAtLastRFReceived = millis() / 1000.0f;
break;
case CommandProvider::CommandType::SYNC_RF:
{
//rfManager.resetSync(); //tmp because app doesn't have button
rfManager.syncRF(data.value1.floatValue);
}
break;
case CommandProvider::CommandType::STOP_SYNC: rfManager.stopSync(); break;
case CommandProvider::CommandType::RESET_SYNC: rfManager.resetSync(); break;
case CommandProvider::CommandType::WAKEUP: rfManager.wakeUp(data.value1.intValue, data.value2.intValue); break;
case CommandProvider::CommandType::POWEROFF: rfManager.powerOff(data.value1.intValue, data.value2.intValue); break;
#endif
#if USE_PLAYER
case CommandProvider::CommandType::PLAY_SHOW: player.play(data.value1.stringValue); break;
case CommandProvider::CommandType::PAUSE_SHOW: player.pause(); break;
case CommandProvider::CommandType::STOP_SHOW: player.stop(); break;
case CommandProvider::CommandType::RESUME_SHOW: player.resume(); break;
case CommandProvider::CommandType::SEEK_SHOW: player.seek(data.value1.floatValue); break;
#endif
#if USE_WIFI
case CommandProvider::CommandType::SET_WIFI_CREDENTIALS:
{
DBG("Set Wifi credentials : " + String(data.value1.stringValue) + ":" + String(data.value2.stringValue));
conf.setWifiSSID(data.value1.stringValue);
conf.setWifiPassword(data.value2.stringValue);
wifiManager.init();
}
break;
#endif
case CommandProvider::CommandType::SET_GLOBAL_CONFIG:
{
String deviceName = String(data.value1.stringValue).equals("*") ? "" : data.value1.stringValue;
conf.setDeviceName(deviceName);
conf.setWifiBLEMode(data.value2.intValue);
DBG("Set Device name : " + conf.getDeviceName() + " and mode wifi : " + String(conf.getWifiMode()) + ", BLE : " + String(conf.getBLEMode()));
FastLED.delay(500);
ESP.restart();
}
break;
default: DBG("Command not handled"); break;
}
}
#if USE_WIFI
void wifiConnectionUpdate()
{
DBG("Wifi connection update " + String(wifiManager.isConnected));
if (wifiManager.isConnected)
{
#if USE_OSC
DBG("Setup OSC now");
oscManager.init();
#endif
#if USE_STREAMING
stream.init();
#endif
}
}
#endif
#if USE_BUTTONS
void handlePress(int id, bool value)
{
DBG("Pressed " + String(id) + ":" + String(value));
#if USE_LEDS
#endif
}
void handleShortPress(int id)
{
DBG("Short press " + String(id));
}
void handleLongPress(int id)
{
DBG("Long press " + String(id));
}
void handleVeryLongPress(int id)
{
DBG("Very long press " + String(id));
sleepESP(true);
}
void handleMultiPress(int id, int count)
{
DBG("Multi press " + String(id) + " : " + String(count));
}
#endif
#if USE_RF
void rfDataCallback()
{
//DBG("RF Data callback");
/*
#if(SERIAL_SYNC)
serialManager.sendTrigger("Changed");
serialManager.sendIntValue("Page", rfManager.sync_pkt.page);
serialManager.sendIntValue("Mode", rfManager.sync_pkt.mode);
serialManager.sendIntValue("WakeUp", rfManager.sync_pkt.wakeup);
serialManager.sendIntValue("PowerOff", rfManager.sync_pkt.poweroff);
serialManager.sendIntValue("LFO-0", rfManager.sync_pkt.lfo[0]);
serialManager.sendIntValue("LFO-1", rfManager.sync_pkt.lfo[1]);
serialManager.sendIntValue("LFO-Active", rfManager.sync_pkt.lfo_active);
serialManager.sendIntValue("Global-Active", rfManager.sync_pkt.global_active);
serialManager.sendIntValue("Hue", rfManager.sync_pkt.global_hue);
serialManager.sendIntValue("Saturation", rfManager.sync_pkt.global_sat);
serialManager.sendIntValue("Value", rfManager.sync_pkt.global_val);
serialManager.sendIntValue("Intensity", rfManager.sync_pkt.global_intensity);
serialManager.sendIntValue("Speed", rfManager.sync_pkt.global_speed);
serialManager.sendIntValue("Density", rfManager.sync_pkt.global_density);
#endif
*/
}
#endif
void sleepESP(bool animate)
{
if (animate)
{
for (int i = 255; i >= 0; i--)
{
CHSV c(30, 255, i);
ledManager.setLed(0, c);
ledManager.setLed(1, c);
FastLED.delay(2);
}
FastLED.delay(500);
}
//esp_sleep_enable_ext0_wakeup(22, HIGH);
esp_sleep_enable_timer_wakeup(1e6);
esp_deep_sleep_start();
}
// ------------------------------ LEDS
void updateLeds()
{
float curTime = millis() / 1000.0f;
CRGB c1 = CRGB::Black;
CRGB c2 = CRGB::Black;
CRGB c3 = CRGB::Black;
if (rfManager.syncing)
{
float rp = max(1 - (curTime - timeAtLastRFReceived) / .3f, 0.f);
c1 = blend(CRGB::Blue, CRGB::Orange, (int)(rp * 255));
c2 = c1;
} else
{
if (wifiManager.isConnecting) c1 = CRGB::Yellow;
else if (wifiManager.isConnected)
{
if (wifiManager.isLocal) c1 = CRGB::Purple;
else c1 = CRGB::Green;
}
#if USE_BLE
if (bleManager.isActivated)
{
if (bleManager.deviceConnected) c2 = CRGB::Green;
else c2 = CRGB::Yellow;
}
#endif
float p1 = max(1 - (curTime - timeAtLastOSCReceived) / .3f, 0.f);
float p2 = max(1 - (curTime - timeAtLastBLEReceived) / .3f, 0.f);
float p3 = max(1 - (curTime - timeAtLastRFReceived) / .3f, 0.f);
c1 = blend(c1, CRGB::White, (int)(p1 * 255));
c2 = blend(c2, CRGB::White, (int)(p2 * 255));
c3 = blend(c3, CRGB::White, (int)(p3 * 255));
}
ledManager.setLed(0, c1, false);
ledManager.setLed(1, c2, true);
ledManager.setLed(2, c3, true);
}
// ------------------------------ SETUP AND LOOP
void setup()
{
//Need to activate mosfet
pinMode(12, OUTPUT);
digitalWrite(27, LOW);
conf.init();
#if USE_SERIAL
serialManager.init();
serialManager.setCommandCallback(&commandCallback);
serialManager.setPatternCallback(&patternCallback);
#endif //SERIAL
#if USE_BUTTONS
btManager.init();
if (digitalRead(btManager.buttonPins[0]))
{
DBG("Button not pressed, sleep.");
sleepESP(false);
return;
}
#endif
#if USE_LEDS
ledManager.init();
#endif
#if USE_RF
rfManager.init();
rfManager.setRFDataCallback(&rfDataCallback);
#endif
#if USE_BLE && USE_SERIAL
bleManager.init();
#endif //BLE
#if USE_WIFI
wifiManager.init();
wifiManager.setCallbackConnectionUpdate(wifiConnectionUpdate);
#if USE_OSC
//wait for wifi event to init
oscManager.setCommandCallback(&commandCallback);
oscManager.setPatternCallback(&patternCallback);
#endif //OSC
#endif //WIFI
#if USE_FILES
fileManager.init();
#endif
#if USE_PLAYER
player.init();
#endif
#if USE_BUTTONS
//only after everything has been handled
btManager.setEventCallbacks(handlePress, handleShortPress, handleLongPress, handleVeryLongPress, handleMultiPress);
#endif
DBG("Bridge is initialized");
}
void loop()
{
#if USE_BUTTONS
btManager.update();
#endif
#if USE_SERIAL
serialManager.update();
#if USE_BLE
bleManager.update();
#endif
#endif
#if USE_WIFI
wifiManager.update();
#if USE_OSC
oscManager.update();
#endif
#if USE_STREAMING
if(stream.update())
{
#if USE_RF
rfManager.setSolidColors(stream.leds);
#endif
}
#endif
#endif
#if USE_RF
rfManager.update();
#endif
#if USE_PLAYER
player.update();
#endif
#if USE_LEDS
updateLeds();
#endif
}