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>
- #include <esp_task_wdt.h>
- #include <WiFi.h>
- #include <RTClib.h>
- #include <PinDefinitionsAndMore.h> // Define macros for input and output pin etc.
- #include <IRremote.hpp>
- #include "TC74.h"
- #define sense1 2000
- #define sense2 2050
- #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 inputSW
- #define dcSenseRight 19 //-> pin / dcErrorRight (current state) / dcErroRightLast (last state of dcErrorRight)
- #define dcSenseLeft 18 //-> pin / dcErrorLeft (current state) / dcErrorLeftLast (last state of dcErrorLeft)
- #define acSense 5 //-> pin / acError (current state) / acErrorLast (last state of acError)
- #define relay 16 // Standby relay
- #define speaker 17 // Speaker relay
- #define WS2812 2 // WS2812 pin
- #define NUMPIXELS 8 // number of LED
- #define LDR 12 // LDR pin
- #define RXN_PIN 26 // Serial1 RX to Nextion TX
- #define TXN_PIN 25 // Serial1 TX to Nextion RX
- #define RX_PIN 27 // Serial2 RX a Amp TX
- #define TX_PIN 14 // Serial2 TX a Amp RX
- #define SDA 21 // I2C Thermometer, Expander, etc.
- #define SCL 22
- #define boot 0 // Nextion page number 0..11
- #define standby 1
- #define main 2
- #define digiSetup 3
- #define ampSetup 4
- #define digiInput 5
- #define digiPreset 6
- #define dsp1 7
- #define dsp2 8
- #define dsp3 9
- #define info 10
- #define dataRecord 11
- RTC_DS3231 rtc;
- TC74 tmp1(0x4A); //A2 Address
- TC74 tmp2(0x4B); //A3 Address
- HardwareSerial uart(2); // Uso de la interfaz de hardware Serial2
- Adafruit_NeoPixel pixels(NUMPIXELS, WS2812, NEO_RGB + NEO_KHZ800);
- AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
- const int powerOffDelayForRelay = 400; //in millis seconds
- //const byte LDR = 12; //Light Dependant Resistor
- const long eventTime_1_LDR = 5000; //check brigtness in ms
- unsigned long previousTime_1 = 0, beepMillis = 0, protectMillis = 0, irMillis = 0, irVolMillis, strengthMillis = 0;
- unsigned long ac_time = 0, ac_time_last = 0;
- boolean powerState = 0, lastPowerState = 0, nextionSetTimebit = 1;
- int offset = 2, beep = 0, NextionPage;
- int digiVolume = 0, dmute = 0, lastPressed = 0, source = 0, dim = 0, dimN = -1, task = 0;
- int initDigi = 0, toDigi = 1;
- int dcErrorRight = 1, dcErrorRightLast = -1, dcErrorLeft = 1, dcErrorLeftLast = -1, acError = 1, acErrorLast = -1; //variables for protection
- int debug = 2; //1 - writing all data; 2 - only protection data
- int ac_protect = 0;
- int self_test = 1;
- int currentPage = -1; // current page of nextion
- int irLastCode = -1;
- int volStep = 2;
- int sourceS = 0;
- int devState = 1;
- String sReceived, nReceived;
- int ev, honap, nap, ora, perc, mperc, hetnapja; //These are where we store the RTC values.
- char daysOfWeek[7][12] = {
- "Sunday",
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday"
- };
- #if (1)
- const char* ssid = "TP-Link_F072";
- const char* password = "12778072";
- #else
- const char* ssid = "SirRouter";
- const char* password = "19801989";
- #endif
- class c_NextionWrite {
- public:
- void init(int speed, int RXN, int TXN) {
- Serial1.begin(speed, SERIAL_8N1, RXN, TXN);
- // if (debug) 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");
- if (debug == 1) Serial.println(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
- }
- void val(String Name, int value) {
- Serial1.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
- if (debug == 1) Serial.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
- }
- void systemVal(String Name, int value) {
- Serial1.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
- if (debug == 1) Serial.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
- }
- void pageChange(int Name) {
- Serial1.print("page " + String(Name) + "\xFF\xFF\xFF");
- if (debug == 1) Serial.print("page " + String(Name) + "\xFF\xFF\xFF");
- NextionPage = Name;
- }
- void setPco(String name, int pco) { // for global variable need a page number / page name too
- Serial1.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
- if (debug == 1) 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 dim(int en) {
- Serial1.print("dim=" + String(en) + "\xFF\xFF\xFF");
- }
- void touchSet(String name, int en) { //nextion.touchSet("preset", 0/1);
- Serial1.print("tsw " + String(name) + "," + String(en) + "\xFF\xFF\xFF");
- }
- void click(String name, int en) {
- Serial.print("click " + String(name) + "," + en + "\xFF\xFF\xFF");
- Serial1.print("click " + String(name) + "," + en + "\xFF\xFF\xFF");
- }
- void setTime(int ev, int honap, int nap, int ora, int perc, int mperc, int hetnapja) {
- //hour = (hour + 24) % 24;
- Serial1.print("rtc0=" + String(ev) + "\xFF\xFF\xFF"); //year
- Serial1.print("rtc1=" + String(honap) + "\xFF\xFF\xFF"); //month
- Serial1.print("rtc2=" + String(nap) + "\xFF\xFF\xFF"); //day
- Serial1.print("rtc3=" + String(ora) + "\xFF\xFF\xFF"); //hour
- Serial1.print("rtc4=" + String(perc) + "\xFF\xFF\xFF"); //minute
- Serial1.print("rtc5=" + String(mperc) + "\xFF\xFF\xFF"); //second
- // Serial1.print("rtc6=" + String(hetnapja) + "\xFF\xFF\xFF"); //days of the Week
- Serial.printf("Nextion time/ hour: %d min: %d sec: %d \n", ora, perc, mperc);
- Serial.println("--------------------");
- }
- };
- c_NextionWrite nextion;
- void initWiFi() { ////////////////////////////////// INIT WIFI
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- Serial.print("Connecting to WiFi ..");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print('.');
- delay(1000);
- }
- Serial.println(WiFi.localIP());
- }
- void readRTC() { ////////////////////////////////// DS3231 realtime clock module
- DateTime now = rtc.now();
- //Serial.print("ESP32 RTC Date Time: ");
- //Serial.print(now.year(), DEC);
- ev = (now.year(), DEC);
- //Serial.print('/');
- //Serial.print(now.month(), DEC);
- honap = (now.month(), DEC);
- //Serial.print('/');
- //Serial.print(now.day(), DEC);
- nap = (now.day(), DEC);
- //Serial.print(" (");
- //Serial.print(daysOfWeek[now.dayOfTheWeek()]);
- //Serial.print(") ");
- //Serial.print(now.hour(), DEC);
- ora = (now.hour(), DEC);
- //Serial.print(':');
- //Serial.print(now.minute(), DEC);
- perc = (now.minute(), DEC);
- //Serial.print(':');
- //Serial.println(now.second(), DEC);
- mperc = (now.second(), DEC);
- //delay(1000); // delay 1 seconds
- }
- void rotary_loop() { ////////////////////////////////// ENCODER LOOP
- if (rotaryEncoder.encoderChanged()) {
- if (debug == 1) Serial.print("Value: ");
- if (debug == 1) Serial.println(rotaryEncoder.readEncoder());
- uart.print("VOL:" + String(rotaryEncoder.readEncoder()) + ";");
- digiVolume = rotaryEncoder.readEncoder();
- }
- if (rotaryEncoder.isEncoderButtonClicked()) {
- rotary_onButtonClick();
- }
- }
- void rotary_onButtonClick() { ////////////////////////////////// ENCODER SW
- static unsigned long lastTimePressed = 0;
- //ignore multiple press in that time milliseconds
- if (millis() - lastTimePressed < 500) {
- return;
- }
- lastTimePressed = millis();
- if (debug == 1) Serial.print("button pressed ");
- if (debug == 1) Serial.print(millis());
- if (debug == 1) Serial.println(" milliseconds after restart");
- if (dmute == 0) {
- uart.print("MUT:1;");
- } else {
- uart.print("MUT:0;");
- }
- }
- void IRAM_ATTR readEncoderISR() { ////////////////////////////////// ENCODER INTERRUPT
- rotaryEncoder.readEncoder_ISR();
- }
- void IRAM_ATTR stateRight_ISR() { ////////////////////////////////// RIGHT INTERRUPT
- dcErrorRight = 1;
- }
- void IRAM_ATTR stateLeft_ISR() { ////////////////////////////////// LEFT INTERRUPT
- dcErrorLeft = 1;
- }
- void IRAM_ATTR stateAC_ISR() { ////////////////////////////////// AC INTERRUPT
- ac_time = millis();
- if (ac_time - ac_time_last > 250) {
- acError = 1;
- ac_protect = 1;
- digitalWrite(relay, 0);
- ac_time_last = ac_time;
- }
- }
- void meta(int t, int e, int v, int i) { ////////////////////////////////// DIGI METADATA
- nextion.vis("main.title", t);
- nextion.vis("main.elapsed", e);
- nextion.vis("main.vendor", v);
- nextion.vis("main.infoText", i);
- }
- void beep_2k() { ////////////////////////////////// 2KhZ BEEP
- unsigned long currentMillis = millis();
- if (currentMillis - beepMillis >= 1000) {
- if (beep == 0) {
- tone(12, 2000, 50);
- } else {
- tone(12, 2000, 50);
- }
- beepMillis = currentMillis;
- }
- }
- void beep_3k() { ////////////////////////////////// 3KhZ BEEP
- unsigned long currentMillis = millis();
- if (currentMillis - beepMillis >= 2000) {
- if (beep == 0) {
- tone(12, 3000, 1000);
- } else {
- tone(12, 3000, 1000);
- }
- beepMillis = currentMillis;
- }
- }
- void inputLed(int input) { ////////////////////////////////// LED FOR SOURCE
- source = input;
- int r = 0, g = 0, b = 0;
- switch (input) {
- case 0: // WiFi - net
- // if (powerState == 0) break;
- r = 59;
- g = 60;
- b = 61;
- break;
- case 1: // Bluetooth - bt
- // if (powerState == 0) break;
- r = 0;
- g = 0;
- b = 50;
- break;
- case 2: // Line-In - line_in
- // if (powerState == 0) break;
- r = 0;
- g = 50;
- b = 0;
- break;
- case 3: // SPDIF - opt
- // if (powerState == 0) break;
- r = 50;
- g = 0;
- b = 0;
- break;
- case 4: // USB-DAC - usb dac
- r = 12;
- g = 67;
- b = 5;
- break;
- case 5: //Standby
- 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.
- }
- void functionLed(int function) { ////////////////////////////////// LED FOR FUNCTIONS
- task = function;
- int r = 0, g = 0, b = 0;
- switch (function) {
- case 0: // task is "ON"
- if (powerState == 0) break;
- r = 49;
- g = 50;
- b = 51;
- break;
- case 1: // task is "OFF"
- if (powerState == 0) break;
- r = 0;
- g = 0;
- b = 50;
- break;
- case 2: // task is "DC ERROR"
- if (powerState == 0) break;
- r = 0;
- g = 50;
- b = 0;
- break;
- case 3: // task is "Temp ERROR"
- if (powerState == 0) break;
- r = 50;
- g = 0;
- b = 0;
- break;
- case 4: // task is "System starting..."
- 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.
- }
- void senseBrightness() { ////////////////////////////////// SENSE BRIGHTNESS
- unsigned long currentTime = millis();
- if (currentTime - previousTime_1 >= eventTime_1_LDR) {
- String m;
- switch (analogRead(LDR)) {
- case 0 ... 40:
- dimN = 25;
- m = "Dark";
- break;
- case 41 ... 800:
- dimN = 40;
- m = "Light";
- break;
- case 801 ... 2000:
- dimN = 60;
- m = "Bright";
- break;
- case 2001 ... 3200:
- dimN = 80;
- m = "Very Bright";
- break;
- case 3201 ... 4500:
- dimN = 100;
- m = "Very Very Bright";
- break;
- }
- if (dim != dimN) {
- nextion.val("ampSetup.nDimESP", dimN);
- nextion.val("ampSetup.hSlideESP", dimN);
- Serial.println(" => " + m);
- Serial.println(analogRead(LDR));
- dim = dimN;
- }
- previousTime_1 = currentTime;
- }
- }
- void senseTemp() { ////////////////////////////////// SENSE TEMPERATURE
- static unsigned long timer1, timer2;
- if (millis() - timer1 > sense1) { // firtst --> 1st
- timer1 = millis();
- //Serial.print("Temp 1: ");
- //Serial.println(tmp1.readTemperature('c'));
- nextion.val("ampSetup.nTempLeft", tmp1.readTemperature('c'));
- }
- if (millis() - timer2 > sense2) {
- timer2 = millis();
- //Serial.print("Temp 2: ");
- //Serial.println(tmp2.readTemperature('C'));
- nextion.val("ampSetup.nTempRight", tmp2.readTemperature('c'));
- }
- }
- void readProtection() { ////////////////////////////////// READ COMPLETE PROTECTION (AC+DC)
- dcErrorRight = digitalRead(dcSenseRight);
- dcErrorLeft = digitalRead(dcSenseLeft);
- acError = digitalRead(acSense);
- if (dcErrorRight != dcErrorRightLast) {
- if (dcErrorRight == 0) {
- if (debug == 2) Serial.println("Right channel is OK!");
- nextion.systemVal("warning", 0);
- nextion.val("ampSetup.vaRight", 0);
- if (dcErrorLeft == 0 && self_test == 1) nextion.val("boot.vaProtection", 1);
- }
- if (dcErrorRight == 1) {
- if (debug == 2) Serial.println("DC voltage on Right channel!");
- nextion.systemVal("warning", 1);
- nextion.val("ampSetup.vaRight", 1);
- if (self_test == 1) nextion.val("boot.vaProtection", 0);
- }
- dcErrorRightLast = dcErrorRight;
- }
- if (dcErrorLeft != dcErrorLeftLast) {
- if (dcErrorLeft == 0) {
- if (debug == 2) Serial.println("Left channel is OK!");
- nextion.systemVal("warning", 0);
- nextion.val("ampSetup.vaLeft", 0);
- if (dcErrorRight == 0 && self_test == 1) nextion.val("boot.vaProtection", 1);
- }
- if (dcErrorLeft == 1) {
- if (debug == 2) Serial.println("DC voltage on Left channel!");
- nextion.systemVal("warning", 1);
- nextion.val("ampSetup.vaLeft", 1);
- if (self_test == 1) nextion.val("boot.vaProtection", 0);
- }
- dcErrorLeftLast = dcErrorLeft;
- }
- if (acError != acErrorLast) {
- if (ac_protect == 1) {
- Serial.println("Interrupt on AC pin");
- }
- if (acError == 0) {
- if (debug == 2) Serial.println("AC is OK!");
- nextion.systemVal("warning", 0);
- nextion.val("ampSetup.vaAC", 0);
- nextion.val("boot.vaSupply", 1);
- }
- if (acError == 1) {
- if (debug == 2) Serial.println("Missing AC voltage!");
- nextion.systemVal("warning", 1);
- nextion.val("ampSetup.vaAC", 1);
- nextion.val("boot.vaSupply", 1); // -> 0
- ac_protect = 0;
- }
- acErrorLast = acError;
- }
- }
- void readAC() { ////////////////////////////////// READ POWER SUPPLY
- acError = digitalRead(acSense);
- if (acError != acErrorLast) {
- if (ac_protect == 1) {
- Serial.println("Interrupt on AC pin");
- }
- if (acError == 0) {
- if (debug == 2) Serial.println("AC is OK!");
- nextion.systemVal("warning", 0);
- nextion.val("ampSetup.vaAC", 0);
- nextion.val("boot.vaSupply", 1);
- }
- if (acError == 1) {
- if (debug == 2) Serial.println("Missing AC voltage!");
- nextion.systemVal("warning", 1);
- nextion.val("ampSetup.vaAC", 1);
- nextion.val("boot.vaSupply", 1); // -> 0
- ac_protect = 0;
- }
- acErrorLast = acError;
- }
- }
- void setup() { ////////////////////////////////// START OF SETUP
- Wire.begin(SDA, SCL);
- Serial.begin(115200);
- Serial1.begin(115200, SERIAL_8N1, RXN_PIN, TXN_PIN);
- uart.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
- while (!Serial1)
- ;
- while (!uart)
- ;
- rtc.begin();
- tmp1.begin();
- tmp2.begin();
- pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
- // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // When uploading the code, the computer's time settings are passed to the RTC. We REM it out the next time we upload.
- pinMode(relay, OUTPUT);
- delay(1);
- //digitalWrite(speaker, 0);
- digitalWrite(relay, 0);
- pinMode(powerSW, INPUT_PULLUP); // buttons is active LOW
- pinMode(dcSenseRight, INPUT_PULLUP);
- pinMode(dcSenseLeft, INPUT_PULLUP);
- pinMode(acSense, INPUT_PULLUP);
- rotaryEncoder.begin();
- rotaryEncoder.setup(readEncoderISR);
- //ir***********************************************************************************
- Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
- // Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
- IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
- Serial.print(F("Ready to receive IR signals of protocols: "));
- printActiveIRProtocols(&Serial);
- Serial.println(F("at pin " STR(IR_RECEIVE_PIN)));
- //ir***********************************************************************************
- inputLed(4);
- nextion.pageChange(boot);
- nextion.txt("boot.infoText", "System is starting...");
- nextion.val("boot.vaMCU", 1);
- delay(1000);
- nextion.val("boot.vaRTC", 1);
- delay(1000);
- nextion.val("boot.vaArylic", 0);
- bool circleValues = false;
- /*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);
- rotaryEncoder.setBoundaries(0, 100, circleValues); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
- attachInterrupt(dcSenseRight, stateRight_ISR, RISING); //Interrupts for protection: left: RISING,right: RISING,AC: FALLING
- attachInterrupt(dcSenseLeft, stateLeft_ISR, RISING);
- attachInterrupt(acSense, stateAC_ISR, FALLING);
- //initWiFi(); //in the kitchen, no WIFI :D
- //String LocalIP = String() + WiFi.localIP()[0] + "." + WiFi.localIP()[1] + "." + WiFi.localIP()[2] + "." + WiFi.localIP()[3];
- //Serial.println(WiFi.localIP());
- //Serial.println(LocalIP);
- if (debug) Serial.println("Starting..");
- if (debug == 1) Serial.println("Started...");
- if (debug == 1) Serial.println(compile_date);
- uart.print("PMT:1;");
- uart.print("BEP:0;");
- uart.print("BEP;");
- uart.print("PMT;");
- nextion.touchSet("main.preset", 0);
- dcErrorRight = digitalRead(dcSenseRight);
- dcErrorLeft = digitalRead(dcSenseLeft);
- acError = digitalRead(acSense);
- //if (debug == 2) Serial.printf("AC-%d DCL-%d DCR-%d\n", acError, dcErrorLeft, dcErrorRight);
- uart.print("SYS:REBOOT;"); //Reboot Digi
- Serial.println(compile_date);
- Serial.println("Arylic_0823");
- }
- void loop() { ////////////////////////////////// START OF LOOP
- readRTC();
- //nextion.setTime(ev, honap, nap, ora, perc, mperc, hetnapja);
- if (strengthMillis + 1000 < millis() && source == 0) {
- uart.print("WSS;");
- strengthMillis = millis();
- }
- if (strengthMillis + 1000 < millis() && source == 1) {
- uart.print("BSS;");
- strengthMillis = millis();
- }
- if (self_test == 0) {
- readProtection();
- senseBrightness();
- senseTemp();
- /*
- // devState == 0
- // devState == 1 -> initial tests (i2c devices, temp, RTC, AC, DC L + R -> DIGI), running self test
- - runing selftest -> Standby or Error page
- // devstate == 2 -> Standby
- - IR power on/off
- - Power SW
- // devState == 3 -> Running...
- - work DIGI all funkction, STA
- - encoder, IR control, Nextion control, protect check, temp, etc.
- // devstate == 4 -> Error mode -> page 2 !!! SPEAKERS & DIGI OFF !!!
- - check protection, temp, main power supply
- - power off?
- switch (devState){
- case 0:
- break;
- case 1:
- devState1
- .
- .
- break;
- case 2:
- }
- */
- if (IrReceiver.decode()) { //////////// IR REMOTE HANDLING
- Serial.println(NextionPage);
- IrReceiver.printIRResultShort(&Serial);
- IrReceiver.printIRSendUsage(&Serial);
- if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
- //Serial.println(F("Received noise or an unknown (or not yet enabled) protocol")); // We have an unknown protocol here, print more inf
- IrReceiver.printIRResultRawFormatted(&Serial, true);
- }
- Serial.println();
- IrReceiver.resume(); // Enable receiving of the next value
- if (IrReceiver.decodedIRData.command == 0x45 && (irMillis + 5000) < millis()) { // Power button && irLastCode != IrReceiver.decodedIRData.command
- if (NextionPage == 2) { //0
- nextion.click("powerOff", 1);
- }
- if (NextionPage == 1) { //2
- nextion.click("powerOn", 1);
- }
- irMillis = millis();
- Serial.println("Pressed: POWER");
- } else if (IrReceiver.decodedIRData.command == 0x19) { // BT button
- uart.print("SRC:BT;");
- Serial.println("Pressed: BT");
- } else if (IrReceiver.decodedIRData.command == 0xD) { // FM button
- //uart.print("SRC:BT;");
- Serial.println("Pressed: FM");
- } else if (IrReceiver.decodedIRData.command == 0x09 && irVolMillis + 300 < millis()) { // VolUp
- if (digiVolume < 100) {
- digiVolume = digiVolume + volStep;
- if (digiVolume > 100) digiVolume = 100;
- }
- irVolMillis = millis();
- Serial.println(digiVolume);
- uart.print("VOL:" + String(digiVolume) + ";");
- Serial.println("Pressed: VOL UP");
- } else if (IrReceiver.decodedIRData.command == 0x15 && irVolMillis + 300 < millis()) { // VolDown
- if (digiVolume > 0) {
- digiVolume = digiVolume - volStep;
- if (digiVolume < 0) digiVolume = 0;
- }
- irVolMillis = millis();
- Serial.println(digiVolume);
- uart.print("VOL:" + String(digiVolume) + ";");
- Serial.println("Pressed: VOL DOWN");
- } else if (IrReceiver.decodedIRData.command == 0x46 && (irMillis + 5000) < millis()) { // Input stepping
- if (sourceS < 4) {
- sourceS++;
- } else {
- sourceS = 0;
- }
- String inp[5] = { "NET", "BT", "LINE-IN", "USBDAC", "OPT" };
- uart.print("SRC:" + inp[sourceS] + ";");
- Serial.println("SRC:" + inp[sourceS] + ";");
- Serial.println(sourceS);
- irMillis = millis();
- Serial.println("Pressed: MODE (input stepping)");
- } else if (IrReceiver.decodedIRData.command == 0x7 && (irMillis + 5000) < millis()) { // Muting
- if (NextionPage == 0) {
- uart.print("MUT:1;");
- }
- if (NextionPage == 0) {
- uart.print("MUT:0;");
- }
- irMillis = millis();
- Serial.println("Pressed: MUTE");
- } else if (IrReceiver.decodedIRData.command == 0x47) { // Scan
- // uart.print("SRC:NET;");
- Serial.println("Pressed: SCAN");
- } else if (IrReceiver.decodedIRData.command == 0x43) { // >> / Next track, only available in BT/NET/USB mode (*1)
- uart.print("NXT;");
- Serial.println("Pressed: NEXT");
- } else if (IrReceiver.decodedIRData.command == 0x40) { // << / Previous track, only available in BT/NET/USB mode (*1)
- uart.print("PRV;");
- Serial.println("Pressed: PREV");
- } else if (IrReceiver.decodedIRData.command == 0x44) { // Play/Pause track, only available in BT/NET/USB mode (*1)
- uart.print("POP;");
- Serial.println("Pressed: PLAY/PAUSE");
- } else if (IrReceiver.decodedIRData.command == 0xC) { // 1
- uart.print("PST:1;");
- // digi.volume=digi.maxVolume-10;
- nextion.txt("main.preset", "Preset 1");
- Serial.println("Pressed: BT1");
- } else if (IrReceiver.decodedIRData.command == 0x18) { // 2
- uart.print("PST:2;");
- nextion.txt("main.preset", "Preset 2");
- Serial.println("Pressed: BT2");
- } else if (IrReceiver.decodedIRData.command == 0x5E) { // 3
- uart.print("PST:3;");
- nextion.txt("main.preset", "Preset 3");
- Serial.println("Pressed: BT3");
- } else if (IrReceiver.decodedIRData.command == 0x8) { // 4
- uart.print("PST:4;");
- nextion.txt("main.preset", "Preset 4");
- Serial.println("Pressed: BT4");
- } else if (IrReceiver.decodedIRData.command == 0x1C) { // 5
- uart.print("PST:5;");
- nextion.txt("main.preset", "Preset 5");
- Serial.println("Pressed: BT5");
- } else if (IrReceiver.decodedIRData.command == 0x5A) { // 6
- uart.print("PST:6;");
- nextion.txt("main.preset", "Preset 6");
- Serial.println("Pressed: BT6");
- } else if (IrReceiver.decodedIRData.command == 0x42) { // 7
- uart.print("PST:7;");
- nextion.txt("main.preset", "Preset 7");
- Serial.println("Pressed: BT7");
- } else if (IrReceiver.decodedIRData.command == 0x52) { // 8
- uart.print("PST:8;");
- nextion.txt("main.preset", "Preset 8");
- Serial.println("Pressed: BT8");
- } else if (IrReceiver.decodedIRData.command == 0x4A) { // 9
- uart.print("PST:9;");
- nextion.txt("main.preset", "Preset 9");
- Serial.println("Pressed: BT9");
- } else if (IrReceiver.decodedIRData.command == 0x16) { // 0
- uart.print("PST:10;");
- nextion.txt("main.preset", "Preset 10");
- Serial.println("Pressed: BT0");
- }
- }
- if (digitalRead(powerSW) == 0 && lastPowerState == 1 && lastPressed + 5000 < millis()) { //////////// POWER SW HANDLING
- if (debug == 1) Serial.println(digitalRead(powerSW));
- if (powerState == 0) {
- powerState = 1;
- uart.print("SYS:REBOOT;");
- Serial.println("---------- Digi REBOOT... ----------");
- tone(12, 1000, 50);
- } else {
- powerState = 0;
- uart.print("SYS:STANDBY;");
- Serial.println("---------- Digi STANDBY... ----------");
- tone(12, 1000, 50);
- inputLed(4);
- }
- lastPressed = millis();
- }
- lastPowerState = digitalRead(powerSW); //1
- rotary_loop();
- while (uart.available()) { //////////// UART HANDLING FOR ARYLIC
- nextion.val("boot.vaArylic", 1);
- sReceived = uart.readStringUntil('\n');
- sReceived.trim();
- //Serial.println(sReceived);
- if (debug == 1) Serial.println("uart:___|----------" + sReceived);
- if (debug == 1) Serial.println("");
- if (sReceived.startsWith("PLA:0")) {
- nextion.txt("main.infoText", "Szünet/Megállítva");
- } else if (sReceived.startsWith("PLA:1")) {
- nextion.txt("main.infoText", "Lejátszás...");
- } else if (sReceived.startsWith("STA:")) { //////////// STA HANDLING
- if (initDigi == 1) {
- initDigi = 0;
- nextion.val("boot.vaArylic", 1);
- } else if (NextionPage != 6) {
- nextion.pageChange(main);
- }
- nextion.vis("main.infoText", 0);
- if (debug == 1) Serial.println("STA received");
- if (debug == 1) Serial.println("Before: " + sReceived);
- nextion.txt("main.infoText", "");
- nextion.vis("main.title", 0);
- nextion.vis("main.elapsed", 0);
- nextion.vis("main.vendor", 0);
- nextion.touchSet("main.preset", 0);
- reduce4();
- if (sReceived.startsWith("NET")) { //////////// WIFI
- sourceS = 0;
- inputLed(0);
- meta(1, 1, 1, 1);
- if (debug == 1) Serial.println("...WIFI...");
- nextion.txt("main.input", "WiFi");
- } else if (sReceived.startsWith("BT")) { //////////// BLUETOOTH
- sourceS = 1;
- inputLed(1);
- meta(0, 0, 0, 0);
- if (debug == 1) Serial.println("...BLUETOOTH...");
- nextion.txt("main.input", "Bluetooth");
- } else if (sReceived.startsWith("LINE-IN")) { //////////// LINE-IN
- sourceS = 2;
- inputLed(2);
- meta(0, 0, 0, 0);
- if (debug == 1) Serial.println("...LINE-IN...");
- nextion.txt("main.input", "Line In");
- } else if (sReceived.startsWith("USBDAC")) { //////////// USB DAC
- sourceS = 3;
- inputLed(3);
- meta(0, 0, 0, 0);
- if (debug == 1) Serial.println("...USB DAC...");
- nextion.txt("main.input", "USB DAC");
- } else if (sReceived.startsWith("OPT")) { //////////// USB DAC
- sourceS = 4;
- inputLed(4);
- meta(0, 0, 0, 0);
- if (debug == 1) Serial.println("...SPDIF...");
- nextion.txt("main.input", "SPDIF");
- }
- } else if (sReceived.startsWith("SYS:STANDBY")) { //////////// POWER OFF HANDLING
- inputLed(4);
- Serial.println("Stand by mode...");
- tone(12, 1000, 50);
- nextion.pageChange(standby);
- digitalWrite(relay, 0);
- nextionSetTimebit = 1;
- } else if (sReceived.startsWith("SRC:")) { //////////// INPUT SOURCE HANDLING
- nextion.vis("main.title", 0);
- nextion.vis("main.elapsed", 0);
- nextion.vis("main.vendor", 0);
- nextion.touchSet("main.preset", 0);
- nextion.pageChange(main);
- reduce4();
- if (sReceived.startsWith("NET")) { //////////// WIFI
- sourceS = 0;
- inputLed(0);
- meta(1, 1, 1, 1);
- if (debug == 1) Serial.println("...WIFI...");
- nextion.txt("main.input", "WiFi");
- } else if (sReceived.startsWith("BT")) { //////////// BLUETOOTH
- sourceS = 1;
- inputLed(1);
- meta(0, 0, 0, 0);
- if (debug == 1) Serial.println("...BLUETOOTH...");
- nextion.txt("main.input", "Bluetooth");
- } else if (sReceived.startsWith("LINE-IN")) { //////////// LINE-IN
- sourceS = 2;
- inputLed(2);
- meta(0, 0, 0, 0);
- if (debug == 1) Serial.println("...LINE-IN...");
- nextion.txt("main.input", "Line In");
- } else if (sReceived.startsWith("USBDAC")) { //////////// USB DAC
- sourceS = 3;
- inputLed(3);
- meta(0, 0, 0, 0);
- if (debug == 1) Serial.println("...USB DAC...");
- nextion.txt("main.input", "USB DAC");
- } else if (sReceived.startsWith("OPT")) { //////////// USB DAC
- sourceS = 4;
- inputLed(4);
- meta(0, 0, 0, 0);
- if (debug == 1) Serial.println("...SPDIF...");
- nextion.txt("main.input", "SPDIF");
- }
- } else if (sReceived.startsWith("VOL:")) { //////////// VOLUME HANDLING
- 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 {
- if (debug == 1) Serial.println("volume: -|:" + sReceived);
- digiVolume = sReceived.toInt();
- nextion.txt("volText", sReceived);
- }
- nextion.systemVal("digiVol", digiVolume);
- rotaryEncoder.setEncoderValue(digiVolume);
- } else if (sReceived.startsWith("MUT:")) { //////////// MUTE HANDLING
- reduce4();
- sReceived = sReceived.substring(0, 1);
- if (debug == 1) Serial.println("Mute:_____/:|" + sReceived);
- if (sReceived == "1") {
- dmute = 1;
- nextion.txt("volText", "MIN");
- nextion.systemVal("digiVol", 0);
- } else if (sReceived == "0") {
- dmute = 0;
- nextion.txt("volText", String(digiVolume));
- nextion.systemVal("digiVol", digiVolume);
- }
- } else if (sReceived.startsWith("BTC:")) { //////////// BLUETOOTH CONNECTION HANDLING
- reduce4();
- sReceived = sReceived.substring(0, 1);
- if (sReceived == "1") {
- nextion.txt("main.infoText", "CONNECTED");
- uart.print("TIT;");
- } else if (sReceived == "0") {
- nextion.txt("main.infoText", "DISCONNECTED");
- }
- } else if (sReceived.endsWith("SYS:ON;")) { //////////// POWER ON HANDLING
- tone(12, 2000, 50);
- nextion.txt("standby.powerOn", "STARTING");
- nextion.setPco("standby.powerOn", 34784);
- Serial.println("arrived SYS:ON...(1)");
- } else if (sReceived.startsWith("NET:")) { //////////// NETWORK CONNECTION HANDLING
- reduce4();
- sReceived = sReceived.substring(0, 1);
- if (sReceived == "1") {
- nextion.txt("main.infoText", "CONNECTED");
- inputLed(0);
- uart.print("TIT;");
- nextion.touchSet("main.preset", 1);
- //inputLed(0);
- } else if (sReceived == "0") {
- nextion.txt("main.infoText", "DISCONNECTED");
- nextion.touchSet("main.preset", 0);
- inputLed(5);
- }
- } else if (sReceived.startsWith("TIT:")) { //////////// Title
- reduce4();
- if (debug == 1) Serial.println("Title: " + sReceived);
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- nextion.txt("main.title", sReceived);
- } else if (sReceived.startsWith("ELP:")) { //////////// Elapsed playing time
- 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);
- if (time > 0) nextion.txt("main.elapsed", timeS);
- } else if (sReceived.startsWith("BAS:")) { //////////// BASS
- reduce4();
- int bass = sReceived.toInt();
- nextion.val("digiSetup.nbass", bass);
- if (bass < 0) {
- bass = 11 - abs(bass);
- } else {
- bass = bass + 11;
- }
- nextion.val("digiSetup.hbass", bass);
- } else if (sReceived.startsWith("TRE:")) { //////////// TREBLE
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- int treb = sReceived.toInt();
- nextion.val("digiSetup.ntreb", treb);
- if (treb < 0) {
- treb = 11 - abs(treb);
- } else {
- treb = treb + 11;
- }
- nextion.val("digiSetup.htreb", treb);
- } else if (sReceived.startsWith("WSS:")) { //////////// WIFI SIGNAL STRENGTH
- reduce4();
- //nextion.txt("main.infoText", sReceived);
- Serial.println("WiFi signal strength: " + sReceived);
- } else if (sReceived.startsWith("BSS:")) { //////////// BLUETOOTH SIGNAL STRENGTH
- reduce4();
- //nextion.txt("main.infoText", sReceived);
- Serial.println("Bluetooth signal strength: " + sReceived);
- } else if (sReceived.startsWith("MXV:")) { //////////// Max volume, %
- reduce4();
- int volMax = sReceived.toInt();
- nextion.val("digiSetup.nVolMax", volMax);
- nextion.val("digiSetup.hVolMax", volMax);
- if (debug == 2) Serial.println(volMax);
- } else if (sReceived.startsWith("VBS:")) { //////////// Virtual Bass ON/OFF
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- if (sReceived == "1") {
- nextion.val("digiSetup.vbs", 1);
- if (debug == 2) Serial.println("VBS: on");
- } else {
- nextion.val("digiSetup.vbs", 0);
- if (debug == 2) Serial.println("VBS: off");
- }
- } else if (sReceived.startsWith("PMT:")) { //////////// Promt Voice ON/OFF
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- if (sReceived == "1") {
- nextion.val("digiSetup.pmt", 1);
- if (debug == 2) Serial.println("PMT: on");
- } else {
- nextion.val("digiSetup.pmt", 0);
- if (debug == 2) Serial.println("PMT: off");
- }
- } else if (sReceived.startsWith("NAM:")) { //////////// Device name
- 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();
- // if (debug) 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;
- // if (debug) Serial.printf("%i. sz=%i\n", i, dsz);
- Nname += char(dsz);
- dsz = 0;
- } else {
- dsz = sz * 16;
- }
- h = 16 - h;
- }
- if (debug == 1) Serial.println(Nname);
- nextion.txt("main.NAME", Nname);
- }
- } else if (sReceived.startsWith("IPA:")) { //////////// Device IP address for connecting to browser's control panel, psw:admin
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1); //removing ";"
- nextion.txt("digiSetup.digiIP", sReceived);
- } else if (sReceived.startsWith("VND:")) { //////////// Vendor - Tidal, Spotify, etc...
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1); //removing ";"
- nextion.txt("main.vaVendor", sReceived);
- } else if (sReceived.startsWith("PST:")) { //////////// Preset, not response, only command (1-10)
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- if (debug == 1) Serial.println(";;;;;;;;;; " + sReceived);
- }
- sReceived = "";
- }
- while (Serial1.available()) { //////////// UART HANDLING FOR NEXTION
- String nReceived = Serial1.readStringUntil(';');
- if (nReceived.startsWith("VST:")) { //////////// VST HANDLING
- String nTemp = nReceived.substring(4); //////////// VST HANDLING
- volStep = nTemp.toInt();
- Serial.println("VST: " + nReceived);
- }
- if (nReceived.startsWith("NPG:")) { //////////// NEXTION PAGE NUMBER
- reduce4n();
- Serial.println(nReceived + " ");
- toDigi = 0;
- }
- if (nReceived == "SYS:STANDBY") { //////////// STANDBY COMMAND
- digitalWrite(relay, 0);
- delay(powerOffDelayForRelay);
- inputLed(4);
- powerState = 0;
- }
- if (nReceived == "LDR:0") { //////////// LDR OFF
- toDigi = 0;
- if (debug == 2) Serial.println("LDR OFF");
- }
- if (nReceived == "LDR:1") { //////////// LDR ON
- toDigi = 0;
- if (debug == 2) Serial.println("LDR ON");
- }
- if (nReceived == "ESP:RESTART") { //////////// FULL RESTART
- ESP.restart();
- }
- if (debug == 1) Serial.println("++++++++++Serial1:__|" + nReceived + ";");
- if (toDigi == 1) uart.print(nReceived + ";");
- toDigi = 1;
- }
- if (NextionPage == 0)
- powerState = 1;
- } else {
- readProtection();
- if (acError == 1 && dcErrorRight == 0 && dcErrorLeft == 0) { //AC ERROR =0
- self_test = 0;
- initDigi = 1;
- uart.print("SYS:REBOOT;");
- digitalWrite(relay, 1);
- }
- Serial.printf("Selftest %d\n", self_test);
- }
- } //////////////////// End of Loop ////////////////////
- void reduce4() {
- sReceived = sReceived.substring(4);
- }
- void reduce4n() {
- nReceived = nReceived.substring(4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement