Advertisement
GaabMM88

Untitled

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