· 4 months ago · May 20, 2025, 02:20 PM
1//For testing only
2int stacount = 0;
3const char compile_date[] = __DATE__;
4//Included with the name printing
5#include <HardwareSerial.h>
6#include "AiEsp32RotaryEncoder.h"
7#include "Arduino.h"
8#include "Wire.h"
9#include <Adafruit_NeoPixel.h>
10#include <esp_task_wdt.h>
11#include <WiFi.h>
12//#include <RTClib.h>
13#include <ErriezDS3231.h>
14#include "PinDefinitionsAndMore.h" // Define macros for input and output pin etc.
15#include <IRremote.hpp>
16#include "TC74.h"
17
18#define sense1 2000
19#define sense2 2050
20#define ROTARY_ENCODER_A_PIN 35
21#define ROTARY_ENCODER_B_PIN 32
22#define ROTARY_ENCODER_BUTTON_PIN 33
23#define ROTARY_ENCODER_VCC_PIN -1 /* 27 put -1 of Rotary encoder Vcc is connected directly to 3,3V; else you can use declared output pin for powering rotary encoder */
24#define ROTARY_ENCODER_STEPS 4
25#define powerSW 23
26#define inputSW //c:\Users\Gaben\Documents\Arduino\Arylic_250115
27#define dcSenseRight 19 //-> pin / dcErrorRight (current state) / dcErroRightLast (last state of dcErrorRight)
28#define dcSenseLeft 18 //-> pin / dcErrorLeft (current state) / dcErrorLeftLast (last state of dcErrorLeft)
29#define acSense 5 //-> pin / acError (current state) / acErrorLast (last state of acError)
30#define pwr 16 // Standby relay
31#define speaker 17 // Speaker relay
32#define WS2812 2 // WS2812 pin
33#define NUMPIXELS 8 // number of LED
34#define LDR 12 // LDR pin
35#define RXN_PIN 26 // Serial1 RX to Nextion TX
36#define TXN_PIN 25 // Serial1 TX to Nextion RX
37#define RX_PIN 27 // Serial2 RX a Amp TX
38#define TX_PIN 14 // Serial2 TX a Amp RX
39#define SDA 21 // I2C Thermometer, Expander, etc.
40#define SCL 22
41
42#define boot 0 // Nextion page number 0..11
43#define standby 1
44#define main 2
45#define digiSetup 3
46#define ampSetup 4
47#define digiInput 5
48#define digiPreset 6
49#define dsp1 7
50#define dsp2 8
51#define dsp3 9
52#define info 10
53#define dataRecord 11
54
55#define DATE_STRING_SHORT 3
56#define ONs 1
57#define OFFs 0
58
59//RTC_DS3231 rtc;
60ErriezDS3231 rtc;
61TC74 tmp1(0x4A); //A2 Address
62TC74 tmp2(0x4B); //A3 Address
63HardwareSerial uart(2); // Uso de la interfaz de hardware Serial2
64Adafruit_NeoPixel pixels(NUMPIXELS, WS2812, NEO_RGB + NEO_KHZ800);
65AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
66
67// Month names in flash
68const char monthNames_P[] PROGMEM = "JanFebMarAprMayJunJulAugSepOctNovDec";
69// Day of the week names in flash
70const char dayNames_P[] PROGMEM = "SunMonTueWedThuFriSat";
71
72const int powerOffDelayForRelay = 400; //in millis seconds
73//const byte LDR = 12; //Light Dependant Resistor
74const long eventTime_1_LDR = 5000; //check brigtness in ms
75unsigned long previousTime_1 = 0, beepMillis = 0, protectMillis = 0, irMillis = 0, irVolMillis, strengthMillis = 0, lastMillis = 0;
76unsigned long ac_time = 0, ac_time_last = 0;
77boolean powerState = 0, lastPowerState = 0, nextionSetTimebit = 1;
78int offset = 2, beep = 0, NextionPage = 0;
79int digiVolume = 0, dmute = 0, lastPressed = 0, source = 0, dim = 0, dimN = -1, task = 0;
80int initDigi = 1, toDigi = 1;
81int dcErrorRight = 1, dcErrorRightLast = -1, dcErrorLeft = 1, dcErrorLeftLast = -1, acError = 1, acErrorLast = -1; //variables for protection
82
83int debug = 1; //1 - writing all data; 2 - only protection data
84
85int ac_protect = 0;
86int self_test = 0;
87int currentPage = -1; // current page of nextion
88int irLastCode = -1;
89int volStep = 2;
90int sourceS = 0;
91int devState = 1;
92int speaker1timeon;
93unsigned long speakerMillis;
94String sReceived, nReceived;
95int ev, honap, nap, ora, perc, mperc, hetnapja; //These are where we store the RTC values.
96int speakerRelayEnable, speakerRelaySet, pwrRelayEnable, pwrRelaySet;
97int protectionErrors;
98
99void readProtectionBoot();
100void readProtectionRun();
101
102void i2cCheck();
103
104#if (1)
105const char* ssid = "TP-Link_F072";
106const char* password = "12778072";
107#else
108const char* ssid = "SirRouter";
109const char* password = "19801989";
110#endif
111
112struct s_v4amp {
113 int mode;
114 int state;
115 //mode = 0 , Initial
116 //mode = 1 , Initial done No AC or DC error
117 //mode = 2 , On (running)
118} v4amp;
119
120struct s_last {
121 int acError;
122 int dcErrorLeft;
123 int dcErrorRight;
124 int protectionError;
125} last;
126
127struct s_timer {
128
129 unsigned long protectionOffDelay; //in the readProtection, off delay
130 unsigned long protectionOnDelay;
131 unsigned long stopwatch;
132} timer;
133
134struct s_i2cdev {
135 String name;
136 int address;
137 int state;
138 String stateStr;
139};
140s_i2cdev tc74_1;
141s_i2cdev tc74_2;
142s_i2cdev rtcModule;
143s_i2cdev rtcEEPROM;
144s_i2cdev dspModule;
145s_i2cdev dspEEPROM;
146
147class c_NextionWrite {
148public:
149 void init(int speed, int RXN, int TXN) {
150 Serial1.begin(speed, SERIAL_8N1, RXN, TXN);
151 // if (debug) Serial.printf("Serial1 - Speed: %d, RX-pin: %d, TX-pin: %d \n", speed, RX, TX);
152 }
153 void txt(String Name, String text) {
154 Serial1.print(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
155 if (debug == 1) Serial.println(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
156 }
157 void val(String Name, int value) {
158 Serial1.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
159 if (debug == 1) Serial.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
160 }
161 void systemVal(String Name, int value) {
162 Serial1.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
163 if (debug == 1) Serial.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
164 }
165 void pageChange(int Name) {
166 Serial1.print("page " + String(Name) + "\xFF\xFF\xFF");
167 if (debug == 1) Serial.print("page " + String(Name) + "\xFF\xFF\xFF");
168 //Serial.println("EPG");
169 NextionPage = Name;
170 }
171 void setPco(String name, int pco) { // for global variable need a page number / page name too
172 Serial1.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
173 if (debug == 1) Serial.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
174 }
175 void timerEnable(String name, int en) {
176 Serial1.print(name + ".en=" + String(en) + "\xFF\xFF\xFF");
177 }
178 void vis(String name, int en) {
179 Serial1.print("vis " + name + "," + String(en) + "\xFF\xFF\xFF");
180 }
181 void dim(int en) {
182 Serial1.print("dim=" + String(en) + "\xFF\xFF\xFF");
183 }
184 void touchSet(String name, int en) { //nextion.touchSet("preset", 0/1);
185 Serial1.print("tsw " + String(name) + "," + String(en) + "\xFF\xFF\xFF");
186 }
187 void click(String name, int en) {
188 Serial.print("click " + String(name) + "," + en + "\xFF\xFF\xFF");
189 Serial1.print("click " + String(name) + "," + en + "\xFF\xFF\xFF");
190 }
191 void setTime(int ev, int honap, int nap, int ora, int perc, int mperc, int hetnapja) {
192 //hour = (hour + 24) % 24;
193 Serial1.print("rtc0=" + String(ev) + "\xFF\xFF\xFF"); //year
194 Serial1.print("rtc1=" + String(honap) + "\xFF\xFF\xFF"); //month
195 Serial1.print("rtc2=" + String(nap) + "\xFF\xFF\xFF"); //day
196 Serial1.print("rtc3=" + String(ora) + "\xFF\xFF\xFF"); //hour
197 Serial1.print("rtc4=" + String(perc) + "\xFF\xFF\xFF"); //minute
198 Serial1.print("rtc5=" + String(mperc) + "\xFF\xFF\xFF"); //second
199 // Serial1.print("rtc6=" + String(hetnapja) + "\xFF\xFF\xFF"); //days of the Week
200 Serial.printf("Nextion time/ hour: %d min: %d sec: %d \n", ora, perc, mperc);
201 Serial.println("--------------------");
202 }
203};
204c_NextionWrite nextion;
205
206class c_RelayClass { //Relay
207public:
208 int speakerEnable;
209 int powerEnable;
210 int acRelayState;
211 int dcRelayState;
212 // CLASS
213 void spk(int set) { // send SPK relay state
214 if (dcRelayState != set) {
215 if (speakerEnable == 1) {
216 Serial.print("Speakers is: " + set);
217 if (set) {
218 Serial.println(" ON");
219 if (acRelayState == 1) {
220 digitalWrite(speaker, HIGH);
221 nextion.txt("main.dcRelay", "ON");
222 }
223 } else {
224 Serial.println("OFF");
225 digitalWrite(speaker, LOW);
226 nextion.txt("main.dcRelay", "OFF");
227 }
228
229 dcRelayState = set;
230 } else {
231 digitalWrite(speaker, LOW);
232 dcRelayState = 0;
233 }
234 }
235 }
236 void power(int set) { // send SPK pwr state
237 if (acRelayState != set) {
238 if (powerEnable == 1) {
239 Serial.print("Main relay is: " + set);
240 if (set) {
241 Serial.println(" ON");
242 nextion.txt("main.acRelay", "ON");
243
244 } else {
245 Serial.println("OFF");
246 nextion.txt("main.acRelay", "OFF");
247 }
248 digitalWrite(pwr, set);
249 acRelayState = set;
250 } else {
251 digitalWrite(pwr, LOW);
252 acRelayState = 0;
253 }
254 }
255 }
256} relays;
257
258
259
260class c_power {
261public:
262 int state = 0;
263 int lastState;
264 int protectionOk;
265 int powerRelayState = 0; //0-Off, 1-On
266 int speakerRelayState = 0; //0-Off, 1-On
267 void on() {
268 relays.powerEnable = 1;
269 relays.speakerEnable = 1;
270
271 nextion.txt("standby.powerOn", "STARTING");
272 nextion.setPco("standby.powerOn", 34784);
273 state = ONs;
274 v4amp.state = 2; //running
275 relays.power(ONs);
276 last.protectionError = 1;
277 while (protectionErrors) {
278 readProtectionRun();
279 }
280 relays.spk(ONs);
281 nextion.pageChange(main);
282 //led set
283 }
284 void off() {
285 //---------------------------------------
286 delay(5000);
287 //---------------------------------------
288 state = OFFs;
289 nextion.systemVal("warning", 0);
290 v4amp.state = 1; //standby
291 relays.spk(OFFs);
292 last.protectionError = 1;
293 relays.power(OFFs);
294 nextion.pageChange(standby);
295 relays.powerEnable = 0;
296 relays.speakerEnable = 0;
297 // v4amp.mode=1;
298 }
299 int firstInitial() {
300 nextion.val("boot.vaMCU", 0);
301 nextion.val("boot.vaSupply", 0);
302 nextion.val("boot.vaProtection", 0);
303 nextion.val("boot.vaRTC", 0);
304 nextion.val("boot.vaArylic", 0);
305 nextion.txt("main.acRelay", "OFF");
306 nextion.txt("main.dcRelay", "OFF");
307
308
309 if (debug == 3) {
310 Serial.printf("Initial check\n");
311 }
312 //-----
313 while (state == 0) { //Nextion <-> ESP32 communication check
314 if ((lastMillis + 1500) < millis()) {
315 nextion.val("vaMCU", 23);
316 lastMillis = millis();
317 }
318 if (Serial1.available() > 0) {
319 nReceived = Serial1.readStringUntil(';');
320 Serial.println(nReceived);
321 }
322 if (nReceived.endsWith("42")) {
323 nextion.val("vaMCU", 1);
324 state = 1;
325 //exit; //break;
326 }
327 Serial.println(nReceived);
328 Serial.printf("State: %i\n", state);
329 }
330 Serial.printf("Nextion Communication test done. State: %i\n", state);
331
332 //------
333 while (state == 1) { //I2C check
334 //to be written later
335 i2cCheck();
336 nextion.val("vaRTC", 1);
337 state = 2;
338 Serial.printf("State: %i\n", state);
339 }
340 Serial.printf("RTC check done. State: %i\n", state);
341 lastMillis = millis();
342
343
344 relays.powerEnable = 1; //AC relay ON (Enabled)
345 relays.speakerEnable = 0;
346 relays.power(ONs);
347 delay(1000);
348 last.acError = 0;
349 last.dcErrorLeft = 0;
350 last.dcErrorRight = 0;
351 v4amp.mode = 2;
352 Serial.println("ReadProtectionBoot");
353 //-----
354 do {
355 readProtectionBoot();
356
357 } while (protectionErrors);
358 relays.speakerEnable = 1;
359
360 nextion.val("boot.vaSupply", 1);
361 nextion.val("boot.vaProtection", 1);
362 Serial.printf("Protection check done. State: %i\n", state);
363
364 int Arylic = 0;
365 Serial.printf("State: %i\n", state);
366 timer.stopwatch = millis();
367 uart.print("SYS:REBOOT;");
368
369 while (state == 2) {
370 Serial.println("Reboot sent");
371 do { //
372 if (uart.available() > 0) {
373 sReceived = uart.readStringUntil(';');
374 Serial.print("rrrrrrrrrrr ");
375 Serial.println(sReceived);
376 // if (sReceived.startsWith("MUT:")) { //aud
377 if (sReceived.startsWith("SYS:ON")) { //aud
378 Serial.println("In the loop");
379
380 nextion.val("vaArylic", 1);
381 delay(1000);
382
383 state = 3;
384 }
385 sReceived = "";
386 }
387 } while (state == 2);
388 uart.print("SYS:STANDBY;");
389 Serial.print("Digi init time: ");
390 }
391
392 if (debug == 3) {
393 Serial.printf("All check done. State: %i\n", state);
394 }
395 Serial.println(state);
396 nextion.val("boot.vaMCU", 0);
397 nextion.val("boot.vaSupply", 0);
398 nextion.val("boot.vaProtection", 0);
399 nextion.val("boot.vaRTC", 0);
400 nextion.val("boot.vaArylic", 0);
401 // If all ok, then automatic page change: "Standby"
402 return 1;
403 }
404} power;
405void reduce4n() {
406 nReceived = nReceived.substring(4);
407 Serial.println("-----reduce4n----");
408}
409void initWiFi() { ////////////////////////////////// INIT WIFI
410 WiFi.mode(WIFI_STA);
411 WiFi.begin(ssid, password);
412 Serial.print("Connecting to WiFi ..");
413 while (WiFi.status() != WL_CONNECTED) {
414 Serial.print('.');
415 delay(1000);
416 }
417 Serial.println(WiFi.localIP());
418}
419void readRTC() { ////////////////////////////////// DS3231 realtime clock module
420 char name[DATE_STRING_SHORT + 1];
421 uint8_t hour;
422 uint8_t min;
423 uint8_t sec;
424 uint8_t mday;
425 uint8_t mon;
426 uint16_t year;
427 uint8_t wday;
428
429 // Read date/time
430 if (!rtc.getDateTime(&hour, &min, &sec, &mday, &mon, &year, &wday)) {
431 Serial.println(F("Read date/time failed"));
432 return;
433 }
434
435 // Print day week
436 strncpy_P(name, &(dayNames_P[wday * DATE_STRING_SHORT]), DATE_STRING_SHORT);
437 name[DATE_STRING_SHORT] = '\0';
438 // Serial.print(name);
439 // Serial.print(F(" "));
440
441 // Print month
442 strncpy_P(name, &(monthNames_P[(mon - 1) * DATE_STRING_SHORT]), DATE_STRING_SHORT);
443 name[DATE_STRING_SHORT] = '\0';
444 // Serial.print(name);
445 // Serial.print(F(" "));
446
447 // Print day month
448 // Serial.print(mday);
449 // Serial.print(F(" "));
450
451 // Print time
452 // Serial.print(hour);
453 Serial1.print("rtc3=" + String(hour) + "\xFF\xFF\xFF"); //hour
454 // Serial.print(F(":"));
455 // if (min < 10) {
456 // Serial.print(F("0"));
457 // }
458 // Serial.print(min);
459 Serial1.print("rtc4=" + String(min) + "\xFF\xFF\xFF"); //minute
460 // Serial.print(F(":"));
461 // if (sec < 10) {
462 // Serial.print(F("0"));
463 // }
464 // Serial.print(sec);
465 Serial1.print("rtc5=" + String(sec) + "\xFF\xFF\xFF"); //second
466 // Serial.print(F(" "));
467
468 // Print year
469 // Serial.println(year);
470
471 // Wait a second
472 //delay(1000);
473}
474void rotary_loop() { ////////////////////////////////// ENCODER LOOP
475 // int tempVol;
476 // if(rotaryEncoder.encoderChanged()){
477 // tempVol=rotaryEncoder.readEncoder();
478 // Serial.print("-");
479 // }
480 if (rotaryEncoder.encoderChanged()) {
481 if (debug == 1) Serial.print("Value: ");
482 if (debug == 1) Serial.println(rotaryEncoder.readEncoder());
483 // if (NextionPage == main) {
484 uart.print("VOL:" + String(rotaryEncoder.readEncoder()) + ";");
485 Serial.println(rotaryEncoder.readEncoder());
486 digiVolume = rotaryEncoder.readEncoder();
487 nextion.systemVal("digiVol", digiVolume);
488 // }
489 int temp1 = rotaryEncoder.readEncoder();
490 }
491 if (rotaryEncoder.isEncoderButtonClicked()) {
492 rotary_onButtonClick();
493 }
494}
495void rotary_onButtonClick() { ////////////////////////////////// ENCODER SW
496 static unsigned long lastTimePressed = 0;
497 //ignore multiple press in that time milliseconds
498 if (millis() - lastTimePressed < 500) {
499 return;
500 }
501 lastTimePressed = millis();
502 if (debug == 1) Serial.print("button pressed ");
503 if (debug == 1) Serial.print(millis());
504 if (debug == 1) Serial.println(" milliseconds after restart");
505 if (dmute == 0) {
506 uart.print("MUT:1;");
507 } else {
508 uart.print("MUT:0;");
509 }
510}
511void IRAM_ATTR readEncoderISR() { ////////////////////////////////// ENCODER INTERRUPT
512 rotaryEncoder.readEncoder_ISR();
513}
514void IRAM_ATTR stateRight_ISR() { ////////////////////////////////// RIGHT INTERRUPT
515 dcErrorRight = 1;
516}
517void IRAM_ATTR stateLeft_ISR() { ////////////////////////////////// LEFT INTERRUPT
518 dcErrorLeft = 1;
519}
520void IRAM_ATTR stateAC_ISR() { ////////////////////////////////// AC INTERRUPT
521 ac_time = millis();
522 if (ac_time - ac_time_last > 250) {
523 acError = 1;
524 ac_protect = 1;
525 digitalWrite(pwr, LOW);
526 ac_time_last = ac_time;
527 }
528}
529void meta(int t, int e, int v, int i) { ////////////////////////////////// DIGI METADATA
530 nextion.vis("main.title", t);
531 nextion.vis("main.elapsed", e);
532 nextion.vis("main.vendor", v);
533 nextion.vis("main.infoText", i);
534}
535void beep_2k() { ////////////////////////////////// 2KhZ BEEP
536 unsigned long currentMillis = millis();
537 if (currentMillis - beepMillis >= 1000) {
538 if (beep == 0) {
539 // tone(12, 2000, 50);
540 } else {
541 // tone(12, 2000, 50);
542 }
543 beepMillis = currentMillis;
544 }
545}
546void beep_3k() { ////////////////////////////////// 3KhZ BEEP
547 unsigned long currentMillis = millis();
548 if (currentMillis - beepMillis >= 2000) {
549 if (beep == 0) {
550 // tone(12, 3000, 1000);
551 } else {
552 // tone(12, 3000, 1000);
553 }
554 beepMillis = currentMillis;
555 }
556}
557void inputLed(int input) { ////////////////////////////////// LED FOR SOURCE
558 source = input;
559 int r = 0, g = 0, b = 0;
560 switch (input) {
561 case 0: // WiFi - net
562 // if (powerState == 0) break;
563 r = 59;
564 g = 60;
565 b = 61;
566 break;
567 case 1: // Bluetooth - bt
568 // if (powerState == 0) break;
569 r = 0;
570 g = 0;
571 b = 50;
572 break;
573 case 2: // Line-In - line_in
574 // if (powerState == 0) break;
575 r = 0;
576 g = 50;
577 b = 0;
578 break;
579 case 3: // SPDIF - opt
580 // if (powerState == 0) break;
581 r = 50;
582 g = 0;
583 b = 0;
584 break;
585 case 4: // USB-DAC - usb dac
586 r = 12;
587 g = 67;
588 b = 5;
589 break;
590 case 5: //Standby
591 r = 55;
592 g = 35;
593 b = 0;
594 break;
595 case 6: //NET connect
596 r = 59;
597 g = 60;
598 b = 61;
599 break;
600 case 7: //NET disconnect
601 r = 29;
602 g = 30;
603 b = 31;
604 break;
605 case 8: //BT connect
606 r = 0;
607 g = 0;
608 b = 50;
609 break;
610 case 9: //BT disconnect
611 r = 0;
612 g = 0;
613 b = 10;
614 break;
615 }
616
617 for (int i = 0; i < NUMPIXELS; i++) {
618 pixels.setPixelColor(i, pixels.Color(g, r, b));
619 }
620 pixels.show(); // Send the updated pixel colors to the hardware.
621}
622void functionLed(int function) { ////////////////////////////////// LED FOR FUNCTIONS
623 task = function;
624 int r = 0, g = 0, b = 0;
625 switch (function) {
626 case 0: // task is "ON"
627 if (powerState == 0) break;
628 r = 49;
629 g = 50;
630 b = 51;
631 break;
632 case 1: // task is "OFF"
633 if (powerState == 0) break;
634 r = 0;
635 g = 0;
636 b = 50;
637 break;
638 case 2: // task is "DC ERROR"
639 if (powerState == 0) break;
640 r = 0;
641 g = 50;
642 b = 0;
643 break;
644 case 3: // task is "Temp ERROR"
645 if (powerState == 0) break;
646 r = 50;
647 g = 0;
648 b = 0;
649 break;
650 case 4: // task is "System starting..."
651 r = 55;
652 g = 35;
653 b = 0;
654 break;
655 }
656 for (int i = 0; i < NUMPIXELS; i++) {
657 pixels.setPixelColor(i, pixels.Color(g, r, b));
658 }
659 pixels.show(); // Send the updated pixel colors to the hardware.
660}
661void senseBrightness() { ////////////////////////////////// SENSE BRIGHTNESS
662 unsigned long currentTime = millis();
663 if (currentTime - previousTime_1 >= eventTime_1_LDR) {
664 String m;
665 switch (analogRead(LDR)) {
666 case 0 ... 40:
667 dimN = 25;
668 m = "Dark";
669 break;
670 case 41 ... 800:
671 dimN = 40;
672 m = "Light";
673 break;
674 case 801 ... 2000:
675 dimN = 60;
676 m = "Bright";
677 break;
678 case 2001 ... 3200:
679 dimN = 80;
680 m = "Very Bright";
681 break;
682 case 3201 ... 4500:
683 dimN = 100;
684 m = "Very Very Bright";
685 break;
686 }
687 if (dim != dimN) {
688 nextion.val("ampSetup.nDimESP", dimN);
689 nextion.val("ampSetup.hSlideESP", dimN);
690 Serial.println(" => " + m);
691 Serial.println(analogRead(LDR));
692 dim = dimN;
693 }
694 previousTime_1 = currentTime;
695 }
696}
697void senseTemp() { ////////////////////////////////// SENSE TEMPERATURE
698 static unsigned long timer1, timer2;
699 if (millis() - timer1 > sense1) { // firtst --> 1st
700 timer1 = millis();
701 //Serial.print("Temp 1: ");
702 //Serial.println(tmp1.readTemperature('c'));
703 if (NextionPage == ampSetup) nextion.val("ampSetup.nTempLeft", tmp1.readTemperature('c'));
704 }
705 if (millis() - timer2 > sense2) {
706 timer2 = millis();
707 //Serial.print("Temp 2: ");
708 //Serial.println(tmp2.readTemperature('C'));
709 if (NextionPage == ampSetup) nextion.val("ampSetup.nTempRight", tmp2.readTemperature('c'));
710 }
711}
712void readProtectionBoot() { ////////////////////////////////// READ COMPLETE PROTECTION (AC+DC)
713 acError = !digitalRead(acSense);
714 dcErrorRight = digitalRead(dcSenseRight);
715 dcErrorLeft = digitalRead(dcSenseLeft);
716 protectionErrors = dcErrorRight + dcErrorLeft + acError + v4amp.mode;
717 if (acError == 1 && last.acError == 0) {
718 relays.spk(OFFs);
719 nextion.val("ampSetup.vaAC", 0); //1 - OK
720 relays.speakerEnable = 0; //no relay switch
721 }
722 last.acError = acError;
723
724 if (dcErrorLeft == 1 && last.dcErrorLeft == 0) { //error happens
725 relays.spk(OFFs);
726 nextion.val("ampSetup.vaLeft", 0);
727 }
728 if (dcErrorLeft == 0 && last.dcErrorLeft == 1) { //no more error
729 relays.spk(OFFs);
730 nextion.val("ampSetup.vaLeft", 0);
731 if (dcErrorRight == 0) {
732 timer.protectionOffDelay = millis();
733 v4amp.mode = 1;
734 }
735 }
736 last.dcErrorLeft = dcErrorLeft;
737 if (dcErrorRight == 1 && last.dcErrorLeft == 0) {
738 relays.spk(OFFs);
739 nextion.val("ampSetup.vaRight", 0);
740 timer.protectionOffDelay = millis();
741 }
742 if (dcErrorRight == 0 && last.dcErrorLeft == 1) {
743 relays.spk(OFFs);
744 nextion.val("ampSetup.vaRight", 0);
745 if (dcErrorLeft == 0) {
746 timer.protectionOffDelay = millis();
747 v4amp.mode = 1;
748 }
749 }
750 last.dcErrorRight = dcErrorRight;
751 if (timer.protectionOffDelay + 5000 < millis() && v4amp.mode == 1) {
752 Serial.print("timer.protectionOffDelay: ");
753 Serial.println(timer.protectionOffDelay);
754 relays.power(OFFs);
755 v4amp.mode = 0;
756 }
757 // Serial.print("timer.protectionOffDelay: ");
758 // Serial.println(millis() - timer.protectionOffDelay);
759
760 if ((dcErrorLeft == 0 || dcErrorRight == 0) && v4amp.mode == 2) {
761 v4amp.mode = 1;
762 timer.protectionOffDelay = millis();
763 Serial.print("a4amp.mode: ");
764 Serial.println(v4amp.mode);
765 delay(2500);
766 }
767}
768void readProtectionRun() {
769 //
770 // v4amp.mode=
771 //
772 //
773 acError = !digitalRead(acSense);
774 dcErrorRight = digitalRead(dcSenseRight);
775 dcErrorLeft = digitalRead(dcSenseLeft);
776 protectionErrors = dcErrorRight + dcErrorLeft + acError;
777
778 // if((last.dcErrorLeft==0&&dcErrorLeft==1)||last.dcErrorRight==0&&dcErrorRight==1){
779 // relays.spk(OFFs);
780 // timer.protectionOffDelay=millis();
781 // }
782 if (protectionErrors == 0 && last.protectionError == 1) { //when error(s) gone
783 // relays.spk(ONs);
784 timer.protectionOnDelay = millis();
785 Serial.println("in the protection");
786 timer.protectionOffDelay = 0 - 5001;
787 } else {
788 // timer.protectionOffDelay=millis();
789 // timer.protectionOnDelay=0-5001;
790 }
791 if (protectionErrors == 0 && last.protectionError == 0 && (timer.protectionOnDelay + 5000 < millis())) { //when there is no error, spk turn on after 5 seconds
792 relays.spk(ONs);
793 // Serial.printf("timer.protectionOnDelay: %u millis(): %u", timer.protectionOnDelay, millis());
794 }
795
796 if (protectionErrors == 1 && last.protectionError == 0) { //when any error(s) occur(s)
797 relays.spk(OFFs);
798 timer.protectionOffDelay = millis();
799 }
800
801 if (timer.protectionOffDelay + 5000 < millis()) { //if there was/were any error/errors and constantly present 5 seconds after the ac(power) relay turns OFF
802 relays.power(OFFs);
803 nextion.systemVal("warning", 1);
804 }
805
806 //last errors save
807 last.acError = acError;
808 last.dcErrorLeft = dcErrorLeft;
809 last.dcErrorRight = dcErrorLeft;
810 last.protectionError = protectionErrors;
811}
812
813void readAC() { ////////////////////////////////// READ POWER SUPPLY
814 acError = digitalRead(acSense);
815
816 if (acError != acErrorLast) {
817 if (ac_protect == 1) {
818 Serial.println("Interrupt on AC pin");
819 }
820 if (acError == 0) {
821 if (debug == 2) Serial.println("AC is OK!");
822 nextion.systemVal("warning", 0);
823 nextion.val("ampSetup.vaAC", 0);
824 nextion.val("boot.vaSupply", 1);
825 }
826 if (acError == 1) {
827 if (debug == 2) Serial.println("Missing AC voltage!");
828 nextion.systemVal("warning", 1);
829 nextion.val("ampSetup.vaAC", 1);
830 nextion.val("boot.vaSupply", 1); // -> 0
831 ac_protect = 0;
832 }
833 acErrorLast = acError;
834 }
835}
836void readSignal() {
837 if (strengthMillis + 1000 < millis() && source == 0) {
838 uart.print("WSS;");
839 strengthMillis = millis();
840 }
841 if (strengthMillis + 1000 < millis() && source == 1) {
842 uart.print("BSS;");
843 strengthMillis = millis();
844 }
845}
846int i2cSend(int address) {
847 Wire.beginTransmission(address);
848 return Wire.endTransmission();
849}
850int numDevices = 6;
851void i2cCheck() {
852 numDevices = 6;
853
854 tc74_1.state = i2cSend(tc74_1.address);
855 numDevices--;
856 (tc74_1.state == 0) ? tc74_1.stateStr = "OK" : tc74_1.stateStr = "FAIL";
857 Serial.printf("%s State: %s I2C state: %i\n", tc74_1.name, tc74_1.stateStr, tc74_1.state);
858 //------------------------------
859 tc74_2.state = i2cSend(tc74_2.address);
860 numDevices--;
861 (tc74_2.state == 0) ? tc74_2.stateStr = "OK" : tc74_2.stateStr = "FAIL";
862 Serial.printf("%s State: %s\n", tc74_1.name, tc74_1.stateStr);
863 //------------------------------
864 rtcModule.state = i2cSend(rtcModule.address);
865 numDevices--;
866 (rtcModule.state == 0) ? rtcModule.stateStr = "OK" : rtcModule.stateStr = "FAIL";
867 Serial.printf("%s State: %s\n", tc74_2.name, tc74_2.stateStr);
868 //------------------------------
869 rtcEEPROM.state = i2cSend(rtcEEPROM.address);
870 numDevices--;
871 (rtcEEPROM.state == 0) ? rtcEEPROM.stateStr = "OK" : rtcEEPROM.stateStr = "FAIL";
872 Serial.printf("%s State: %s\n", rtcEEPROM.name, rtcEEPROM.stateStr);
873 //------------------------------
874 dspModule.state = i2cSend(dspModule.address);
875 numDevices--;
876 (dspModule.state == 0) ? dspModule.stateStr = "OK" : dspModule.stateStr = "FAIL";
877 Serial.printf("%s State: %s\n", dspModule.name, dspModule.stateStr);
878 //------------------------------
879 dspEEPROM.state = i2cSend(dspModule.address);
880 numDevices--;
881 (dspEEPROM.state == 0) ? dspEEPROM.stateStr = "OK" : dspEEPROM.stateStr = "FAIL";
882 Serial.printf("%s State: %s\n", dspEEPROM.name, dspEEPROM.stateStr);
883
884 if (numDevices == 0) {
885 //Serial.println("I2C devices OK");
886 }
887}
888
889void setup() { ////////////////////////////////// START OF SETUP
890 v4amp.mode = boot;
891 Wire.begin(SDA, SCL);
892 Serial.begin(115200);
893 Serial1.begin(115200, SERIAL_8N1, RXN_PIN, TXN_PIN);
894 uart.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
895 while (!Serial1)
896 ;
897 while (!uart)
898 ;
899 Serial.print("File: ");
900 Serial.println(F(__FILE__));
901 delay(3000);
902
903 rtc.begin();
904 tmp1.begin();
905 tmp2.begin();
906 pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
907 //clock.setDateTime(__DATE__, __TIME__);
908 //rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // When uploading the code, the computer's time settings are passed to the RTC. We REM it out the next time we upload.
909 //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
910
911 tc74_1 = { "TempL", 0x4A, 0, "FAIL" }; //TC74 LEFT
912 tc74_2 = { "TempR", 0X4B, 0, "FAIL" }; //TC74 RIGHT
913 rtcModule = { "RTC", 0x68, 0, "FAIL" };
914 rtcEEPROM = { "RTC_EEPROM", 0x57, 0, "FAIL" };
915 dspModule = { "DSP_Module", 0x68, 0, "FAIL" };
916 dspEEPROM = { "DSP_EEPROM", 0x0F, 0, "FAIL" };
917 ac_time = 0;
918 for (int i = 0; i < 5; i++) {
919 // Serial.printf("ac_time: %i\n", ac_time);
920 Serial.print("ac_time: ");
921 Serial.println(ac_time);
922 ac_time--;
923 delay(200);
924 }
925 pinMode(pwr, OUTPUT);
926 digitalWrite(pwr, LOW);
927 pinMode(speaker, OUTPUT);
928 digitalWrite(speaker, LOW);
929
930 pinMode(powerSW, INPUT_PULLUP); // buttons is active LOW
931 pinMode(dcSenseRight, INPUT_PULLUP);
932 pinMode(dcSenseLeft, INPUT_PULLUP);
933 pinMode(acSense, INPUT_PULLUP);
934 rotaryEncoder.begin();
935 rotaryEncoder.setup(readEncoderISR);
936 //ir***********************************************************************************
937 // Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
938 //Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
939 IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
940 // Serial.print(F("Ready to receive IR signals of protocols: "));
941 // printActiveIRProtocols(&Serial);
942 // Serial.println(F("at pin " STR(IR_RECEIVE_PIN)));
943 //ir***********************************************************************************
944 inputLed(4);
945 nextion.pageChange(boot);
946 nextion.txt("boot.infoText", "System is starting...");
947
948 bool circleValues = false;
949 /*Rotary acceleration introduced 25.2.2021.
950 * in case range to select is huge, for example - select a value between 0 and 1000 and we want 785
951 * without accelerateion you need long time to get to that number
952 * Using acceleration, faster you turn, faster will the value raise.
953 * For fine tuning slow down.
954 */
955 //rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
956 rotaryEncoder.setAcceleration(0); //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration
957 rotaryEncoder.setEncoderValue(digiVolume);
958 rotaryEncoder.setBoundaries(0, 100, circleValues); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
959 // attachInterrupt(dcSenseRight, stateRight_ISR, RISING); //Interrupts for protection: left: RISING,right: RISING,AC: FALLING
960 // attachInterrupt(dcSenseLeft, stateLeft_ISR, RISING);
961 // attachInterrupt(acSense, stateAC_ISR, FALLING);
962 //initWiFi(); //in the kitchen, no WIFI :D
963 //String LocalIP = String() + WiFi.localIP()[0] + "." + WiFi.localIP()[1] + "." + WiFi.localIP()[2] + "." + WiFi.localIP()[3];
964 //Serial.println(WiFi.localIP());
965 //Serial.println(LocalIP);
966 if (debug) Serial.println("Starting..");
967 if (debug == 1) Serial.println("Started...");
968 if (debug == 1) Serial.println(compile_date);
969 // uart.print("PMT:1;");
970 // uart.print("BEP:0;");
971 // uart.print("BEP;");
972 // uart.print("PMT;");
973 nextion.touchSet("main.preset", 0);
974 dcErrorRight = digitalRead(dcSenseRight);
975 dcErrorLeft = digitalRead(dcSenseLeft);
976 acError = digitalRead(acSense);
977
978 delay(10000);//if (debug == 2) Serial.printf("AC-%d DCL-%d DCR-%d\n", acError, dcErrorLeft, dcErrorRight);
979
980 uart.print("SYS:STANDBY;"); //Reboot Digi
981 Serial.println("Digi Off");
982 Serial.println("---------------------------------------------");
983Serial.println(compile_date);
984 Serial.println("Arylic_0411"); //__FILE__
985 readRTC();
986 // digitalWrite(pwr, HIGH);
987 relays.power(ONs);
988 last.protectionError = 1;
989 power.firstInitial();
990 v4amp.mode = 2;
991}
992void loop() { ////////////////////////////////// START OF LOOP
993
994
995 if (v4amp.mode == 2) {
996
997
998 readSignal();
999
1000 readProtectionRun();
1001 senseBrightness();
1002 senseTemp();
1003 rotary_loop();
1004
1005 if (IrReceiver.decode()) { //////////// IR REMOTE HANDLING
1006 // Serial.println(NextionPage);
1007 // IrReceiver.printIRResultShort(&Serial);
1008 // IrReceiver.printIRSendUsage(&Serial);
1009 if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
1010 // Serial.println(F("Received noise or an unknown (or not yet enabled) protocol")); // We have an unknown protocol here, print more inf
1011 // IrReceiver.printIRResultRawFormatted(&Serial, true);
1012 }
1013 Serial.println();
1014 IrReceiver.resume(); // Enable receiving of the next value
1015 if (IrReceiver.decodedIRData.command == 0x45 && (irMillis + 5000) < millis()) { // Power button && irLastCode != IrReceiver.decodedIRData.command
1016 if (NextionPage == 2) { //0
1017 nextion.click("powerOff", 1);
1018 // tone(23, 1000, 50);
1019 }
1020 // if (NextionPage == 1) { //2 //To turn ON by remote controller
1021 // nextion.click("powerOn", 1);
1022 // readRTC();
1023 // // tone(23, 1000, 50);
1024 // }
1025 irMillis = millis();
1026 Serial.println("Pressed: POWER");
1027 } else if (IrReceiver.decodedIRData.command == 0x19) { // BT button
1028 uart.print("SRC:BT;");
1029 Serial.println("Pressed: BT");
1030 } else if (IrReceiver.decodedIRData.command == 0xD) { // FM button
1031 //uart.print("SRC:BT;");
1032 Serial.println("Pressed: FM");
1033 } else if (IrReceiver.decodedIRData.command == 0x09 && irVolMillis + 300 < millis()) { // VolUp
1034 if (digiVolume < 100) {
1035 digiVolume = digiVolume + volStep;
1036 if (digiVolume > 100) digiVolume = 100;
1037 }
1038 irVolMillis = millis();
1039 Serial.println(digiVolume);
1040 uart.print("VOL:" + String(digiVolume) + ";");
1041 Serial.println("Pressed: VOL UP");
1042 } else if (IrReceiver.decodedIRData.command == 0x15 && irVolMillis + 300 < millis()) { // VolDown
1043 if (digiVolume > 0) {
1044 digiVolume = digiVolume - volStep;
1045 if (digiVolume < 0) digiVolume = 0;
1046 }
1047 irVolMillis = millis();
1048 Serial.println(digiVolume);
1049 uart.print("VOL:" + String(digiVolume) + ";");
1050 Serial.println("Pressed: VOL DOWN");
1051 } else if (IrReceiver.decodedIRData.command == 0x46 && (irMillis + 5000) < millis()) { // Input stepping
1052 if (sourceS < 4) {
1053 sourceS++;
1054 } else {
1055 sourceS = 0;
1056 }
1057 String inp[5] = { "NET", "BT", "LINE-IN", "USBDAC", "OPT" };
1058 uart.print("SRC:" + inp[sourceS] + ";");
1059 Serial.println("SRC:" + inp[sourceS] + ";");
1060 Serial.println(sourceS);
1061 irMillis = millis();
1062 Serial.println("Pressed: MODE (input stepping)");
1063 } else if (IrReceiver.decodedIRData.command == 0x7 && (irMillis + 5000) < millis()) { // Muting
1064 if (NextionPage == main) {
1065 uart.print("MUT:1;");
1066 }
1067 if (NextionPage == main) {
1068 uart.print("MUT:0;");
1069 }
1070 irMillis = millis();
1071 Serial.println("Pressed: MUTE");
1072 } else if (IrReceiver.decodedIRData.command == 0x47) { // Scan
1073 // uart.print("SRC:NET;");
1074 Serial.println("Pressed: SCAN");
1075 } else if (IrReceiver.decodedIRData.command == 0x43) { // >> / Next track, only available in BT/NET/USB mode (*1)
1076 uart.print("NXT;");
1077 Serial.println("Pressed: NEXT");
1078 } else if (IrReceiver.decodedIRData.command == 0x40) { // << / Previous track, only available in BT/NET/USB mode (*1)
1079 uart.print("PRE;");
1080 Serial.println("Pressed: PREV");
1081 } else if (IrReceiver.decodedIRData.command == 0x44) { // Play/Pause track, only available in BT/NET/USB mode (*1)
1082 uart.print("POP;");
1083 Serial.println("Pressed: PLAY/PAUSE");
1084 } else if (IrReceiver.decodedIRData.command == 0xC) { // 1
1085 uart.print("PST:1;");
1086 // digi.volume=digi.maxVolume-10;
1087 nextion.txt("main.preset", "Preset 1");
1088 Serial.println("Pressed: BT1");
1089 } else if (IrReceiver.decodedIRData.command == 0x18) { // 2
1090 uart.print("PST:2;");
1091 nextion.txt("main.preset", "Preset 2");
1092 Serial.println("Pressed: BT2");
1093 } else if (IrReceiver.decodedIRData.command == 0x5E) { // 3
1094 uart.print("PST:3;");
1095 nextion.txt("main.preset", "Preset 3");
1096 Serial.println("Pressed: BT3");
1097 } else if (IrReceiver.decodedIRData.command == 0x8) { // 4
1098 uart.print("PST:4;");
1099 nextion.txt("main.preset", "Preset 4");
1100 Serial.println("Pressed: BT4");
1101 } else if (IrReceiver.decodedIRData.command == 0x1C) { // 5
1102 uart.print("PST:5;");
1103 nextion.txt("main.preset", "Preset 5");
1104 Serial.println("Pressed: BT5");
1105 } else if (IrReceiver.decodedIRData.command == 0x5A) { // 6
1106 uart.print("PST:6;");
1107 nextion.txt("main.preset", "Preset 6");
1108 Serial.println("Pressed: BT6");
1109 } else if (IrReceiver.decodedIRData.command == 0x42) { // 7
1110 uart.print("PST:7;");
1111 nextion.txt("main.preset", "Preset 7");
1112 Serial.println("Pressed: BT7");
1113 } else if (IrReceiver.decodedIRData.command == 0x52) { // 8
1114 uart.print("PST:8;");
1115 nextion.txt("main.preset", "Preset 8");
1116 Serial.println("Pressed: BT8");
1117 } else if (IrReceiver.decodedIRData.command == 0x4A) { // 9
1118 uart.print("PST:9;");
1119 nextion.txt("main.preset", "Preset 9");
1120 Serial.println("Pressed: BT9");
1121 } else if (IrReceiver.decodedIRData.command == 0x16) { // 0
1122 uart.print("PST:10;");
1123 nextion.txt("main.preset", "Preset 10");
1124 Serial.println("Pressed: BT0");
1125 }
1126 }
1127
1128 if (digitalRead(powerSW) == 0 && lastPressed + 5000 < millis()) { //////////// POWER SW HANDLING digitalRead(powerSW) == 0 &&
1129 power.off();
1130 lastPressed = millis();
1131 }
1132
1133
1134 while (uart.available()) { //////////// UART HANDLING FOR ARYLIC
1135 sReceived = uart.readStringUntil('\n');
1136 sReceived.trim();
1137 if (debug == 1) Serial.println("uart:___|----------" + sReceived);
1138 if (debug == 1) Serial.println("");
1139 if (sReceived.startsWith("PLA:0")) {
1140 nextion.txt("main.infoText", "Szünet/Megállítva");
1141 } else if (sReceived.startsWith("PLA:1")) {
1142 nextion.txt("main.infoText", "Lejátszás...");
1143 } else if (sReceived.startsWith("STA:")) { //////////// STA HANDLING
1144 Serial.print("STA: ");
1145 nextion.vis("main.infoText", 0);
1146 if (debug == 1) Serial.println("STA received");
1147 if (debug == 1) Serial.println("Before: " + sReceived);
1148 nextion.txt("main.infoText", "");
1149 // nextion.vis("main.title", 0);
1150 // nextion.vis("main.elapsed", 0);
1151 // nextion.vis("main.vendor", 0);
1152 nextion.touchSet("main.preset", 0);
1153 reduce4();
1154
1155 if (sReceived.startsWith("NET")) { //////////// WIFI
1156 sourceS = 0;
1157 inputLed(0);
1158 meta(1, 1, 1, 1);
1159 if (debug == 1) Serial.println("...WIFI...");
1160 nextion.txt("main.input", "WiFi");
1161 } else if (sReceived.startsWith("BT")) { //////////// BLUETOOTH
1162 sourceS = 1;
1163 inputLed(1);
1164 meta(0, 0, 0, 0);
1165 if (debug == 1) Serial.println("...BLUETOOTH...");
1166 nextion.txt("main.input", "Bluetooth");
1167 } else if (sReceived.startsWith("LINE-IN")) { //////////// LINE-IN
1168 sourceS = 2;
1169 inputLed(2);
1170 meta(0, 0, 0, 0);
1171 if (debug == 1) Serial.println("...LINE-IN...");
1172 nextion.txt("main.input", "Line In");
1173 } else if (sReceived.startsWith("USBDAC")) { //////////// USB DAC
1174 sourceS = 3;
1175 inputLed(3);
1176 meta(0, 0, 0, 0);
1177 if (debug == 1) Serial.println("...USB DAC...");
1178 nextion.txt("main.input", "USB DAC");
1179 } else if (sReceived.startsWith("OPT")) { //////////// USB DAC
1180 sourceS = 4;
1181 inputLed(4);
1182 meta(0, 0, 0, 0);
1183 if (debug == 1) Serial.println("...SPDIF...");
1184 nextion.txt("main.input", "SPDIF");
1185 }
1186 } else if (sReceived.startsWith("SYS:STANDBY")) { //////////// POWER OFF HANDLING
1187 power.off();
1188
1189 } else if (sReceived.startsWith("SRC:")) { //////////// INPUT SOURCE HANDLING
1190 // nextion.vis("main.title", 0);
1191 // nextion.vis("main.elapsed", 0);
1192 // nextion.vis("main.vendor", 0);
1193 nextion.touchSet("main.preset", 0);
1194 //nextion.pageChange(main);
1195 reduce4();
1196
1197 if (sReceived.startsWith("NET")) { //////////// WIFI
1198 sourceS = 0;
1199 inputLed(0);
1200 meta(1, 1, 1, 1);
1201 if (debug == 1) Serial.println("...WIFI...");
1202 nextion.txt("main.input", "WiFi");
1203 } else if (sReceived.startsWith("BT")) { //////////// BLUETOOTH
1204 sourceS = 1;
1205 inputLed(1);
1206 meta(0, 0, 0, 0);
1207 if (debug == 1) Serial.println("...BLUETOOTH...");
1208 nextion.txt("main.input", "Bluetooth");
1209 } else if (sReceived.startsWith("LINE-IN")) { //////////// LINE-IN
1210 sourceS = 2;
1211 inputLed(2);
1212 meta(0, 0, 0, 0);
1213 if (debug == 1) Serial.println("...LINE-IN...");
1214 nextion.txt("main.input", "Line In");
1215 } else if (sReceived.startsWith("USBDAC")) { //////////// USB DAC
1216 sourceS = 3;
1217 inputLed(3);
1218 meta(0, 0, 0, 0);
1219 if (debug == 1) Serial.println("...USB DAC...");
1220 nextion.txt("main.input", "USB DAC");
1221 } else if (sReceived.startsWith("OPT")) { //////////// USB DAC
1222 sourceS = 4;
1223 inputLed(4);
1224 meta(0, 0, 0, 0);
1225 if (debug == 1) Serial.println("...SPDIF...");
1226 nextion.txt("main.input", "SPDIF");
1227 }
1228 } else if (sReceived.startsWith("VOL:") && (NextionPage == main)) { //////////// VOLUME HANDLING
1229 reduce4();
1230 int index = sReceived.indexOf(';');
1231 sReceived = sReceived.substring(0, index);
1232 if (sReceived == "100") {
1233 nextion.txt("main.volText", "MAX");
1234 } else if (sReceived == "0") {
1235 nextion.txt("main.volText", "MIN");
1236 } else {
1237 if (debug == 1) Serial.println("volume: -|:" + sReceived);
1238 digiVolume = sReceived.toInt();
1239 nextion.txt("main.volText", sReceived);
1240 }
1241 nextion.systemVal("digiVol", digiVolume);
1242 rotaryEncoder.setEncoderValue(digiVolume);
1243 } else if (sReceived.startsWith("MUT:")) { //////////// MUTE HANDLING
1244 reduce4();
1245 sReceived = sReceived.substring(0, 1);
1246 if (debug == 1) Serial.println("Mute:_____/:|" + sReceived);
1247 if (sReceived == "1") {
1248 dmute = 1;
1249 nextion.txt("main.volText", "MIN");
1250 nextion.systemVal("digiVol", 0);
1251 } else if (sReceived == "0") {
1252 dmute = 0;
1253 nextion.txt("main.volText", String(digiVolume));
1254 nextion.systemVal("digiVol", digiVolume);
1255 }
1256
1257 } else if (sReceived.startsWith("BTC:")) { //////////// BLUETOOTH CONNECTION HANDLING
1258 reduce4();
1259 sReceived = sReceived.substring(0, 1);
1260 if (sReceived == "1") {
1261 //nextion.txt("main.infoText", "CONNECTED");
1262 uart.print("TIT;");
1263 inputLed(8);
1264 } else if (sReceived == "0") {
1265 //nextion.txt("main.infoText", "DISCONNECTED");
1266 inputLed(9);
1267 }
1268 } else if (sReceived.endsWith("SYS:ON;") && (NextionPage == standby)) { //////////// POWER ON HANDLING //25.01.10
1269 power.on();
1270 } else if (sReceived.startsWith("NET:")) { //////////// NETWORK CONNECTION HANDLING
1271 reduce4();
1272 sReceived = sReceived.substring(0, 1);
1273 if (sReceived == "1") {
1274 //nextion.txt("main.infoText", "CONNECTED");
1275 inputLed(6);
1276 uart.print("TIT;");
1277 nextion.touchSet("main.preset", 1);
1278 //inputLed(0);
1279 } else if (sReceived == "0") {
1280 //nextion.txt("main.infoText", "DISCONNECTED");
1281 nextion.touchSet("main.preset", 0);
1282 inputLed(7);
1283 }
1284 } else if (sReceived.startsWith("TIT:") && (NextionPage == main)) { //////////// Title
1285 reduce4();
1286 if (debug == 1) Serial.println("Title: " + sReceived);
1287 sReceived = sReceived.substring(0, sReceived.length() - 1);
1288 nextion.txt("main.title", sReceived);
1289 } else if (sReceived.startsWith("ART:") && (NextionPage == main)) { //////////// Artist
1290 reduce4();
1291 if (debug == 1) Serial.println("Artist: " + sReceived);
1292 sReceived = sReceived.substring(0, sReceived.length() - 1);
1293 nextion.txt("main.artist", sReceived);
1294 } else if (sReceived.startsWith("ELP:")) { //////////// Elapsed playing time
1295 reduce4();
1296 int index = sReceived.indexOf("/");
1297 sReceived = sReceived.substring(0, index);
1298 // Serial1.println(sReceived);
1299 long time = sReceived.toInt();
1300 time = time / 100;
1301 int tenth = time % 10;
1302 time = time / 10;
1303 long hour = time / 3600;
1304 time = time - (hour * 3600);
1305 long min = time / 60;
1306 long sec = time - (min * 60);
1307 String timeS = "Time: ";
1308 if (hour < 10) timeS += "0";
1309 timeS += String(hour) + ":";
1310 if (min < 10) timeS += "0";
1311 timeS += String(min) + ":";
1312 if (sec < 10) timeS += "0";
1313 timeS += String(sec); // + "." + String(tenth);
1314 if (time > 0) nextion.txt("main.elapsed", timeS);
1315
1316 } else if (sReceived.startsWith("BAS:")) { //////////// BASS
1317 reduce4();
1318 int bass = sReceived.toInt();
1319 nextion.val("digiSetup.nbass", bass);
1320 if (bass < 0) {
1321 bass = 11 - abs(bass);
1322 } else {
1323 bass = bass + 11;
1324 }
1325 nextion.val("digiSetup.hbass", bass);
1326
1327 } else if (sReceived.startsWith("TRE:")) { //////////// TREBLE
1328 reduce4();
1329 sReceived = sReceived.substring(0, sReceived.length() - 1);
1330 int treb = sReceived.toInt();
1331 nextion.val("digiSetup.ntreb", treb);
1332 if (treb < 0) {
1333 treb = 11 - abs(treb);
1334 } else {
1335 treb = treb + 11;
1336 }
1337 nextion.val("digiSetup.htreb", treb);
1338
1339 } else if (sReceived.startsWith("WSS:") && (NextionPage == digiSetup)) { //////////// WIFI SIGNAL STRENGTH
1340 reduce4();
1341 int signalWifi = sReceived.toInt();
1342 Serial.println("WiFi signal strength: " + sReceived);
1343 nextion.val("digiSetup.nWifi", signalWifi);
1344
1345 } else if (sReceived.startsWith("BSS:") && (NextionPage == digiSetup)) { //////////// BLUETOOTH SIGNAL STRENGTH
1346 reduce4();
1347 int signalBT = sReceived.toInt();
1348 Serial.println("Bluetooth signal strength: " + sReceived);
1349 nextion.val("digiSetup.nBt", signalBT);
1350
1351 } else if (sReceived.startsWith("MXV:")) { //////////// Max volume, %
1352 reduce4();
1353 int volMax = sReceived.toInt();
1354 nextion.val("digiSetup.nVolMax", volMax);
1355 nextion.val("digiSetup.hVolMax", volMax);
1356 if (debug == 2) Serial.println(volMax);
1357
1358 } else if (sReceived.startsWith("VBS:")) { //////////// Virtual Bass ON/OFF
1359 reduce4();
1360 sReceived = sReceived.substring(0, sReceived.length() - 1);
1361 if (sReceived == "1") {
1362 nextion.val("digiSetup.vbs", 1);
1363 if (debug == 2) Serial.println("VBS: on");
1364 } else {
1365 nextion.val("digiSetup.vbs", 0);
1366 if (debug == 2) Serial.println("VBS: off");
1367 }
1368 } else if (sReceived.startsWith("PMT:")) { //////////// Promt Voice ON/OFF
1369 reduce4();
1370 sReceived = sReceived.substring(0, sReceived.length() - 1);
1371 if (sReceived == "1") {
1372 nextion.val("digiSetup.pmt", 1);
1373 if (debug == 2) Serial.println("PMT: on");
1374 } else {
1375 nextion.val("digiSetup.pmt", 0);
1376 if (debug == 2) Serial.println("PMT: off");
1377 }
1378
1379 } else if (sReceived.startsWith("NAM:")) { //////////// Device name
1380 reduce4();
1381 sReceived = sReceived.substring(0, sReceived.length() - 1);
1382 String dname, Nname;
1383 int h = 16, sz = 0, dsz = 0;
1384 if (sReceived.length() > 0) {
1385 for (int i = 0; i <= sReceived.length() - 1; i = i + 1) {
1386 dname = sReceived.substring(i, i + 1);
1387 if (dname.toInt() >= 0 && dname.toInt() <= 9) {
1388 sz = dname.toInt();
1389 // if (debug) Serial.println(dname);
1390 }
1391 if (dname == "A") sz = 10;
1392 if (dname == "B") sz = 11;
1393 if (dname == "C") sz = 12;
1394 if (dname == "D") sz = 13;
1395 if (dname == "E") sz = 14;
1396 if (dname == "F") sz = 15;
1397 // sz += sz * h;
1398 if (h == 0) {
1399 dsz += sz;
1400 // if (debug) Serial.printf("%i. sz=%i\n", i, dsz);
1401 Nname += char(dsz);
1402 dsz = 0;
1403 } else {
1404 dsz = sz * 16;
1405 }
1406 h = 16 - h;
1407 }
1408 if (debug == 1) Serial.println(Nname);
1409 nextion.txt("main.NAME", Nname);
1410 }
1411 } else if (sReceived.startsWith("IPA:")) { //////////// Device IP address for connecting to browser's control panel, psw:admin
1412 reduce4();
1413 sReceived = sReceived.substring(0, sReceived.length() - 1); //removing ";"
1414 nextion.txt("digiSetup.digiIP", sReceived);
1415 } else if (sReceived.startsWith("VND:")) { //////////// Vendor - Tidal, Spotify, etc...
1416 reduce4();
1417 sReceived = sReceived.substring(0, sReceived.length() - 1); //removing ";"
1418 nextion.txt("main.vaVendor", sReceived);
1419 } else if (sReceived.startsWith("PST:")) { //////////// Preset, not response, only command (1-10)
1420 reduce4();
1421 sReceived = sReceived.substring(0, sReceived.length() - 1);
1422 if (debug == 1) Serial.println(";;;;;;;;;; " + sReceived);
1423 } else if (sReceived.startsWith("VST:")) {
1424 reduce4();
1425 String nTemp = sReceived.substring(4); //////////// VST HANDLING
1426 volStep = sReceived.toInt();
1427 nextion.val("digiSetup.nVolStep", volStep);
1428 nextion.val("digiSetup.hVolStep", volStep);
1429 Serial.println("VST: " + sReceived);
1430 }
1431 sReceived = "";
1432 }
1433 while (Serial1.available()) { //////////// UART HANDLING FOR NEXTION
1434 nReceived = Serial1.readStringUntil(';');
1435 Serial.print("String received from Nextion: ");
1436 Serial.println(nReceived);
1437 // Serial.println(nReceived);
1438 if (nReceived.startsWith("NPG:")) { //////////// NEXTION PAGE NUMBER
1439 reduce4n();
1440 // nReceived = nReceived.substring(4);
1441 Serial.println(nReceived + " ");
1442 Serial.print("Nextion PageChange to: ");
1443 Serial.println(nReceived.toInt());
1444 NextionPage = nReceived.toInt();
1445 if (NextionPage == 2) {
1446 power.on();
1447 }
1448 // nextion.pageChange(NextionPage);
1449 toDigi = 0;
1450 }
1451 if (nReceived == "SYS:STANDBY") { //////////// STANDBY COMMAND
1452 power.off();
1453 }
1454 if (nReceived == "LDR:0") { //////////// LDR OFF
1455 toDigi = 0;
1456 if (debug == 2) Serial.println("LDR OFF");
1457 }
1458 if (nReceived == "LDR:1") { //////////// LDR ON
1459 toDigi = 0;
1460 if (debug == 2) Serial.println("LDR ON");
1461 }
1462 if (nReceived == "ESP:RESTART") { //////////// FULL RESTART
1463 ESP.restart();
1464 }
1465 if (nReceived == "ESP:SPKON") { //////////// SPEAKER RESTART
1466 digitalWrite(speaker, HIGH);
1467 nextion.systemVal("warning", 0);
1468 nextion.vis("ampSetup.resetSPK", 0);
1469 }
1470 if (debug == 1) Serial.println("++++++++++Serial1:__|" + nReceived + ";");
1471 if (toDigi == 1) uart.print(nReceived + ";");
1472 toDigi = 1;
1473 }
1474 nReceived = "";
1475 if (NextionPage == 0) powerState = 1;
1476 } else if (v4amp.mode == 1) {
1477 if (power.state == ONs) {
1478 power.off();
1479 }
1480 if (digitalRead(powerSW) == 0 && lastPressed + 5000 < millis()) { //////////// POWER SW HANDLING digitalRead(powerSW) == 0 &&
1481 power.on();
1482 lastPressed = millis();
1483 }
1484 }
1485} //////////////////// End of Loop ////////////////////
1486void reduce4() {
1487 sReceived = sReceived.substring(4);
1488}
1489