-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathESP32_simpleDMX.ino
2695 lines (2291 loc) · 58.3 KB
/
ESP32_simpleDMX.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*/
#include "FS.h"
#include <SPIFFS.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <WiFiAP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
//#include <Hash.h>
//#include <ESPmDNS.h>
#include "mdns_defines.h"
#include <MDNS_Generic.h>
#include <Update.h>
#include <WebSocketsServer.h>
#include <WebSocketsClient.h>
#include <esp_dmx.h>
#include <ArtnetWifi.h>
#include <elapsedMillis.h>
#include "ParseCommand.h" // parse serial command
// web pages
#include "index.h"
#include "manager_html.h"
#include "ok_html.h"
#include "failed_html.h"
#include "error_404.h"
#include "error_405.h"
#define SSID_DMXui "DMXui-01" //should be different for each devices
#define PASS_DMXui "DMXuiDMXui"
#define DNS_DMXui "dmxui"
#define HTTP_PORT 80
#define WEBSOCKET_PORT 81
#define PING_INTERVAL 1000
#define PONG_TIMEOUT 3000
#define DISCONNECT_TIMEOUT_COUNT 2
#define DMXTX_pin 16
#define DMXRX_pin 17
#define DMXDIR_pin 18
#define DMXPORT 1
#define REFRESH_DMX_RATE 23 // in ms ; approx 44Hz
#define DMX_CH 512
#define FADERS_MAX 128
#define MEM_MAX 8
#define FX_MAXNB 2
#define NB_FX 4
#define DISPLAY_REFRESH 10 // *20ms
#define SEND_ALL_STATE_TO_WS_TIMER 200 // in ms
#define LED_PIN LED_BUILTIN // ESP32 pin connected to LED
#define FLASH_TIMER 800 //millis
#define BLINK_TIMER 500 //millis
#define uS_TO_S_FACTOR 1000000ULL
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
#define FORMAT_SPIFFS_IF_FAILED true
//~ //timer
//~ #define _TIMERINTERRUPT_LOGLEVEL_ 3
//~ // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
//~ #include "ESP32TimerInterrupt.h"
//~ ESP32Timer ITimer0(0);
//~ //interrupts
//~ portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
//~ volatile int interruptCounter;
File myFile;
WiFiUDP udp;
MDNS mdns(udp);
ParseCommand sCmd;
ArtnetWifi artnet;
typedef uint8_t fract8;
typedef uint16_t accum88; ///< ANSI: unsigned short _Accum. 8 bits int, 8 bits fraction
template <class T> int FILE_writeAnything(File f, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++)
f.write(*p++);
return i;
}
template <class T> int FILE_readAnything(File f, T& value)
{
byte* p = (byte*)(void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value) && f.available(); i++)
*p++ = f.read();
return i;
}
//WIFI *****************************************************************
/* AP IP Address details */
IPAddress AP_local_ip(10,0,0,1);
IPAddress AP_gateway(10,0,0,1);
IPAddress AP_subnet(255,255,255,0);
struct cli_s{
char ssid[48];
char password[48];
};
cli_s client;
RTC_DATA_ATTR int flag_wifi_client=false;
//web spiffs ************************
String filesDropdownOptions = "";
String savePath = "";
String savePathInput = "";
bool rebooting = false;
//DMX *******************************
dmx_port_t dmxPort = DMXPORT;
uint8_t dmxTxBuffer[DMX_CH] = {0};
elapsedMillis refresh_DMX_rate=0;
uint8_t master=255;
bool flag_black_all=false;
struct dimmer{
uint8_t value=0;
uint16_t patch=0;
uint8_t max_val=255;
uint8_t type=0;
};
dimmer dimmers[DMX_CH];
void reset_dimmers()
{
for(int i=0; i< DMX_CH;i++)
{
dimmers[i].value=0;
dimmers[i].patch=0;
dimmers[i].max_val=255;
dimmers[i].type=0;
}
}
struct fader{
uint8_t value=0;
bool fx[NB_FX];
char name[9]={0};
};
fader faders[FADERS_MAX];
struct fx_settings{
uint8_t type=0;
uint8_t speed=60;
uint8_t max_val=255;
uint8_t min_val=0;
};
fx_settings fxs[FX_MAXNB];
struct scene{
uint8_t value=0;
fader faders_memstate[FADERS_MAX];
bool fx[NB_FX];
char name[9]={0};
};
scene scenes[MEM_MAX];
enum {
CLD, //clear dimmers
FULL, // full dimmers
BLACK, // black master
CLF, // clear faders
CLS, // clear scenes
PATCH // patch 1:1
};
enum {
DIM,
ONOFF,
QUADHI,
QUADLO,
INVDIM
};
enum {
FXA,
INV_FXA,
FXB,
INV_FXB
};
enum{
FX_TYPE,
FX_BEAT,
FX_MIN,
FX_MAX
};
enum{
FX_DIM,
FX_STROBE,
FX_SIN,
FX_SAW
};
enum {
WS_DMX,
WS_DIMVAL,
WS_DIMMAX,
WS_DIMPTCH,
WS_DIMTYPE,
WS_FADVAL,
WS_FADFX,
WS_FADNAME,
WS_MASTVAL,
WS_FXVAL,
WS_SCNVAL,
WS_SCNNAME,
WS_BLACK
};
//ARTNET***********************************************************
struct artnet_s{
bool enable=false;
uint16_t universe=0;
};
artnet_s artnetDMX;
uint32_t artnetLastSeq=0;
//UI **************************************************************
uint8_t ui_faders_page_state=1;
uint16_t ui_faders_by_page=16;
uint16_t ui_faders_page_lw_ch=1;
uint16_t ui_faders_page_hi_ch=16;
char ui_faders_html[30];
bool flag_continuous_refresh_vu_ui=false;
elapsedMillis refresh_vu_ui_rate=0;
//speed test dmx frame***********************
uint32_t speedTest_res=0;
int dmxRateLPF=0;
//deep sleep *******************************************
esp_sleep_wakeup_cause_t wakeup_reason;
//LED ***************************************************
elapsedMillis flash_led_timer, blink_led_timer;
bool flag_flash_led, flag_blink_led;
//serial debug flag *********************************
boolean debug_flag = false;
//BeatGenerators *****************************
/// Pre-calculated lookup table used in sin8() and cos8() functions
const uint8_t b_m16_interleave[] = { 0, 49, 49, 41, 90, 27, 117, 10 };
uint8_t squarewave8( uint8_t in, uint8_t pulsewidth=128)
{
if( in < pulsewidth || (pulsewidth == 255)) {
return 255;
} else {
return 0;
}
}
uint8_t sin8( uint8_t theta)
{
uint8_t offset = theta;
if( theta & 0x40 ) {
offset = (uint8_t)255 - offset;
}
offset &= 0x3F; // 0..63
uint8_t secoffset = offset & 0x0F; // 0..15
if( theta & 0x40) ++secoffset;
uint8_t section = offset >> 4; // 0..3
uint8_t s2 = section * 2;
const uint8_t* p = b_m16_interleave;
p += s2;
uint8_t b = *p;
++p;
uint8_t m16 = *p;
uint8_t mx = (m16 * secoffset) >> 4;
int8_t y = mx + b;
if( theta & 0x80 ) y = -y;
y += 128;
return y;
}
/// Generates a 16-bit "sawtooth" wave at a given BPM, with BPM
/// specified in Q8.8 fixed-point format.
/// @param beats_per_minute_88 the frequency of the wave, in Q8.8 format
/// @param timebase the time offset of the wave from the millis() timer
/// @warning The BPM parameter **MUST** be provided in Q8.8 format! E.g.
/// for 120 BPM it would be 120*256 = 30720. If you just want to specify
/// "120", use beat16() or beat8().
uint16_t beat88( accum88 beats_per_minute_88, uint32_t timebase = 0)
{
// BPM is 'beats per minute', or 'beats per 60000ms'.
// To avoid using the (slower) division operator, we
// want to convert 'beats per 60000ms' to 'beats per 65536ms',
// and then use a simple, fast bit-shift to divide by 65536.
//
// The ratio 65536:60000 is 279.620266667:256; we'll call it 280:256.
// The conversion is accurate to about 0.05%, more or less,
// e.g. if you ask for "120 BPM", you'll get about "119.93".
return (((millis()) - timebase) * beats_per_minute_88 * 280) >> 16;
}
/// Generates a 16-bit "sawtooth" wave at a given BPM
/// @param beats_per_minute the frequency of the wave, in decimal
/// @param timebase the time offset of the wave from the millis() timer
uint16_t beat16( accum88 beats_per_minute, uint32_t timebase = 0)
{
// Convert simple 8-bit BPM's to full Q8.8 accum88's if needed
if( beats_per_minute < 256) beats_per_minute <<= 8;
return beat88(beats_per_minute, timebase);
}
/// Generates an 8-bit "sawtooth" wave at a given BPM
/// @param beats_per_minute the frequency of the wave, in decimal
/// @param timebase the time offset of the wave from the millis() timer
uint8_t beat8( accum88 beats_per_minute, uint32_t timebase = 0)
{
return beat16( beats_per_minute, timebase) >> 8;
}
/// Generates an 8-bit sine wave at a given BPM that oscillates within
/// a given range.
/// @param beats_per_minute the frequency of the wave, in decimal
/// @param lowest the lowest output value of the sine wave
/// @param highest the highest output value of the sine wave
/// @param timebase the time offset of the wave from the millis() timer
/// @param phase_offset phase offset of the wave from the current position
uint8_t beatsin8( accum88 beats_per_minute, uint8_t lowest = 0, uint8_t highest = 255, uint32_t timebase = 0, uint8_t phase_offset = 0)
{
uint8_t beat = beat8( beats_per_minute, timebase);
uint8_t beatsin = sin8( beat + phase_offset);
uint8_t rangewidth = highest - lowest;
uint8_t scaledbeat = scale8( beatsin, rangewidth);
uint8_t result = lowest + scaledbeat;
return result;
}
/// @} BeatGenerators
/// @} lib8tion, to exclude timekeeping functions
uint8_t scale8( uint8_t i, fract8 scale)
{
return (((uint16_t)i) * (1+(uint16_t)(scale))) >> 8;
}
/*
//websocket client (slave mode) ***************************************
WebSocketsClient webSocket_client;
void initWebSocketClient()
{
// server address, port and URL
webSocket_client.begin("192.168.0.123", 81, "/vu.html");
// event handler
webSocket_client.onEvent(webSocketEvent_client);
}
void webSocketEvent_client(WStype_t type, uint8_t* payload, size_t length)
{
switch (type) {
case WStype_DISCONNECTED :
if(debug_flag)Serial.printf("[INFO] disconnected from master\n");
break;
case WStype_CONNECTED :
if(debug_flag)Serial.printf("[INFO] Connected to master\n");
break;
case WStype_BIN:
if(debug_flag)Serial.printf("[INFO] Received bin: %s\n");
if(payload[0]==WS_DMX)
{
for (int i = 0; i < DMX_CH; i++)
{
if(i < length - 1)
{
dmxTxBuffer[i] = payload[i + 1];
}else{
dmxTxBuffer[i] = 0;
}
}
}
break;
}
}
//UDP slave ************************************************************
WiFiUDP UdpSlave;
void initUdpSlave()
{
UdpSlave.begin(DMX2UDPport);
}
void check_udp()
{
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
// Just test touch pin - Touch0 is T0 which is on GPIO 4.
Udp.printf(String(touchRead(T0)).c_str(),2);
Udp.endPacket();
}
*/
//WEB_SOCKET_SERVER ****************************************************
AsyncWebServer server(HTTP_PORT);
WebSocketsServer webSocket = WebSocketsServer(WEBSOCKET_PORT); // WebSocket server on port 81
char WSstrbuff[80];
void initWebSocket()
{
// Initialize WebSocket server
webSocket.begin();
webSocket.onEvent(webSocketEvent);
//webSocket.enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount);
webSocket.enableHeartbeat(PING_INTERVAL, PONG_TIMEOUT, DISCONNECT_TIMEOUT_COUNT);
delay(500);
if(debug_flag)
{
Serial.print("[BOOT] WebSocket server's port: ");
Serial.println(WEBSOCKET_PORT);
Serial.flush();
}
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
{
IPAddress ip_WS;
switch (type) {
case WStype_DISCONNECTED :
//blink_led(true);
cmd_unlock_refresh_vu_ui();
if(debug_flag)Serial.printf("[INFO] [%u] Disconnected!\n", num);
break;
case WStype_CONNECTED :
ip_WS = webSocket.remoteIP(num);
if(debug_flag)Serial.printf("[INFO] [%u] Connected from %d.%d.%d.%d\n", num, ip_WS[0], ip_WS[1], ip_WS[2], ip_WS[3]);
// Send a response back to the client
snprintf(WSstrbuff,ARRAY_SIZE(WSstrbuff),"Connected from %d.%d.%d.%d\n", ip_WS[0], ip_WS[1], ip_WS[2], ip_WS[3]);
webSocket.sendTXT(num, WSstrbuff);
//blink_led(false);
//set_led(false);
break;
case WStype_TEXT :
if(debug_flag)Serial.printf("[INFO] [%u] Received text: %s\n", num, payload);
sCmd.readCommand((const char*)payload);
//flash_led();
// Send a response back to the client
//webSocket.sendTXT(num, "Received: " + String((char*)payload));
// Send a response back to ALL the clients
//webSocket.broadcastTXT("OK " + String((char*)payload));
break;
}
}
void WSui_SendDmxState()
{
uint8_t data[DMX_CH+1];
uint8_t * bytePtr = (uint8_t*) &data;
//send dmx view***********************
data[0]=WS_DMX; //send DMX
memmove(bytePtr + 1, dmxTxBuffer, ARRAY_SIZE(dmxTxBuffer));
webSocket.broadcastBIN(data, sizeof(data));
}
void WSui_SendDimmersState()
{
uint8_t data[DMX_CH+1];
uint8_t * bytePtr = (uint8_t*) &data;
/*
//send dmx view***********************
data[0]=WS_DMX; //send DMX
memmove(bytePtr + 1, dmxTxBuffer, ARRAY_SIZE(dmxTxBuffer));
webSocket.broadcastBIN(data, sizeof(data));
* */
//send dimmers states***********************
data[0]=WS_DIMVAL; //send dimmers id
for(int d=0; d < DMX_CH; d++)
{
data[d+1]=dimmers[d].value;
}
webSocket.broadcastBIN(data, sizeof(data));
data[0]=WS_DIMMAX; //send dimmers max
for(int d=0; d < DMX_CH; d++)
{
data[d+1]=dimmers[d].max_val;
}
webSocket.broadcastBIN(data, sizeof(data));
data[0]=WS_DIMPTCH; //send dimmers patch
for(int d=0; d < DMX_CH; d++)
{
data[d+1]=(uint8_t)dimmers[d].patch;
}
webSocket.broadcastBIN(data, sizeof(data));
data[0]=WS_DIMTYPE; //send dimmers type
for(int d=0; d < DMX_CH; d++)
{
data[d+1]=(uint8_t)dimmers[d].type;
}
webSocket.broadcastBIN(data, sizeof(data));
data[0]=WS_BLACK; //send blk type
data[1]=(uint8_t)flag_black_all;
webSocket.broadcastBIN(data, 2);
if(debug_flag)Serial.println("send dimmers state");
}
void WSui_SendFadersState()
{
uint8_t data[DMX_CH+1];
uint8_t * bytePtr = (uint8_t*) &data;
/*
//send dmx view***********************
data[0]=WS_DMX; //send DMX
memcpy(bytePtr + 1, dmxTxBuffer, ARRAY_SIZE(dmxTxBuffer));
webSocket.broadcastBIN(data, sizeof(data));
* */
//send faders states***********************
data[0]=WS_FADVAL; //send faders val
for(int d=0; d < FADERS_MAX; d++)
{
data[d+1]=faders[d].value;
}
webSocket.broadcastBIN(data, FADERS_MAX + 1);
data[0]=WS_FADFX; //send faders fx
for(int d=0; d < FADERS_MAX; d++)
{
data[d+1]=faders[d].fx[3] + faders[d].fx[2] * 2 + faders[d].fx[1] * 4 + faders[d].fx[0] * 8;
}
webSocket.broadcastBIN(data, FADERS_MAX + 1);
data[0]=WS_FADNAME; //send faders name
for(int d=0; d < FADERS_MAX; d++)
{
if(faders[d].name[0] != 0)
{
data[1]=d;
mempcpy(bytePtr + 2, faders[d].name, 9);
webSocket.broadcastBIN(data, 11);
}
}
data[0]=WS_MASTVAL; //send masterVal
data[1]=master;
webSocket.broadcastBIN(data, 2);
if(debug_flag)Serial.println("send faders state");
}
void WSui_SendScenesState()
{
uint8_t data[DMX_CH+1];
uint8_t * bytePtr = (uint8_t*) &data;
/*
//send dmx view***********************
data[0]=WS_DMX; //send DMX
memcpy(bytePtr + 1, dmxTxBuffer, ARRAY_SIZE(dmxTxBuffer));
webSocket.broadcastBIN(data, sizeof(data));
* */
//send scenes states***********************
data[0]=WS_SCNVAL; //send faders val
for(int d=0; d < MEM_MAX; d++)
{
data[d+1]=scenes[d].value;
}
webSocket.broadcastBIN(data, MEM_MAX + 1);
/*
data[0]=WS_FADFX; //send faders fx
for(int d=0; d < FADERS_MAX; d++)
{
data[d+1]=faders[d].fx[3] + faders[d].fx[2] * 2 + faders[d].fx[1] * 4 + faders[d].fx[0] * 8;
}
webSocket.broadcastBIN(data, FADERS_MAX + 1);
*/
data[0]=WS_SCNNAME; //send scenes name
for(int d=0; d < MEM_MAX; d++)
{
if(scenes[d].name[0] != 0)
{
data[1]=d;
mempcpy(bytePtr + 2, scenes[d].name, 9);
webSocket.broadcastBIN(data, 11);
}
}
data[0]=WS_MASTVAL; //send masterVal
data[1]=master;
webSocket.broadcastBIN(data, 2);
if(debug_flag)Serial.println("send scenes state");
}
void WSui_SendFXsState()
{
uint8_t data[DMX_CH+1];
uint8_t * bytePtr = (uint8_t*) &data;
uint8_t count;
/*
//send dmx view***********************
data[0]=WS_DMX; //send DMX
memcpy(bytePtr + 1, dmxTxBuffer, ARRAY_SIZE(dmxTxBuffer));
webSocket.broadcastBIN(data, sizeof(data));
* */
//send fxs states***********************
data[0]=WS_FXVAL; //send fxs val
count=1;
for(int d=0; d < FX_MAXNB; d++)
{
data[ count ]=fxs[d].type;
count++;
data[ count ]=fxs[d].speed;
count++;
data[ count ]=fxs[d].min_val;
count++;
data[ count ]=fxs[d].max_val;
count++;
}
webSocket.broadcastBIN(data, count);
}
//onboard led *********************************************************
void flash_led()
{
flag_flash_led=true;
flash_led_timer=0;
}
void blink_led(boolean state)
{
flag_blink_led=state;
}
void set_led(boolean state)
{
digitalWrite(LED_PIN,state);
}
void toggle_led()
{
digitalWrite(LED_PIN,!digitalRead(LED_PIN));
}
void update_led()
{
if(flag_flash_led)
{
if(flash_led_timer < FLASH_TIMER / 2)
{
digitalWrite(LED_PIN,true);
}
else if(flash_led_timer < FLASH_TIMER)
{
digitalWrite(LED_PIN,false);
}else{
flag_flash_led=false;
}
}
else if(flag_blink_led)
{
if(blink_led_timer < BLINK_TIMER)
{
digitalWrite(LED_PIN,bitRead(blink_led_timer,6)); //pulse at 32ms
}
else if(blink_led_timer < (2*BLINK_TIMER))
{
digitalWrite(LED_PIN,false);
}
else
{
blink_led_timer =0;
}
}
}
//WIFI ****************************************************************
void initWifi()
{
int count=0;
if(loadDataFromSPIFFS("wifi.bin", client))flag_wifi_client=true;
WiFi.disconnect(true);
if(flag_wifi_client)
{
WiFi.mode(WIFI_AP_STA);
}else{
WiFi.mode(WIFI_AP);
}
delay(500);
//version AP
if(debug_flag)Serial.println("[WiFi] create WiFi AP");
WiFi.softAPConfig(AP_local_ip, AP_gateway, AP_subnet);
WiFi.softAP(SSID_DMXui, PASS_DMXui);
delay(200);
//version STA
if(flag_wifi_client)
{
WiFi.begin(client.ssid, client.password);
if(debug_flag)Serial.print("[WiFi] Connecting to WiFi:");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
if(debug_flag)Serial.print(".");
count++;
if(count > 15)break;
}
if(debug_flag)Serial.println();
if(count > 15)
{
//error on client restart in wifi AP mode
if(debug_flag)Serial.println("[WiFi] Error can not connect as client");
SPIFFS.remove("/spiffs/wifi.bin");
flag_wifi_client=false;
dodo10s;
}
}
if(debug_flag)Serial.print("[Wifi] Web Server's IP(s): ");
if(debug_flag)Serial.print(WiFi.softAPIP());
if(flag_wifi_client)
{
if(debug_flag)Serial.print(", ");
if(debug_flag)Serial.print(WiFi.localIP());
}
if(debug_flag)Serial.println();
if(debug_flag)Serial.flush();
}
//mDNS init
void init_mDNS()
{
if(flag_wifi_client)
{
mdns.begin(WiFi.localIP(), DNS_DMXui);
}else{
mdns.begin(WiFi.softAPIP(), DNS_DMXui);
}
delay(100);
mdns.addServiceRecord("dmxui._http", 80, MDNSServiceTCP);
if(debug_flag)Serial.print("[BOOT] mDNS host: ");
if(debug_flag)Serial.println(DNS_DMXui);
}
//HTTP ************************************************************
void initHttp()
{
// Serve the specified HTML pages
//index.html *************
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
if(debug_flag)Serial.println("Web Server: home page");
String html = HTML_CONTENT_HOME; // Use the HTML content from the index.h file
IPAddress ip=request->client()->remoteIP();
char buffip[20];
snprintf(buffip,20,"%d.%d.%d.%d",ip[0], ip[1], ip[2], ip[3]);
html.replace("%YOUR_IP%", buffip);
snprintf(buffip,20,"%ld",esp_get_minimum_free_heap_size());
html.replace("%MINFREE%", buffip);
snprintf(buffip,20,"%u ms, avg: %d ms",speedTest_res,dmxRateLPF);
html.replace("%DMXRATE%", buffip);
if(!artnetDMX.enable)
{
snprintf(buffip,20,"display:none;");
html.replace("%ARTNET%", buffip);
}else{
snprintf(buffip,20,"color:red;");
html.replace("%ARTNET%", buffip);
}
request->send(200, "text/html", html);
});
//dimmers.html *************
server.on("/dimmers.html", HTTP_GET, [](AsyncWebServerRequest *request) {
if(debug_flag)Serial.println("DMX-UI: dimmers");
request->send(SPIFFS, "/dimmers.html", "text/html");
//request->send(SPIFFS, “/dimmers.html”, String(), false, fader_processor);
});
//faders.html *************
server.on("/faders.html", HTTP_GET, [](AsyncWebServerRequest *request) {
if(debug_flag)Serial.println("DMX-UI: faders");
// Check for the 'page' parameter in the query string
if (request->hasArg("page"))
{
String page = request->arg("page");
if (page == ">")
{
ui_faders_page_change(ui_faders_page_state + 1);
}
else if (page == "<")
{
ui_faders_page_change(ui_faders_page_state - 1);
}
else if (page.toInt())
{
ui_faders_page_change(page.toInt());
}
}
//request->send(SPIFFS, "/index.html", "text/html");
request->send(SPIFFS, "/faders.html", String(), false, faders_processor);
});
//scenes.html *************
server.on("/scenes.html", HTTP_GET, [](AsyncWebServerRequest *request) {
if(debug_flag)Serial.println("DMX-UI: scenes");
request->send(SPIFFS, "/scenes.html", "text/html");
//request->send(SPIFFS, “/dimmers.html”, String(), false, fader_processor);
});
//fx.html *********************
server.on("/fx1.html", HTTP_GET, [](AsyncWebServerRequest *request) {
if(debug_flag)Serial.println("DMX-UI: fx1");
//request->send(SPIFFS, "/dimmers.html", "text/html");
request->send(SPIFFS, "/fx.html", String(), false, fx1_processor);
});
server.on("/fx2.html", HTTP_GET, [](AsyncWebServerRequest *request) {
if(debug_flag)Serial.println("DMX-UI: fx2");
//request->send(SPIFFS, "/dimmers.html", "text/html");
request->send(SPIFFS, "/fx.html", String(), false, fx2_processor);
});
//settings.html *********************
server.on("/settings.html", HTTP_GET, [](AsyncWebServerRequest *request) {
if(debug_flag)Serial.println("DMX-UI: settings");
bool redir=false;
if (request->hasArg("saveALL"))
{
saveDMXDatasToSPIFFS();
redir=true;
}
if (request->hasArg("loadALL"))
{
loadDMXDatasFromSPIFFS();
redir=true;
}
if (request->hasArg("loadDIM"))
{
loadDMXDatas_DIM();
redir=true;
}
if (request->hasArg("loadFAD"))
{
loadDMXDatas_FAD();
redir=true;
}
if (request->hasArg("loadFX"))
{
loadDMXDatas_FX();
redir=true;
}
if (request->hasArg("loadSCNALL"))
{
loadDMXDatas_SCNALL();
redir=true;
}
if (request->hasArg("loadSCN"))
{
String scn = request->arg("loadSCN");
if (scn.toInt() > 0 && scn.toInt() < 9)
{
loadDMXDatas_SCN((uint8_t)scn.toInt());
}
redir=true;
}
//saves
if (request->hasArg("saveDIM"))
{
saveDMXDatas_DIM();
redir=true;
}
if (request->hasArg("saveFAD"))
{
saveDMXDatas_FAD();
redir=true;
}
if (request->hasArg("saveFX"))
{
saveDMXDatas_FX();
redir=true;
}
if (request->hasArg("saveSCNALL"))
{
saveDMXDatas_SCNALL();
redir=true;
}
if (request->hasArg("saveSCN"))
{
String scn = request->arg("saveSCN");
if (scn.toInt() > 0 && scn.toInt() < 9)
{
saveDMXDatas_SCN((uint8_t)scn.toInt());
}
redir=true;
}
if (request->hasArg("RESET_ALL"))
{
resetallDMXDatas();
redir=true;
}
if (request->hasArg("REBOOT"))
{
webSocket.broadcastTXT("SYSTEM REBOOT !!");
//delay(500);
rebooting=true;
redir=true;
}
if(redir)
{
request->redirect("/settings.html");
}
else
{
request->send(SPIFFS, "/settings.html", "text/html");
}
//request->send(SPIFFS, “/fx.html”, String(), false, fx1_processor);
});
//vu.html *********************
server.on("/vu.html", HTTP_GET, [](AsyncWebServerRequest *request) {
if(debug_flag)Serial.println("DMX-UI: vu");
request->send(SPIFFS, "/vu.html", "text/html");