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