Advertisement
GaabMM88

Untitled

Aug 8th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.76 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 = 0;
  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.       inputLed(4);
  275.       Serial.println("Stand by mode...");
  276.       nextion.pageChange(3);
  277.       digitalWrite(relay, 0);
  278.     } else if (sReceived.startsWith("SRC:")) {
  279.       nextion.vis("bt_text", 0);
  280.       nextion.val("wifi", 0);
  281.       nextion.val("bluetooth", 0);
  282.       nextion.val("line_in", 0);
  283.       nextion.val("usbdac", 0);
  284.       reduce4();
  285.       if (sReceived.startsWith("USBDAC")) {
  286. inputLed(3);
  287.         nextion.val("usbdac", 1);
  288.         // nextion.txt("bt_text", "USBDAC");   //rem if not needed
  289.         Serial.println("...USBADC...");
  290.       } else if (sReceived.startsWith("NET")) {
  291. inputLed(0);
  292.         nextion.val("wifi", 1);
  293.         nextion.vis("bt_text", 1);
  294.         // nextion.txt("bt_text", "WIFI");   //rem if not needed
  295.         Serial.println("...WIFI...");
  296.       } else if (sReceived.startsWith("BT")) {
  297. inputLed(1);
  298.         nextion.val("bluetooth", 1);
  299.         nextion.vis("bt_text", 1);
  300.         // nextion.txt("bt_text", "BLUETOOTH");    //rem if not needed
  301.         Serial.println("...BLUETOOTH...");
  302.       } else if (sReceived.startsWith("LINE-IN")) {
  303. inputLed(2);
  304.         nextion.val("line_in", 1);
  305.         // nextion.txt("bt_text", "LINE IN");    //rem if not needed
  306.         Serial.println("...LINE-IN...");
  307.       }
  308.     } else if (sReceived.startsWith("VOL:"))  //end of SRC:
  309.     {
  310.       reduce4();
  311.       int index = sReceived.indexOf(';');
  312.       sReceived = sReceived.substring(0, index);
  313.       if (sReceived == "100") {
  314.         nextion.txt("t0", "MAX");
  315.       } else if (sReceived == "0") {
  316.         nextion.txt("t0", "MIN");
  317.       } else {
  318.         Serial.println("volume:  -|:" + sReceived);
  319.         digiVolume = sReceived.toInt();
  320.         nextion.txt("t0", sReceived);
  321.       }
  322.       nextion.val("h0", digiVolume);
  323.       rotaryEncoder.setEncoderValue(digiVolume);
  324.     } else if (sReceived.startsWith("MUT:"))  //end of VOL:
  325.     {
  326.       reduce4();
  327.       sReceived = sReceived.substring(0, 1);
  328.       Serial.println("Mute:_____:|" + sReceived);
  329.       if (sReceived == "1") {
  330.         dmute = 1;
  331.         nextion.txt("t0", "MIN");
  332.         nextion.val("h0", 0);
  333.       } else if (sReceived == "0") {
  334.         dmute = 0;
  335.         nextion.txt("t0", String(digiVolume));
  336.         nextion.val("h0", digiVolume);
  337.       }
  338.  
  339.     } else if (sReceived.startsWith("BTC:") || sReceived.startsWith("NET:"))  //end of MUT:
  340.     {
  341.       reduce4();
  342.       sReceived = sReceived.substring(0, 1);
  343.       if (sReceived == "1") {
  344.         nextion.txt("bt_text", "CONNECTED");
  345.         uart.print("TIT;");
  346.       } else if (sReceived == "0") {
  347.         nextion.txt("bt_text", "DISCONNECTED");
  348.       }
  349.     } else if (sReceived.endsWith("SYS:ON;")) {
  350.       nextion.txt("b0", "STARTED");
  351.       nextion.setPco("b0", 34784);
  352.       Serial.println("arrived SYS:ON...");
  353.     } else if (sReceived.startsWith("TIT:")) {
  354.       reduce4();
  355.       Serial.println("Title: " + sReceived);
  356.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  357.       nextion.txt("t1", sReceived);
  358.       // nextion.timerEnable("tm1", 1);
  359.     } else if (sReceived.startsWith("PMT:0")) {
  360.       nextion.txt("pmt", "PMT ON");
  361.       Serial.println(sReceived);
  362.     } else if (sReceived.startsWith("PMT:1")) {
  363.       nextion.txt("pmt", "PMT OFF");
  364.       Serial.println(sReceived);
  365.     } else if (sReceived.startsWith("ELP:")) {
  366.       reduce4();
  367.       int index = sReceived.indexOf("/");
  368.       sReceived = sReceived.substring(0, index);
  369.       // Serial1.println(sReceived);
  370.       long time = sReceived.toInt();
  371.       time = time / 100;
  372.       int tenth = time % 10;
  373.       time = time / 10;
  374.       long hour = time / 3600;
  375.       time = time - (hour * 3600);
  376.       long min = time / 60;
  377.       long sec = time - (min * 60);
  378.  
  379.       String timeS = "Time: ";
  380.       if (hour < 10) timeS += "0";
  381.       timeS += String(hour) + ":";
  382.       if (min < 10) timeS += "0";
  383.       timeS += String(min) + ":";
  384.       if (sec < 10) timeS += "0";
  385.       timeS += String(sec) + "." + String(tenth);
  386.  
  387.       // sReceived = String(hour) + ":" + String(min) + ":" + String(sec);
  388.       // Serial.println("ELP:.....|" + timeS);
  389.       if (time > 0) nextion.txt("t2", timeS);
  390.     } else if (sReceived.startsWith("BAS:")) {
  391.       // Serial.println("BASS BEFORE SUBSTRING....|" + sReceived);
  392.       reduce4();
  393.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  394.       // Serial.println("BASS STILL STRING:.....|" + sReceived);
  395.       int bass = sReceived.toInt();
  396.       // Serial.println("BASS INT:....|" + String(bass));
  397.       nextion.val("nbass", bass);
  398.       if (bass < 0) {
  399.         bass = 11 - abs(bass);
  400.       } else {
  401.         bass = bass + 11;
  402.       }
  403.       // Serial.println("BASS AFTERCALC....|"+String(bass));
  404.       nextion.val("hbass", bass);
  405.       // Serial.println("BASS:.....|" + String(bass));
  406.     } else if (sReceived.startsWith("TRE:")) {
  407.       reduce4();
  408.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  409.       int treb = sReceived.toInt();
  410.       Serial.println("TREB INT VALUE:...|" + String(treb));
  411.       nextion.val("ntreb", treb);
  412.       if (treb < 0) {
  413.         treb = 11 - abs(treb);
  414.       } else {
  415.         treb = treb + 11;
  416.       }
  417.       nextion.val("htreb", treb);
  418.     } else if (sReceived.startsWith("VBS:")) {
  419.       reduce4();
  420.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  421.       if (sReceived == "1") {
  422.         nextion.val("vbs", 1);
  423.         nextion.txt("t3", "ON");
  424.       } else {
  425.         nextion.val("vbs", 0);
  426.         nextion.txt("t3", "OFF");
  427.       }
  428.     } else if (sReceived.startsWith("VST:")) {
  429.       reduce4();
  430.       // write something to nextion SETUP (1) page
  431.       nextion.val("nVolStep", sReceived.toInt());
  432.       nextion.val("hVolStep", sReceived.toInt());
  433.     } else if (sReceived.startsWith("NAM:")) {
  434.       reduce4();
  435.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  436.       String dname, Nname;
  437.       int h = 16, sz = 0, dsz = 0;
  438.       if (sReceived.length() > 0) {
  439.         for (int i = 0; i <= sReceived.length() - 1; i = i + 1) {
  440.           dname = sReceived.substring(i, i + 1);
  441.           if (dname.toInt() >= 0 && dname.toInt() <= 9) {
  442.             sz = dname.toInt();
  443.             // Serial.println(dname);
  444.           }
  445.           if (dname == "A") sz = 10;
  446.           if (dname == "B") sz = 11;
  447.           if (dname == "C") sz = 12;
  448.           if (dname == "D") sz = 13;
  449.           if (dname == "E") sz = 14;
  450.           if (dname == "F") sz = 15;
  451.           // sz += sz * h;
  452.           if (h == 0) {
  453.             dsz += sz;
  454.             Serial.printf("%i. sz=%i\n", i, dsz);
  455.  
  456.             // Serial.printf("%i, | ", dsz);
  457.             Nname += char(dsz);
  458.             dsz = 0;
  459.           } else {
  460.             dsz = sz * 16;
  461.           }
  462.           h = 16 - h;
  463.         }
  464.         Serial.println(Nname);
  465.         nextion.txt("NAME", Nname);
  466.       }
  467.     } else if (sReceived.startsWith("IPA:")) {
  468.       reduce4();
  469.       sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  470.       nextion.txt("page1.t5", sReceived);
  471.     } else if (sReceived.startsWith("TME:")) {
  472.       int offset = 2;
  473.       String dc = sReceived.substring(21, 23);
  474.       int hour = dc.toInt();
  475.       if (hour % 2) {
  476.         dc = " ";
  477.       } else {
  478.         dc = ":";
  479.       }
  480.       // Serial.println("Second: "+dc);
  481.       String st = sReceived.substring(15, 17);
  482.       hour = st.toInt();
  483.  
  484.       // Serial.printf("Hour: %i\n",hour);
  485.  
  486.       hour = hour + offset;  //1+(-4)=-3 , -3+24=21 % 24 = 21 /// 22+(-4)=18 , 18+24=42 , 42 % 24 = 18
  487.       hour = (hour + 24) % 24;
  488.       if (hour < 10) {
  489.         sReceived = "0" + String(hour) + dc + sReceived.substring(18, 20);
  490.       } else {
  491.         sReceived = String(hour) + dc + sReceived.substring(18, 20);
  492.       }
  493.       Serial.println(sReceived);
  494.       nextion.txt("t6", sReceived);
  495.     } else if (sReceived.startsWith("VND:")) {
  496.       reduce4();
  497.       sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  498.       nextion.txt("vVendor", sReceived);
  499.     }
  500.  
  501.  
  502.     sReceived = "";
  503.   }  //end of uart
  504.  
  505.  
  506.  
  507.   while (Serial1.available()) {
  508.     String nReceived = Serial1.readStringUntil(';');
  509.     Serial.println("Serial1:__|" + nReceived + ";");
  510.  
  511.     uart.print(nReceived + ";");
  512.   }
  513. }
  514. void reduce4() {
  515.   sReceived = sReceived.substring(4);
  516. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement