Advertisement
GaabMM88

Untitled

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