Advertisement
GaabMM88

Untitled

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