Advertisement
GaabMM88

Untitled

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