Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int stacount = 0;
- 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 <ErriezDS3231.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 13
- #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 pwr 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
- #define DATE_STRING_SHORT 3
- #define ONs 1
- #define OFFs 0
- //RTC_DS3231 rtc;
- ErriezDS3231 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);
- // Month names in flash
- const char monthNames_P[] PROGMEM = "JanFebMarAprMayJunJulAugSepOctNovDec";
- // Day of the week names in flash
- const char dayNames_P[] PROGMEM = "SunMonTueWedThuFriSat";
- 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, lastMillis = 0;
- unsigned long ac_time = 0, ac_time_last = 0;
- boolean powerState = 0, lastPowerState = 0, nextionSetTimebit = 1;
- int offset = 2, beep = 0, NextionPage = 0;
- int digiVolume = 0, dmute = 0, lastPressed = 0, source = 0, dim = 0, dimN = -1, task = 0;
- int initDigi = 1, toDigi = 1;
- int dcErrorRight = 1, dcErrorRightLast = -1, dcErrorLeft = 1, dcErrorLeftLast = -1, acError = 1, acErrorLast = -1; //variables for protection
- int debug = 1; //1 - writing all data; 2 - only protection data
- int ac_protect = 0;
- int self_test = 0;
- int currentPage = -1; // current page of nextion
- int irLastCode = -1;
- int volStep = 2;
- int sourceS = 0;
- int devState = 1;
- int speaker1timeon;
- unsigned long speakerMillis;
- String sReceived, nReceived;
- int ev, honap, nap, ora, perc, mperc, hetnapja; //These are where we store the RTC values.
- int speakerRelayEnable, speakerRelaySet, pwrRelayEnable, pwrRelaySet;
- int protectionErrors;
- void readProtection();
- void i2cCheck();
- #if (1)
- const char* ssid = "TP-Link_F072";
- const char* password = "12778072";
- #else
- const char* ssid = "SirRouter";
- const char* password = "19801989";
- #endif
- struct s_i2cdev {
- String name;
- int address;
- int state;
- String stateStr;
- };
- s_i2cdev tc74_1;
- s_i2cdev tc74_2;
- s_i2cdev rtcModule;
- s_i2cdev rtcEEPROM;
- s_i2cdev dspModule;
- s_i2cdev dspEEPROM;
- class c_RelayClass {
- public:
- int speakerEnable;
- int powerEnable;
- // CLASS
- void spk(int set) { // send SPK relay state
- if (speakerEnable == 1) {
- Serial.print("Speakers is: " + set);
- if (set) {
- Serial.println(" ON");
- } else {
- Serial.println("OFF");
- }
- digitalWrite(speaker, set);
- } else {
- digitalWrite(speaker, LOW);
- }
- }
- void power(int set) { // send SPK pwr state
- if (powerEnable == 1) {
- Serial.print("Main relay is: " + set);
- if (set) {
- Serial.println(" ON");
- } else {
- Serial.println("OFF");
- }
- digitalWrite(pwr, set);
- } else {
- digitalWrite(pwr, LOW);
- }
- }
- } relays;
- 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");
- //Serial.println("EPG");
- 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;
- class c_power {
- public:
- int state = 0;
- int lastState;
- void on() {
- state = ONs;
- relays.power(ONs);
- while (protectionErrors) {
- readProtection();
- }
- relays.spk(ONs);
- nextion.pageChange(main);
- }
- int firstInitial() {
- nextion.val("vaMCU", 0);
- nextion.val("boot.vaSupply", 0);
- nextion.val("boot.vaProtection", 0);
- nextion.val("vaRTC", 0);
- nextion.val("vaArylic", 0);
- if (debug == 3) {
- Serial.printf("Initial check\n");
- }
- //-----
- while (state == 0) { //Nextion <-> ESP32 communication check
- if ((lastMillis + 1500) < millis()) {
- nextion.val("vaMCU", 23);
- lastMillis = millis();
- }
- if (Serial1.available() > 0) {
- nReceived = Serial1.readStringUntil(';');
- Serial.println(nReceived);
- }
- if (nReceived.endsWith("42")) {
- nextion.val("vaMCU", 1);
- state = 1;
- //exit; //break;
- }
- Serial.println(nReceived);
- Serial.printf("State: %i\n", state);
- }
- Serial.printf("Nextion Communication test done. State: %i\n", state);
- //------
- while (state == 1) { //I2C check
- //to be written later
- i2cCheck();
- nextion.val("vaRTC", 1);
- state = 2;
- Serial.printf("State: %i\n", state);
- }
- Serial.printf("RTC check done. State: %i\n", state);
- lastMillis = millis();
- relays.powerEnable = 1; //AC relay ON (Enabled)
- relays.speakerEnable=0;
- relays.power(ONs);
- delay(1000);
- //-----
- do {
- readProtection();
- Serial.println("ReadProtection");
- } while (protectionErrors);
- nextion.val("boot.vaSupply", 1);
- nextion.val("boot.vaProtection", 1);
- Serial.printf("Protection check done. State: %i\n", state);
- int Arylic = 0;
- Serial.printf("State: %i\n", state);
- uart.print("SYS:REBOOT;");
- while (state == 2) {
- Serial.println("Reboot sent");
- do { //
- if (uart.available() > 0) {
- sReceived = uart.readStringUntil(';');
- Serial.print("rrrrrrrrrrr ");
- Serial.println(sReceived);
- if (sReceived.startsWith("MUT:")) { //aud
- Serial.println("In the loop");
- nextion.val("vaArylic", 1);
- delay(1000);
- state = 3;
- }
- sReceived = "";
- }
- } while (state == 2);
- // uart.print("SYS:STANDBY;");
- }
- if (debug == 3) {
- Serial.printf("All check done. State: %i\n", state);
- }
- Serial.println(state);
- // If all ok, then automatic page change: "Standby"
- return 1;
- }
- } power;
- void reduce4n() {
- nReceived = nReceived.substring(4);
- Serial.println("-----reduce4n----");
- }
- 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
- char name[DATE_STRING_SHORT + 1];
- uint8_t hour;
- uint8_t min;
- uint8_t sec;
- uint8_t mday;
- uint8_t mon;
- uint16_t year;
- uint8_t wday;
- // Read date/time
- if (!rtc.getDateTime(&hour, &min, &sec, &mday, &mon, &year, &wday)) {
- Serial.println(F("Read date/time failed"));
- return;
- }
- // Print day week
- strncpy_P(name, &(dayNames_P[wday * DATE_STRING_SHORT]), DATE_STRING_SHORT);
- name[DATE_STRING_SHORT] = '\0';
- // Serial.print(name);
- // Serial.print(F(" "));
- // Print month
- strncpy_P(name, &(monthNames_P[(mon - 1) * DATE_STRING_SHORT]), DATE_STRING_SHORT);
- name[DATE_STRING_SHORT] = '\0';
- // Serial.print(name);
- // Serial.print(F(" "));
- // Print day month
- // Serial.print(mday);
- // Serial.print(F(" "));
- // Print time
- // Serial.print(hour);
- Serial1.print("rtc3=" + String(hour) + "\xFF\xFF\xFF"); //hour
- // Serial.print(F(":"));
- // if (min < 10) {
- // Serial.print(F("0"));
- // }
- // Serial.print(min);
- Serial1.print("rtc4=" + String(min) + "\xFF\xFF\xFF"); //minute
- // Serial.print(F(":"));
- // if (sec < 10) {
- // Serial.print(F("0"));
- // }
- // Serial.print(sec);
- Serial1.print("rtc5=" + String(sec) + "\xFF\xFF\xFF"); //second
- // Serial.print(F(" "));
- // Print year
- // Serial.println(year);
- // Wait a second
- //delay(1000);
- }
- void rotary_loop() { ////////////////////////////////// ENCODER LOOP
- // int tempVol;
- // if(rotaryEncoder.encoderChanged()){
- // tempVol=rotaryEncoder.readEncoder();
- // Serial.print("-");
- // }
- if (rotaryEncoder.encoderChanged()) {
- if (debug == 1) Serial.print("Value: ");
- if (debug == 1) Serial.println(rotaryEncoder.readEncoder());
- if (NextionPage == main) {
- uart.print("VOL:" + String(rotaryEncoder.readEncoder()) + ";");
- Serial.println(rotaryEncoder.readEncoder());
- digiVolume = rotaryEncoder.readEncoder();
- }
- int temp1 = 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(pwr, LOW);
- 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;
- case 6: //NET connect
- r = 59;
- g = 60;
- b = 61;
- break;
- case 7: //NET disconnect
- r = 29;
- g = 30;
- b = 31;
- break;
- case 8: //BT connect
- r = 0;
- g = 0;
- b = 50;
- break;
- case 9: //BT disconnect
- r = 0;
- g = 0;
- b = 10;
- 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'));
- if (NextionPage == ampSetup) nextion.val("ampSetup.nTempLeft", tmp1.readTemperature('c'));
- }
- if (millis() - timer2 > sense2) {
- timer2 = millis();
- //Serial.print("Temp 2: ");
- //Serial.println(tmp2.readTemperature('C'));
- if (NextionPage == ampSetup) nextion.val("ampSetup.nTempRight", tmp2.readTemperature('c'));
- }
- }
- void readProtection() { ////////////////////////////////// READ COMPLETE PROTECTION (AC+DC)
- acError = !digitalRead(acSense);
- dcErrorRight = digitalRead(dcSenseRight);
- dcErrorLeft = digitalRead(dcSenseLeft);
- protectionErrors = dcErrorRight + dcErrorLeft + acError;
- 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 (dcErrorLeft == 1 || dcErrorRight == 1) {
- digitalWrite(speaker, LOW);
- //AC relay OFF
- nextion.vis("ampSetup.reset", 1);
- Serial.println("DC voltage on output, speakers is OFF");
- }
- 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 readSignal() {
- if (strengthMillis + 1000 < millis() && source == 0) {
- uart.print("WSS;");
- strengthMillis = millis();
- }
- if (strengthMillis + 1000 < millis() && source == 1) {
- uart.print("BSS;");
- strengthMillis = millis();
- }
- }
- int i2cSend(int address) {
- Wire.beginTransmission(address);
- return Wire.endTransmission();
- }
- int numDevices = 6;
- void i2cCheck() {
- numDevices = 6;
- tc74_1.state = i2cSend(tc74_1.address);
- numDevices--;
- (tc74_1.state == 0) ? tc74_1.stateStr = "OK" : tc74_1.stateStr = "FAIL";
- Serial.printf("%s State: %s I2C state: %i\n", tc74_1.name, tc74_1.stateStr, tc74_1.state);
- //------------------------------
- tc74_2.state = i2cSend(tc74_2.address);
- numDevices--;
- (tc74_2.state == 0) ? tc74_2.stateStr = "OK" : tc74_2.stateStr = "FAIL";
- Serial.printf("%s State: %s\n", tc74_1.name, tc74_1.stateStr);
- //------------------------------
- rtcModule.state = i2cSend(rtcModule.address);
- numDevices--;
- (rtcModule.state == 0) ? rtcModule.stateStr = "OK" : rtcModule.stateStr = "FAIL";
- Serial.printf("%s State: %s\n", tc74_2.name, tc74_2.stateStr);
- //------------------------------
- rtcEEPROM.state = i2cSend(rtcEEPROM.address);
- numDevices--;
- (rtcEEPROM.state == 0) ? rtcEEPROM.stateStr = "OK" : rtcEEPROM.stateStr = "FAIL";
- Serial.printf("%s State: %s\n", rtcEEPROM.name, rtcEEPROM.stateStr);
- //------------------------------
- dspModule.state = i2cSend(dspModule.address);
- numDevices--;
- (dspModule.state == 0) ? dspModule.stateStr = "OK" : dspModule.stateStr = "FAIL";
- Serial.printf("%s State: %s\n", dspModule.name, dspModule.stateStr);
- //------------------------------
- dspEEPROM.state = i2cSend(dspModule.address);
- numDevices--;
- (dspEEPROM.state == 0) ? dspEEPROM.stateStr = "OK" : dspEEPROM.stateStr = "FAIL";
- Serial.printf("%s State: %s\n", dspEEPROM.name, dspEEPROM.stateStr);
- if (numDevices == 0) {
- //Serial.println("I2C devices OK");
- }
- }
- 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)
- //clock.setDateTime(__DATE__, __TIME__);
- //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.
- //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- tc74_1 = { "TempL", 0x4A, 0, "FAIL" }; //TC74 LEFT
- tc74_2 = { "TempR", 0X4B, 0, "FAIL" }; //TC74 RIGHT
- rtcModule = { "RTC", 0x68, 0, "FAIL" };
- rtcEEPROM = { "RTC_EEPROM", 0x57, 0, "FAIL" };
- dspModule = { "DSP_Module", 0x34, 0, "FAIL" };
- dspEEPROM = { "DSP_EEPROM", 0x50, 0, "FAIL" };
- pinMode(pwr, OUTPUT);
- digitalWrite(pwr, LOW);
- pinMode(speaker, OUTPUT);
- digitalWrite(speaker, LOW);
- 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...");
- 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:STANDBY;"); //Reboot Digi
- Serial.println("Digi Off");
- delay(5000);
- Serial.println(compile_date);
- Serial.println("Arylic_0823");
- readRTC();
- digitalWrite(pwr, HIGH);
- power.firstInitial();
- }
- void loop() { ////////////////////////////////// START OF LOOP
- // while (NextionPage == boot) {
- // // reading nextion
- // // powerSW
- // // IR
- // }
- // while (NextionPage == standby) {
- // // reading nextion
- // // powerSW
- // // IR
- // }
- // while (NextionPage == main) {
- // // reading nextion
- // // reading digi
- // // reading dsp
- // // check protection
- // // powerSW
- // // IR
- // }
- if (NextionPage == main && speaker1timeon == 1 && millis() > speakerMillis) {
- digitalWrite(speaker, HIGH);
- speaker1timeon = 0;
- }
- // if (dcErrorLeft == 0 && dcErrorRight == 0) {
- // // relays.spk(ON);
- // relays.spk(ON);
- // }
- // speaker1timeon = 0;
- // }
- readSignal();
- if (self_test == 0) {
- readProtection();
- senseBrightness();
- senseTemp();
- rotary_loop();
- 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);
- // tone(23, 1000, 50);
- }
- if (NextionPage == 1) { //2
- nextion.click("powerOn", 1);
- readRTC();
- // tone(23, 1000, 50);
- }
- 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 == main) {
- uart.print("MUT:1;");
- }
- if (NextionPage == main) {
- 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("PRE;");
- 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 digitalRead(powerSW) == 0 &&
- if (debug == 1) Serial.println(digitalRead(powerSW));
- if (powerState == 0 && NextionPage == 1) {
- powerState = 1;
- uart.print("SYS:REBOOT;");
- Serial.println("---------- Power ON with button... ----------");
- // tone(12, 1000, 50);
- } else {
- powerState = 0;
- //if (NextionPage == 2)
- uart.print("SYS:STANDBY;");
- Serial.println("---------- Power OFF with button... ----------");
- // tone(12, 1000, 50);
- inputLed(4);
- }
- lastPressed = millis();
- }
- lastPowerState = digitalRead(powerSW); //1
- while (uart.available()) { //////////// UART HANDLING FOR ARYLIC
- if (NextionPage == boot) nextion.val("boot.vaArylic", 1);
- sReceived = uart.readStringUntil('\n');
- sReceived.trim();
- 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
- Serial.print("STA: ");
- Serial.println(stacount);
- stacount++;
- if (initDigi == 1) {
- Serial.println("STA, InitDigi1");
- initDigi = 0; //25.01.10
- nextion.val("boot.vaArylic", 1);
- } else if (NextionPage != main) {
- Serial.println("STA, InitDigi0");
- //nextion.pageChange(main);
- Serial.println("Page changed...");
- speakerMillis = millis() + 5000;
- }
- 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
- initDigi = 0; //25.01.10
- digitalWrite(pwr, LOW);
- digitalWrite(speaker, LOW);
- //
- nextion.pageChange(standby);
- //
- inputLed(4);
- Serial.println("POWER OFF!!");
- // // tone(23, 1000, 50);
- } 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:") && (NextionPage == main)) { //////////// 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;");
- inputLed(8);
- } else if (sReceived == "0") {
- //nextion.txt("main.infoText", "DISCONNECTED");
- inputLed(9);
- }
- } else if (sReceived.endsWith("SYS:ON;") && (NextionPage == standby)) { //////////// POWER ON HANDLING //25.01.10
- // tone(23, 2000, 50);
- nextion.txt("standby.powerOn", "STARTING");
- nextion.setPco("standby.powerOn", 34784);
- Serial.println("arrived SYS:ON...(1)");
- digitalWrite(pwr, HIGH);
- nextion.pageChange(main);
- // if (NextionPage == main) {
- // digitalWrite(speaker, HIGH);
- // }
- speaker1timeon = 1;
- } else if (sReceived.startsWith("NET:")) { //////////// NETWORK CONNECTION HANDLING
- reduce4();
- sReceived = sReceived.substring(0, 1);
- if (sReceived == "1") {
- //nextion.txt("main.infoText", "CONNECTED");
- inputLed(6);
- 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(7);
- }
- } else if (sReceived.startsWith("TIT:") && (NextionPage == main)) { //////////// 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("ART:") && (NextionPage == main)) { //////////// Artist
- reduce4();
- if (debug == 1) Serial.println("Artist: " + sReceived);
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- nextion.txt("main.artist", 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:") && (NextionPage == digiSetup)) { //////////// WIFI SIGNAL STRENGTH
- reduce4();
- int signalWifi = sReceived.toInt();
- Serial.println("WiFi signal strength: " + sReceived);
- nextion.val("digiSetup.nWifi", signalWifi);
- } else if (sReceived.startsWith("BSS:") && (NextionPage == digiSetup)) { //////////// BLUETOOTH SIGNAL STRENGTH
- reduce4();
- int signalBT = sReceived.toInt();
- Serial.println("Bluetooth signal strength: " + sReceived);
- nextion.val("digiSetup.nBt", signalBT);
- } 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);
- } else if (sReceived.startsWith("VST:")) {
- reduce4();
- String nTemp = sReceived.substring(4); //////////// VST HANDLING
- volStep = sReceived.toInt();
- nextion.val("digiSetup.nVolStep", volStep);
- nextion.val("digiSetup.hVolStep", volStep);
- Serial.println("VST: " + sReceived);
- }
- sReceived = "";
- }
- while (Serial1.available()) { //////////// UART HANDLING FOR NEXTION
- nReceived = Serial1.readStringUntil(';');
- // Serial.println(nReceived);
- if (nReceived.startsWith("NPG:")) { //////////// NEXTION PAGE NUMBER
- reduce4n();
- // nReceived = nReceived.substring(4);
- Serial.println(nReceived + " ");
- Serial.print("Nextion PageChange to: ");
- Serial.println(nReceived.toInt());
- NextionPage = nReceived.toInt();
- toDigi = 0;
- }
- if (nReceived == "SYS:STANDBY") { //////////// STANDBY COMMAND
- Serial.println("StandBy for the Nextion...");
- digitalWrite(pwr, LOW);
- digitalWrite(speaker, LOW);
- //delay(powerOffDelayForRelay);
- inputLed(4);
- // tone(23, 1000, 50);
- 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 (nReceived == "ESP:SPKON") { //////////// SPEAKER RESTART
- digitalWrite(speaker, HIGH);
- nextion.systemVal("warning", 0);
- nextion.vis("ampSetup.resetSPK", 0);
- }
- if (debug == 1) Serial.println("++++++++++Serial1:__|" + nReceived + ";");
- if (toDigi == 1) uart.print(nReceived + ";");
- toDigi = 1;
- }
- if (NextionPage == 0) powerState = 1;
- } else {
- readProtection();
- while (uart.available()) { //////////// UART HANDLING FOR ARYLIC
- sReceived = uart.readStringUntil('\n');
- sReceived.trim();
- //reduce4();
- Serial.println(sReceived);
- Serial.println("bakkfitty");
- if (sReceived.startsWith("STA:")) {
- nextion.val("boot.vaArylic", 1);
- Serial.println("-----------------------------------------------------------------------------------");
- if (acError == 0 && dcErrorRight == 0 && dcErrorLeft == 0) { //AC ERROR =0
- self_test = 0;
- //delay(5000);
- Serial.printf("Selftest %d\n", self_test);
- }
- // initDigi = 1;
- //uart.print("SYS:REBOOT;");
- // digitalWrite(pwr, HIGH);
- // Serial.println("PSU is ON!");
- }
- }
- sReceived = "";
- }
- } //////////////////// End of Loop ////////////////////
- void reduce4() {
- sReceived = sReceived.substring(4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement