diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 7d668b8cf..36092c780 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -206,6 +206,10 @@ void setRealtimePixel(uint16_t i, byte r, byte g, byte b, byte w); void refreshNodeList(); void sendSysInfoUDP(); +//network.cpp +int getSignalQuality(int rssi); +void WiFiEvent(WiFiEvent_t event); + //um_manager.cpp class Usermod { public: @@ -251,12 +255,43 @@ void userSetup(); void userConnected(); void userLoop(); +//util.cpp +bool oappend(const char* txt); // append new c string to temp buffer efficiently +bool oappendi(int i); // append new number to temp buffer efficiently +void sappend(char stype, const char* key, int val); +void sappends(char stype, const char* key, char* val); +void prepareHostname(char* hostname); +void _setRandomColor(bool _sec, bool fromButton); +bool isAsterisksOnly(const char* str, byte maxLen); + //wled_eeprom.cpp void applyMacro(byte index); void deEEP(); void deEEPSettings(); void clearEEPROM(); +//wled_math.cpp +#ifndef WLED_USE_REAL_MATH + float cos_t(float phi); + float sin_t(float x); + float tan_t(float x); + float acos_t(float x); + float asin_t(float x); + float atan_t(float x); + float floor_t(float x); + float fmod_t(float num, float denom); +#else + #include + #define sin_t sin + #define cos_t cos + #define tan_t tan + #define asin_t asin + #define acos_t acos + #define atan_t atan + #define fmod_t fmod + #define floor_t floor +#endif + //wled_serial.cpp void handleSerial(); @@ -280,8 +315,6 @@ void sendDataWs(AsyncWebSocketClient * client = nullptr); //xml.cpp void XML_response(AsyncWebServerRequest *request, char* dest = nullptr); void URL_response(AsyncWebServerRequest *request); -void sappend(char stype, const char* key, int val); -void sappends(char stype, const char* key, char* val); void getSettingsJS(byte subPage, char* dest); #endif diff --git a/wled00/json.cpp b/wled00/json.cpp index 7ae408a3e..74d6f605c 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -456,26 +456,6 @@ void serializeState(JsonObject root, bool forPreset, bool includeBri, bool segme } } -//by https://github.com/tzapu/WiFiManager/blob/master/WiFiManager.cpp -int getSignalQuality(int rssi) -{ - int quality = 0; - - if (rssi <= -100) - { - quality = 0; - } - else if (rssi >= -50) - { - quality = 100; - } - else - { - quality = 2 * (rssi + 100); - } - return quality; -} - void serializeInfo(JsonObject root) { root[F("ver")] = versionString; diff --git a/wled00/network.cpp b/wled00/network.cpp new file mode 100644 index 000000000..d22fd8100 --- /dev/null +++ b/wled00/network.cpp @@ -0,0 +1,153 @@ +#include "wled.h" +#include "fcn_declare.h" +#include "wled_ethernet.h" + + +#ifdef WLED_USE_ETHERNET +// The following six pins are neither configurable nor +// can they be re-assigned through IOMUX / GPIO matrix. +// See https://docs.espressif.com/projects/esp-idf/en/latest/esp32/hw-reference/esp32/get-started-ethernet-kit-v1.1.html#ip101gri-phy-interface +const managed_pin_type esp32_nonconfigurable_ethernet_pins[WLED_ETH_RSVD_PINS_COUNT] = { + { 21, true }, // RMII EMAC TX EN == When high, clocks the data on TXD0 and TXD1 to transmitter + { 19, true }, // RMII EMAC TXD0 == First bit of transmitted data + { 22, true }, // RMII EMAC TXD1 == Second bit of transmitted data + { 25, false }, // RMII EMAC RXD0 == First bit of received data + { 26, false }, // RMII EMAC RXD1 == Second bit of received data + { 27, true }, // RMII EMAC CRS_DV == Carrier Sense and RX Data Valid +}; + +const ethernet_settings ethernetBoards[] = { + // None + { + }, + + // WT32-EHT01 + // Please note, from my testing only these pins work for LED outputs: + // IO2, IO4, IO12, IO14, IO15 + // These pins do not appear to work from my testing: + // IO35, IO36, IO39 + { + 1, // eth_address, + 16, // eth_power, + 23, // eth_mdc, + 18, // eth_mdio, + ETH_PHY_LAN8720, // eth_type, + ETH_CLOCK_GPIO0_IN // eth_clk_mode + }, + + // ESP32-POE + { + 0, // eth_address, + 12, // eth_power, + 23, // eth_mdc, + 18, // eth_mdio, + ETH_PHY_LAN8720, // eth_type, + ETH_CLOCK_GPIO17_OUT // eth_clk_mode + }, + + // WESP32 + { + 0, // eth_address, + -1, // eth_power, + 16, // eth_mdc, + 17, // eth_mdio, + ETH_PHY_LAN8720, // eth_type, + ETH_CLOCK_GPIO0_IN // eth_clk_mode + }, + + // QuinLed-ESP32-Ethernet + { + 0, // eth_address, + 5, // eth_power, + 23, // eth_mdc, + 18, // eth_mdio, + ETH_PHY_LAN8720, // eth_type, + ETH_CLOCK_GPIO17_OUT // eth_clk_mode + }, + + // TwilightLord-ESP32 Ethernet Shield + { + 0, // eth_address, + 5, // eth_power, + 23, // eth_mdc, + 18, // eth_mdio, + ETH_PHY_LAN8720, // eth_type, + ETH_CLOCK_GPIO17_OUT // eth_clk_mode + }, + + // ESP3DEUXQuattro + { + 1, // eth_address, + -1, // eth_power, + 23, // eth_mdc, + 18, // eth_mdio, + ETH_PHY_LAN8720, // eth_type, + ETH_CLOCK_GPIO17_OUT // eth_clk_mode + } +}; +#endif + + +//by https://github.com/tzapu/WiFiManager/blob/master/WiFiManager.cpp +int getSignalQuality(int rssi) +{ + int quality = 0; + + if (rssi <= -100) + { + quality = 0; + } + else if (rssi >= -50) + { + quality = 100; + } + else + { + quality = 2 * (rssi + 100); + } + return quality; +} + + +//handle Ethernet connection event +void WiFiEvent(WiFiEvent_t event) +{ + #ifdef WLED_USE_ETHERNET + char hostname[25] = "wled-"; + #endif + + switch (event) { +#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_ETHERNET) + case SYSTEM_EVENT_ETH_START: + DEBUG_PRINT(F("ETH Started")); + break; + case SYSTEM_EVENT_ETH_CONNECTED: + DEBUG_PRINT(F("ETH Connected")); + if (!apActive) { + WiFi.disconnect(true); + } + if (staticIP != (uint32_t)0x00000000 && staticGateway != (uint32_t)0x00000000) { + ETH.config(staticIP, staticGateway, staticSubnet, IPAddress(8, 8, 8, 8)); + } else { + ETH.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); + } + // convert the "serverDescription" into a valid DNS hostname (alphanumeric) + prepareHostname(hostname); + ETH.setHostname(hostname); + showWelcomePage = false; + break; + case SYSTEM_EVENT_ETH_DISCONNECTED: + DEBUG_PRINT(F("ETH Disconnected")); + // This doesn't really affect ethernet per se, + // as it's only configured once. Rather, it + // may be necessary to reconnect the WiFi when + // ethernet disconnects, as a way to provide + // alternative access to the device. + forceReconnect = true; + break; +#endif + default: + break; + } +} + diff --git a/wled00/ntp.cpp b/wled00/ntp.cpp index d844c8f83..4513f9784 100644 --- a/wled00/ntp.cpp +++ b/wled00/ntp.cpp @@ -1,17 +1,6 @@ #include "src/dependencies/timezone/Timezone.h" #include "wled.h" -#ifndef WLED_USE_REAL_MATH - #include "wled_math.h" -#else - #define sin_t sin - #define cos_t cos - #define tan_t tan - #define asin_t asin - #define acos_t acos - #define atan_t atan - #define fmod_t fmod - #define floor_t floor -#endif +#include "fcn_declare.h" /* * Acquires time from NTP server diff --git a/wled00/set.cpp b/wled00/set.cpp index 6d9dbe703..6940055ee 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -4,29 +4,6 @@ * Receives client input */ -void _setRandomColor(bool _sec,bool fromButton) -{ - lastRandomIndex = strip.get_random_wheel_index(lastRandomIndex); - if (_sec){ - colorHStoRGB(lastRandomIndex*256,255,colSec); - } else { - colorHStoRGB(lastRandomIndex*256,255,col); - } - if (fromButton) colorUpdated(2); -} - - -bool isAsterisksOnly(const char* str, byte maxLen) -{ - for (byte i = 0; i < maxLen; i++) { - if (str[i] == 0) break; - if (str[i] != '*') return false; - } - //at this point the password contains asterisks only - return (str[0] != 0); //false on empty string -} - - //called upon POST settings form submit void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) { diff --git a/wled00/util.cpp b/wled00/util.cpp new file mode 100644 index 000000000..31540f228 --- /dev/null +++ b/wled00/util.cpp @@ -0,0 +1,131 @@ +#include "wled.h" +#include "fcn_declare.h" +#include "const.h" + + +//append a numeric setting to string buffer +void sappend(char stype, const char* key, int val) +{ + char ds[] = "d.Sf."; + + switch(stype) + { + case 'c': //checkbox + oappend(ds); + oappend(key); + oappend(".checked="); + oappendi(val); + oappend(";"); + break; + case 'v': //numeric + oappend(ds); + oappend(key); + oappend(".value="); + oappendi(val); + oappend(";"); + break; + case 'i': //selectedIndex + oappend(ds); + oappend(key); + oappend(SET_F(".selectedIndex=")); + oappendi(val); + oappend(";"); + break; + } +} + + +//append a string setting to buffer +void sappends(char stype, const char* key, char* val) +{ + switch(stype) + { + case 's': {//string (we can interpret val as char*) + String buf = val; + //convert "%" to "%%" to make EspAsyncWebServer happy + buf.replace("%","%%"); + oappend("d.Sf."); + oappend(key); + oappend(".value=\""); + oappend(buf.c_str()); + oappend("\";"); + break;} + case 'm': //message + oappend(SET_F("d.getElementsByClassName")); + oappend(key); + oappend(SET_F(".innerHTML=\"")); + oappend(val); + oappend("\";"); + break; + } +} + + +bool oappendi(int i) +{ + char s[11]; + sprintf(s, "%d", i); + return oappend(s); +} + + +bool oappend(const char* txt) +{ + uint16_t len = strlen(txt); + if (olen + len >= OMAX) + return false; // buffer full + strcpy(obuf + olen, txt); + olen += len; + return true; +} + + +void prepareHostname(char* hostname) +{ + const char *pC = serverDescription; + uint8_t pos = 5; + + while (*pC && pos < 24) { // while !null and not over length + if (isalnum(*pC)) { // if the current char is alpha-numeric append it to the hostname + hostname[pos] = *pC; + pos++; + } else if (*pC == ' ' || *pC == '_' || *pC == '-' || *pC == '+' || *pC == '!' || *pC == '?' || *pC == '*') { + hostname[pos] = '-'; + pos++; + } + // else do nothing - no leading hyphens and do not include hyphens for all other characters. + pC++; + } + // if the hostname is left blank, use the mac address/default mdns name + if (pos < 6) { + sprintf(hostname + 5, "%*s", 6, escapedMac.c_str() + 6); + } else { //last character must not be hyphen + while (pos > 0 && hostname[pos -1] == '-') { + hostname[pos -1] = 0; + pos--; + } + } +} + + +void _setRandomColor(bool _sec, bool fromButton) +{ + lastRandomIndex = strip.get_random_wheel_index(lastRandomIndex); + if (_sec){ + colorHStoRGB(lastRandomIndex*256,255,colSec); + } else { + colorHStoRGB(lastRandomIndex*256,255,col); + } + if (fromButton) colorUpdated(2); +} + + +bool isAsterisksOnly(const char* str, byte maxLen) +{ + for (byte i = 0; i < maxLen; i++) { + if (str[i] == 0) break; + if (str[i] != '*') return false; + } + //at this point the password contains asterisks only + return (str[0] != 0); //false on empty string +} diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 3a1f6c63a..ced469163 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -8,90 +8,6 @@ #include "soc/rtc_cntl_reg.h" #endif -#ifdef WLED_USE_ETHERNET -// The following six pins are neither configurable nor -// can they be re-assigned through IOMUX / GPIO matrix. -// See https://docs.espressif.com/projects/esp-idf/en/latest/esp32/hw-reference/esp32/get-started-ethernet-kit-v1.1.html#ip101gri-phy-interface -const managed_pin_type esp32_nonconfigurable_ethernet_pins[WLED_ETH_RSVD_PINS_COUNT] = { - { 21, true }, // RMII EMAC TX EN == When high, clocks the data on TXD0 and TXD1 to transmitter - { 19, true }, // RMII EMAC TXD0 == First bit of transmitted data - { 22, true }, // RMII EMAC TXD1 == Second bit of transmitted data - { 25, false }, // RMII EMAC RXD0 == First bit of received data - { 26, false }, // RMII EMAC RXD1 == Second bit of received data - { 27, true }, // RMII EMAC CRS_DV == Carrier Sense and RX Data Valid -}; - -const ethernet_settings ethernetBoards[] = { - // None - { - }, - - // WT32-EHT01 - // Please note, from my testing only these pins work for LED outputs: - // IO2, IO4, IO12, IO14, IO15 - // These pins do not appear to work from my testing: - // IO35, IO36, IO39 - { - 1, // eth_address, - 16, // eth_power, - 23, // eth_mdc, - 18, // eth_mdio, - ETH_PHY_LAN8720, // eth_type, - ETH_CLOCK_GPIO0_IN // eth_clk_mode - }, - - // ESP32-POE - { - 0, // eth_address, - 12, // eth_power, - 23, // eth_mdc, - 18, // eth_mdio, - ETH_PHY_LAN8720, // eth_type, - ETH_CLOCK_GPIO17_OUT // eth_clk_mode - }, - - // WESP32 - { - 0, // eth_address, - -1, // eth_power, - 16, // eth_mdc, - 17, // eth_mdio, - ETH_PHY_LAN8720, // eth_type, - ETH_CLOCK_GPIO0_IN // eth_clk_mode - }, - - // QuinLed-ESP32-Ethernet - { - 0, // eth_address, - 5, // eth_power, - 23, // eth_mdc, - 18, // eth_mdio, - ETH_PHY_LAN8720, // eth_type, - ETH_CLOCK_GPIO17_OUT // eth_clk_mode - }, - - // TwilightLord-ESP32 Ethernet Shield - { - 0, // eth_address, - 5, // eth_power, - 23, // eth_mdc, - 18, // eth_mdio, - ETH_PHY_LAN8720, // eth_type, - ETH_CLOCK_GPIO17_OUT // eth_clk_mode - }, - - // ESP3DEUXQuattro - { - 1, // eth_address, - -1, // eth_power, - 23, // eth_mdc, - 18, // eth_mdio, - ETH_PHY_LAN8720, // eth_type, - ETH_CLOCK_GPIO17_OUT // eth_clk_mode - } -}; -#endif - /* * Main WLED class implementation. Mostly initialization and connection logic */ @@ -116,92 +32,6 @@ void WLED::reset() ESP.restart(); } -bool oappendi(int i) -{ - char s[11]; - sprintf(s, "%d", i); - return oappend(s); -} - -bool oappend(const char* txt) -{ - uint16_t len = strlen(txt); - if (olen + len >= OMAX) - return false; // buffer full - strcpy(obuf + olen, txt); - olen += len; - return true; -} - -void prepareHostname(char* hostname) -{ - const char *pC = serverDescription; - uint8_t pos = 5; - - while (*pC && pos < 24) { // while !null and not over length - if (isalnum(*pC)) { // if the current char is alpha-numeric append it to the hostname - hostname[pos] = *pC; - pos++; - } else if (*pC == ' ' || *pC == '_' || *pC == '-' || *pC == '+' || *pC == '!' || *pC == '?' || *pC == '*') { - hostname[pos] = '-'; - pos++; - } - // else do nothing - no leading hyphens and do not include hyphens for all other characters. - pC++; - } - // if the hostname is left blank, use the mac address/default mdns name - if (pos < 6) { - sprintf(hostname + 5, "%*s", 6, escapedMac.c_str() + 6); - } else { //last character must not be hyphen - while (pos > 0 && hostname[pos -1] == '-') { - hostname[pos -1] = 0; - pos--; - } - } -} - -//handle Ethernet connection event -void WiFiEvent(WiFiEvent_t event) -{ - #ifdef WLED_USE_ETHERNET - char hostname[25] = "wled-"; - #endif - - switch (event) { -#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_ETHERNET) - case SYSTEM_EVENT_ETH_START: - DEBUG_PRINT(F("ETH Started")); - break; - case SYSTEM_EVENT_ETH_CONNECTED: - DEBUG_PRINT(F("ETH Connected")); - if (!apActive) { - WiFi.disconnect(true); - } - if (staticIP != (uint32_t)0x00000000 && staticGateway != (uint32_t)0x00000000) { - ETH.config(staticIP, staticGateway, staticSubnet, IPAddress(8, 8, 8, 8)); - } else { - ETH.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); - } - // convert the "serverDescription" into a valid DNS hostname (alphanumeric) - prepareHostname(hostname); - ETH.setHostname(hostname); - showWelcomePage = false; - break; - case SYSTEM_EVENT_ETH_DISCONNECTED: - DEBUG_PRINT(F("ETH Disconnected")); - // This doesn't really affect ethernet per se, - // as it's only configured once. Rather, it - // may be necessary to reconnect the WiFi when - // ethernet disconnects, as a way to provide - // alternative access to the device. - forceReconnect = true; - break; -#endif - default: - break; - } -} - void WLED::loop() { #ifdef WLED_DEBUG diff --git a/wled00/wled.h b/wled00/wled.h index 9ddc4f0f2..3c867bed9 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -641,10 +641,9 @@ WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager()); #define WLED_WIFI_CONFIGURED (strlen(clientSSID) >= 1 && strcmp(clientSSID, DEFAULT_CLIENT_SSID) != 0) #define WLED_MQTT_CONNECTED (mqtt != nullptr && mqtt->connected()) -// append new c string to temp buffer efficiently -bool oappend(const char* txt); -// append new number to temp buffer efficiently -bool oappendi(int i); +//macro to convert F to const +#define SET_F(x) (const char*)F(x) + class WLED { public: diff --git a/wled00/wled00.vcxproj b/wled00/wled00.vcxproj index e095b8f76..9e30b6f1e 100644 --- a/wled00/wled00.vcxproj +++ b/wled00/wled00.vcxproj @@ -114,8 +114,6 @@ - - @@ -126,7 +124,6 @@ - @@ -198,6 +195,7 @@ + @@ -223,7 +221,9 @@ + + diff --git a/wled00/wled_math.h b/wled00/wled_math.cpp similarity index 98% rename from wled00/wled_math.h rename to wled00/wled_math.cpp index b25014bf4..d9af41624 100644 --- a/wled00/wled_math.h +++ b/wled00/wled_math.cpp @@ -1,6 +1,3 @@ -#ifndef WLED_MATH_H -#define WLED_MATH_H - /* * Contains some trigonometric functions. * The ANSI C equivalents are likely faster, but using any sin/cos/tan function incurs a memory penalty of 460 bytes on ESP8266, likely for lookup tables. @@ -135,5 +132,3 @@ float fmod_t(float num, float denom) { #endif return res; } - -#endif diff --git a/wled00/xml.cpp b/wled00/xml.cpp index ced556991..c8ba91ba7 100644 --- a/wled00/xml.cpp +++ b/wled00/xml.cpp @@ -5,9 +5,6 @@ * Sending XML status files to client */ -//macro to convert F to const -#define SET_F(x) (const char*)F(x) - //build XML response to HTTP /win API request void XML_response(AsyncWebServerRequest *request, char* dest) { @@ -123,62 +120,6 @@ void URL_response(AsyncWebServerRequest *request) if (request != nullptr) request->send(200, "text/html", obuf); } -//append a numeric setting to string buffer -void sappend(char stype, const char* key, int val) -{ - char ds[] = "d.Sf."; - - switch(stype) - { - case 'c': //checkbox - oappend(ds); - oappend(key); - oappend(".checked="); - oappendi(val); - oappend(";"); - break; - case 'v': //numeric - oappend(ds); - oappend(key); - oappend(".value="); - oappendi(val); - oappend(";"); - break; - case 'i': //selectedIndex - oappend(ds); - oappend(key); - oappend(SET_F(".selectedIndex=")); - oappendi(val); - oappend(";"); - break; - } -} - -//append a string setting to buffer -void sappends(char stype, const char* key, char* val) -{ - switch(stype) - { - case 's': {//string (we can interpret val as char*) - String buf = val; - //convert "%" to "%%" to make EspAsyncWebServer happy - buf.replace("%","%%"); - oappend("d.Sf."); - oappend(key); - oappend(".value=\""); - oappend(buf.c_str()); - oappend("\";"); - break;} - case 'm': //message - oappend(SET_F("d.getElementsByClassName")); - oappend(key); - oappend(SET_F(".innerHTML=\"")); - oappend(val); - oappend("\";"); - break; - } -} - void extractPin(JsonObject &obj, const char *key) { if (obj[key].is()) { JsonArray pins = obj[key].as();