Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- const char compile_date[] = __DATE__;
- //Included with the name printing
- #include <HardwareSerial.h>
- #include "AiEsp32RotaryEncoder.h"
- #include "Arduino.h"
- #include "Wire.h"
- #include <Adafruit_NeoPixel.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 muteSW
- //#define inputSW
- #define powerSW 23
- #define dcSenseRight 19
- #define dcSenseLeft 18
- #define acSense 5
- #define relay 13
- #define WS2812 2
- #define NUMPIXELS 8
- Adafruit_NeoPixel pixels(NUMPIXELS, WS2812, NEO_RGB + NEO_KHZ800);
- int offset = 2;
- int digiVolume = 0, dmute = 0, lastPressed = 0, source = 0;
- boolean powerState = 0, lastPowerState = 0, nextionSetTimebit = 1;
- //boolean dcSense = 0, lastDcSense = 0;
- //boolean acSense = 0, lastAcSense = 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();
- }
- void inputLed(int input) {
- source = input;
- int r = 0, g = 0, b = 0;
- switch (input) {
- case 0: // WiFi
- r = 49;
- g = 50;
- b = 51;
- break;
- case 1: // Bluetooth
- r = 0;
- g = 0;
- b = 50;
- break;
- case 2: // Line-In
- r = 0;
- g = 50;
- b = 0;
- break;
- case 3: // USB-DAC
- r = 50;
- g = 0;
- b = 0;
- break;
- case 4: // OFF
- r = 55;
- g = 35;
- b = 0;
- break;
- }
- for(int i=0; i<NUMPIXELS; i++){
- pixels.setPixelColor(i, pixels.Color(g, r, b));
- pixels.show(); // Send the updated pixel colors to the hardware.
- }
- }
- 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");
- }
- void setTime(int hour, int min, int sec){
- hour = (hour + 24) % 24;
- Serial1.print("rtc3=" String(hour) + "\xFF\xFF\xFF");
- Serial1.print("rtc4=" String(min) + "\xFF\xFF\xFF");
- Serial1.print("rtc5=" String(sec) + "\xFF\xFF\xFF");
- Serial.printf("Nextion time/ hour: %d min: %d sec: %d \n", hour,min,sec);
- Serial.println("--------------------");
- }
- };
- 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
- #define SDA 21 // I2C Thermometer, Expander, etc.
- #define SCL 22
- //#define INT ?
- void setup() {
- pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
- pinMode(relay, OUTPUT);
- delay(1);
- digitalWrite(relay, 0);
- //pinMode(muteSW, INPUT);
- //pinMode(inputSW, INPUT);
- pinMode(powerSW, INPUT_PULLUP);
- pinMode(dcSenseRight, INPUT_PULLUP);
- pinMode(dcSenseLeft, INPUT_PULLUP);
- pinMode(acSense, INPUT_PULLUP);
- 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:
- Wire.begin(SDA, SCL);
- 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...");
- Serial.println(compile_date);
- uart.print("PMT:1;");
- uart.print("BEP:0;");
- uart.print("BEP;");
- uart.print("PMT;");
- nextion.txt("bt_text", "ESP32 now starting...");
- delay(500);
- nextion.vis("bt_text", 0);
- delay(500);
- nextion.vis("bt_text", 1);
- //uart.print("SYS:REBOOT;");
- //delay(3500);
- uart.print("SYS:STANDBY;");
- uart.print("SYS:STANDBY;");
- uart.print("SYS:STANDBY;");
- uart.print("SYS:STANDBY;");
- Serial.println("Initial OFF");
- nextion.pageChange(3);
- inputLed(4);
- }
- 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...");
- inputLed(4);
- }
- lastPressed = millis();
- }
- if ((digitalRead(dcSenseRight) == 0)) {
- Serial.println("DC RIGHT FAIL");
- }
- if ((digitalRead(dcSenseLeft) == 0)) {
- Serial.println("DC LEFT FAIL");
- }
- if ((digitalRead(acSense) == 0)) {
- Serial.println("AC FAIL");
- }
- 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", "Szünet/Megállítva");
- } else if (sReceived.startsWith("PLA:1")) {
- nextion.txt("bt_text", "Lejátszás...");
- } 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", "");
- nextion.vis("bt3", 0);
- nextion.vis("title", 0);
- nextion.vis("elapsed", 0);
- nextion.vis("vendor", 0);
- // reduce4();
- reduce4();
- if (sReceived.startsWith("USBDAC")) {
- nextion.val("usbdac", 1);
- inputLed(3);
- // nextion.txt("bt_text", "USBDAC"); //rem if not needed(ONLY FOR TESTING)
- Serial.println("...USBADC...");
- } else if (sReceived.startsWith("NET")) {
- nextion.vis("title", 1);
- nextion.vis("elapsed", 1);
- nextion.vis("vendor", 1);
- nextion.val("wifi", 1);
- nextion.vis("bt_text", 1);
- inputLed(0);
- // 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.vis("bt3", 1);
- inputLed(1);
- // nextion.txt("bt_text", "BLUETOOTH"); //rem if not needed
- Serial.println("...BLUETOOTH...");
- } else if (sReceived.startsWith("LINE-IN")) {
- nextion.val("line_in", 1);
- inputLed(2);
- // nextion.txt("bt_text", "LINE IN"); //rem if not needed
- Serial.println("...LINE-IN...");
- }
- } else if (sReceived.startsWith("SYS:STANDBY")) //END OF STA:
- {
- inputLed(4);
- Serial.println("Stand by mode...");
- nextion.pageChange(3);
- digitalWrite(relay, 0);
- nextionSetTimebit=1;
- } 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);
- nextion.vis("title", 0);
- nextion.vis("elapsed", 0);
- nextion.vis("vendor", 0);
- reduce4();
- if (sReceived.startsWith("USBDAC")) {
- inputLed(3);
- nextion.val("usbdac", 1);
- // nextion.txt("bt_text", "USBDAC"); //rem if not needed
- Serial.println("...USBDAC...");
- } else if (sReceived.startsWith("NET")) {
- inputLed(0);
- nextion.val("wifi", 1);
- nextion.vis("bt_text", 1);
- // nextion.txt("bt_text", "WIFI"); //rem if not needed
- Serial.println("...WIFI...");
- nextion.vis("title", 1);
- nextion.vis("elapsed", 1);
- nextion.vis("vendor", 1);
- } else if (sReceived.startsWith("BT")) {
- inputLed(1);
- 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")) {
- inputLed(2);
- 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("volText", "MAX");
- } else if (sReceived == "0") {
- nextion.txt("volText", "MIN");
- } else {
- Serial.println("volume: -|:" + sReceived);
- digiVolume = sReceived.toInt();
- nextion.txt("volText", sReceived);
- }
- nextion.val("volSlide", 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("volText", "MIN");
- nextion.val("volSlide", 0);
- } else if (sReceived == "0") {
- dmute = 0;
- nextion.txt("volText", String(digiVolume));
- nextion.val("volSlide", 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("powerOn", "STARTED");
- nextion.setPco("powerOn", 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("title", 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);
- // if(nextionSetTimebit==1){
- // nextion.setTime(hour,min,sec);
- // }
- 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("elapsed", 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 = 0, dsz = 0;
- if (sReceived.length() > 0) {
- 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();
- // Serial.println(dname);
- }
- 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;
- if (h == 0) {
- dsz += sz;
- Serial.printf("%i. sz=%i\n", i, dsz);
- // Serial.printf("%i, | ", dsz);
- Nname += char(dsz);
- dsz = 0;
- } else {
- dsz = sz * 16;
- }
- h = 16 - h;
- }
- Serial.println(Nname);
- nextion.txt("NAME", Nname);
- }
- } else if (sReceived.startsWith("IPA:")) {
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1); //removing ";"
- nextion.txt("page1.t5", sReceived);
- } else if (sReceived.startsWith("TME:")) {
- String dc = sReceived.substring(21, 23);
- String nc = sReceived;
- int hour = dc.toInt();
- if (hour % 2) {
- dc = " ";
- } else {
- dc = ":";
- }
- // Serial.println("Second: "+dc);
- String st = sReceived.substring(15, 17);
- hour = st.toInt();
- // Serial.printf("Hour: %i\n",hour);
- hour = hour + offset; //1+(-4)=-3 , -3+24=21 % 24 = 21 /// 22+(-4)=18 , 18+24=42 , 42 % 24 = 18
- hour = (hour + 24) % 24;
- if (hour < 10) {
- sReceived = "0" + String(hour) + dc + sReceived.substring(18, 20);
- } else {
- sReceived = String(hour) + dc + sReceived.substring(18, 20);
- }
- Serial.println(sReceived);
- nextion.txt("t6", sReceived);
- dc = nc.substring(4,8);
- Serial.println(dc);
- Serial.println(nextionSetTimebit);
- if(dc!="2000"&& nextionSetTimebit==1){
- dc=nc.substring(15,17);
- int hour = dc.toInt();
- dc=nc.substring(18,20);
- int min = dc.toInt();
- dc=nc.substring(21,23);
- int sec = dc.toInt();
- nextion.setTime(hour,min,sec);
- nextionSetTimebit=0;
- }
- } else if (sReceived.startsWith("VND:")) {
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1); //removing ";"
- nextion.txt("vVendor", sReceived);
- }
- 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