Advertisement
GaabMM88

2

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