Advertisement
GaabMM88

Untitled

Jan 3rd, 2025
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 40.50 KB | None | 0 0
  1. const char compile_date[] = __DATE__;
  2. //Included with the name printing
  3. #include <HardwareSerial.h>
  4. #include "AiEsp32RotaryEncoder.h"
  5. #include "Arduino.h"
  6. #include "Wire.h"
  7. #include <Adafruit_NeoPixel.h>
  8. #include <esp_task_wdt.h>
  9. #include <WiFi.h>
  10. #include <RTClib.h>
  11. #include <PinDefinitionsAndMore.h>  // Define macros for input and output pin etc.
  12. #include <IRremote.hpp>
  13. #include "TC74.h"
  14.  
  15. #define sense1 2000
  16. #define sense2 2050
  17. #define ROTARY_ENCODER_A_PIN 35
  18. #define ROTARY_ENCODER_B_PIN 32
  19. #define ROTARY_ENCODER_BUTTON_PIN 33
  20. #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 */
  21. #define ROTARY_ENCODER_STEPS 4
  22. #define powerSW 23
  23. #define inputSW
  24. #define dcSenseRight 19  //-> pin / dcErrorRight (current state) / dcErroRightLast (last state of dcErrorRight)
  25. #define dcSenseLeft 18   //-> pin / dcErrorLeft (current state) / dcErrorLeftLast (last state of dcErrorLeft)
  26. #define acSense 5        //-> pin / acError (current state) / acErrorLast (last state of acError)
  27. #define relay 16         // Standby relay
  28. #define speaker 17       // Speaker relay
  29. #define WS2812 2         // WS2812 pin
  30. #define NUMPIXELS 8      // number of LED
  31. #define LDR 12           // LDR pin
  32. #define RXN_PIN 26       // Serial1 RX to Nextion TX
  33. #define TXN_PIN 25       // Serial1 TX to Nextion RX
  34. #define RX_PIN 27        // Serial2 RX a Amp TX
  35. #define TX_PIN 14        // Serial2 TX a Amp RX
  36. #define SDA 21           // I2C Thermometer, Expander, etc.
  37. #define SCL 22
  38.  
  39. #define boot 0  // Nextion page number 0..11
  40. #define standby 1
  41. #define main 2
  42. #define digiSetup 3
  43. #define ampSetup 4
  44. #define digiInput 5
  45. #define digiPreset 6
  46. #define dsp1 7
  47. #define dsp2 8
  48. #define dsp3 9
  49. #define info 10
  50. #define dataRecord 11
  51.  
  52. RTC_DS3231 rtc;
  53. TC74 tmp1(0x4A);         //A2 Address
  54. TC74 tmp2(0x4B);         //A3 Address
  55. HardwareSerial uart(2);  // Uso de la interfaz de hardware Serial2
  56. Adafruit_NeoPixel pixels(NUMPIXELS, WS2812, NEO_RGB + NEO_KHZ800);
  57. AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
  58.  
  59. const int powerOffDelayForRelay = 400;  //in millis seconds
  60. //const byte LDR = 12;                //Light Dependant Resistor
  61. const long eventTime_1_LDR = 5000;  //check brigtness in ms
  62. unsigned long previousTime_1 = 0, beepMillis = 0, protectMillis = 0, irMillis = 0, irVolMillis, strengthMillis = 0;
  63. unsigned long ac_time = 0, ac_time_last = 0;
  64. boolean powerState = 0, lastPowerState = 0, nextionSetTimebit = 1;
  65. int offset = 2, beep = 0, NextionPage;
  66. int digiVolume = 0, dmute = 0, lastPressed = 0, source = 0, dim = 0, dimN = -1, task = 0;
  67. int initDigi = 0, toDigi = 1;
  68. int dcErrorRight = 1, dcErrorRightLast = -1, dcErrorLeft = 1, dcErrorLeftLast = -1, acError = 1, acErrorLast = -1;  //variables for protection
  69. int debug = 2;                                                                                                      //1 - writing all data; 2 - only protection data
  70. int ac_protect = 0;
  71. int self_test = 1;
  72. int currentPage = -1;  // current page of nextion
  73. int irLastCode = -1;
  74. int volStep = 2;
  75. int sourceS = 0;
  76. int devState = 1;
  77. String sReceived, nReceived;
  78. int ev, honap, nap, ora, perc, mperc, hetnapja;  //These are where we store the RTC values.
  79. char daysOfWeek[7][12] = {
  80.   "Sunday",
  81.   "Monday",
  82.   "Tuesday",
  83.   "Wednesday",
  84.   "Thursday",
  85.   "Friday",
  86.   "Saturday"
  87. };
  88.  
  89.  
  90. #if (1)
  91. const char* ssid = "TP-Link_F072";
  92. const char* password = "12778072";
  93. #else
  94. const char* ssid = "SirRouter";
  95. const char* password = "19801989";
  96. #endif
  97.  
  98. class c_NextionWrite {
  99. public:
  100.   void init(int speed, int RXN, int TXN) {
  101.     Serial1.begin(speed, SERIAL_8N1, RXN, TXN);
  102.     // if (debug) Serial.printf("Serial1 - Speed: %d, RX-pin: %d, TX-pin: %d \n", speed, RX, TX);
  103.   }
  104.   void txt(String Name, String text) {
  105.     Serial1.print(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
  106.     if (debug == 1) Serial.println(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
  107.   }
  108.   void val(String Name, int value) {
  109.     Serial1.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
  110.     if (debug == 1) Serial.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
  111.   }
  112.   void systemVal(String Name, int value) {
  113.     Serial1.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
  114.     if (debug == 1) Serial.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
  115.   }
  116.   void pageChange(int Name) {
  117.     Serial1.print("page " + String(Name) + "\xFF\xFF\xFF");
  118.     if (debug == 1) Serial.print("page " + String(Name) + "\xFF\xFF\xFF");
  119.     NextionPage = Name;
  120.   }
  121.   void setPco(String name, int pco) {  // for global variable need a page number / page name too
  122.     Serial1.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
  123.     if (debug == 1) Serial.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
  124.   }
  125.   void timerEnable(String name, int en) {
  126.     Serial1.print(name + ".en=" + String(en) + "\xFF\xFF\xFF");
  127.   }
  128.   void vis(String name, int en) {
  129.     Serial1.print("vis " + name + "," + String(en) + "\xFF\xFF\xFF");
  130.   }
  131.   void dim(int en) {
  132.     Serial1.print("dim=" + String(en) + "\xFF\xFF\xFF");
  133.   }
  134.   void touchSet(String name, int en) {  //nextion.touchSet("preset", 0/1);
  135.     Serial1.print("tsw " + String(name) + "," + String(en) + "\xFF\xFF\xFF");
  136.   }
  137.   void click(String name, int en) {
  138.     Serial.print("click " + String(name) + "," + en + "\xFF\xFF\xFF");
  139.     Serial1.print("click " + String(name) + "," + en + "\xFF\xFF\xFF");
  140.   }
  141.  
  142.   void setTime(int ev, int honap, int nap, int ora, int perc, int mperc, int hetnapja) {
  143.     //hour = (hour + 24) % 24;
  144.     Serial1.print("rtc0=" + String(ev) + "\xFF\xFF\xFF");     //year
  145.     Serial1.print("rtc1=" + String(honap) + "\xFF\xFF\xFF");  //month
  146.     Serial1.print("rtc2=" + String(nap) + "\xFF\xFF\xFF");    //day
  147.     Serial1.print("rtc3=" + String(ora) + "\xFF\xFF\xFF");    //hour
  148.     Serial1.print("rtc4=" + String(perc) + "\xFF\xFF\xFF");   //minute
  149.     Serial1.print("rtc5=" + String(mperc) + "\xFF\xFF\xFF");  //second
  150.     // Serial1.print("rtc6=" + String(hetnapja) + "\xFF\xFF\xFF"); //days of the Week
  151.     Serial.printf("Nextion time/ hour: %d min: %d sec: %d \n", ora, perc, mperc);
  152.     Serial.println("--------------------");
  153.   }
  154. };
  155. c_NextionWrite nextion;
  156.  
  157.  
  158. void initWiFi() {  ////////////////////////////////// INIT WIFI
  159.   WiFi.mode(WIFI_STA);
  160.   WiFi.begin(ssid, password);
  161.   Serial.print("Connecting to WiFi ..");
  162.   while (WiFi.status() != WL_CONNECTED) {
  163.     Serial.print('.');
  164.     delay(1000);
  165.   }
  166.   Serial.println(WiFi.localIP());
  167. }
  168. void readRTC() {  ////////////////////////////////// DS3231 realtime clock module
  169.   DateTime now = rtc.now();
  170.   //Serial.print("ESP32 RTC Date Time: ");
  171.   //Serial.print(now.year(), DEC);
  172.   ev = (now.year(), DEC);
  173.   //Serial.print('/');
  174.   //Serial.print(now.month(), DEC);
  175.   honap = (now.month(), DEC);
  176.   //Serial.print('/');
  177.   //Serial.print(now.day(), DEC);
  178.   nap = (now.day(), DEC);
  179.   //Serial.print(" (");
  180.   //Serial.print(daysOfWeek[now.dayOfTheWeek()]);
  181.   //Serial.print(") ");
  182.   //Serial.print(now.hour(), DEC);
  183.   ora = (now.hour(), DEC);
  184.   //Serial.print(':');
  185.   //Serial.print(now.minute(), DEC);
  186.   perc = (now.minute(), DEC);
  187.   //Serial.print(':');
  188.   //Serial.println(now.second(), DEC);
  189.   mperc = (now.second(), DEC);
  190.  
  191.   //delay(1000);  // delay 1 seconds
  192. }
  193. void rotary_loop() {  ////////////////////////////////// ENCODER LOOP
  194.   if (rotaryEncoder.encoderChanged()) {
  195.     if (debug == 1) Serial.print("Value: ");
  196.     if (debug == 1) Serial.println(rotaryEncoder.readEncoder());
  197.     uart.print("VOL:" + String(rotaryEncoder.readEncoder()) + ";");
  198.     digiVolume = rotaryEncoder.readEncoder();
  199.   }
  200.   if (rotaryEncoder.isEncoderButtonClicked()) {
  201.     rotary_onButtonClick();
  202.   }
  203. }
  204. void rotary_onButtonClick() {  ////////////////////////////////// ENCODER SW
  205.   static unsigned long lastTimePressed = 0;
  206.   //ignore multiple press in that time milliseconds
  207.   if (millis() - lastTimePressed < 500) {
  208.     return;
  209.   }
  210.   lastTimePressed = millis();
  211.   if (debug == 1) Serial.print("button pressed ");
  212.   if (debug == 1) Serial.print(millis());
  213.   if (debug == 1) Serial.println(" milliseconds after restart");
  214.   if (dmute == 0) {
  215.     uart.print("MUT:1;");
  216.   } else {
  217.     uart.print("MUT:0;");
  218.   }
  219. }
  220. void IRAM_ATTR readEncoderISR() {  ////////////////////////////////// ENCODER INTERRUPT
  221.   rotaryEncoder.readEncoder_ISR();
  222. }
  223. void IRAM_ATTR stateRight_ISR() {  ////////////////////////////////// RIGHT INTERRUPT
  224.   dcErrorRight = 1;
  225. }
  226. void IRAM_ATTR stateLeft_ISR() {  ////////////////////////////////// LEFT INTERRUPT
  227.   dcErrorLeft = 1;
  228. }
  229. void IRAM_ATTR stateAC_ISR() {  ////////////////////////////////// AC INTERRUPT
  230.   ac_time = millis();
  231.   if (ac_time - ac_time_last > 250) {
  232.     acError = 1;
  233.     ac_protect = 1;
  234.     digitalWrite(relay, 0);
  235.     ac_time_last = ac_time;
  236.   }
  237. }
  238. void meta(int t, int e, int v, int i) {  ////////////////////////////////// DIGI METADATA
  239.   nextion.vis("main.title", t);
  240.   nextion.vis("main.elapsed", e);
  241.   nextion.vis("main.vendor", v);
  242.   nextion.vis("main.infoText", i);
  243. }
  244. void beep_2k() {  ////////////////////////////////// 2KhZ BEEP
  245.   unsigned long currentMillis = millis();
  246.   if (currentMillis - beepMillis >= 1000) {
  247.     if (beep == 0) {
  248.       tone(12, 2000, 50);
  249.     } else {
  250.       tone(12, 2000, 50);
  251.     }
  252.     beepMillis = currentMillis;
  253.   }
  254. }
  255. void beep_3k() {  ////////////////////////////////// 3KhZ BEEP
  256.   unsigned long currentMillis = millis();
  257.   if (currentMillis - beepMillis >= 2000) {
  258.     if (beep == 0) {
  259.       tone(12, 3000, 1000);
  260.     } else {
  261.       tone(12, 3000, 1000);
  262.     }
  263.     beepMillis = currentMillis;
  264.   }
  265. }
  266. void inputLed(int input) {  ////////////////////////////////// LED FOR SOURCE
  267.   source = input;
  268.   int r = 0, g = 0, b = 0;
  269.   switch (input) {
  270.     case 0:  // WiFi - net
  271.       // if (powerState == 0) break;
  272.       r = 59;
  273.       g = 60;
  274.       b = 61;
  275.       break;
  276.     case 1:  // Bluetooth - bt
  277.       // if (powerState == 0) break;
  278.       r = 0;
  279.       g = 0;
  280.       b = 50;
  281.       break;
  282.     case 2:  // Line-In - line_in
  283.       // if (powerState == 0) break;
  284.       r = 0;
  285.       g = 50;
  286.       b = 0;
  287.       break;
  288.     case 3:  // SPDIF - opt
  289.       // if (powerState == 0) break;
  290.       r = 50;
  291.       g = 0;
  292.       b = 0;
  293.       break;
  294.     case 4:  // USB-DAC - usb dac
  295.       r = 12;
  296.       g = 67;
  297.       b = 5;
  298.  
  299.       break;
  300.     case 5:  //Standby
  301.       r = 55;
  302.       g = 35;
  303.       b = 0;
  304.       break;
  305.   }
  306.  
  307.   for (int i = 0; i < NUMPIXELS; i++) {
  308.     pixels.setPixelColor(i, pixels.Color(g, r, b));
  309.   }
  310.   pixels.show();  // Send the updated pixel colors to the hardware.
  311. }
  312. void functionLed(int function) {  ////////////////////////////////// LED FOR FUNCTIONS
  313.   task = function;
  314.   int r = 0, g = 0, b = 0;
  315.   switch (function) {
  316.     case 0:  // task is "ON"
  317.       if (powerState == 0) break;
  318.       r = 49;
  319.       g = 50;
  320.       b = 51;
  321.       break;
  322.     case 1:  // task is "OFF"
  323.       if (powerState == 0) break;
  324.       r = 0;
  325.       g = 0;
  326.       b = 50;
  327.       break;
  328.     case 2:  // task is "DC ERROR"
  329.       if (powerState == 0) break;
  330.       r = 0;
  331.       g = 50;
  332.       b = 0;
  333.       break;
  334.     case 3:  // task is "Temp ERROR"
  335.       if (powerState == 0) break;
  336.       r = 50;
  337.       g = 0;
  338.       b = 0;
  339.       break;
  340.     case 4:  // task is "System starting..."
  341.       r = 55;
  342.       g = 35;
  343.       b = 0;
  344.       break;
  345.   }
  346.   for (int i = 0; i < NUMPIXELS; i++) {
  347.     pixels.setPixelColor(i, pixels.Color(g, r, b));
  348.   }
  349.   pixels.show();  // Send the updated pixel colors to the hardware.
  350. }
  351. void senseBrightness() {  ////////////////////////////////// SENSE BRIGHTNESS
  352.   unsigned long currentTime = millis();
  353.   if (currentTime - previousTime_1 >= eventTime_1_LDR) {
  354.     String m;
  355.     switch (analogRead(LDR)) {
  356.       case 0 ... 40:
  357.         dimN = 25;
  358.         m = "Dark";
  359.         break;
  360.       case 41 ... 800:
  361.         dimN = 40;
  362.         m = "Light";
  363.         break;
  364.       case 801 ... 2000:
  365.         dimN = 60;
  366.         m = "Bright";
  367.         break;
  368.       case 2001 ... 3200:
  369.         dimN = 80;
  370.         m = "Very Bright";
  371.         break;
  372.       case 3201 ... 4500:
  373.         dimN = 100;
  374.         m = "Very Very Bright";
  375.         break;
  376.     }
  377.     if (dim != dimN) {
  378.       nextion.val("ampSetup.nDimESP", dimN);
  379.       nextion.val("ampSetup.hSlideESP", dimN);
  380.       Serial.println(" => " + m);
  381.       Serial.println(analogRead(LDR));
  382.       dim = dimN;
  383.     }
  384.     previousTime_1 = currentTime;
  385.   }
  386. }
  387. void senseTemp() {  ////////////////////////////////// SENSE TEMPERATURE
  388.   static unsigned long timer1, timer2;
  389.   if (millis() - timer1 > sense1) {  // firtst --> 1st
  390.     timer1 = millis();
  391.     //Serial.print("Temp 1: ");
  392.     //Serial.println(tmp1.readTemperature('c'));
  393.     nextion.val("ampSetup.nTempLeft", tmp1.readTemperature('c'));
  394.   }
  395.   if (millis() - timer2 > sense2) {
  396.     timer2 = millis();
  397.     //Serial.print("Temp 2: ");
  398.     //Serial.println(tmp2.readTemperature('C'));
  399.     nextion.val("ampSetup.nTempRight", tmp2.readTemperature('c'));
  400.   }
  401. }
  402. void readProtection() {  ////////////////////////////////// READ COMPLETE PROTECTION (AC+DC)
  403.   dcErrorRight = digitalRead(dcSenseRight);
  404.   dcErrorLeft = digitalRead(dcSenseLeft);
  405.   acError = digitalRead(acSense);
  406.  
  407.   if (dcErrorRight != dcErrorRightLast) {
  408.     if (dcErrorRight == 0) {
  409.       if (debug == 2) Serial.println("Right channel is OK!");
  410.       nextion.systemVal("warning", 0);
  411.       nextion.val("ampSetup.vaRight", 0);
  412.       if (dcErrorLeft == 0 && self_test == 1) nextion.val("boot.vaProtection", 1);
  413.     }
  414.     if (dcErrorRight == 1) {
  415.       if (debug == 2) Serial.println("DC voltage on Right channel!");
  416.       nextion.systemVal("warning", 1);
  417.       nextion.val("ampSetup.vaRight", 1);
  418.       if (self_test == 1) nextion.val("boot.vaProtection", 0);
  419.     }
  420.     dcErrorRightLast = dcErrorRight;
  421.   }
  422.  
  423.   if (dcErrorLeft != dcErrorLeftLast) {
  424.     if (dcErrorLeft == 0) {
  425.       if (debug == 2) Serial.println("Left channel is OK!");
  426.       nextion.systemVal("warning", 0);
  427.       nextion.val("ampSetup.vaLeft", 0);
  428.       if (dcErrorRight == 0 && self_test == 1) nextion.val("boot.vaProtection", 1);
  429.     }
  430.     if (dcErrorLeft == 1) {
  431.       if (debug == 2) Serial.println("DC voltage on Left channel!");
  432.       nextion.systemVal("warning", 1);
  433.       nextion.val("ampSetup.vaLeft", 1);
  434.       if (self_test == 1) nextion.val("boot.vaProtection", 0);
  435.     }
  436.     dcErrorLeftLast = dcErrorLeft;
  437.   }
  438.  
  439.   if (acError != acErrorLast) {
  440.     if (ac_protect == 1) {
  441.       Serial.println("Interrupt on AC pin");
  442.     }
  443.     if (acError == 0) {
  444.       if (debug == 2) Serial.println("AC is OK!");
  445.       nextion.systemVal("warning", 0);
  446.       nextion.val("ampSetup.vaAC", 0);
  447.       nextion.val("boot.vaSupply", 1);
  448.     }
  449.     if (acError == 1) {
  450.       if (debug == 2) Serial.println("Missing AC voltage!");
  451.       nextion.systemVal("warning", 1);
  452.       nextion.val("ampSetup.vaAC", 1);
  453.       nextion.val("boot.vaSupply", 1);  // -> 0
  454.       ac_protect = 0;
  455.     }
  456.     acErrorLast = acError;
  457.   }
  458. }
  459. void readAC() {  ////////////////////////////////// READ POWER SUPPLY
  460.   acError = digitalRead(acSense);
  461.  
  462.   if (acError != acErrorLast) {
  463.     if (ac_protect == 1) {
  464.       Serial.println("Interrupt on AC pin");
  465.     }
  466.     if (acError == 0) {
  467.       if (debug == 2) Serial.println("AC is OK!");
  468.       nextion.systemVal("warning", 0);
  469.       nextion.val("ampSetup.vaAC", 0);
  470.       nextion.val("boot.vaSupply", 1);
  471.     }
  472.     if (acError == 1) {
  473.       if (debug == 2) Serial.println("Missing AC voltage!");
  474.       nextion.systemVal("warning", 1);
  475.       nextion.val("ampSetup.vaAC", 1);
  476.       nextion.val("boot.vaSupply", 1);  // -> 0
  477.       ac_protect = 0;
  478.     }
  479.     acErrorLast = acError;
  480.   }
  481. }
  482. void setup() {  ////////////////////////////////// START OF SETUP
  483.   Wire.begin(SDA, SCL);
  484.   Serial.begin(115200);
  485.   Serial1.begin(115200, SERIAL_8N1, RXN_PIN, TXN_PIN);
  486.   uart.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
  487.   while (!Serial1)
  488.     ;
  489.   while (!uart)
  490.     ;
  491.   rtc.begin();
  492.   tmp1.begin();
  493.   tmp2.begin();
  494.   pixels.begin();  // INITIALIZE NeoPixel strip object (REQUIRED)
  495.                    // 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.
  496.   pinMode(relay, OUTPUT);
  497.   delay(1);
  498.   //digitalWrite(speaker, 0);
  499.   digitalWrite(relay, 0);
  500.   pinMode(powerSW, INPUT_PULLUP);  // buttons is active LOW
  501.   pinMode(dcSenseRight, INPUT_PULLUP);
  502.   pinMode(dcSenseLeft, INPUT_PULLUP);
  503.   pinMode(acSense, INPUT_PULLUP);
  504.   rotaryEncoder.begin();
  505.   rotaryEncoder.setup(readEncoderISR);
  506.   //ir***********************************************************************************
  507.   Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
  508.   // Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
  509.   IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
  510.   Serial.print(F("Ready to receive IR signals of protocols: "));
  511.   printActiveIRProtocols(&Serial);
  512.   Serial.println(F("at pin " STR(IR_RECEIVE_PIN)));
  513.   //ir***********************************************************************************
  514.   inputLed(4);
  515.   nextion.pageChange(boot);
  516.   nextion.txt("boot.infoText", "System is starting...");
  517.   nextion.val("boot.vaMCU", 1);
  518.   delay(1000);
  519.   nextion.val("boot.vaRTC", 1);
  520.   delay(1000);
  521.   nextion.val("boot.vaArylic", 0);
  522.  
  523.  
  524.  
  525.   bool circleValues = false;
  526.   /*Rotary acceleration introduced 25.2.2021.
  527.    * in case range to select is huge, for example - select a value between 0 and 1000 and we want 785
  528.    * without accelerateion you need long time to get to that number
  529.    * Using acceleration, faster you turn, faster will the value raise.
  530.    * For fine tuning slow down.
  531.    */
  532.   //rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
  533.   rotaryEncoder.setAcceleration(0);  //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration
  534.   rotaryEncoder.setEncoderValue(digiVolume);
  535.   rotaryEncoder.setBoundaries(0, 100, circleValues);      //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
  536.   attachInterrupt(dcSenseRight, stateRight_ISR, RISING);  //Interrupts for protection: left: RISING,right: RISING,AC: FALLING
  537.   attachInterrupt(dcSenseLeft, stateLeft_ISR, RISING);
  538.   attachInterrupt(acSense, stateAC_ISR, FALLING);
  539.   //initWiFi();   //in the kitchen, no WIFI :D
  540.   //String LocalIP = String() + WiFi.localIP()[0] + "." + WiFi.localIP()[1] + "." + WiFi.localIP()[2] + "." + WiFi.localIP()[3];
  541.   //Serial.println(WiFi.localIP());
  542.   //Serial.println(LocalIP);
  543.   if (debug) Serial.println("Starting..");
  544.   if (debug == 1) Serial.println("Started...");
  545.   if (debug == 1) Serial.println(compile_date);
  546.   uart.print("PMT:1;");
  547.   uart.print("BEP:0;");
  548.   uart.print("BEP;");
  549.   uart.print("PMT;");
  550.   nextion.touchSet("main.preset", 0);
  551.   dcErrorRight = digitalRead(dcSenseRight);
  552.   dcErrorLeft = digitalRead(dcSenseLeft);
  553.   acError = digitalRead(acSense);
  554.   //if (debug == 2) Serial.printf("AC-%d DCL-%d DCR-%d\n", acError, dcErrorLeft, dcErrorRight);
  555.   uart.print("SYS:REBOOT;");  //Reboot Digi
  556.   Serial.println(compile_date);
  557.   Serial.println("Arylic_0823");
  558. }
  559. void loop() {  ////////////////////////////////// START OF LOOP
  560.   readRTC();
  561.   //nextion.setTime(ev, honap, nap, ora, perc, mperc, hetnapja);
  562.  
  563.  
  564.   if (strengthMillis + 1000 < millis() && source == 0) {
  565.     uart.print("WSS;");
  566.     strengthMillis = millis();
  567.   }
  568.   if (strengthMillis + 1000 < millis() && source == 1) {
  569.     uart.print("BSS;");
  570.     strengthMillis = millis();
  571.   }
  572.   if (self_test == 0) {
  573.     readProtection();
  574.     senseBrightness();
  575.     senseTemp();
  576.     /*
  577. // devState == 0
  578. // devState == 1 -> initial tests (i2c devices, temp, RTC, AC, DC L + R ->  DIGI), running self test
  579.     - runing selftest -> Standby or Error page
  580. // devstate == 2 -> Standby
  581.     - IR power on/off
  582.     - Power SW
  583. // devState == 3 -> Running...
  584.     - work DIGI all funkction, STA
  585.     - encoder, IR control, Nextion control, protect check, temp, etc.
  586. // devstate == 4 -> Error mode -> page 2  !!! SPEAKERS & DIGI OFF !!!
  587.     - check protection, temp, main power supply
  588.     - power off?
  589.  
  590. switch (devState){
  591.   case 0:
  592.   break;
  593.   case 1:
  594.     devState1
  595.     .
  596.     .
  597.     break;
  598.   case 2:
  599. }
  600. */
  601.     if (IrReceiver.decode()) {  //////////// IR REMOTE HANDLING
  602.       Serial.println(NextionPage);
  603.       IrReceiver.printIRResultShort(&Serial);
  604.       IrReceiver.printIRSendUsage(&Serial);
  605.       if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
  606.         //Serial.println(F("Received noise or an unknown (or not yet enabled) protocol"));  // We have an unknown protocol here, print more inf
  607.         IrReceiver.printIRResultRawFormatted(&Serial, true);
  608.       }
  609.       Serial.println();
  610.       IrReceiver.resume();                                                             // Enable receiving of the next value
  611.       if (IrReceiver.decodedIRData.command == 0x45 && (irMillis + 5000) < millis()) {  // Power button && irLastCode != IrReceiver.decodedIRData.command
  612.         if (NextionPage == 2) {                                                        //0
  613.           nextion.click("powerOff", 1);
  614.         }
  615.         if (NextionPage == 1) {  //2
  616.           nextion.click("powerOn", 1);
  617.         }
  618.         irMillis = millis();
  619.         Serial.println("Pressed: POWER");
  620.       } else if (IrReceiver.decodedIRData.command == 0x19) {  // BT button
  621.         uart.print("SRC:BT;");
  622.         Serial.println("Pressed: BT");
  623.       } else if (IrReceiver.decodedIRData.command == 0xD) {  // FM button
  624.         //uart.print("SRC:BT;");
  625.         Serial.println("Pressed: FM");
  626.       } else if (IrReceiver.decodedIRData.command == 0x09 && irVolMillis + 300 < millis()) {  // VolUp
  627.         if (digiVolume < 100) {
  628.           digiVolume = digiVolume + volStep;
  629.           if (digiVolume > 100) digiVolume = 100;
  630.         }
  631.         irVolMillis = millis();
  632.         Serial.println(digiVolume);
  633.         uart.print("VOL:" + String(digiVolume) + ";");
  634.         Serial.println("Pressed: VOL UP");
  635.       } else if (IrReceiver.decodedIRData.command == 0x15 && irVolMillis + 300 < millis()) {  // VolDown
  636.         if (digiVolume > 0) {
  637.           digiVolume = digiVolume - volStep;
  638.           if (digiVolume < 0) digiVolume = 0;
  639.         }
  640.         irVolMillis = millis();
  641.         Serial.println(digiVolume);
  642.         uart.print("VOL:" + String(digiVolume) + ";");
  643.         Serial.println("Pressed: VOL DOWN");
  644.       } else if (IrReceiver.decodedIRData.command == 0x46 && (irMillis + 5000) < millis()) {  // Input stepping
  645.         if (sourceS < 4) {
  646.           sourceS++;
  647.         } else {
  648.           sourceS = 0;
  649.         }
  650.         String inp[5] = { "NET", "BT", "LINE-IN", "USBDAC", "OPT" };
  651.         uart.print("SRC:" + inp[sourceS] + ";");
  652.         Serial.println("SRC:" + inp[sourceS] + ";");
  653.         Serial.println(sourceS);
  654.         irMillis = millis();
  655.         Serial.println("Pressed: MODE (input stepping)");
  656.       } else if (IrReceiver.decodedIRData.command == 0x7 && (irMillis + 5000) < millis()) {  // Muting
  657.         if (NextionPage == 0) {
  658.           uart.print("MUT:1;");
  659.         }
  660.         if (NextionPage == 0) {
  661.           uart.print("MUT:0;");
  662.         }
  663.         irMillis = millis();
  664.         Serial.println("Pressed: MUTE");
  665.       } else if (IrReceiver.decodedIRData.command == 0x47) {  // Scan
  666.         // uart.print("SRC:NET;");
  667.         Serial.println("Pressed: SCAN");
  668.       } else if (IrReceiver.decodedIRData.command == 0x43) {  // >> / Next track, only available in BT/NET/USB mode (*1)
  669.         uart.print("NXT;");
  670.         Serial.println("Pressed: NEXT");
  671.       } else if (IrReceiver.decodedIRData.command == 0x40) {  // << / Previous track, only available in BT/NET/USB mode (*1)
  672.         uart.print("PRV;");
  673.         Serial.println("Pressed: PREV");
  674.       } else if (IrReceiver.decodedIRData.command == 0x44) {  // Play/Pause track, only available in BT/NET/USB mode (*1)
  675.         uart.print("POP;");
  676.         Serial.println("Pressed: PLAY/PAUSE");
  677.       } else if (IrReceiver.decodedIRData.command == 0xC) {  // 1
  678.         uart.print("PST:1;");
  679.         // digi.volume=digi.maxVolume-10;
  680.         nextion.txt("main.preset", "Preset 1");
  681.         Serial.println("Pressed: BT1");
  682.       } else if (IrReceiver.decodedIRData.command == 0x18) {  // 2
  683.         uart.print("PST:2;");
  684.         nextion.txt("main.preset", "Preset 2");
  685.         Serial.println("Pressed: BT2");
  686.       } else if (IrReceiver.decodedIRData.command == 0x5E) {  // 3
  687.         uart.print("PST:3;");
  688.         nextion.txt("main.preset", "Preset 3");
  689.         Serial.println("Pressed: BT3");
  690.       } else if (IrReceiver.decodedIRData.command == 0x8) {  // 4
  691.         uart.print("PST:4;");
  692.         nextion.txt("main.preset", "Preset 4");
  693.         Serial.println("Pressed: BT4");
  694.       } else if (IrReceiver.decodedIRData.command == 0x1C) {  // 5
  695.         uart.print("PST:5;");
  696.         nextion.txt("main.preset", "Preset 5");
  697.         Serial.println("Pressed: BT5");
  698.       } else if (IrReceiver.decodedIRData.command == 0x5A) {  // 6
  699.         uart.print("PST:6;");
  700.         nextion.txt("main.preset", "Preset 6");
  701.         Serial.println("Pressed: BT6");
  702.       } else if (IrReceiver.decodedIRData.command == 0x42) {  // 7
  703.         uart.print("PST:7;");
  704.         nextion.txt("main.preset", "Preset 7");
  705.         Serial.println("Pressed: BT7");
  706.       } else if (IrReceiver.decodedIRData.command == 0x52) {  // 8
  707.         uart.print("PST:8;");
  708.         nextion.txt("main.preset", "Preset 8");
  709.         Serial.println("Pressed: BT8");
  710.       } else if (IrReceiver.decodedIRData.command == 0x4A) {  // 9
  711.         uart.print("PST:9;");
  712.         nextion.txt("main.preset", "Preset 9");
  713.         Serial.println("Pressed: BT9");
  714.       } else if (IrReceiver.decodedIRData.command == 0x16) {  // 0
  715.         uart.print("PST:10;");
  716.         nextion.txt("main.preset", "Preset 10");
  717.         Serial.println("Pressed: BT0");
  718.       }
  719.     }
  720.  
  721.     if (digitalRead(powerSW) == 0 && lastPowerState == 1 && lastPressed + 5000 < millis()) {  //////////// POWER SW HANDLING
  722.       if (debug == 1) Serial.println(digitalRead(powerSW));
  723.       if (powerState == 0) {
  724.         powerState = 1;
  725.         uart.print("SYS:REBOOT;");
  726.         Serial.println("---------- Digi REBOOT... ----------");
  727.         tone(12, 1000, 50);
  728.       } else {
  729.         powerState = 0;
  730.         uart.print("SYS:STANDBY;");
  731.         Serial.println("---------- Digi STANDBY... ----------");
  732.         tone(12, 1000, 50);
  733.         inputLed(4);
  734.       }
  735.       lastPressed = millis();
  736.     }
  737.     lastPowerState = digitalRead(powerSW);  //1
  738.     rotary_loop();
  739.     while (uart.available()) {  //////////// UART HANDLING FOR ARYLIC
  740.       nextion.val("boot.vaArylic", 1);
  741.       sReceived = uart.readStringUntil('\n');
  742.       sReceived.trim();
  743.       //Serial.println(sReceived);
  744.       if (debug == 1) Serial.println("uart:___|----------" + sReceived);
  745.       if (debug == 1) Serial.println("");
  746.       if (sReceived.startsWith("PLA:0")) {
  747.         nextion.txt("main.infoText", "Szünet/Megállítva");
  748.       } else if (sReceived.startsWith("PLA:1")) {
  749.         nextion.txt("main.infoText", "Lejátszás...");
  750.       } else if (sReceived.startsWith("STA:")) {  //////////// STA HANDLING
  751.         if (initDigi == 1) {
  752.           initDigi = 0;
  753.           nextion.val("boot.vaArylic", 1);
  754.         } else if (NextionPage != 6) {
  755.           nextion.pageChange(main);
  756.         }
  757.         nextion.vis("main.infoText", 0);
  758.         if (debug == 1) Serial.println("STA received");
  759.         if (debug == 1) Serial.println("Before: " + sReceived);
  760.         nextion.txt("main.infoText", "");
  761.         nextion.vis("main.title", 0);
  762.         nextion.vis("main.elapsed", 0);
  763.         nextion.vis("main.vendor", 0);
  764.         nextion.touchSet("main.preset", 0);
  765.         reduce4();
  766.  
  767.         if (sReceived.startsWith("NET")) {  //////////// WIFI
  768.           sourceS = 0;
  769.           inputLed(0);
  770.           meta(1, 1, 1, 1);
  771.           if (debug == 1) Serial.println("...WIFI...");
  772.           nextion.txt("main.input", "WiFi");
  773.         } else if (sReceived.startsWith("BT")) {  //////////// BLUETOOTH
  774.           sourceS = 1;
  775.           inputLed(1);
  776.           meta(0, 0, 0, 0);
  777.           if (debug == 1) Serial.println("...BLUETOOTH...");
  778.           nextion.txt("main.input", "Bluetooth");
  779.         } else if (sReceived.startsWith("LINE-IN")) {  //////////// LINE-IN
  780.           sourceS = 2;
  781.           inputLed(2);
  782.           meta(0, 0, 0, 0);
  783.           if (debug == 1) Serial.println("...LINE-IN...");
  784.           nextion.txt("main.input", "Line In");
  785.         } else if (sReceived.startsWith("USBDAC")) {  //////////// USB DAC
  786.           sourceS = 3;
  787.           inputLed(3);
  788.           meta(0, 0, 0, 0);
  789.           if (debug == 1) Serial.println("...USB DAC...");
  790.           nextion.txt("main.input", "USB DAC");
  791.         } else if (sReceived.startsWith("OPT")) {  //////////// USB DAC
  792.           sourceS = 4;
  793.           inputLed(4);
  794.           meta(0, 0, 0, 0);
  795.           if (debug == 1) Serial.println("...SPDIF...");
  796.           nextion.txt("main.input", "SPDIF");
  797.         }
  798.  
  799.       } else if (sReceived.startsWith("SYS:STANDBY")) {  //////////// POWER OFF HANDLING
  800.         inputLed(4);
  801.         Serial.println("Stand by mode...");
  802.         tone(12, 1000, 50);
  803.         nextion.pageChange(standby);
  804.         digitalWrite(relay, 0);
  805.         nextionSetTimebit = 1;
  806.       } else if (sReceived.startsWith("SRC:")) {  //////////// INPUT SOURCE HANDLING
  807.         nextion.vis("main.title", 0);
  808.         nextion.vis("main.elapsed", 0);
  809.         nextion.vis("main.vendor", 0);
  810.         nextion.touchSet("main.preset", 0);
  811.         nextion.pageChange(main);
  812.         reduce4();
  813.  
  814.         if (sReceived.startsWith("NET")) {  //////////// WIFI
  815.           sourceS = 0;
  816.           inputLed(0);
  817.           meta(1, 1, 1, 1);
  818.           if (debug == 1) Serial.println("...WIFI...");
  819.           nextion.txt("main.input", "WiFi");
  820.         } else if (sReceived.startsWith("BT")) {  //////////// BLUETOOTH
  821.           sourceS = 1;
  822.           inputLed(1);
  823.           meta(0, 0, 0, 0);
  824.           if (debug == 1) Serial.println("...BLUETOOTH...");
  825.           nextion.txt("main.input", "Bluetooth");
  826.         } else if (sReceived.startsWith("LINE-IN")) {  //////////// LINE-IN
  827.           sourceS = 2;
  828.           inputLed(2);
  829.           meta(0, 0, 0, 0);
  830.           if (debug == 1) Serial.println("...LINE-IN...");
  831.           nextion.txt("main.input", "Line In");
  832.         } else if (sReceived.startsWith("USBDAC")) {  //////////// USB DAC
  833.           sourceS = 3;
  834.           inputLed(3);
  835.           meta(0, 0, 0, 0);
  836.           if (debug == 1) Serial.println("...USB DAC...");
  837.           nextion.txt("main.input", "USB DAC");
  838.         } else if (sReceived.startsWith("OPT")) {  //////////// USB DAC
  839.           sourceS = 4;
  840.           inputLed(4);
  841.           meta(0, 0, 0, 0);
  842.           if (debug == 1) Serial.println("...SPDIF...");
  843.           nextion.txt("main.input", "SPDIF");
  844.         }
  845.       } else if (sReceived.startsWith("VOL:")) {  //////////// VOLUME HANDLING
  846.         reduce4();
  847.         int index = sReceived.indexOf(';');
  848.         sReceived = sReceived.substring(0, index);
  849.         if (sReceived == "100") {
  850.           nextion.txt("volText", "MAX");
  851.         } else if (sReceived == "0") {
  852.           nextion.txt("volText", "MIN");
  853.         } else {
  854.           if (debug == 1) Serial.println("volume:  -|:" + sReceived);
  855.           digiVolume = sReceived.toInt();
  856.           nextion.txt("volText", sReceived);
  857.         }
  858.         nextion.systemVal("digiVol", digiVolume);
  859.         rotaryEncoder.setEncoderValue(digiVolume);
  860.       } else if (sReceived.startsWith("MUT:")) {  //////////// MUTE HANDLING
  861.         reduce4();
  862.         sReceived = sReceived.substring(0, 1);
  863.         if (debug == 1) Serial.println("Mute:_____/:|" + sReceived);
  864.         if (sReceived == "1") {
  865.           dmute = 1;
  866.           nextion.txt("volText", "MIN");
  867.           nextion.systemVal("digiVol", 0);
  868.         } else if (sReceived == "0") {
  869.           dmute = 0;
  870.           nextion.txt("volText", String(digiVolume));
  871.           nextion.systemVal("digiVol", digiVolume);
  872.         }
  873.  
  874.       } else if (sReceived.startsWith("BTC:")) {  //////////// BLUETOOTH CONNECTION HANDLING
  875.         reduce4();
  876.         sReceived = sReceived.substring(0, 1);
  877.         if (sReceived == "1") {
  878.           nextion.txt("main.infoText", "CONNECTED");
  879.           uart.print("TIT;");
  880.         } else if (sReceived == "0") {
  881.           nextion.txt("main.infoText", "DISCONNECTED");
  882.         }
  883.       } else if (sReceived.endsWith("SYS:ON;")) {  //////////// POWER ON HANDLING
  884.         tone(12, 2000, 50);
  885.         nextion.txt("standby.powerOn", "STARTING");
  886.         nextion.setPco("standby.powerOn", 34784);
  887.         Serial.println("arrived SYS:ON...(1)");
  888.       } else if (sReceived.startsWith("NET:")) {  //////////// NETWORK CONNECTION HANDLING
  889.         reduce4();
  890.         sReceived = sReceived.substring(0, 1);
  891.         if (sReceived == "1") {
  892.           nextion.txt("main.infoText", "CONNECTED");
  893.           inputLed(0);
  894.           uart.print("TIT;");
  895.           nextion.touchSet("main.preset", 1);
  896.           //inputLed(0);
  897.         } else if (sReceived == "0") {
  898.           nextion.txt("main.infoText", "DISCONNECTED");
  899.           nextion.touchSet("main.preset", 0);
  900.           inputLed(5);
  901.         }
  902.       } else if (sReceived.startsWith("TIT:")) {  //////////// Title
  903.         reduce4();
  904.         if (debug == 1) Serial.println("Title: " + sReceived);
  905.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  906.         nextion.txt("main.title", sReceived);
  907.  
  908.       } else if (sReceived.startsWith("ELP:")) {  //////////// Elapsed playing time
  909.         reduce4();
  910.         int index = sReceived.indexOf("/");
  911.         sReceived = sReceived.substring(0, index);
  912.         // Serial1.println(sReceived);
  913.         long time = sReceived.toInt();
  914.         time = time / 100;
  915.         int tenth = time % 10;
  916.         time = time / 10;
  917.         long hour = time / 3600;
  918.         time = time - (hour * 3600);
  919.         long min = time / 60;
  920.         long sec = time - (min * 60);
  921.         String timeS = "Time: ";
  922.         if (hour < 10) timeS += "0";
  923.         timeS += String(hour) + ":";
  924.         if (min < 10) timeS += "0";
  925.         timeS += String(min) + ":";
  926.         if (sec < 10) timeS += "0";
  927.         timeS += String(sec);  // + "." + String(tenth);
  928.         if (time > 0) nextion.txt("main.elapsed", timeS);
  929.  
  930.       } else if (sReceived.startsWith("BAS:")) {  //////////// BASS
  931.         reduce4();
  932.         int bass = sReceived.toInt();
  933.         nextion.val("digiSetup.nbass", bass);
  934.         if (bass < 0) {
  935.           bass = 11 - abs(bass);
  936.         } else {
  937.           bass = bass + 11;
  938.         }
  939.         nextion.val("digiSetup.hbass", bass);
  940.  
  941.       } else if (sReceived.startsWith("TRE:")) {  //////////// TREBLE
  942.         reduce4();
  943.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  944.         int treb = sReceived.toInt();
  945.         nextion.val("digiSetup.ntreb", treb);
  946.         if (treb < 0) {
  947.           treb = 11 - abs(treb);
  948.         } else {
  949.           treb = treb + 11;
  950.         }
  951.         nextion.val("digiSetup.htreb", treb);
  952.  
  953.       } else if (sReceived.startsWith("WSS:")) {  //////////// WIFI SIGNAL STRENGTH
  954.         reduce4();
  955.         //nextion.txt("main.infoText", sReceived);
  956.         Serial.println("WiFi signal strength: " + sReceived);
  957.  
  958.       } else if (sReceived.startsWith("BSS:")) {  //////////// BLUETOOTH SIGNAL STRENGTH
  959.         reduce4();
  960.         //nextion.txt("main.infoText", sReceived);
  961.         Serial.println("Bluetooth signal strength: " + sReceived);
  962.  
  963.  
  964.       } else if (sReceived.startsWith("MXV:")) {  //////////// Max volume, %
  965.         reduce4();
  966.         int volMax = sReceived.toInt();
  967.         nextion.val("digiSetup.nVolMax", volMax);
  968.         nextion.val("digiSetup.hVolMax", volMax);
  969.         if (debug == 2) Serial.println(volMax);
  970.  
  971.       } else if (sReceived.startsWith("VBS:")) {  //////////// Virtual Bass ON/OFF
  972.         reduce4();
  973.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  974.         if (sReceived == "1") {
  975.           nextion.val("digiSetup.vbs", 1);
  976.           if (debug == 2) Serial.println("VBS: on");
  977.         } else {
  978.           nextion.val("digiSetup.vbs", 0);
  979.           if (debug == 2) Serial.println("VBS: off");
  980.         }
  981.       } else if (sReceived.startsWith("PMT:")) {  //////////// Promt Voice ON/OFF
  982.         reduce4();
  983.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  984.         if (sReceived == "1") {
  985.           nextion.val("digiSetup.pmt", 1);
  986.           if (debug == 2) Serial.println("PMT: on");
  987.         } else {
  988.           nextion.val("digiSetup.pmt", 0);
  989.           if (debug == 2) Serial.println("PMT: off");
  990.         }
  991.  
  992.       } else if (sReceived.startsWith("NAM:")) {  //////////// Device name
  993.         reduce4();
  994.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  995.         String dname, Nname;
  996.         int h = 16, sz = 0, dsz = 0;
  997.         if (sReceived.length() > 0) {
  998.           for (int i = 0; i <= sReceived.length() - 1; i = i + 1) {
  999.             dname = sReceived.substring(i, i + 1);
  1000.             if (dname.toInt() >= 0 && dname.toInt() <= 9) {
  1001.               sz = dname.toInt();
  1002.               // if (debug) Serial.println(dname);
  1003.             }
  1004.             if (dname == "A") sz = 10;
  1005.             if (dname == "B") sz = 11;
  1006.             if (dname == "C") sz = 12;
  1007.             if (dname == "D") sz = 13;
  1008.             if (dname == "E") sz = 14;
  1009.             if (dname == "F") sz = 15;
  1010.             // sz += sz * h;
  1011.             if (h == 0) {
  1012.               dsz += sz;
  1013.               // if (debug) Serial.printf("%i. sz=%i\n", i, dsz);
  1014.               Nname += char(dsz);
  1015.               dsz = 0;
  1016.             } else {
  1017.               dsz = sz * 16;
  1018.             }
  1019.             h = 16 - h;
  1020.           }
  1021.           if (debug == 1) Serial.println(Nname);
  1022.           nextion.txt("main.NAME", Nname);
  1023.         }
  1024.       } else if (sReceived.startsWith("IPA:")) {  //////////// Device IP address for connecting to browser's control panel, psw:admin
  1025.         reduce4();
  1026.         sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  1027.         nextion.txt("digiSetup.digiIP", sReceived);
  1028.       } else if (sReceived.startsWith("VND:")) {  //////////// Vendor - Tidal, Spotify, etc...
  1029.         reduce4();
  1030.         sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  1031.         nextion.txt("main.vaVendor", sReceived);
  1032.       } else if (sReceived.startsWith("PST:")) {  //////////// Preset, not response, only command (1-10)
  1033.         reduce4();
  1034.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  1035.         if (debug == 1) Serial.println(";;;;;;;;;; " + sReceived);
  1036.       }
  1037.       sReceived = "";
  1038.     }
  1039.     while (Serial1.available()) {  //////////// UART HANDLING FOR NEXTION
  1040.       String nReceived = Serial1.readStringUntil(';');
  1041.       if (nReceived.startsWith("VST:")) {       //////////// VST HANDLING
  1042.         String nTemp = nReceived.substring(4);  //////////// VST HANDLING
  1043.         volStep = nTemp.toInt();
  1044.         Serial.println("VST: " + nReceived);
  1045.       }
  1046.       if (nReceived.startsWith("NPG:")) {  //////////// NEXTION PAGE NUMBER
  1047.         reduce4n();
  1048.         Serial.println(nReceived + "  ");
  1049.         toDigi = 0;
  1050.       }
  1051.       if (nReceived == "SYS:STANDBY") {  //////////// STANDBY COMMAND
  1052.         digitalWrite(relay, 0);
  1053.         delay(powerOffDelayForRelay);
  1054.         inputLed(4);
  1055.         powerState = 0;
  1056.       }
  1057.       if (nReceived == "LDR:0") {  //////////// LDR OFF
  1058.         toDigi = 0;
  1059.         if (debug == 2) Serial.println("LDR OFF");
  1060.       }
  1061.       if (nReceived == "LDR:1") {  //////////// LDR ON
  1062.         toDigi = 0;
  1063.         if (debug == 2) Serial.println("LDR ON");
  1064.       }
  1065.       if (nReceived == "ESP:RESTART") {  //////////// FULL RESTART
  1066.         ESP.restart();
  1067.       }
  1068.       if (debug == 1) Serial.println("++++++++++Serial1:__|" + nReceived + ";");
  1069.       if (toDigi == 1) uart.print(nReceived + ";");
  1070.       toDigi = 1;
  1071.     }
  1072.     if (NextionPage == 0)
  1073.       powerState = 1;
  1074.   } else {
  1075.     readProtection();
  1076.     if (acError == 1 && dcErrorRight == 0 && dcErrorLeft == 0) {  //AC ERROR =0
  1077.       self_test = 0;
  1078.       initDigi = 1;
  1079.       uart.print("SYS:REBOOT;");
  1080.       digitalWrite(relay, 1);
  1081.     }
  1082.     Serial.printf("Selftest %d\n", self_test);
  1083.   }
  1084. }  //////////////////// End of Loop  ////////////////////
  1085. void reduce4() {
  1086.   sReceived = sReceived.substring(4);
  1087. }
  1088. void reduce4n() {
  1089.   nReceived = nReceived.substring(4);
  1090. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement