Advertisement
GaabMM88

Untitled

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