Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <HardwareSerial.h>
- #include "AiEsp32RotaryEncoder.h"
- #include "Arduino.h"
- HardwareSerial uart(2); // Uso de la interfaz de hardware Serial2
- #define ROTARY_ENCODER_A_PIN 35
- #define ROTARY_ENCODER_B_PIN 32
- #define ROTARY_ENCODER_BUTTON_PIN 33
- #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 */
- #define ROTARY_ENCODER_STEPS 4
- #define powerSW 23
- #define relay 13
- int digiVolume = 0, dmute = 0, lastPressed = 0;
- boolean powerState = 0, lastPowerState = 0;
- String sReceived;
- //instead of changing here, rather change numbers above
- AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
- void rotary_onButtonClick() {
- static unsigned long lastTimePressed = 0;
- //ignore multiple press in that time milliseconds
- if (millis() - lastTimePressed < 500) {
- return;
- }
- lastTimePressed = millis();
- Serial.print("button pressed ");
- Serial.print(millis());
- Serial.println(" milliseconds after restart");
- if (dmute == 0) {
- uart.print("MUT:1;");
- } else {
- uart.print("MUT:0;");
- }
- }
- void rotary_loop() {
- //dont print anything unless value changed
- if (rotaryEncoder.encoderChanged()) {
- Serial.print("Value: ");
- Serial.println(rotaryEncoder.readEncoder());
- uart.print("VOL:" + String(rotaryEncoder.readEncoder()) + ";");
- digiVolume = rotaryEncoder.readEncoder();
- }
- if (rotaryEncoder.isEncoderButtonClicked()) {
- rotary_onButtonClick();
- }
- }
- void IRAM_ATTR readEncoderISR() {
- rotaryEncoder.readEncoder_ISR();
- }
- class c_NextionWrite {
- public:
- void init(int speed, int RXN, int TXN) {
- Serial1.begin(speed, SERIAL_8N1, RXN, TXN);
- // Serial.printf("Serial1 - Speed: %d, RX-pin: %d, TX-pin: %d \n", speed, RX, TX);
- }
- void txt(String Name, String text) {
- Serial1.print(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
- Serial.println(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
- }
- void val(String Name, int value) {
- Serial1.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
- Serial.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
- }
- void pageChange(int nr) {
- Serial1.print("page " + String(nr) + "\xFF\xFF\xFF");
- Serial.print("page " + String(nr) + "\xFF\xFF\xFF");
- }
- void setPco(String name, int pco) {
- Serial1.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
- Serial.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
- }
- void timerEnable(String name, int en) {
- Serial1.print(name + ".en=" + String(en) + "\xFF\xFF\xFF");
- }
- void vis(String name, int en) {
- Serial1.print("vis " + name + "," + String(en) + "\xFF\xFF\xFF");
- }
- };
- c_NextionWrite nextion;
- #define RXN_PIN 26 // Serial1 RX to Nextion TX
- #define TXN_PIN 25 // Serial1 TX to Nextion RX
- #define RX_PIN 14 // Serial2 RX a Amp TX
- #define TX_PIN 27 // Serial2 TX a Amp RX
- void setup() {
- pinMode(relay, OUTPUT);
- digitalWrite(relay, 0);
- pinMode(powerSW, INPUT);
- rotaryEncoder.begin();
- rotaryEncoder.setup(readEncoderISR);
- //set boundaries and if values should cycle or not
- //in this example we will set possible values between 0 and 1000;
- bool circleValues = false;
- rotaryEncoder.setBoundaries(0, 100, circleValues); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
- /*Rotary acceleration introduced 25.2.2021.
- * in case range to select is huge, for example - select a value between 0 and 1000 and we want 785
- * without accelerateion you need long time to get to that number
- * Using acceleration, faster you turn, faster will the value raise.
- * For fine tuning slow down.
- */
- //rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
- rotaryEncoder.setAcceleration(0); //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration
- rotaryEncoder.setEncoderValue(digiVolume);
- // put your setup code here, to run once:
- Serial.begin(115200);
- Serial.println("Starting..");
- Serial1.begin(115200, SERIAL_8N1, RXN_PIN, TXN_PIN);
- // Inicializar la comunicación UART
- uart.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
- // uart.setTimeout(2000);
- delay(2000);
- Serial.println("Started...");
- uart.print("PMT:1;");
- uart.print("BEP:0;");
- uart.print("BEP;");
- uart.print("PMT;");
- nextion.txt("bt_text", "Hello World");
- delay(2000);
- nextion.vis("bt_text", 0);
- delay(2000);
- nextion.vis("bt_text", 1);
- }
- void loop() {
- if (digitalRead(powerSW) == 0 && lastPowerState == 1 && lastPressed + 5000 < millis()) {
- Serial.println(digitalRead(powerSW));
- if (powerState == 0) {
- powerState = 1;
- uart.print("SYS:REBOOT;");
- Serial.println("Digi REBOOT...");
- } else {
- powerState = 0;
- uart.print("SYS:STANDBY;");
- Serial.println("Digi STANDBY...");
- }
- lastPressed = millis();
- }
- lastPowerState = digitalRead(powerSW); //1
- rotary_loop();
- // put your main code here, to run repeatedly:
- while (uart.available()) {
- sReceived = uart.readStringUntil('\n');
- sReceived.trim();
- Serial.println("uart:___|" + sReceived);
- if (sReceived.startsWith("PLA:0")) {
- nextion.txt("bt_text", "Pause/Stop");
- } else if (sReceived.startsWith("PLA:1")) {
- nextion.txt("bt_text", "Playing...");
- } else if (sReceived.startsWith("STA:")) {
- digitalWrite(relay, 1);
- nextion.pageChange(0);
- nextion.vis("bt_text", 0);
- Serial.println("STA received");
- Serial.println("Before: " + sReceived);
- nextion.val("wifi", 0);
- nextion.val("bluetooth", 0);
- nextion.val("line_in", 0);
- nextion.val("usbdac", 0);
- nextion.txt("bt_text", "");
- // reduce4();
- reduce4();
- if (sReceived.startsWith("USBDAC")) {
- nextion.val("usbdac", 1);
- // nextion.txt("bt_text", "USBDAC"); //rem if not needed(ONLY FOR TESTING)
- Serial.println("...USBADC...");
- } else if (sReceived.startsWith("NET")) {
- nextion.val("wifi", 1);
- nextion.vis("bt_text", 1);
- // nextion.txt("bt_text", "WIFI"); //rem if not needed
- Serial.println("...WIFI...");
- } else if (sReceived.startsWith("BT")) {
- nextion.val("bluetooth", 1);
- nextion.vis("bt_text", 1);
- // nextion.txt("bt_text", "BLUETOOTH"); //rem if not needed
- Serial.println("...BLUETOOTH...");
- } else if (sReceived.startsWith("LINE-IN")) {
- nextion.val("line_in", 1);
- // nextion.txt("bt_text", "LINE IN"); //rem if not needed
- Serial.println("...LINE-IN...");
- }
- } else if (sReceived.startsWith("SYS:STANDBY")) //END OF STA:
- {
- Serial.println("Stand by mode...");
- nextion.pageChange(3);
- digitalWrite(relay, 0);
- } else if (sReceived.startsWith("SRC:")) {
- nextion.vis("bt_text", 0);
- nextion.val("wifi", 0);
- nextion.val("bluetooth", 0);
- nextion.val("line_in", 0);
- nextion.val("usbdac", 0);
- reduce4();
- if (sReceived.startsWith("USBDAC")) {
- nextion.val("usbdac", 1);
- // nextion.txt("bt_text", "USBDAC"); //rem if not needed
- Serial.println("...USBADC...");
- } else if (sReceived.startsWith("NET")) {
- nextion.val("wifi", 1);
- nextion.vis("bt_text", 1);
- // nextion.txt("bt_text", "WIFI"); //rem if not needed
- Serial.println("...WIFI...");
- } else if (sReceived.startsWith("BT")) {
- nextion.val("bluetooth", 1);
- nextion.vis("bt_text", 1);
- // nextion.txt("bt_text", "BLUETOOTH"); //rem if not needed
- Serial.println("...BLUETOOTH...");
- } else if (sReceived.startsWith("LINE-IN")) {
- nextion.val("line_in", 1);
- // nextion.txt("bt_text", "LINE IN"); //rem if not needed
- Serial.println("...LINE-IN...");
- }
- } else if (sReceived.startsWith("VOL:")) //end of SRC:
- {
- reduce4();
- int index = sReceived.indexOf(';');
- sReceived = sReceived.substring(0, index);
- if (sReceived == "100") {
- nextion.txt("t0", "MAX");
- } else if (sReceived == "0") {
- nextion.txt("t0", "MIN");
- } else {
- Serial.println("volume: -|:" + sReceived);
- digiVolume = sReceived.toInt();
- nextion.txt("t0", sReceived);
- }
- nextion.val("h0", digiVolume);
- rotaryEncoder.setEncoderValue(digiVolume);
- } else if (sReceived.startsWith("MUT:")) //end of VOL:
- {
- reduce4();
- sReceived = sReceived.substring(0, 1);
- Serial.println("Mute:_____:|" + sReceived);
- if (sReceived == "1") {
- dmute = 1;
- nextion.txt("t0", "MIN");
- nextion.val("h0", 0);
- } else if (sReceived == "0") {
- dmute = 0;
- nextion.txt("t0", String(digiVolume));
- nextion.val("h0", digiVolume);
- }
- } else if (sReceived.startsWith("BTC:") || sReceived.startsWith("NET:")) //end of MUT:
- {
- reduce4();
- sReceived = sReceived.substring(0, 1);
- if (sReceived == "1") {
- nextion.txt("bt_text", "CONNECTED");
- uart.print("TIT;");
- } else if (sReceived == "0") {
- nextion.txt("bt_text", "DISCONNECTED");
- }
- } else if (sReceived.endsWith("SYS:ON;")) {
- nextion.txt("b0", "STARTED");
- nextion.setPco("b0", 34784);
- Serial.println("arrived SYS:ON...");
- } else if (sReceived.startsWith("TIT:")) {
- reduce4();
- Serial.println("Title: " + sReceived);
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- nextion.txt("t1", sReceived);
- // nextion.timerEnable("tm1", 1);
- } else if (sReceived.startsWith("PMT:0")) {
- nextion.txt("pmt", "PMT ON");
- Serial.println(sReceived);
- } else if (sReceived.startsWith("PMT:1")) {
- nextion.txt("pmt", "PMT OFF");
- Serial.println(sReceived);
- } else if (sReceived.startsWith("ELP:")) {
- reduce4();
- int index = sReceived.indexOf("/");
- sReceived = sReceived.substring(0, index);
- // Serial1.println(sReceived);
- long time = sReceived.toInt();
- time = time / 100;
- int tenth = time % 10;
- time = time / 10;
- long hour = time / 3600;
- time = time - (hour * 3600);
- long min = time / 60;
- long sec = time - (min * 60);
- String timeS = "Time: ";
- if (hour < 10) timeS += "0";
- timeS += String(hour) + ":";
- if (min < 10) timeS += "0";
- timeS += String(min) + ":";
- if (sec < 10) timeS += "0";
- timeS += String(sec) + "." + String(tenth);
- // sReceived = String(hour) + ":" + String(min) + ":" + String(sec);
- // Serial.println("ELP:.....|" + timeS);
- if (time > 0) nextion.txt("t2", timeS);
- } else if (sReceived.startsWith("BAS:")) {
- // Serial.println("BASS BEFORE SUBSTRING....|" + sReceived);
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- // Serial.println("BASS STILL STRING:.....|" + sReceived);
- int bass = sReceived.toInt();
- // Serial.println("BASS INT:....|" + String(bass));
- nextion.val("nbass", bass);
- if (bass < 0) {
- bass = 11 - abs(bass);
- } else {
- bass = bass + 11;
- }
- // Serial.println("BASS AFTERCALC....|"+String(bass));
- nextion.val("hbass", bass);
- // Serial.println("BASS:.....|" + String(bass));
- } else if (sReceived.startsWith("TRE:")) {
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- int treb = sReceived.toInt();
- Serial.println("TREB INT VALUE:...|" + String(treb));
- nextion.val("ntreb", treb);
- if (treb < 0) {
- treb = 11 - abs(treb);
- } else {
- treb = treb + 11;
- }
- nextion.val("htreb", treb);
- } else if (sReceived.startsWith("VBS:")) {
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- if (sReceived == "1") {
- nextion.val("vbs", 1);
- nextion.txt("t3", "ON");
- } else {
- nextion.val("vbs", 0);
- nextion.txt("t3", "OFF");
- }
- } else if (sReceived.startsWith("VST:")) {
- reduce4();
- // write something to nextion SETUP (1) page
- nextion.val("nVolStep", sReceived.toInt());
- nextion.val("hVolStep", sReceived.toInt());
- } else if (sReceived.startsWith("NAM:")) {
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- String dname, Nname;
- int h = 16, sz;
- for (int i = 0; i <= sReceived.length() - 1; i = i + 1) {
- dname = sReceived.substring(i, i + 1);
- if (dname.toInt() >= 0 && dname.toInt() <= 9) {
- sz = dname.toInt();
- }
- if (dname == "A") sz = 10;
- if (dname == "B") sz = 11;
- if (dname == "C") sz = 12;
- if (dname == "D") sz = 13;
- if (dname == "E") sz = 14;
- if (dname == "F") sz = 15;
- sz += sz * h;
- h = 16 - h;
- if (h == 0) {
- Nname += char(sz);
- sz=0;
- }
- Serial.println(Nname);
- }
- nextion.txt("t1", Nname);
- }
- sReceived = "";
- } //end of uart
- while (Serial1.available()) {
- String nReceived = Serial1.readStringUntil(';');
- Serial.println("Serial1:__|" + nReceived + ";");
- uart.print(nReceived + ";");
- }
- }
- void reduce4() {
- sReceived = sReceived.substring(4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement