Advertisement
GaabMM88

Untitled

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