Advertisement
GaabMM88

Untitled

Aug 8th, 2024
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.82 KB | None | 0 0
  1. //
  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.  
  10. HardwareSerial uart(2);  // Uso de la interfaz de hardware Serial2
  11. #define ROTARY_ENCODER_A_PIN 35
  12. #define ROTARY_ENCODER_B_PIN 32
  13. #define ROTARY_ENCODER_BUTTON_PIN 33
  14. #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 */
  15. #define ROTARY_ENCODER_STEPS 4
  16.  
  17. //#define muteSW
  18. //#define inputSW
  19. #define powerSW 23
  20. #define dcSenseRight 19
  21. #define dcSenseLeft 18
  22. #define acSense 5
  23.  
  24. #define relay 13
  25. #define WS2812 2
  26. #define NUMPIXELS 1
  27. Adafruit_NeoPixel pixels(NUMPIXELS, WS2812, NEO_RGB + NEO_KHZ800);
  28.  
  29. int digiVolume = 0, dmute = 0, lastPressed = 0;
  30. boolean powerState = 0, lastPowerState = 0;
  31. String sReceived;
  32. //instead of changing here, rather change numbers above
  33. AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
  34.  
  35. void rotary_onButtonClick() {
  36.   static unsigned long lastTimePressed = 0;
  37.   //ignore multiple press in that time milliseconds
  38.   if (millis() - lastTimePressed < 500) {
  39.     return;
  40.   }
  41.   lastTimePressed = millis();
  42.   Serial.print("button pressed ");
  43.   Serial.print(millis());
  44.   Serial.println(" milliseconds after restart");
  45.   if (dmute == 0) {
  46.     uart.print("MUT:1;");
  47.   } else {
  48.     uart.print("MUT:0;");
  49.   }
  50. }
  51.  
  52. void rotary_loop() {
  53.   //dont print anything unless value changed
  54.   if (rotaryEncoder.encoderChanged()) {
  55.     Serial.print("Value: ");
  56.     Serial.println(rotaryEncoder.readEncoder());
  57.     uart.print("VOL:" + String(rotaryEncoder.readEncoder()) + ";");
  58.     digiVolume = rotaryEncoder.readEncoder();
  59.   }
  60.   if (rotaryEncoder.isEncoderButtonClicked()) {
  61.     rotary_onButtonClick();
  62.   }
  63. }
  64.  
  65. void IRAM_ATTR readEncoderISR() {
  66.   rotaryEncoder.readEncoder_ISR();
  67. }
  68.  
  69. void inputLed(int input) {
  70.   int r = 0, g = 0, b = 0;
  71.   switch (input) {
  72.     case 0:  // WiFi
  73.       r = 50;
  74.       g = 50;
  75.       b = 50;
  76.       break;
  77.     case 1:  // Bluetooth
  78.       r = 0;
  79.       g = 0;
  80.       b = 50;
  81.       break;
  82.     case 2:  // Line-In
  83.       r = 0;
  84.       g = 50;
  85.       b = 0;
  86.       break;
  87.     case 3:  // USB-DAC
  88.       r = 50;
  89.       g = 0;
  90.       b = 0;
  91.       break;
  92.     case 4:  // OFF
  93.       r = 0;
  94.       g = 0;
  95.       b = 0;
  96.       break;
  97.   }
  98.   pixels.setPixelColor(0, pixels.Color(r, g, b));
  99.   pixels.show();  // Send the updated pixel colors to the hardware.
  100. }
  101.  
  102.  
  103. class c_NextionWrite {
  104. public:
  105.   void init(int speed, int RXN, int TXN) {
  106.     Serial1.begin(speed, SERIAL_8N1, RXN, TXN);
  107.     // Serial.printf("Serial1 - Speed: %d, RX-pin: %d, TX-pin: %d \n", speed, RX, TX);
  108.   }
  109.   void txt(String Name, String text) {
  110.     Serial1.print(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
  111.     Serial.println(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
  112.   }
  113.   void val(String Name, int value) {
  114.     Serial1.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
  115.     Serial.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
  116.   }
  117.   void pageChange(int nr) {
  118.     Serial1.print("page " + String(nr) + "\xFF\xFF\xFF");
  119.     Serial.print("page " + String(nr) + "\xFF\xFF\xFF");
  120.   }
  121.   void setPco(String name, int pco) {
  122.     Serial1.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
  123.     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. };
  132. c_NextionWrite nextion;
  133.  
  134. #define RXN_PIN 26  // Serial1 RX to Nextion TX
  135. #define TXN_PIN 25  // Serial1 TX to Nextion RX
  136.  
  137. #define RX_PIN 14  // Serial2 RX a Amp TX
  138. #define TX_PIN 27  // Serial2 TX a Amp RX
  139.  
  140. #define SDA 21  // I2C Thermometer, Expander, etc.
  141. #define SCL 22
  142. //#define INT ?
  143.  
  144. void setup() {
  145.   pixels.begin();  // INITIALIZE NeoPixel strip object (REQUIRED)
  146.   pinMode(relay, OUTPUT);
  147.   digitalWrite(relay, 0);
  148.   //pinMode(muteSW, INPUT);
  149.   //pinMode(inputSW, INPUT);
  150.   pinMode(powerSW, INPUT);
  151.   pinMode(dcSenseRight, INPUT);
  152.   pinMode(dcSenseLeft, INPUT);
  153.   pinMode(acSense, INPUT);
  154.   rotaryEncoder.begin();
  155.   rotaryEncoder.setup(readEncoderISR);
  156.   //set boundaries and if values should cycle or not
  157.   //in this example we will set possible values between 0 and 1000;
  158.   bool circleValues = false;
  159.   rotaryEncoder.setBoundaries(0, 100, circleValues);  //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
  160.  
  161.   /*Rotary acceleration introduced 25.2.2021.
  162.    * in case range to select is huge, for example - select a value between 0 and 1000 and we want 785
  163.    * without accelerateion you need long time to get to that number
  164.    * Using acceleration, faster you turn, faster will the value raise.
  165.    * For fine tuning slow down.
  166.    */
  167.   //rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
  168.   rotaryEncoder.setAcceleration(0);  //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration
  169.   rotaryEncoder.setEncoderValue(digiVolume);
  170.  
  171.   // put your setup code here, to run once:
  172.   Wire.begin(SDA, SCL);
  173.   Serial.begin(115200);
  174.   Serial.println("Starting..");
  175.   Serial1.begin(115200, SERIAL_8N1, RXN_PIN, TXN_PIN);
  176.   // Inicializar la comunicación UART
  177.   uart.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
  178.   // uart.setTimeout(2000);
  179.   delay(2000);
  180.   Serial.println("Started...");
  181.   Serial.println(compile_date);
  182.   uart.print("PMT:1;");
  183.   uart.print("BEP:0;");
  184.   uart.print("BEP;");
  185.   uart.print("PMT;");
  186.   nextion.txt("bt_text", "ESP32 now starting...");
  187.   delay(500);
  188.   nextion.vis("bt_text", 0);
  189.   delay(500);
  190.   nextion.vis("bt_text", 1);
  191.   //uart.print("SYS:REBOOT;");
  192.   //delay(3500);
  193.   uart.print("SYS:STANDBY;");
  194.   uart.print("SYS:STANDBY;");
  195.   uart.print("SYS:STANDBY;");
  196.   uart.print("SYS:STANDBY;");
  197.   Serial.println("Initial OFF");
  198.   nextion.pageChange(3);
  199.   inputLed(4);
  200. }
  201.  
  202. void loop() {
  203.  
  204.   if (digitalRead(powerSW) == 0 && lastPowerState == 1 && lastPressed + 5000 < millis()) {
  205.     Serial.println(digitalRead(powerSW));
  206.  
  207.     if (powerState == 0) {
  208.       powerState = 1;
  209.       uart.print("SYS:REBOOT;");
  210.       Serial.println("Digi REBOOT...");
  211.     } else {
  212.       powerState = 0;
  213.       uart.print("SYS:STANDBY;");
  214.       Serial.println("Digi STANDBY...");
  215.       inputLed(4);
  216.     }
  217.     lastPressed = millis();
  218.   }
  219.  
  220.  
  221.   lastPowerState = digitalRead(powerSW);  //1
  222.   rotary_loop();
  223.   // put your main code here, to run repeatedly:
  224.   while (uart.available()) {
  225.     sReceived = uart.readStringUntil('\n');
  226.     sReceived.trim();
  227.     Serial.println("uart:___|" + sReceived);
  228.     if (sReceived.startsWith("PLA:0")) {
  229.       nextion.txt("bt_text", "Pause/Stop");
  230.     } else if (sReceived.startsWith("PLA:1")) {
  231.       nextion.txt("bt_text", "Playing...");
  232.     } else if (sReceived.startsWith("STA:")) {
  233.       digitalWrite(relay, 1);
  234.       nextion.pageChange(0);
  235.       nextion.vis("bt_text", 0);
  236.       Serial.println("STA received");
  237.       Serial.println("Before: " + sReceived);
  238.       nextion.val("wifi", 0);
  239.       nextion.val("bluetooth", 0);
  240.       nextion.val("line_in", 0);
  241.       nextion.val("usbdac", 0);
  242.       nextion.txt("bt_text", "");
  243.       nextion.vis("bt3", 0);
  244.       // reduce4();
  245.       reduce4();
  246.       if (sReceived.startsWith("USBDAC")) {
  247.         nextion.val("usbdac", 1);
  248.         inputLed(3);
  249.         // nextion.txt("bt_text", "USBDAC");   //rem if not needed(ONLY FOR TESTING)
  250.         Serial.println("...USBADC...");
  251.       } else if (sReceived.startsWith("NET")) {
  252.         nextion.val("wifi", 1);
  253.         nextion.vis("bt_text", 1);
  254.         inputLed(0);
  255.         // nextion.txt("bt_text", "WIFI");   //rem if not needed
  256.         Serial.println("...WIFI...");
  257.       } else if (sReceived.startsWith("BT")) {
  258.         nextion.val("bluetooth", 1);
  259.         nextion.vis("bt_text", 1);
  260.         nextion.vis("bt3", 1);
  261.         inputLed(1);
  262.         // nextion.txt("bt_text", "BLUETOOTH");    //rem if not needed
  263.         Serial.println("...BLUETOOTH...");
  264.       } else if (sReceived.startsWith("LINE-IN")) {
  265.         nextion.val("line_in", 1);
  266.         inputLed(2);
  267.         // nextion.txt("bt_text", "LINE IN");    //rem if not needed
  268.         Serial.println("...LINE-IN...");
  269.       }
  270.     } else if (sReceived.startsWith("SYS:STANDBY"))  //END OF STA:
  271.     {
  272.       inputLed(4);
  273.       Serial.println("Stand by mode...");
  274.       nextion.pageChange(3);
  275.       digitalWrite(relay, 0);
  276.     } else if (sReceived.startsWith("SRC:")) {
  277.       nextion.vis("bt_text", 0);
  278.       nextion.val("wifi", 0);
  279.       nextion.val("bluetooth", 0);
  280.       nextion.val("line_in", 0);
  281.       nextion.val("usbdac", 0);
  282.       reduce4();
  283.       if (sReceived.startsWith("USBDAC")) {
  284.         inputLed(3);
  285.         nextion.val("usbdac", 1);
  286.         // nextion.txt("bt_text", "USBDAC");   //rem if not needed
  287.         Serial.println("...USBADC...");
  288.       } else if (sReceived.startsWith("NET")) {
  289.         inputLed(0);
  290.         nextion.val("wifi", 1);
  291.         nextion.vis("bt_text", 1);
  292.         // nextion.txt("bt_text", "WIFI");   //rem if not needed
  293.         Serial.println("...WIFI...");
  294.       } else if (sReceived.startsWith("BT")) {
  295.         inputLed(1);
  296.         nextion.val("bluetooth", 1);
  297.         nextion.vis("bt_text", 1);
  298.         // nextion.txt("bt_text", "BLUETOOTH");    //rem if not needed
  299.         Serial.println("...BLUETOOTH...");
  300.       } else if (sReceived.startsWith("LINE-IN")) {
  301.         inputLed(2);
  302.         nextion.val("line_in", 1);
  303.         // nextion.txt("bt_text", "LINE IN");    //rem if not needed
  304.         Serial.println("...LINE-IN...");
  305.       }
  306.     } else if (sReceived.startsWith("VOL:"))  //end of SRC:
  307.     {
  308.       reduce4();
  309.       int index = sReceived.indexOf(';');
  310.       sReceived = sReceived.substring(0, index);
  311.       if (sReceived == "100") {
  312.         nextion.txt("t0", "MAX");
  313.       } else if (sReceived == "0") {
  314.         nextion.txt("t0", "MIN");
  315.       } else {
  316.         Serial.println("volume:  -|:" + sReceived);
  317.         digiVolume = sReceived.toInt();
  318.         nextion.txt("t0", sReceived);
  319.       }
  320.       nextion.val("h0", digiVolume);
  321.       rotaryEncoder.setEncoderValue(digiVolume);
  322.     } else if (sReceived.startsWith("MUT:"))  //end of VOL:
  323.     {
  324.       reduce4();
  325.       sReceived = sReceived.substring(0, 1);
  326.       Serial.println("Mute:_____:|" + sReceived);
  327.       if (sReceived == "1") {
  328.         dmute = 1;
  329.         nextion.txt("t0", "MIN");
  330.         nextion.val("h0", 0);
  331.       } else if (sReceived == "0") {
  332.         dmute = 0;
  333.         nextion.txt("t0", String(digiVolume));
  334.         nextion.val("h0", digiVolume);
  335.       }
  336.  
  337.     } else if (sReceived.startsWith("BTC:") || sReceived.startsWith("NET:"))  //end of MUT:
  338.     {
  339.       reduce4();
  340.       sReceived = sReceived.substring(0, 1);
  341.       if (sReceived == "1") {
  342.         nextion.txt("bt_text", "CONNECTED");
  343.         uart.print("TIT;");
  344.       } else if (sReceived == "0") {
  345.         nextion.txt("bt_text", "DISCONNECTED");
  346.       }
  347.     } else if (sReceived.endsWith("SYS:ON;")) {
  348.       nextion.txt("b0", "STARTED");
  349.       nextion.setPco("b0", 34784);
  350.       Serial.println("arrived SYS:ON...");
  351.     } else if (sReceived.startsWith("TIT:")) {
  352.       reduce4();
  353.       Serial.println("Title: " + sReceived);
  354.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  355.       nextion.txt("t1", sReceived);
  356.       // nextion.timerEnable("tm1", 1);
  357.     } else if (sReceived.startsWith("PMT:0")) {
  358.       nextion.txt("pmt", "PMT ON");
  359.       Serial.println(sReceived);
  360.     } else if (sReceived.startsWith("PMT:1")) {
  361.       nextion.txt("pmt", "PMT OFF");
  362.       Serial.println(sReceived);
  363.     } else if (sReceived.startsWith("ELP:")) {
  364.       reduce4();
  365.       int index = sReceived.indexOf("/");
  366.       sReceived = sReceived.substring(0, index);
  367.       // Serial1.println(sReceived);
  368.       long time = sReceived.toInt();
  369.       time = time / 100;
  370.       int tenth = time % 10;
  371.       time = time / 10;
  372.       long hour = time / 3600;
  373.       time = time - (hour * 3600);
  374.       long min = time / 60;
  375.       long sec = time - (min * 60);
  376.  
  377.       String timeS = "Time: ";
  378.       if (hour < 10) timeS += "0";
  379.       timeS += String(hour) + ":";
  380.       if (min < 10) timeS += "0";
  381.       timeS += String(min) + ":";
  382.       if (sec < 10) timeS += "0";
  383.       timeS += String(sec) + "." + String(tenth);
  384.  
  385.       // sReceived = String(hour) + ":" + String(min) + ":" + String(sec);
  386.       // Serial.println("ELP:.....|" + timeS);
  387.       if (time > 0) nextion.txt("t2", timeS);
  388.     } else if (sReceived.startsWith("BAS:")) {
  389.       // Serial.println("BASS BEFORE SUBSTRING....|" + sReceived);
  390.       reduce4();
  391.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  392.       // Serial.println("BASS STILL STRING:.....|" + sReceived);
  393.       int bass = sReceived.toInt();
  394.       // Serial.println("BASS INT:....|" + String(bass));
  395.       nextion.val("nbass", bass);
  396.       if (bass < 0) {
  397.         bass = 11 - abs(bass);
  398.       } else {
  399.         bass = bass + 11;
  400.       }
  401.       // Serial.println("BASS AFTERCALC....|"+String(bass));
  402.       nextion.val("hbass", bass);
  403.       // Serial.println("BASS:.....|" + String(bass));
  404.     } else if (sReceived.startsWith("TRE:")) {
  405.       reduce4();
  406.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  407.       int treb = sReceived.toInt();
  408.       Serial.println("TREB INT VALUE:...|" + String(treb));
  409.       nextion.val("ntreb", treb);
  410.       if (treb < 0) {
  411.         treb = 11 - abs(treb);
  412.       } else {
  413.         treb = treb + 11;
  414.       }
  415.       nextion.val("htreb", treb);
  416.     } else if (sReceived.startsWith("VBS:")) {
  417.       reduce4();
  418.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  419.       if (sReceived == "1") {
  420.         nextion.val("vbs", 1);
  421.         nextion.txt("t3", "ON");
  422.       } else {
  423.         nextion.val("vbs", 0);
  424.         nextion.txt("t3", "OFF");
  425.       }
  426.     } else if (sReceived.startsWith("VST:")) {
  427.       reduce4();
  428.       // write something to nextion SETUP (1) page
  429.       nextion.val("nVolStep", sReceived.toInt());
  430.       nextion.val("hVolStep", sReceived.toInt());
  431.     } else if (sReceived.startsWith("NAM:")) {
  432.       reduce4();
  433.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  434.       String dname, Nname;
  435.       int h = 16, sz = 0, dsz = 0;
  436.       if (sReceived.length() > 0) {
  437.         for (int i = 0; i <= sReceived.length() - 1; i = i + 1) {
  438.           dname = sReceived.substring(i, i + 1);
  439.           if (dname.toInt() >= 0 && dname.toInt() <= 9) {
  440.             sz = dname.toInt();
  441.             // Serial.println(dname);
  442.           }
  443.           if (dname == "A") sz = 10;
  444.           if (dname == "B") sz = 11;
  445.           if (dname == "C") sz = 12;
  446.           if (dname == "D") sz = 13;
  447.           if (dname == "E") sz = 14;
  448.           if (dname == "F") sz = 15;
  449.           // sz += sz * h;
  450.           if (h == 0) {
  451.             dsz += sz;
  452.             Serial.printf("%i. sz=%i\n", i, dsz);
  453.  
  454.             // Serial.printf("%i, | ", dsz);
  455.             Nname += char(dsz);
  456.             dsz = 0;
  457.           } else {
  458.             dsz = sz * 16;
  459.           }
  460.           h = 16 - h;
  461.         }
  462.         Serial.println(Nname);
  463.         nextion.txt("NAME", Nname);
  464.       }
  465.     } else if (sReceived.startsWith("IPA:")) {
  466.       reduce4();
  467.       sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  468.       nextion.txt("page1.t5", sReceived);
  469.     } else if (sReceived.startsWith("TME:")) {
  470.       int offset = 2;
  471.       String dc = sReceived.substring(21, 23);
  472.       int hour = dc.toInt();
  473.       if (hour % 2) {
  474.         dc = " ";
  475.       } else {
  476.         dc = ":";
  477.       }
  478.       // Serial.println("Second: "+dc);
  479.       String st = sReceived.substring(15, 17);
  480.       hour = st.toInt();
  481.  
  482.       // Serial.printf("Hour: %i\n",hour);
  483.  
  484.       hour = hour + offset;  //1+(-4)=-3 , -3+24=21 % 24 = 21 /// 22+(-4)=18 , 18+24=42 , 42 % 24 = 18
  485.       hour = (hour + 24) % 24;
  486.       if (hour < 10) {
  487.         sReceived = "0" + String(hour) + dc + sReceived.substring(18, 20);
  488.       } else {
  489.         sReceived = String(hour) + dc + sReceived.substring(18, 20);
  490.       }
  491.       Serial.println(sReceived);
  492.       nextion.txt("t6", sReceived);
  493.     } else if (sReceived.startsWith("VND:")) {
  494.       reduce4();
  495.       sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  496.       nextion.txt("vVendor", sReceived);
  497.     }
  498.  
  499.  
  500.     sReceived = "";
  501.   }  //end of uart
  502.  
  503.  
  504.  
  505.   while (Serial1.available()) {
  506.     String nReceived = Serial1.readStringUntil(';');
  507.     Serial.println("Serial1:__|" + nReceived + ";");
  508.  
  509.     uart.print(nReceived + ";");
  510.   }
  511. }
  512. void reduce4() {
  513.   sReceived = sReceived.substring(4);
  514. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement