Advertisement
GaabMM88

Untitled

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