From f7f41980b6cbfa6e60c7d93506bd991155e9ec08 Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Tue, 4 Feb 2020 10:02:01 +0300 Subject: [PATCH] Headers cleanup (#2134) * Config: remove prototypes.h * fixup order * word * add missing * fix warning while still using .ino * bad at c/p; --- code/espurna/alexa.h | 28 ++ code/espurna/alexa.ino | 43 +- code/espurna/api.h | 31 ++ code/espurna/api.ino | 8 +- code/espurna/board.ino | 289 ++++++++++++- code/espurna/broker.h | 6 +- code/espurna/compat.h | 87 ++++ code/espurna/config/all.h | 6 +- code/espurna/config/arduino.h | 2 + code/espurna/config/defaults.h | 6 + code/espurna/config/general.h | 6 +- code/espurna/config/hardware.h | 2 + code/espurna/config/progmem.h | 395 ------------------ code/espurna/config/prototypes.h | 319 -------------- code/espurna/config/sensors.h | 2 + code/espurna/config/types.h | 2 + code/espurna/config/version.h | 6 + code/espurna/config/webui.h | 30 +- code/espurna/crash.h | 48 +++ code/espurna/crash.ino | 43 +- code/espurna/domoticz.h | 28 ++ code/espurna/domoticz.ino | 7 +- code/espurna/espurna.ino | 32 +- code/espurna/gpio.h | 15 + code/espurna/gpio.ino | 2 + code/espurna/homeassistant.h | 19 + code/espurna/homeassistant.ino | 3 +- code/espurna/i2c.h | 42 ++ code/espurna/i2c.ino | 3 - code/espurna/ir.h | 21 + code/espurna/ir.ino | 10 +- code/espurna/light.ino | 3 +- code/espurna/mqtt.h | 54 +++ code/espurna/mqtt.ino | 10 +- code/espurna/ntp.ino | 2 +- code/espurna/ota.h | 99 ++--- code/espurna/ota.ino | 79 ++++ code/espurna/ota_arduinoota.ino | 1 + code/espurna/ota_asynctcp.ino | 4 +- code/espurna/ota_httpupdate.ino | 8 +- code/espurna/relay.ino | 6 +- code/espurna/rfbridge.h | 24 ++ code/espurna/rfbridge.ino | 7 +- code/espurna/rfm69.h | 29 ++ code/espurna/rfm69.ino | 3 +- code/espurna/{config => }/rtcmem.h | 17 +- code/espurna/rtcmem.ino | 2 + code/espurna/sensor.ino | 106 ++++- code/espurna/settings.ino | 2 + code/espurna/storage_eeprom.h | 24 ++ .../{eeprom.ino => storage_eeprom.ino} | 6 +- code/espurna/system.h | 8 + code/espurna/terminal.h | 21 + code/espurna/terminal.ino | 1 + code/espurna/thermostat.h | 19 + code/espurna/thermostat.ino | 6 +- code/espurna/timelibshim.h | 120 ++++++ code/espurna/uartmqtt.h | 18 + code/espurna/uartmqtt.ino | 4 +- code/espurna/utils.h | 2 + code/espurna/utils.ino | 27 +- code/espurna/web.h | 41 ++ code/espurna/web.ino | 10 +- code/espurna/wifi.h | 36 ++ code/espurna/ws.h | 1 + code/espurna/ws.ino | 6 +- 66 files changed, 1398 insertions(+), 949 deletions(-) create mode 100644 code/espurna/alexa.h create mode 100644 code/espurna/api.h create mode 100644 code/espurna/compat.h delete mode 100644 code/espurna/config/progmem.h delete mode 100644 code/espurna/config/prototypes.h create mode 100644 code/espurna/crash.h create mode 100644 code/espurna/domoticz.h create mode 100644 code/espurna/gpio.h create mode 100644 code/espurna/homeassistant.h create mode 100644 code/espurna/i2c.h create mode 100644 code/espurna/ir.h create mode 100644 code/espurna/mqtt.h create mode 100644 code/espurna/ota.ino create mode 100644 code/espurna/rfbridge.h create mode 100644 code/espurna/rfm69.h rename code/espurna/{config => }/rtcmem.h (77%) create mode 100644 code/espurna/storage_eeprom.h rename code/espurna/{eeprom.ino => storage_eeprom.ino} (97%) create mode 100644 code/espurna/terminal.h create mode 100644 code/espurna/thermostat.h create mode 100644 code/espurna/timelibshim.h create mode 100644 code/espurna/uartmqtt.h create mode 100644 code/espurna/web.h diff --git a/code/espurna/alexa.h b/code/espurna/alexa.h new file mode 100644 index 00000000..9b9346b3 --- /dev/null +++ b/code/espurna/alexa.h @@ -0,0 +1,28 @@ +/* + +ALEXA MODULE + +Copyright (C) 2016-2019 by Xose Pérez + +*/ + +#pragma once + +#include "web.h" + +struct alexa_queue_element_t { + unsigned char device_id; + bool state; + unsigned char value; +}; + +#if ALEXA_SUPPORT + +#include +#include + +bool alexaEnabled(); +void alexaSetup(); + +#endif // ALEXA_SUPPORT == 1 + diff --git a/code/espurna/alexa.ino b/code/espurna/alexa.ino index a9b7214b..072bd994 100644 --- a/code/espurna/alexa.ino +++ b/code/espurna/alexa.ino @@ -8,18 +8,15 @@ Copyright (C) 2016-2019 by Xose Pérez #if ALEXA_SUPPORT -#include "relay.h" -#include "broker.h" +#include -#include -fauxmoESP alexa; +#include "alexa.h" +#include "broker.h" +#include "relay.h" +#include "ws.h" +#include "web.h" -#include -typedef struct { - unsigned char device_id; - bool state; - unsigned char value; -} alexa_queue_element_t; +fauxmoESP _alexa; static std::queue _alexa_queue; // ----------------------------------------------------------------------------- @@ -36,17 +33,17 @@ void _alexaWebSocketOnConnected(JsonObject& root) { } void _alexaConfigure() { - alexa.enable(wifiConnected() && alexaEnabled()); + _alexa.enable(wifiConnected() && alexaEnabled()); } #if WEB_SUPPORT bool _alexaBodyCallback(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) { - return alexa.process(request->client(), request->method() == HTTP_GET, request->url(), String((char *)data)); + return _alexa.process(request->client(), request->method() == HTTP_GET, request->url(), String((char *)data)); } bool _alexaRequestCallback(AsyncWebServerRequest *request) { String body = (request->hasParam("body", true)) ? request->getParam("body", true)->value() : String(); - return alexa.process(request->client(), request->method() == HTTP_GET, request->url(), body); + return _alexa.process(request->client(), request->method() == HTTP_GET, request->url(), body); } #endif @@ -60,14 +57,14 @@ void _alexaBrokerCallback(const String& topic, unsigned char id, unsigned int va } if (topic.equals(MQTT_TOPIC_CHANNEL)) { - alexa.setState(id + 1, value > 0, value); + _alexa.setState(id + 1, value > 0, value); } if (topic.equals(MQTT_TOPIC_RELAY)) { #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT if (id > 0) return; #endif - alexa.setState(id, value, value > 0 ? 255 : 0); + _alexa.setState(id, value, value > 0 ? 255 : 0); } } @@ -85,8 +82,8 @@ void alexaSetup() { moveSetting("fauxmoEnabled", "alexaEnabled"); // Basic fauxmoESP configuration - alexa.createServer(!WEB_SUPPORT); - alexa.setPort(80); + _alexa.createServer(!WEB_SUPPORT); + _alexa.setPort(80); // Use custom alexa hostname if defined, device hostname otherwise String hostname = getSetting("alexaName", ALEXA_HOSTNAME); @@ -98,11 +95,11 @@ void alexaSetup() { #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT // Global switch - alexa.addDevice(hostname.c_str()); + _alexa.addDevice(hostname.c_str()); // For each channel for (unsigned char i = 1; i <= lightChannels(); i++) { - alexa.addDevice((hostname + " " + i).c_str()); + _alexa.addDevice((hostname + " " + i).c_str()); } // Relays @@ -110,10 +107,10 @@ void alexaSetup() { unsigned int relays = relayCount(); if (relays == 1) { - alexa.addDevice(hostname.c_str()); + _alexa.addDevice(hostname.c_str()); } else { for (unsigned int i=1; i<=relays; i++) { - alexa.addDevice((hostname + " " + i).c_str()); + _alexa.addDevice((hostname + " " + i).c_str()); } } @@ -140,7 +137,7 @@ void alexaSetup() { }); // Callback - alexa.onSetState([&](unsigned char device_id, const char * name, bool state, unsigned char value) { + _alexa.onSetState([&](unsigned char device_id, const char * name, bool state, unsigned char value) { alexa_queue_element_t element; element.device_id = device_id; element.state = state; @@ -159,7 +156,7 @@ void alexaSetup() { void alexaLoop() { - alexa.handle(); + _alexa.handle(); while (!_alexa_queue.empty()) { diff --git a/code/espurna/api.h b/code/espurna/api.h new file mode 100644 index 00000000..54e86ee8 --- /dev/null +++ b/code/espurna/api.h @@ -0,0 +1,31 @@ +/* + +API MODULE + +Copyright (C) 2016-2019 by Xose Pérez + +*/ + +#pragma once + +#include "web.h" + +#include + +// TODO: need these prototypes for .ino +using api_get_callback_f = std::function; +using api_put_callback_f = std::function ; + +#if API_SUPPORT + +#include +#include +#include + +#include + +#if WEB_SUPPORT + void apiRegister(const char * key, api_get_callback_f getFn, api_put_callback_f putFn = nullptr); +#endif + +#endif // API_SUPPORT == 1 diff --git a/code/espurna/api.ino b/code/espurna/api.ino index 910a60cc..309fcba4 100644 --- a/code/espurna/api.ino +++ b/code/espurna/api.ino @@ -8,12 +8,10 @@ Copyright (C) 2016-2019 by Xose Pérez #if API_SUPPORT -#include -#include -#include -#include - +#include "api.h" #include "system.h" +#include "web.h" +#include "ws.h" typedef struct { char * key; diff --git a/code/espurna/board.ino b/code/espurna/board.ino index f4c44fdb..a0ba05cf 100644 --- a/code/espurna/board.ino +++ b/code/espurna/board.ino @@ -6,6 +6,293 @@ BOARD MODULE #include "board.h" +//-------------------------------------------------------------------------------- + +PROGMEM const char espurna_modules[] = + #if ALEXA_SUPPORT + "ALEXA " + #endif + #if API_SUPPORT + "API " + #endif + #if BROKER_SUPPORT + "BROKER " + #endif + #if BUTTON_SUPPORT + "BUTTON " + #endif + #if DEBUG_SERIAL_SUPPORT + "DEBUG_SERIAL " + #endif + #if DEBUG_TELNET_SUPPORT + "DEBUG_TELNET " + #endif + #if DEBUG_UDP_SUPPORT + "DEBUG_UDP " + #endif + #if DEBUG_WEB_SUPPORT + "DEBUG_WEB " + #endif + #if DOMOTICZ_SUPPORT + "DOMOTICZ " + #endif + #if ENCODER_SUPPORT + "ENCODER " + #endif + #if HOMEASSISTANT_SUPPORT + "HOMEASSISTANT " + #endif + #if I2C_SUPPORT + "I2C " + #endif + #if INFLUXDB_SUPPORT + "INFLUXDB " + #endif + #if IR_SUPPORT + "IR " + #endif + #if LED_SUPPORT + "LED " + #endif + #if LLMNR_SUPPORT + "LLMNR " + #endif + #if MDNS_CLIENT_SUPPORT + "MDNS_CLIENT " + #endif + #if MDNS_SERVER_SUPPORT + "MDNS_SERVER " + #endif + #if MQTT_SUPPORT + "MQTT " + #endif + #if NETBIOS_SUPPORT + "NETBIOS " + #endif + #if NOFUSS_SUPPORT + "NOFUSS " + #endif + #if NTP_SUPPORT + "NTP " + #endif + #if RFM69_SUPPORT + "RFM69 " + #endif + #if RF_SUPPORT + "RF " + #endif + #if SCHEDULER_SUPPORT + "SCHEDULER " + #endif + #if SENSOR_SUPPORT + "SENSOR " + #endif + #if SPIFFS_SUPPORT + "SPIFFS " + #endif + #if SSDP_SUPPORT + "SSDP " + #endif + #if TELNET_SUPPORT + #if TELNET_SERVER == TELNET_SERVER_WIFISERVER + "TELNET_SYNC " + #else + "TELNET " + #endif // TELNET_SERVER == TELNET_SERVER_WIFISERVER + #endif + #if TERMINAL_SUPPORT + "TERMINAL " + #endif + #if THERMOSTAT_SUPPORT + "THERMOSTAT " + #endif + #if THERMOSTAT_DISPLAY_SUPPORT + "THERMOSTAT_DISPLAY " + #endif + #if THINGSPEAK_SUPPORT + "THINGSPEAK " + #endif + #if UART_MQTT_SUPPORT + "UART_MQTT " + #endif + #if WEB_SUPPORT + "WEB " + #endif + ""; + +PROGMEM const char espurna_ota_modules[] = + #if OTA_ARDUINOOTA_SUPPORT + "ARDUINO " + #endif + #if (OTA_CLIENT == OTA_CLIENT_ASYNCTCP) + "ASYNCTCP " + #endif + #if (OTA_CLIENT == OTA_CLIENT_HTTPUPDATE) + #if (SECURE_CLIENT == SECURE_CLIENT_NONE) + "*HTTPUPDATE " + #endif + #if (SECURE_CLIENT == SECURE_CLIENT_AXTLS) + "*HTTPUPDATE_AXTLS " + #endif + #if (SECURE_CLIENT == SECURE_CLIENT_BEARSSL) + "*HTTPUPDATE_BEARSSL " + #endif + #endif // OTA_CLIENT_HTTPUPDATE + #if OTA_MQTT_SUPPORT + "MQTT " + #endif + #if WEB_SUPPORT + "WEB " + #endif + ""; + +PROGMEM const char espurna_webui[] = + #if WEBUI_IMAGE == WEBUI_IMAGE_SMALL + "SMALL" + #endif + #if WEBUI_IMAGE == WEBUI_IMAGE_LIGHT + "LIGHT" + #endif + #if WEBUI_IMAGE == WEBUI_IMAGE_SENSOR + "SENSOR" + #endif + #if WEBUI_IMAGE == WEBUI_IMAGE_RFBRIDGE + "RFBRIDGE" + #endif + #if WEBUI_IMAGE == WEBUI_IMAGE_RFM69 + "RFM69" + #endif + #if WEBUI_IMAGE == WEBUI_IMAGE_LIGHTFOX + "LIGHTFOX" + #endif + #if WEBUI_IMAGE == WEBUI_IMAGE_THERMOSTAT + "THERMOSTAT" + #endif + #if WEBUI_IMAGE == WEBUI_IMAGE_FULL + "FULL" + #endif + ""; + +#if SENSOR_SUPPORT + +PROGMEM const char espurna_sensors[] = + #if AM2320_SUPPORT + "AM2320_I2C " + #endif + #if ANALOG_SUPPORT + "ANALOG " + #endif + #if BH1750_SUPPORT + "BH1750 " + #endif + #if BMP180_SUPPORT + "BMP180 " + #endif + #if BMX280_SUPPORT + "BMX280 " + #endif + #if CSE7766_SUPPORT + "CSE7766 " + #endif + #if DALLAS_SUPPORT + "DALLAS " + #endif + #if DHT_SUPPORT + "DHTXX " + #endif + #if DIGITAL_SUPPORT + "DIGITAL " + #endif + #if ECH1560_SUPPORT + "ECH1560 " + #endif + #if EMON_ADC121_SUPPORT + "EMON_ADC121 " + #endif + #if EMON_ADS1X15_SUPPORT + "EMON_ADX1X15 " + #endif + #if EMON_ANALOG_SUPPORT + "EMON_ANALOG " + #endif + #if EVENTS_SUPPORT + "EVENTS " + #endif + #if GEIGER_SUPPORT + "GEIGER " + #endif + #if GUVAS12SD_SUPPORT + "GUVAS12SD " + #endif + #if HLW8012_SUPPORT + "HLW8012 " + #endif + #if LDR_SUPPORT + "LDR " + #endif + #if MHZ19_SUPPORT + "MHZ19 " + #endif + #if MICS2710_SUPPORT + "MICS2710 " + #endif + #if MICS5525_SUPPORT + "MICS5525 " + #endif + #if NTC_SUPPORT + "NTC " + #endif + #if PMSX003_SUPPORT + "PMSX003 " + #endif + #if PULSEMETER_SUPPORT + "PULSEMETER " + #endif + #if PZEM004T_SUPPORT + "PZEM004T " + #endif + #if SDS011_SUPPORT + "SDS011 " + #endif + #if SENSEAIR_SUPPORT + "SENSEAIR " + #endif + #if SHT3X_I2C_SUPPORT + "SHT3X_I2C " + #endif + #if SI7021_SUPPORT + "SI7021 " + #endif + #if SONAR_SUPPORT + "SONAR " + #endif + #if T6613_SUPPORT + "T6613 " + #endif + #if TMP3X_SUPPORT + "TMP3X " + #endif + #if V9261F_SUPPORT + "V9261F " + #endif + #if VEML6075_SUPPORT + "VEML6075 " + #endif + #if VL53L1X_SUPPORT + "VL53L1X " + #endif + #if EZOPH_SUPPORT + "EZOPH " + #endif + #if ADE7953_SUPPORT + "ADE7953 " + #endif + ""; + +#endif // SENSOR_SUPPORT == 1 + +//-------------------------------------------------------------------------------- + String getIdentifier() { char buffer[20]; snprintf_P(buffer, sizeof(buffer), PSTR("%s-%06X"), APP_NAME, ESP.getChipId()); @@ -24,7 +311,7 @@ String getEspurnaOTAModules() { String getEspurnaSensors() { return FPSTR(espurna_sensors); } -#endif +#endif // SENSOR_SUPPORT == 1 String getEspurnaWebUI() { return FPSTR(espurna_webui); diff --git a/code/espurna/broker.h b/code/espurna/broker.h index 48b7bb33..7cf35002 100644 --- a/code/espurna/broker.h +++ b/code/espurna/broker.h @@ -6,10 +6,10 @@ Copyright (C) 2017-2019 by Xose Pérez */ -#if BROKER_SUPPORT - #pragma once +#if BROKER_SUPPORT + #include #include #include @@ -56,4 +56,4 @@ using SensorReportBroker = TBroker; -#endif // BROKER_SUPPORT +#endif // BROKER_SUPPORT == 1 diff --git a/code/espurna/compat.h b/code/espurna/compat.h new file mode 100644 index 00000000..9ccde247 --- /dev/null +++ b/code/espurna/compat.h @@ -0,0 +1,87 @@ +/* + +COMPATIBILITY BETWEEN 2.3.0 and latest versions + +*/ + +#pragma once + +// ----------------------------------------------------------------------------- +// Core version 2.4.2 and higher changed the cont_t structure to a pointer: +// https://github.com/esp8266/Arduino/commit/5d5ea92a4d004ab009d5f642629946a0cb8893dd#diff-3fa12668b289ccb95b7ab334833a4ba8L35 +// Core version 2.5.0 introduced EspClass helper method: +// https://github.com/esp8266/Arduino/commit/0e0e34c614fe8a47544c9998201b1d9b3c24eb18 +// ----------------------------------------------------------------------------- + +extern "C" { + #include +#if defined(ARDUINO_ESP8266_RELEASE_2_3_0) \ + || defined(ARDUINO_ESP8266_RELEASE_2_4_0) \ + || defined(ARDUINO_ESP8266_RELEASE_2_4_1) + extern cont_t g_cont; + #define getFreeStack() cont_get_free_stack(&g_cont) +#elif defined(ARDUINO_ESP8266_RELEASE_2_4_2) + extern cont_t* g_pcont; + #define getFreeStack() cont_get_free_stack(g_pcont) +#else + #define getFreeStack() ESP.getFreeContStack() +#endif +} + +#include + +// ----------------------------------------------------------------------------- +// ref: https://github.com/esp8266/Arduino/blob/master/tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h +// __STRINGIZE && __STRINGIZE_NX && PROGMEM definitions port +// ----------------------------------------------------------------------------- + +// Do not replace macros unless running version older than 2.5.0 +#if defined(ARDUINO_ESP8266_RELEASE_2_3_0) \ + || defined(ARDUINO_ESP8266_RELEASE_2_4_0) \ + || defined(ARDUINO_ESP8266_RELEASE_2_4_1) \ + || defined(ARDUINO_ESP8266_RELEASE_2_4_2) + +// Quoting esp8266/Arduino comments: +// "Since __section__ is supposed to be only use for global variables, +// there could be conflicts when a static/inlined function has them in the +// same file as a non-static PROGMEM object. +// Ref: https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Variable-Attributes.html +// Place each progmem object into its own named section, avoiding conflicts" + +#define __TO_STR_(A) #A +#define __TO_STR(A) __TO_STR_(A) + +#undef PROGMEM +#define PROGMEM __attribute__((section( "\".irom.text." __FILE__ "." __TO_STR(__LINE__) "." __TO_STR(__COUNTER__) "\""))) + +// "PSTR() macro modified to start on a 32-bit boundary. This adds on average +// 1.5 bytes/string, but in return memcpy_P and strcpy_P will work 4~8x faster" +#undef PSTR +#define PSTR(s) (__extension__({static const char __c[] __attribute__((__aligned__(4))) PROGMEM = (s); &__c[0];})) + +#endif + +// ----------------------------------------------------------------------------- +// Division by zero bug +// https://github.com/esp8266/Arduino/pull/2397 +// https://github.com/esp8266/Arduino/pull/2408 +// ----------------------------------------------------------------------------- + +#if defined(ARDUINO_ESP8266_RELEASE_2_3_0) +long __attribute__((deprecated("Please avoid using map() with Core 2.3.0"))) map(long x, long in_min, long in_max, long out_min, long out_max); +#endif + +// ----------------------------------------------------------------------------- +// std::make_unique backport for C++11, since we still use it +// ----------------------------------------------------------------------------- +#if 201103L >= __cplusplus +namespace std { + template + std::unique_ptr make_unique(Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); + } +} +#endif + +#define UNUSED(x) (void)(x) + diff --git a/code/espurna/config/all.h b/code/espurna/config/all.h index 5f330344..21eb0c97 100644 --- a/code/espurna/config/all.h +++ b/code/espurna/config/all.h @@ -19,7 +19,11 @@ */ +#pragma once + +#include #include +#include #ifdef USE_CUSTOM_H #include "custom.h" @@ -34,7 +38,5 @@ #include "deprecated.h" #include "general.h" #include "dependencies.h" -#include "prototypes.h" #include "sensors.h" #include "webui.h" -#include "progmem.h" diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index 4f2de0f0..e0b8c136 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -3,6 +3,8 @@ // Uncomment the appropiate line(s) to build from the Arduino IDE //-------------------------------------------------------------------------------- +#pragma once + //-------------------------------------------------------------------------------- // Hardware //-------------------------------------------------------------------------------- diff --git a/code/espurna/config/defaults.h b/code/espurna/config/defaults.h index 8faff909..f904820c 100644 --- a/code/espurna/config/defaults.h +++ b/code/espurna/config/defaults.h @@ -1,3 +1,9 @@ +// ----------------------------------------------------------------------------- +// DEFAULTS PER MODULE +// ----------------------------------------------------------------------------- + +#pragma once + // ----------------------------------------------------------------------------- // Buttons // ----------------------------------------------------------------------------- diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 4c810b97..286350d5 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -1,8 +1,10 @@ -// // //------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // Do not change this file unless you know what you are doing -// Configuration settings are in the settings.h file +// To override user configuration, please see custom.h //------------------------------------------------------------------------------ +#pragma once + //------------------------------------------------------------------------------ // GENERAL //------------------------------------------------------------------------------ diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 1d560ae2..b28dd085 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -20,6 +20,8 @@ // // Besides, other hardware specific information should be stated here +#pragma once + // ----------------------------------------------------------------------------- // Custom hardware // ----------------------------------------------------------------------------- diff --git a/code/espurna/config/progmem.h b/code/espurna/config/progmem.h deleted file mode 100644 index 18a3315b..00000000 --- a/code/espurna/config/progmem.h +++ /dev/null @@ -1,395 +0,0 @@ -//-------------------------------------------------------------------------------- -// PROGMEM definitions -//-------------------------------------------------------------------------------- - -//-------------------------------------------------------------------------------- -// Various strings -//-------------------------------------------------------------------------------- - -PROGMEM const char pstr_unknown[] = "UNKNOWN"; - -//-------------------------------------------------------------------------------- -// Reset reasons -//-------------------------------------------------------------------------------- - -PROGMEM const char custom_reset_hardware[] = "Hardware button"; -PROGMEM const char custom_reset_web[] = "Reboot from web interface"; -PROGMEM const char custom_reset_terminal[] = "Reboot from terminal"; -PROGMEM const char custom_reset_mqtt[] = "Reboot from MQTT"; -PROGMEM const char custom_reset_rpc[] = "Reboot from RPC"; -PROGMEM const char custom_reset_ota[] = "Reboot after successful OTA update"; -PROGMEM const char custom_reset_http[] = "Reboot from HTTP"; -PROGMEM const char custom_reset_nofuss[] = "Reboot after successful NoFUSS update"; -PROGMEM const char custom_reset_upgrade[] = "Reboot after successful web update"; -PROGMEM const char custom_reset_factory[] = "Factory reset"; -PROGMEM const char* const custom_reset_string[] = { - custom_reset_hardware, custom_reset_web, custom_reset_terminal, - custom_reset_mqtt, custom_reset_rpc, custom_reset_ota, - custom_reset_http, custom_reset_nofuss, custom_reset_upgrade, - custom_reset_factory -}; - -//-------------------------------------------------------------------------------- -// Capabilities -//-------------------------------------------------------------------------------- - -PROGMEM const char espurna_modules[] = - #if ALEXA_SUPPORT - "ALEXA " - #endif - #if API_SUPPORT - "API " - #endif - #if BROKER_SUPPORT - "BROKER " - #endif - #if BUTTON_SUPPORT - "BUTTON " - #endif - #if DEBUG_SERIAL_SUPPORT - "DEBUG_SERIAL " - #endif - #if DEBUG_TELNET_SUPPORT - "DEBUG_TELNET " - #endif - #if DEBUG_UDP_SUPPORT - "DEBUG_UDP " - #endif - #if DEBUG_WEB_SUPPORT - "DEBUG_WEB " - #endif - #if DOMOTICZ_SUPPORT - "DOMOTICZ " - #endif - #if ENCODER_SUPPORT - "ENCODER " - #endif - #if HOMEASSISTANT_SUPPORT - "HOMEASSISTANT " - #endif - #if I2C_SUPPORT - "I2C " - #endif - #if INFLUXDB_SUPPORT - "INFLUXDB " - #endif - #if IR_SUPPORT - "IR " - #endif - #if LED_SUPPORT - "LED " - #endif - #if LLMNR_SUPPORT - "LLMNR " - #endif - #if MDNS_CLIENT_SUPPORT - "MDNS_CLIENT " - #endif - #if MDNS_SERVER_SUPPORT - "MDNS_SERVER " - #endif - #if MQTT_SUPPORT - "MQTT " - #endif - #if NETBIOS_SUPPORT - "NETBIOS " - #endif - #if NOFUSS_SUPPORT - "NOFUSS " - #endif - #if NTP_SUPPORT - "NTP " - #endif - #if RFM69_SUPPORT - "RFM69 " - #endif - #if RF_SUPPORT - "RF " - #endif - #if SCHEDULER_SUPPORT - "SCHEDULER " - #endif - #if SENSOR_SUPPORT - "SENSOR " - #endif - #if SPIFFS_SUPPORT - "SPIFFS " - #endif - #if SSDP_SUPPORT - "SSDP " - #endif - #if TELNET_SUPPORT - #if TELNET_SERVER == TELNET_SERVER_WIFISERVER - "TELNET_SYNC " - #else - "TELNET " - #endif // TELNET_SERVER == TELNET_SERVER_WIFISERVER - #endif - #if TERMINAL_SUPPORT - "TERMINAL " - #endif - #if THERMOSTAT_SUPPORT - "THERMOSTAT " - #endif - #if THERMOSTAT_DISPLAY_SUPPORT - "THERMOSTAT_DISPLAY " - #endif - #if THINGSPEAK_SUPPORT - "THINGSPEAK " - #endif - #if UART_MQTT_SUPPORT - "UART_MQTT " - #endif - #if WEB_SUPPORT - "WEB " - #endif - ""; - -PROGMEM const char espurna_ota_modules[] = - #if OTA_ARDUINOOTA_SUPPORT - "ARDUINO " - #endif - #if (OTA_CLIENT == OTA_CLIENT_ASYNCTCP) - "ASYNCTCP " - #endif - #if (OTA_CLIENT == OTA_CLIENT_HTTPUPDATE) - #if (SECURE_CLIENT == SECURE_CLIENT_NONE) - "*HTTPUPDATE " - #endif - #if (SECURE_CLIENT == SECURE_CLIENT_AXTLS) - "*HTTPUPDATE_AXTLS " - #endif - #if (SECURE_CLIENT == SECURE_CLIENT_BEARSSL) - "*HTTPUPDATE_BEARSSL " - #endif - #endif // OTA_CLIENT_HTTPUPDATE - #if OTA_MQTT_SUPPORT - "MQTT " - #endif - #if WEB_SUPPORT - "WEB " - #endif - ""; - -//-------------------------------------------------------------------------------- -// Sensors -//-------------------------------------------------------------------------------- - -#if SENSOR_SUPPORT - -PROGMEM const char espurna_sensors[] = - #if AM2320_SUPPORT - "AM2320_I2C " - #endif - #if ANALOG_SUPPORT - "ANALOG " - #endif - #if BH1750_SUPPORT - "BH1750 " - #endif - #if BMP180_SUPPORT - "BMP180 " - #endif - #if BMX280_SUPPORT - "BMX280 " - #endif - #if CSE7766_SUPPORT - "CSE7766 " - #endif - #if DALLAS_SUPPORT - "DALLAS " - #endif - #if DHT_SUPPORT - "DHTXX " - #endif - #if DIGITAL_SUPPORT - "DIGITAL " - #endif - #if ECH1560_SUPPORT - "ECH1560 " - #endif - #if EMON_ADC121_SUPPORT - "EMON_ADC121 " - #endif - #if EMON_ADS1X15_SUPPORT - "EMON_ADX1X15 " - #endif - #if EMON_ANALOG_SUPPORT - "EMON_ANALOG " - #endif - #if EVENTS_SUPPORT - "EVENTS " - #endif - #if GEIGER_SUPPORT - "GEIGER " - #endif - #if GUVAS12SD_SUPPORT - "GUVAS12SD " - #endif - #if HLW8012_SUPPORT - "HLW8012 " - #endif - #if LDR_SUPPORT - "LDR " - #endif - #if MHZ19_SUPPORT - "MHZ19 " - #endif - #if MICS2710_SUPPORT - "MICS2710 " - #endif - #if MICS5525_SUPPORT - "MICS5525 " - #endif - #if NTC_SUPPORT - "NTC " - #endif - #if PMSX003_SUPPORT - "PMSX003 " - #endif - #if PULSEMETER_SUPPORT - "PULSEMETER " - #endif - #if PZEM004T_SUPPORT - "PZEM004T " - #endif - #if SDS011_SUPPORT - "SDS011 " - #endif - #if SENSEAIR_SUPPORT - "SENSEAIR " - #endif - #if SHT3X_I2C_SUPPORT - "SHT3X_I2C " - #endif - #if SI7021_SUPPORT - "SI7021 " - #endif - #if SONAR_SUPPORT - "SONAR " - #endif - #if T6613_SUPPORT - "T6613 " - #endif - #if TMP3X_SUPPORT - "TMP3X " - #endif - #if V9261F_SUPPORT - "V9261F " - #endif - #if VEML6075_SUPPORT - "VEML6075 " - #endif - #if VL53L1X_SUPPORT - "VL53L1X " - #endif - #if EZOPH_SUPPORT - "EZOPH " - #endif - #if ADE7953_SUPPORT - "ADE7953 " - #endif - ""; - - -PROGMEM const unsigned char magnitude_decimals[] = { - 0, - 1, 0, 2, // THP - 3, 0, 0, 0, 0, 0, 0, 0, // Power decimals - 0, 0, 0, // analog, digital, event - 0, 0, 0, // PM - 0, 0, - 0, 0, 3, // UVA, UVB, UVI - 3, 0, - 4, 4, // Geiger Counter decimals - 0, - 0, 0, 0, 3 // NO2, CO, Ohms, pH -}; - -PROGMEM const char magnitude_unknown_topic[] = "unknown"; -PROGMEM const char magnitude_temperature_topic[] = "temperature"; -PROGMEM const char magnitude_humidity_topic[] = "humidity"; -PROGMEM const char magnitude_pressure_topic[] = "pressure"; -PROGMEM const char magnitude_current_topic[] = "current"; -PROGMEM const char magnitude_voltage_topic[] = "voltage"; -PROGMEM const char magnitude_active_power_topic[] = "power"; -PROGMEM const char magnitude_apparent_power_topic[] = "apparent"; -PROGMEM const char magnitude_reactive_power_topic[] = "reactive"; -PROGMEM const char magnitude_power_factor_topic[] = "factor"; -PROGMEM const char magnitude_energy_topic[] = "energy"; -PROGMEM const char magnitude_energy_delta_topic[] = "energy_delta"; -PROGMEM const char magnitude_analog_topic[] = "analog"; -PROGMEM const char magnitude_digital_topic[] = "digital"; -PROGMEM const char magnitude_event_topic[] = "event"; -PROGMEM const char magnitude_pm1dot0_topic[] = "pm1dot0"; -PROGMEM const char magnitude_pm2dot5_topic[] = "pm2dot5"; -PROGMEM const char magnitude_pm10_topic[] = "pm10"; -PROGMEM const char magnitude_co2_topic[] = "co2"; -PROGMEM const char magnitude_lux_topic[] = "lux"; -PROGMEM const char magnitude_uva_topic[] = "uva"; -PROGMEM const char magnitude_uvb_topic[] = "uvb"; -PROGMEM const char magnitude_uvi_topic[] = "uvi"; -PROGMEM const char magnitude_distance_topic[] = "distance"; -PROGMEM const char magnitude_hcho_topic[] = "hcho"; -PROGMEM const char magnitude_geiger_cpm_topic[] = "ldr_cpm"; // local dose rate [Counts per minute] -PROGMEM const char magnitude_geiger_sv_topic[] = "ldr_uSvh"; // local dose rate [µSievert per hour] -PROGMEM const char magnitude_count_topic[] = "count"; -PROGMEM const char magnitude_no2_topic[] = "no2"; -PROGMEM const char magnitude_co_topic[] = "co"; -PROGMEM const char magnitude_resistance_topic[] = "resistance"; -PROGMEM const char magnitude_ph_topic[] = "ph"; - -PROGMEM const char* const magnitude_topics[] = { - magnitude_unknown_topic, magnitude_temperature_topic, magnitude_humidity_topic, - magnitude_pressure_topic, magnitude_current_topic, magnitude_voltage_topic, - magnitude_active_power_topic, magnitude_apparent_power_topic, magnitude_reactive_power_topic, - magnitude_power_factor_topic, magnitude_energy_topic, magnitude_energy_delta_topic, - magnitude_analog_topic, magnitude_digital_topic, magnitude_event_topic, - magnitude_pm1dot0_topic, magnitude_pm2dot5_topic, magnitude_pm10_topic, - magnitude_co2_topic, magnitude_lux_topic, - magnitude_uva_topic, magnitude_uvb_topic, magnitude_uvi_topic, - magnitude_distance_topic, magnitude_hcho_topic, - magnitude_geiger_cpm_topic, magnitude_geiger_sv_topic, - magnitude_count_topic, - magnitude_no2_topic, magnitude_co_topic, magnitude_resistance_topic, magnitude_ph_topic -}; - -PROGMEM const char magnitude_empty[] = ""; -PROGMEM const char magnitude_celsius[] = "°C"; -PROGMEM const char magnitude_fahrenheit[] = "°F"; -PROGMEM const char magnitude_percentage[] = "%"; -PROGMEM const char magnitude_hectopascals[] = "hPa"; -PROGMEM const char magnitude_amperes[] = "A"; -PROGMEM const char magnitude_volts[] = "V"; -PROGMEM const char magnitude_watts[] = "W"; -PROGMEM const char magnitude_kw[] = "kW"; -PROGMEM const char magnitude_joules[] = "J"; -PROGMEM const char magnitude_kwh[] = "kWh"; -PROGMEM const char magnitude_ugm3[] = "µg/m³"; -PROGMEM const char magnitude_ppm[] = "ppm"; -PROGMEM const char magnitude_lux[] = "lux"; -PROGMEM const char magnitude_distance[] = "m"; -PROGMEM const char magnitude_mgm3[] = "mg/m³"; -PROGMEM const char magnitude_geiger_cpm[] = "cpm"; // Counts per Minute: Unit of local dose rate (Geiger counting) -PROGMEM const char magnitude_geiger_sv[] = "µSv/h"; // µSievert per hour: 2nd unit of local dose rate (Geiger counting) -PROGMEM const char magnitude_resistance[] = "ohm"; - - -PROGMEM const char* const magnitude_units[] = { - magnitude_empty, magnitude_celsius, magnitude_percentage, - magnitude_hectopascals, magnitude_amperes, magnitude_volts, - magnitude_watts, magnitude_watts, magnitude_watts, - magnitude_percentage, magnitude_joules, magnitude_joules, - magnitude_empty, magnitude_empty, magnitude_empty, - magnitude_ugm3, magnitude_ugm3, magnitude_ugm3, - magnitude_ppm, magnitude_lux, - magnitude_empty, magnitude_empty, magnitude_empty, - magnitude_distance, magnitude_mgm3, - magnitude_geiger_cpm, magnitude_geiger_sv, // Geiger counter units - magnitude_empty, // - magnitude_ppm, magnitude_ppm, // NO2 & CO2 - magnitude_resistance, - magnitude_empty // pH -}; - -#endif diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h deleted file mode 100644 index efa57913..00000000 --- a/code/espurna/config/prototypes.h +++ /dev/null @@ -1,319 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -extern "C" { - #include "user_interface.h" - extern struct rst_info resetInfo; -} - -#define UNUSED(x) (void)(x) - -// ----------------------------------------------------------------------------- -// System -// ----------------------------------------------------------------------------- - -#define LWIP_INTERNAL -#include -#undef LWIP_INTERNAL - -extern "C" { - #include - #include - #include - #include // ip_addr_t - #include // ERR_x - #include // dns_gethostbyname - #include // ip4/ip6 helpers - #include // LWIP_VERSION_MAJOR -} - -// ref: https://github.com/me-no-dev/ESPAsyncTCP/pull/115/files#diff-e2e636049095cc1ff920c1bfabf6dcacR8 -// This is missing with Core 2.3.0 and is sometimes missing from the build flags. Assume HIGH_BANDWIDTH version. -#ifndef TCP_MSS -#define TCP_MSS (1460) -#endif - -// ----------------------------------------------------------------------------- -// PROGMEM -// ----------------------------------------------------------------------------- - -#include - -// ref: https://github.com/esp8266/Arduino/blob/master/tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h -// __STRINGIZE && __STRINGIZE_NX && PROGMEM definitions port - -// Do not replace macros unless running version older than 2.5.0 -#if defined(ARDUINO_ESP8266_RELEASE_2_3_0) \ - || defined(ARDUINO_ESP8266_RELEASE_2_4_0) \ - || defined(ARDUINO_ESP8266_RELEASE_2_4_1) \ - || defined(ARDUINO_ESP8266_RELEASE_2_4_2) - -// Quoting esp8266/Arduino comments: -// "Since __section__ is supposed to be only use for global variables, -// there could be conflicts when a static/inlined function has them in the -// same file as a non-static PROGMEM object. -// Ref: https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Variable-Attributes.html -// Place each progmem object into its own named section, avoiding conflicts" - -#define __TO_STR_(A) #A -#define __TO_STR(A) __TO_STR_(A) - -#undef PROGMEM -#define PROGMEM __attribute__((section( "\".irom.text." __FILE__ "." __TO_STR(__LINE__) "." __TO_STR(__COUNTER__) "\""))) - -// "PSTR() macro modified to start on a 32-bit boundary. This adds on average -// 1.5 bytes/string, but in return memcpy_P and strcpy_P will work 4~8x faster" -#undef PSTR -#define PSTR(s) (__extension__({static const char __c[] __attribute__((__aligned__(4))) PROGMEM = (s); &__c[0];})) - -#endif - -// ----------------------------------------------------------------------------- -// API -// ----------------------------------------------------------------------------- - -using api_get_callback_f = std::function; -using api_put_callback_f = std::function ; - -#if WEB_SUPPORT - void apiRegister(const char * key, api_get_callback_f getFn, api_put_callback_f putFn = NULL); -#endif - -// Core version 2.4.2 and higher changed the cont_t structure to a pointer: -// https://github.com/esp8266/Arduino/commit/5d5ea92a4d004ab009d5f642629946a0cb8893dd#diff-3fa12668b289ccb95b7ab334833a4ba8L35 -// Core version 2.5.0 introduced EspClass helper method: -// https://github.com/esp8266/Arduino/commit/0e0e34c614fe8a47544c9998201b1d9b3c24eb18 -extern "C" { - #include -#if defined(ARDUINO_ESP8266_RELEASE_2_3_0) \ - || defined(ARDUINO_ESP8266_RELEASE_2_4_0) \ - || defined(ARDUINO_ESP8266_RELEASE_2_4_1) - extern cont_t g_cont; - #define getFreeStack() cont_get_free_stack(&g_cont) -#elif defined(ARDUINO_ESP8266_RELEASE_2_4_2) - extern cont_t* g_pcont; - #define getFreeStack() cont_get_free_stack(g_pcont) -#else - #define getFreeStack() ESP.getFreeContStack() -#endif -} - -void infoMemory(const char* , unsigned int, unsigned int); -unsigned int getFreeHeap(); -unsigned int getInitialFreeHeap(); - -// ----------------------------------------------------------------------------- -// Domoticz -// ----------------------------------------------------------------------------- -#if DOMOTICZ_SUPPORT - template void domoticzSend(const char * key, T value); - template void domoticzSend(const char * key, T nvalue, const char * svalue); -#endif - -// ----------------------------------------------------------------------------- -// EEPROM_ROTATE -// ----------------------------------------------------------------------------- -#include -EEPROM_Rotate EEPROMr; - -void eepromSectorsDebug(); - -// ----------------------------------------------------------------------------- -// GPIO -// ----------------------------------------------------------------------------- -bool gpioValid(unsigned char gpio); -bool gpioGetLock(unsigned char gpio); -bool gpioReleaseLock(unsigned char gpio); - -// ----------------------------------------------------------------------------- -// Homeassistant -// ----------------------------------------------------------------------------- -struct ha_config_t; -void haSetup(); - -// ----------------------------------------------------------------------------- -// I2C -// ----------------------------------------------------------------------------- -void i2cScan(); -void i2cClearBus(); -bool i2cGetLock(unsigned char address); -bool i2cReleaseLock(unsigned char address); -unsigned char i2cFindAndLock(size_t size, unsigned char * addresses); - -void i2c_wakeup(uint8_t address); -uint8_t i2c_write_buffer(uint8_t address, uint8_t * buffer, size_t len); -uint8_t i2c_write_uint8(uint8_t address, uint8_t value); -uint8_t i2c_write_uint8(uint8_t address, uint8_t reg, uint8_t value); -uint8_t i2c_write_uint8(uint8_t address, uint8_t reg, uint8_t value1, uint8_t value2); -uint8_t i2c_write_uint16(uint8_t address, uint16_t value); -uint8_t i2c_write_uint16(uint8_t address, uint8_t reg, uint16_t value); -uint8_t i2c_read_uint8(uint8_t address); -uint8_t i2c_read_uint8(uint8_t address, uint8_t reg); -uint16_t i2c_read_uint16(uint8_t address); -uint16_t i2c_read_uint16(uint8_t address, uint8_t reg); -uint16_t i2c_read_uint16_le(uint8_t address, uint8_t reg); -int16_t i2c_read_int16(uint8_t address, uint8_t reg); -int16_t i2c_read_int16_le(uint8_t address, uint8_t reg); -void i2c_read_buffer(uint8_t address, uint8_t * buffer, size_t len); - -// ----------------------------------------------------------------------------- -// MQTT -// ----------------------------------------------------------------------------- - -#if MQTT_LIBRARY == MQTT_LIBRARY_ASYNCMQTTCLIENT - #include - #include -#elif MQTT_LIBRARY == MQTT_LIBRARY_ARDUINOMQTT - #include -#elif MQTT_LIBRARY == MQTT_LIBRARY_PUBSUBCLIENT - #include -#endif - -using mqtt_callback_f = std::function; -using mqtt_msg_t = std::pair; // topic, payload - -void mqttRegister(mqtt_callback_f callback); - -String mqttTopic(const char * magnitude, bool is_set); -String mqttTopic(const char * magnitude, unsigned int index, bool is_set); - -String mqttMagnitude(char * topic); - -bool mqttSendRaw(const char * topic, const char * message, bool retain); -bool mqttSendRaw(const char * topic, const char * message); - -void mqttSend(const char * topic, const char * message, bool force, bool retain); -void mqttSend(const char * topic, const char * message, bool force); -void mqttSend(const char * topic, const char * message); - -void mqttSend(const char * topic, unsigned int index, const char * message, bool force); -void mqttSend(const char * topic, unsigned int index, const char * message); - -const String& mqttPayloadOnline(); -const String& mqttPayloadOffline(); -const char* mqttPayloadStatus(bool status); - -void mqttSendStatus(); - -// ----------------------------------------------------------------------------- -// OTA -// ----------------------------------------------------------------------------- - -#include - -#if OTA_CLIENT == OTA_CLIENT_ASYNCTCP - #include -#endif - -#if OTA_CLIENT == OTA_CLIENT_HTTPUPDATE - #include - #include -#endif - -#if SECURE_CLIENT != SECURE_CLIENT_NONE - #include -#endif // SECURE_CLIENT != SECURE_CLIENT_NONE - -// ----------------------------------------------------------------------------- -// RFM69 -// ----------------------------------------------------------------------------- -typedef struct { - unsigned long messageID; - unsigned char packetID; - unsigned char senderID; - unsigned char targetID; - char * key; - char * value; - int16_t rssi; -} packet_t; - -// ----------------------------------------------------------------------------- -// Terminal -// ----------------------------------------------------------------------------- - -class Embedis; // FIXME: order -using embedis_command_f = void (*)(Embedis*); - -void terminalRegisterCommand(const String& name, embedis_command_f func); -void terminalInject(void *data, size_t len); -Stream& terminalSerial(); - -// ----------------------------------------------------------------------------- -// WebServer -// ----------------------------------------------------------------------------- - -class AsyncClient; -class AsyncWebServer; - -#if WEB_SUPPORT - #include - AsyncWebServer * webServer(); -#else - class AsyncWebServerRequest; - class ArRequestHandlerFunction; - class AsyncWebSocketClient; - class AsyncWebSocket; - class AwsEventType; -#endif - -using web_body_callback_f = std::function; -using web_request_callback_f = std::function; -void webBodyRegister(web_body_callback_f); -void webRequestRegister(web_request_callback_f); - -// ----------------------------------------------------------------------------- -// WIFI -// ----------------------------------------------------------------------------- -#include -struct wifi_scan_info_t; -using wifi_scan_f = std::function; -using wifi_callback_f = std::function; -void wifiRegister(wifi_callback_f callback); -bool wifiConnected(); - -#if LWIP_VERSION_MAJOR == 1 -#include -#else // LWIP_VERSION_MAJOR >= 2 -#include -#endif - -// ----------------------------------------------------------------------------- -// THERMOSTAT -// ----------------------------------------------------------------------------- -using thermostat_callback_f = std::function; -#if THERMOSTAT_SUPPORT - void thermostatRegister(thermostat_callback_f callback); -#endif - -// ----------------------------------------------------------------------------- -// RTC MEMORY -// ----------------------------------------------------------------------------- -#include "rtcmem.h" - -// ----------------------------------------------------------------------------- -// Warn about broken Arduino functions -// ----------------------------------------------------------------------------- - -// Division by zero bug -// https://github.com/esp8266/Arduino/pull/2397 -// https://github.com/esp8266/Arduino/pull/2408 -#if defined(ARDUINO_ESP8266_RELEASE_2_3_0) -long __attribute__((deprecated("Please avoid using map() with Core 2.3.0"))) map(long x, long in_min, long in_max, long out_min, long out_max); -#endif - -// ----------------------------------------------------------------------------- -// std::make_unique backport for C++11 -// ----------------------------------------------------------------------------- -#if 201103L >= __cplusplus -namespace std { - template - std::unique_ptr make_unique(Args&&... args) { - return std::unique_ptr(new T(std::forward(args)...)); - } -} -#endif diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 9d0ead61..a19b486f 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -2,6 +2,8 @@ // SENSORS - General configuration // ============================================================================= +#pragma once + #ifndef SENSOR_DEBUG #define SENSOR_DEBUG 0 // Debug sensors #endif diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h index 071aadd2..38d28aa6 100644 --- a/code/espurna/config/types.h +++ b/code/espurna/config/types.h @@ -3,6 +3,8 @@ // Do not touch this definitions //------------------------------------------------------------------------------ +#pragma once + // ----------------------------------------------------------------------------- // WIFI // ----------------------------------------------------------------------------- diff --git a/code/espurna/config/version.h b/code/espurna/config/version.h index 901e4dd5..33349d95 100644 --- a/code/espurna/config/version.h +++ b/code/espurna/config/version.h @@ -1,3 +1,9 @@ +// ----------------------------------------------------------------------------- +// APP NAME AND VERSION +// ----------------------------------------------------------------------------- + +#pragma once + #define APP_NAME "ESPURNA" #define APP_VERSION "1.14.2-dev" #define APP_AUTHOR "xose.perez@gmail.com" diff --git a/code/espurna/config/webui.h b/code/espurna/config/webui.h index 91b06bca..2407e850 100644 --- a/code/espurna/config/webui.h +++ b/code/espurna/config/webui.h @@ -2,6 +2,8 @@ // WEB UI IMAGE // ----------------------------------------------------------------------------- +#pragma once + #define WEBUI_IMAGE_SMALL 0 #define WEBUI_IMAGE_LIGHT 1 #define WEBUI_IMAGE_SENSOR 2 @@ -67,31 +69,3 @@ #define WEBUI_IMAGE WEBUI_IMAGE_SMALL #endif -#include - -PROGMEM const char espurna_webui[] = - #if WEBUI_IMAGE == WEBUI_IMAGE_SMALL - "SMALL" - #endif - #if WEBUI_IMAGE == WEBUI_IMAGE_LIGHT - "LIGHT" - #endif - #if WEBUI_IMAGE == WEBUI_IMAGE_SENSOR - "SENSOR" - #endif - #if WEBUI_IMAGE == WEBUI_IMAGE_RFBRIDGE - "RFBRIDGE" - #endif - #if WEBUI_IMAGE == WEBUI_IMAGE_RFM69 - "RFM69" - #endif - #if WEBUI_IMAGE == WEBUI_IMAGE_LIGHTFOX - "LIGHTFOX" - #endif - #if WEBUI_IMAGE == WEBUI_IMAGE_THERMOSTAT - "THERMOSTAT" - #endif - #if WEBUI_IMAGE == WEBUI_IMAGE_FULL - "FULL" - #endif - ""; diff --git a/code/espurna/crash.h b/code/espurna/crash.h new file mode 100644 index 00000000..707127b2 --- /dev/null +++ b/code/espurna/crash.h @@ -0,0 +1,48 @@ +// ----------------------------------------------------------------------------- +// Save crash info +// Taken from krzychb EspSaveCrash +// https://github.com/krzychb/EspSaveCrash +// ----------------------------------------------------------------------------- + +#pragma once + +#define SAVE_CRASH_EEPROM_OFFSET 0x0100 // initial address for crash data + +/** + * Structure of the single crash data set + * + * 1. Crash time + * 2. Restart reason + * 3. Exception cause + * 4. epc1 + * 5. epc2 + * 6. epc3 + * 7. excvaddr + * 8. depc + * 9. adress of stack start + * 10. adress of stack end + * 11. stack trace size + * 12. stack trace bytes + * ... + */ +#define SAVE_CRASH_CRASH_TIME 0x00 // 4 bytes +#define SAVE_CRASH_RESTART_REASON 0x04 // 1 byte +#define SAVE_CRASH_EXCEPTION_CAUSE 0x05 // 1 byte +#define SAVE_CRASH_EPC1 0x06 // 4 bytes +#define SAVE_CRASH_EPC2 0x0A // 4 bytes +#define SAVE_CRASH_EPC3 0x0E // 4 bytes +#define SAVE_CRASH_EXCVADDR 0x12 // 4 bytes +#define SAVE_CRASH_DEPC 0x16 // 4 bytes +#define SAVE_CRASH_STACK_START 0x1A // 4 bytes +#define SAVE_CRASH_STACK_END 0x1E // 4 bytes +#define SAVE_CRASH_STACK_SIZE 0x22 // 2 bytes +#define SAVE_CRASH_STACK_TRACE 0x24 // variable + +constexpr size_t crashUsedSpace() { + return (SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_SIZE + 2); +} + + +void crashClear(); +void crashDump(); +void crashSetup(); diff --git a/code/espurna/crash.ino b/code/espurna/crash.ino index 1c9ff660..d2054a3c 100644 --- a/code/espurna/crash.ino +++ b/code/espurna/crash.ino @@ -8,43 +8,10 @@ #include #include -#include -extern "C" { - #include "user_interface.h" -} - -#define SAVE_CRASH_EEPROM_OFFSET 0x0100 // initial address for crash data - -/** - * Structure of the single crash data set - * - * 1. Crash time - * 2. Restart reason - * 3. Exception cause - * 4. epc1 - * 5. epc2 - * 6. epc3 - * 7. excvaddr - * 8. depc - * 9. adress of stack start - * 10. adress of stack end - * 11. stack trace size - * 12. stack trace bytes - * ... - */ -#define SAVE_CRASH_CRASH_TIME 0x00 // 4 bytes -#define SAVE_CRASH_RESTART_REASON 0x04 // 1 byte -#define SAVE_CRASH_EXCEPTION_CAUSE 0x05 // 1 byte -#define SAVE_CRASH_EPC1 0x06 // 4 bytes -#define SAVE_CRASH_EPC2 0x0A // 4 bytes -#define SAVE_CRASH_EPC3 0x0E // 4 bytes -#define SAVE_CRASH_EXCVADDR 0x12 // 4 bytes -#define SAVE_CRASH_DEPC 0x16 // 4 bytes -#define SAVE_CRASH_STACK_START 0x1A // 4 bytes -#define SAVE_CRASH_STACK_END 0x1E // 4 bytes -#define SAVE_CRASH_STACK_SIZE 0x22 // 2 bytes -#define SAVE_CRASH_STACK_TRACE 0x24 // variable +#include "system.h" +#include "storage_eeprom.h" +#include "crash.h" uint16_t _save_crash_stack_trace_max = SAVE_CRASH_STACK_TRACE_MAX; bool _save_crash_enabled = true; @@ -171,10 +138,6 @@ void crashDump() { } -constexpr size_t crashUsedSpace() { - return (SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_SIZE + 2); -} - void crashSetup() { #if TERMINAL_SUPPORT diff --git a/code/espurna/domoticz.h b/code/espurna/domoticz.h new file mode 100644 index 00000000..2073056f --- /dev/null +++ b/code/espurna/domoticz.h @@ -0,0 +1,28 @@ +/* + +DOMOTICZ MODULE + +Copyright (C) 2016-2019 by Xose Pérez + +*/ + +#pragma once + +#if DOMOTICZ_SUPPORT + +#include + +#include + +template +void domoticzSend(const char * key, T value); +template +void domoticzSend(const char * key, T nvalue, const char * svalue); + +void domoticzSendRelay(unsigned char relayID, bool status); +void domoticzSendRelays(); + +void domoticzSetup(); +bool domoticzEnabled(); + +#endif // DOMOTICZ_SUPPORT == 1 diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index 439ae312..34c8e5c7 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -8,11 +8,10 @@ Copyright (C) 2016-2019 by Xose Pérez #if DOMOTICZ_SUPPORT -#include "relay.h" #include "broker.h" - -#include -#include +#include "domoticz.h" +#include "mqtt.h" +#include "relay.h" bool _dcz_enabled = false; std::bitset _dcz_relay_state; diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index ac8bcd86..f50a9d12 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -21,20 +21,44 @@ along with this program. If not, see . #include "config/all.h" +#include +#include +#include +#include +#include + #include "board.h" +#include "compat.h" +#include "storage_eeprom.h" +#include "gpio.h" +#include "settings.h" +#include "system.h" +#include "terminal.h" +#include "utils.h" +#include "wifi.h" + +#include "alexa.h" +#include "api.h" #include "broker.h" #include "button.h" #include "debug.h" +#include "domoticz.h" +#include "homeassistant.h" +#include "i2c.h" +#include "ir.h" #include "led.h" +#include "mqtt.h" #include "ntp.h" +#include "ota.h" #include "relay.h" +#include "rfm69.h" #include "rpnrules.h" -#include "settings.h" -#include "system.h" +#include "rtcmem.h" +#include "thermostat.h" #include "tuya.h" -#include "utils.h" -#include "wifi.h" +#include "web.h" #include "ws.h" + #include "libs/HeapStats.h" using void_callback_f = void (*)(); diff --git a/code/espurna/gpio.h b/code/espurna/gpio.h new file mode 100644 index 00000000..c66442c3 --- /dev/null +++ b/code/espurna/gpio.h @@ -0,0 +1,15 @@ +/* + +GPIO MODULE + +Copyright (C) 2017-2019 by Xose Pérez + +*/ + +#pragma once + +bool gpioValid(unsigned char gpio); +bool gpioGetLock(unsigned char gpio); +bool gpioReleaseLock(unsigned char gpio); + +void gpioSetup(); diff --git a/code/espurna/gpio.ino b/code/espurna/gpio.ino index ba0f63bb..d9066817 100644 --- a/code/espurna/gpio.ino +++ b/code/espurna/gpio.ino @@ -6,6 +6,8 @@ Copyright (C) 2017-2019 by Xose Pérez */ +#include "gpio.h" + #include constexpr const size_t GPIO_PINS = 17; diff --git a/code/espurna/homeassistant.h b/code/espurna/homeassistant.h new file mode 100644 index 00000000..7afc7da8 --- /dev/null +++ b/code/espurna/homeassistant.h @@ -0,0 +1,19 @@ +/* + +HOME ASSISTANT MODULE + +Copyright (C) 2017-2019 by Xose Pérez + +*/ + +#pragma once + +#if HOMEASSISTANT_SUPPORT + +#include + +struct ha_config_t; + +void haSetup(); + +#endif // HOMEASSISTANT_SUPPORT == 1 diff --git a/code/espurna/homeassistant.ino b/code/espurna/homeassistant.ino index b5d1d075..20eb00cb 100644 --- a/code/espurna/homeassistant.ino +++ b/code/espurna/homeassistant.ino @@ -10,8 +10,9 @@ Copyright (C) 2017-2019 by Xose Pérez #include #include -#include +#include "homeassistant.h" +#include "mqtt.h" #include "ws.h" bool _ha_enabled = false; diff --git a/code/espurna/i2c.h b/code/espurna/i2c.h new file mode 100644 index 00000000..da5b9352 --- /dev/null +++ b/code/espurna/i2c.h @@ -0,0 +1,42 @@ +/* + +I2C MODULE + +Copyright (C) 2017-2019 by Xose Pérez + +*/ + +#pragma once + +#if I2C_SUPPORT + +#if I2C_USE_BRZO +#include +#else +#include +#endif + +void i2c_wakeup(uint8_t address); +uint8_t i2c_write_buffer(uint8_t address, uint8_t * buffer, size_t len); +uint8_t i2c_write_uint8(uint8_t address, uint8_t value); +uint8_t i2c_write_uint8(uint8_t address, uint8_t reg, uint8_t value); +uint8_t i2c_write_uint8(uint8_t address, uint8_t reg, uint8_t value1, uint8_t value2); +uint8_t i2c_write_uint16(uint8_t address, uint16_t value); +uint8_t i2c_write_uint16(uint8_t address, uint8_t reg, uint16_t value); +uint8_t i2c_read_uint8(uint8_t address); +uint8_t i2c_read_uint8(uint8_t address, uint8_t reg); +uint16_t i2c_read_uint16(uint8_t address); +uint16_t i2c_read_uint16(uint8_t address, uint8_t reg); +uint16_t i2c_read_uint16_le(uint8_t address, uint8_t reg); +int16_t i2c_read_int16(uint8_t address, uint8_t reg); +int16_t i2c_read_int16_le(uint8_t address, uint8_t reg); +void i2c_read_buffer(uint8_t address, uint8_t * buffer, size_t len); + +void i2cScan(); +void i2cClearBus(); +bool i2cGetLock(unsigned char address); +bool i2cReleaseLock(unsigned char address); + +unsigned char i2cFindAndLock(size_t size, unsigned char * addresses); + +#endif // I2C_SUPPORT == 1 diff --git a/code/espurna/i2c.ino b/code/espurna/i2c.ino index 9bf0cb18..ba019e12 100644 --- a/code/espurna/i2c.ino +++ b/code/espurna/i2c.ino @@ -11,10 +11,7 @@ Copyright (C) 2017-2019 by Xose Pérez unsigned int _i2c_locked[16] = {0}; #if I2C_USE_BRZO -#include "brzo_i2c.h" unsigned long _i2c_scl_frequency = 0; -#else -#include "Wire.h" #endif // ----------------------------------------------------------------------------- diff --git a/code/espurna/ir.h b/code/espurna/ir.h new file mode 100644 index 00000000..2b67df3e --- /dev/null +++ b/code/espurna/ir.h @@ -0,0 +1,21 @@ +/* + +IR MODULE + +Copyright (C) 2018 by Alexander Kolesnikov (raw and MQTT implementation) +Copyright (C) 2017-2019 by François Déchery +Copyright (C) 2016-2019 by Xose Pérez + +*/ + +#pragma once + +#if IR_SUPPORT + +#include +#include +#include + +void irSetup(); + +#endif // IR_SUPPORT == 1 diff --git a/code/espurna/ir.ino b/code/espurna/ir.ino index c85cb17a..a85e8650 100644 --- a/code/espurna/ir.ino +++ b/code/espurna/ir.ino @@ -48,22 +48,16 @@ Raw messages: #if IR_SUPPORT +#include "ir.h" +#include "mqtt.h" #include "relay.h" -#include - #if defined(IR_RX_PIN) - - #include IRrecv _ir_receiver(IR_RX_PIN, IR_BUFFER_SIZE, IR_TIMEOUT, true); - decode_results _ir_results; - #endif // defined(IR_RX_PIN) #if defined(IR_TX_PIN) - - #include IRsend _ir_sender(IR_TX_PIN); #if IR_USE_RAW diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 3d1ce35d..4fbd9fd1 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -8,8 +8,9 @@ Copyright (C) 2016-2019 by Xose Pérez #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE -#include "tuya.h" #include "broker.h" +#include "mqtt.h" +#include "tuya.h" #include "ws.h" #include "light.h" diff --git a/code/espurna/mqtt.h b/code/espurna/mqtt.h new file mode 100644 index 00000000..0c79d08c --- /dev/null +++ b/code/espurna/mqtt.h @@ -0,0 +1,54 @@ +/* + +MQTT MODULE + +Copyright (C) 2016-2019 by Xose Pérez +Updated secure client support by Niek van der Maas < mail at niekvandermaas dot nl> + +*/ + +#pragma once + +#include + +#include +#include + +using mqtt_callback_f = std::function; +using mqtt_msg_t = std::pair; // topic, payload + +#if MQTT_SUPPORT + +#if MQTT_LIBRARY == MQTT_LIBRARY_ASYNCMQTTCLIENT + #include + #include +#elif MQTT_LIBRARY == MQTT_LIBRARY_ARDUINOMQTT + #include +#elif MQTT_LIBRARY == MQTT_LIBRARY_PUBSUBCLIENT + #include +#endif + +void mqttRegister(mqtt_callback_f callback); + +String mqttTopic(const char * magnitude, bool is_set); +String mqttTopic(const char * magnitude, unsigned int index, bool is_set); + +String mqttMagnitude(char * topic); + +bool mqttSendRaw(const char * topic, const char * message, bool retain); +bool mqttSendRaw(const char * topic, const char * message); + +void mqttSend(const char * topic, const char * message, bool force, bool retain); +void mqttSend(const char * topic, const char * message, bool force); +void mqttSend(const char * topic, const char * message); + +void mqttSend(const char * topic, unsigned int index, const char * message, bool force); +void mqttSend(const char * topic, unsigned int index, const char * message); + +const String& mqttPayloadOnline(); +const String& mqttPayloadOffline(); +const char* mqttPayloadStatus(bool status); + +void mqttSendStatus(); + +#endif // MQTT_SUPPORT == 1 diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index e1761e8b..eb8ed9d4 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -9,19 +9,19 @@ Updated secure client support by Niek van der Maas < mail at niekvandermaas dot #if MQTT_SUPPORT -#include -#include -#include -#include +#include "mqtt.h" + #include #include #include #include "system.h" -#include "libs/SecureClientHelpers.h" +#include "mqtt.h" #include "ntp.h" #include "ws.h" +#include "libs/SecureClientHelpers.h" + #if MQTT_LIBRARY == MQTT_LIBRARY_ASYNCMQTTCLIENT AsyncMqttClient _mqtt; diff --git a/code/espurna/ntp.ino b/code/espurna/ntp.ino index 354346f7..93689453 100644 --- a/code/espurna/ntp.ino +++ b/code/espurna/ntp.ino @@ -56,7 +56,7 @@ static time_t _ntp_ts = 0; static tm _ntp_tm_local; static tm _ntp_tm_utc; -static void _ntpTmCache(time_t ts) { +void _ntpTmCache(time_t ts) { if (_ntp_ts != ts) { _ntp_ts = ts; localtime_r(&_ntp_ts, &_ntp_tm_local); diff --git a/code/espurna/ota.h b/code/espurna/ota.h index bd469639..6c89aeef 100644 --- a/code/espurna/ota.h +++ b/code/espurna/ota.h @@ -1,79 +1,44 @@ /* -OTA COMMON FUNCTIONS +OTA MODULE */ #pragma once -#include "ws.h" #include -void otaPrintError() { - if (Update.hasError()) { - #if TERMINAL_SUPPORT - Update.printError(terminalSerial()); - #elif DEBUG_SERIAL_SUPPORT && defined(DEBUG_PORT) - Update.printError(DEBUG_PORT); - #endif - } -} - -bool otaFinalize(size_t size, int reason, bool evenIfRemaining = false) { - if (Update.isRunning() && Update.end(evenIfRemaining)) { - DEBUG_MSG_P(PSTR("[OTA] Success: %7u bytes\n"), size); - deferredReset(500, reason); - return true; - } - - otaPrintError(); - eepromRotate(true); - - return false; -} +#if OTA_ARDUINOOTA_SUPPORT + +#include +void arduinoOtaSetup(); + +#endif // OTA_ARDUINOOTA_SUPPORT == 1 + +#if OTA_CLIENT == OTA_CLIENT_ASYNCTCP + +#include +void otaClientSetup(); + +#endif // OTA_CLIENT == OTA_CLIENT_ASYNCTCP + +#if OTA_CLIENT == OTA_CLIENT_HTTPUPDATE + +#include +#include +void otaClientSetup(); + +#endif // OTA_CLIENT == OTA_CLIENT_HTTPUPDATE + +#if SECURE_CLIENT != SECURE_CLIENT_NONE +#include +#endif + +void otaPrintError(); +bool otaFinalize(size_t size, int reason, bool evenIfRemaining = false); // Helper methods from UpdaterClass that need to be called manually for async mode, // because we are not using Stream interface to feed it data. -bool otaVerifyHeader(uint8_t* data, size_t len) { - if (len < 4) { - return false; - } - - // ref: https://github.com/esp8266/Arduino/pull/6820 - // accept gzip, let unpacker figure things out later - if (data[0] == 0x1F && data[1] == 0x8B) { - return true; - } - - // Check for magic byte with a normal .bin - if (data[0] != 0xE9) { - return false; - } - - // Make sure that flash config can be recognized and fit the flash - const auto flash_config = ESP.magicFlashChipSize((data[3] & 0xf0) >> 4); - if (flash_config && (flash_config > ESP.getFlashChipRealSize())) { - return false; - } - - return true; -} - -void otaProgress(size_t bytes, size_t each = 8192u) { - // Removed to avoid websocket ping back during upgrade (see #1574) - // TODO: implement as separate from debugging message - #if WEB_SUPPORT - if (wsConnected()) return; - #endif - - // Telnet and serial will still output things, but slightly throttled - static size_t last = 0; - if (bytes < last) { - last = 0; - } - - if ((bytes > each) && (bytes - each > last)) { - DEBUG_MSG_P(PSTR("[OTA] Progress: %7u bytes\r"), bytes); - last = bytes; - } -} +bool otaVerifyHeader(uint8_t* data, size_t len); + +void otaProgress(size_t bytes, size_t each = 8192u); diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino new file mode 100644 index 00000000..1327c31b --- /dev/null +++ b/code/espurna/ota.ino @@ -0,0 +1,79 @@ +/* + +OTA MODULE COMMON FUNCTIONS + +*/ + +#include "ota.h" +#include "system.h" +#include "terminal.h" +#include "ws.h" + +void otaPrintError() { + if (Update.hasError()) { + #if TERMINAL_SUPPORT + Update.printError(terminalSerial()); + #elif DEBUG_SERIAL_SUPPORT && defined(DEBUG_PORT) + Update.printError(DEBUG_PORT); + #endif + } +} + +bool otaFinalize(size_t size, int reason, bool evenIfRemaining) { + if (Update.isRunning() && Update.end(evenIfRemaining)) { + DEBUG_MSG_P(PSTR("[OTA] Success: %7u bytes\n"), size); + deferredReset(500, reason); + return true; + } + + otaPrintError(); + eepromRotate(true); + + return false; +} + +// Helper methods from UpdaterClass that need to be called manually for async mode, +// because we are not using Stream interface to feed it data. +bool otaVerifyHeader(uint8_t* data, size_t len) { + if (len < 4) { + return false; + } + + // ref: https://github.com/esp8266/Arduino/pull/6820 + // accept gzip, let unpacker figure things out later + if (data[0] == 0x1F && data[1] == 0x8B) { + return true; + } + + // Check for magic byte with a normal .bin + if (data[0] != 0xE9) { + return false; + } + + // Make sure that flash config can be recognized and fit the flash + const auto flash_config = ESP.magicFlashChipSize((data[3] & 0xf0) >> 4); + if (flash_config && (flash_config > ESP.getFlashChipRealSize())) { + return false; + } + + return true; +} + +void otaProgress(size_t bytes, size_t each) { + // Removed to avoid websocket ping back during upgrade (see #1574) + // TODO: implement as separate from debugging message + #if WEB_SUPPORT + if (wsConnected()) return; + #endif + + // Telnet and serial will still output things, but slightly throttled + static size_t last = 0; + if (bytes < last) { + last = 0; + } + + if ((bytes > each) && (bytes - each > last)) { + DEBUG_MSG_P(PSTR("[OTA] Progress: %7u bytes\r"), bytes); + last = bytes; + } +} diff --git a/code/espurna/ota_arduinoota.ino b/code/espurna/ota_arduinoota.ino index c00f7949..3767760d 100644 --- a/code/espurna/ota_arduinoota.ino +++ b/code/espurna/ota_arduinoota.ino @@ -8,6 +8,7 @@ Copyright (C) 2016-2019 by Xose Pérez #if OTA_ARDUINOOTA_SUPPORT +#include "ota.h" #include "system.h" #include "ws.h" diff --git a/code/espurna/ota_asynctcp.ino b/code/espurna/ota_asynctcp.ino index 9fdfc711..901a300f 100644 --- a/code/espurna/ota_asynctcp.ino +++ b/code/espurna/ota_asynctcp.ino @@ -17,8 +17,10 @@ Copyright (C) 2016-2019 by Xose Pérez #include #include -#include "system.h" +#include "mqtt.h" #include "ota.h" +#include "system.h" +#include "terminal.h" #include "libs/URL.h" diff --git a/code/espurna/ota_httpupdate.ino b/code/espurna/ota_httpupdate.ino index 23021586..3b93ba50 100644 --- a/code/espurna/ota_httpupdate.ino +++ b/code/espurna/ota_httpupdate.ino @@ -14,11 +14,11 @@ Copyright (C) 2019 by Maxim Prokhorov #include -#include -#include -#include - +#include "mqtt.h" +#include "ota.h" #include "system.h" +#include "terminal.h" + #include "libs/URL.h" #include "libs/TypeChecks.h" #include "libs/SecureClientHelpers.h" diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 609e6707..67853084 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -6,15 +6,17 @@ Copyright (C) 2016-2019 by Xose Pérez */ -#include #include #include #include #include #include -#include "relay.h" #include "broker.h" +#include "storage_eeprom.h" +#include "settings.h" +#include "mqtt.h" +#include "relay.h" #include "tuya.h" #include "ws.h" diff --git a/code/espurna/rfbridge.h b/code/espurna/rfbridge.h new file mode 100644 index 00000000..efc355bc --- /dev/null +++ b/code/espurna/rfbridge.h @@ -0,0 +1,24 @@ +/* + +RF BRIDGE MODULE + +Copyright (C) 2016-2019 by Xose Pérez + +*/ + +#if RF_SUPPORT + +#if RFB_DIRECT +#include +#endif + +void rfbStatus(unsigned char id, bool status); +void rfbLearn(unsigned char id, bool status); + +String rfbRetrieve(unsigned char id, bool status); +void rfbStore(unsigned char id, bool status, const char * code); + +void rfbForget(unsigned char id, bool status); +void rfbSetup(); + +#endif // RF_SUPPORT == 1 diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index a6cd0c83..6cf5e5a3 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -10,11 +10,10 @@ Copyright (C) 2016-2019 by Xose Pérez #include -#if RFB_DIRECT -#include -#endif - #include "relay.h" +#include "rfbridge.h" +#include "terminal.h" +#include "mqtt.h" #include "ws.h" // ----------------------------------------------------------------------------- diff --git a/code/espurna/rfm69.h b/code/espurna/rfm69.h new file mode 100644 index 00000000..9e4acd2a --- /dev/null +++ b/code/espurna/rfm69.h @@ -0,0 +1,29 @@ +/* + +RFM69 MODULE + +Copyright (C) 2016-2017 by Xose Pérez + +*/ + +#pragma once + +struct packet_t; + +#if RFM69_SUPPORT + +#include "libs/RFM69Wrap.h" + +struct packet_t { + unsigned long messageID; + unsigned char packetID; + unsigned char senderID; + unsigned char targetID; + char * key; + char * value; + int16_t rssi; +}; + +void rfm69Setup(); + +#endif // RFM69_SUPPORT == 1 diff --git a/code/espurna/rfm69.ino b/code/espurna/rfm69.ino index fb51f428..94f900a0 100644 --- a/code/espurna/rfm69.ino +++ b/code/espurna/rfm69.ino @@ -8,7 +8,8 @@ Copyright (C) 2016-2017 by Xose Pérez #if RFM69_SUPPORT -#include "libs/RFM69Wrap.h" +#include "mqtt.h" +#include "rfm69.h" #include "ws.h" #define RFM69_PACKET_SEPARATOR ':' diff --git a/code/espurna/config/rtcmem.h b/code/espurna/rtcmem.h similarity index 77% rename from code/espurna/config/rtcmem.h rename to code/espurna/rtcmem.h index f5108257..67baccdd 100644 --- a/code/espurna/config/rtcmem.h +++ b/code/espurna/rtcmem.h @@ -1,3 +1,11 @@ +/* + +RTMEM MODULE + +Copyright (C) 2019 by Maxim Prokhorov + +*/ + #pragma once // Base address of USER RTC memory @@ -16,17 +24,20 @@ // Change this when modifying RtcmemData #define RTCMEM_MAGIC 0x45535075 -// XXX When using bitfields / inner structs / etc: +// XXX: All access must be 4-byte aligned and always at full length. +// +// For example, using bitfields / inner structs / etc: // ... // uint32_t a : 8; // uint32_t b : 8; // uint32_t c : 8; // uint32_t d : 8; // ... +// +// This would not write the expected thing: // mem->d = 4; -// At the same time writes 4 to the a, b and c -// TODO replace with custom memory segment in ldscript +// TODO replace with custom memory segment in ldscript? struct RtcmemData { uint32_t magic; uint32_t sys; diff --git a/code/espurna/rtcmem.ino b/code/espurna/rtcmem.ino index efa34295..c7d09758 100644 --- a/code/espurna/rtcmem.ino +++ b/code/espurna/rtcmem.ino @@ -2,6 +2,8 @@ RTMEM MODULE +Copyright (C) 2019 by Maxim Prokhorov + */ bool _rtcmem_status = false; diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 14f8b61c..3fb5baf3 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -11,12 +11,116 @@ Copyright (C) 2016-2019 by Xose Pérez #include #include -#include "relay.h" #include "broker.h" +#include "mqtt.h" +#include "relay.h" +#include "terminal.h" #include "ws.h" //-------------------------------------------------------------------------------- +PROGMEM const unsigned char magnitude_decimals[] = { + 0, + 1, 0, 2, // THP + 3, 0, 0, 0, 0, 0, 0, 0, // Power decimals + 0, 0, 0, // analog, digital, event + 0, 0, 0, // PM + 0, 0, + 0, 0, 3, // UVA, UVB, UVI + 3, 0, + 4, 4, // Geiger Counter decimals + 0, + 0, 0, 0, 3 // NO2, CO, Ohms, pH +}; + +PROGMEM const char magnitude_unknown_topic[] = "unknown"; +PROGMEM const char magnitude_temperature_topic[] = "temperature"; +PROGMEM const char magnitude_humidity_topic[] = "humidity"; +PROGMEM const char magnitude_pressure_topic[] = "pressure"; +PROGMEM const char magnitude_current_topic[] = "current"; +PROGMEM const char magnitude_voltage_topic[] = "voltage"; +PROGMEM const char magnitude_active_power_topic[] = "power"; +PROGMEM const char magnitude_apparent_power_topic[] = "apparent"; +PROGMEM const char magnitude_reactive_power_topic[] = "reactive"; +PROGMEM const char magnitude_power_factor_topic[] = "factor"; +PROGMEM const char magnitude_energy_topic[] = "energy"; +PROGMEM const char magnitude_energy_delta_topic[] = "energy_delta"; +PROGMEM const char magnitude_analog_topic[] = "analog"; +PROGMEM const char magnitude_digital_topic[] = "digital"; +PROGMEM const char magnitude_event_topic[] = "event"; +PROGMEM const char magnitude_pm1dot0_topic[] = "pm1dot0"; +PROGMEM const char magnitude_pm2dot5_topic[] = "pm2dot5"; +PROGMEM const char magnitude_pm10_topic[] = "pm10"; +PROGMEM const char magnitude_co2_topic[] = "co2"; +PROGMEM const char magnitude_lux_topic[] = "lux"; +PROGMEM const char magnitude_uva_topic[] = "uva"; +PROGMEM const char magnitude_uvb_topic[] = "uvb"; +PROGMEM const char magnitude_uvi_topic[] = "uvi"; +PROGMEM const char magnitude_distance_topic[] = "distance"; +PROGMEM const char magnitude_hcho_topic[] = "hcho"; +PROGMEM const char magnitude_geiger_cpm_topic[] = "ldr_cpm"; // local dose rate [Counts per minute] +PROGMEM const char magnitude_geiger_sv_topic[] = "ldr_uSvh"; // local dose rate [µSievert per hour] +PROGMEM const char magnitude_count_topic[] = "count"; +PROGMEM const char magnitude_no2_topic[] = "no2"; +PROGMEM const char magnitude_co_topic[] = "co"; +PROGMEM const char magnitude_resistance_topic[] = "resistance"; +PROGMEM const char magnitude_ph_topic[] = "ph"; + +PROGMEM const char* const magnitude_topics[] = { + magnitude_unknown_topic, magnitude_temperature_topic, magnitude_humidity_topic, + magnitude_pressure_topic, magnitude_current_topic, magnitude_voltage_topic, + magnitude_active_power_topic, magnitude_apparent_power_topic, magnitude_reactive_power_topic, + magnitude_power_factor_topic, magnitude_energy_topic, magnitude_energy_delta_topic, + magnitude_analog_topic, magnitude_digital_topic, magnitude_event_topic, + magnitude_pm1dot0_topic, magnitude_pm2dot5_topic, magnitude_pm10_topic, + magnitude_co2_topic, magnitude_lux_topic, + magnitude_uva_topic, magnitude_uvb_topic, magnitude_uvi_topic, + magnitude_distance_topic, magnitude_hcho_topic, + magnitude_geiger_cpm_topic, magnitude_geiger_sv_topic, + magnitude_count_topic, + magnitude_no2_topic, magnitude_co_topic, magnitude_resistance_topic, magnitude_ph_topic +}; + +PROGMEM const char magnitude_empty[] = ""; +PROGMEM const char magnitude_celsius[] = "°C"; +PROGMEM const char magnitude_fahrenheit[] = "°F"; +PROGMEM const char magnitude_percentage[] = "%"; +PROGMEM const char magnitude_hectopascals[] = "hPa"; +PROGMEM const char magnitude_amperes[] = "A"; +PROGMEM const char magnitude_volts[] = "V"; +PROGMEM const char magnitude_watts[] = "W"; +PROGMEM const char magnitude_kw[] = "kW"; +PROGMEM const char magnitude_joules[] = "J"; +PROGMEM const char magnitude_kwh[] = "kWh"; +PROGMEM const char magnitude_ugm3[] = "µg/m³"; +PROGMEM const char magnitude_ppm[] = "ppm"; +PROGMEM const char magnitude_lux[] = "lux"; +PROGMEM const char magnitude_distance[] = "m"; +PROGMEM const char magnitude_mgm3[] = "mg/m³"; +PROGMEM const char magnitude_geiger_cpm[] = "cpm"; // Counts per Minute: Unit of local dose rate (Geiger counting) +PROGMEM const char magnitude_geiger_sv[] = "µSv/h"; // µSievert per hour: 2nd unit of local dose rate (Geiger counting) +PROGMEM const char magnitude_resistance[] = "ohm"; + + +PROGMEM const char* const magnitude_units[] = { + magnitude_empty, magnitude_celsius, magnitude_percentage, + magnitude_hectopascals, magnitude_amperes, magnitude_volts, + magnitude_watts, magnitude_watts, magnitude_watts, + magnitude_percentage, magnitude_joules, magnitude_joules, + magnitude_empty, magnitude_empty, magnitude_empty, + magnitude_ugm3, magnitude_ugm3, magnitude_ugm3, + magnitude_ppm, magnitude_lux, + magnitude_empty, magnitude_empty, magnitude_empty, + magnitude_distance, magnitude_mgm3, + magnitude_geiger_cpm, magnitude_geiger_sv, // Geiger counter units + magnitude_empty, // + magnitude_ppm, magnitude_ppm, // NO2 & CO2 + magnitude_resistance, + magnitude_empty // pH +}; + +//-------------------------------------------------------------------------------- + #include "filters/LastFilter.h" #include "filters/MaxFilter.h" #include "filters/MedianFilter.h" diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index b6606b47..dea63fd8 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -10,6 +10,8 @@ Copyright (C) 2016-2019 by Xose Pérez #include +#include "storage_eeprom.h" + #include "settings_internal.h" #include "settings.h" diff --git a/code/espurna/storage_eeprom.h b/code/espurna/storage_eeprom.h new file mode 100644 index 00000000..a12958c4 --- /dev/null +++ b/code/espurna/storage_eeprom.h @@ -0,0 +1,24 @@ +/* + +EEPROM MODULE + +*/ + +// XXX: with case insensitive filesystem, if named eeprom.h *could* +// be included as EEPROM.h and fail after including rotate library down below + +#pragma once + +#include + +extern EEPROM_Rotate EEPROMr; + +void eepromSectorsDebug(); +void eepromRotate(bool value); +uint32_t eepromCurrent(); +String eepromSectors(); + +void eepromBackup(uint32_t index); +void eepromCommit(); + +void eepromSetup(); diff --git a/code/espurna/eeprom.ino b/code/espurna/storage_eeprom.ino similarity index 97% rename from code/espurna/eeprom.ino rename to code/espurna/storage_eeprom.ino index 9b03f80e..e4b65af8 100644 --- a/code/espurna/eeprom.ino +++ b/code/espurna/storage_eeprom.ino @@ -4,10 +4,10 @@ EEPROM MODULE */ -#include - -// ----------------------------------------------------------------------------- +#include "debug.h" +#include "storage_eeprom.h" +EEPROM_Rotate EEPROMr; bool _eeprom_commit = false; uint32_t _eeprom_commit_count = 0; diff --git a/code/espurna/system.h b/code/espurna/system.h index c28e840a..daf66cec 100644 --- a/code/espurna/system.h +++ b/code/espurna/system.h @@ -8,8 +8,16 @@ Copyright (C) 2019 by Xose Pérez #pragma once +#include +#include + #include +extern "C" { + #include "user_interface.h" + extern struct rst_info resetInfo; +} + uint32_t systemResetReason(); uint8_t systemStabilityCounter(); void systemStabilityCounter(uint8_t count); diff --git a/code/espurna/terminal.h b/code/espurna/terminal.h new file mode 100644 index 00000000..f1516847 --- /dev/null +++ b/code/espurna/terminal.h @@ -0,0 +1,21 @@ +/* + +TERMINAL MODULE + +Copyright (C) 2016-2019 by Xose Pérez + +*/ + +#pragma once + +#if TERMINAL_SUPPORT + +#include "libs/EmbedisWrap.h" + +using embedis_command_f = void (*)(Embedis*); + +void terminalRegisterCommand(const String& name, embedis_command_f func); +void terminalInject(void *data, size_t len); +Stream& terminalSerial(); + +#endif // TERMINAL_SUPPORT == 1 diff --git a/code/espurna/terminal.ino b/code/espurna/terminal.ino index 5d24584d..d5f0751e 100644 --- a/code/espurna/terminal.ino +++ b/code/espurna/terminal.ino @@ -10,6 +10,7 @@ Copyright (C) 2016-2019 by Xose Pérez #include "settings.h" #include "system.h" +#include "terminal.h" #include "utils.h" #include "libs/StreamInjector.h" #include "libs/HeapStats.h" diff --git a/code/espurna/thermostat.h b/code/espurna/thermostat.h new file mode 100644 index 00000000..a25c7a90 --- /dev/null +++ b/code/espurna/thermostat.h @@ -0,0 +1,19 @@ +/* + +THERMOSTAT MODULE + +Copyright (C) 2017 by Dmitry Blinov + +*/ + +#pragma once + +#include +#include + +#if THERMOSTAT_DISPLAY_SUPPORT +#include // alias for `#include "SSD1306Wire.h"` +#endif + +using thermostat_callback_f = std::function; +void thermostatRegister(thermostat_callback_f callback); diff --git a/code/espurna/thermostat.ino b/code/espurna/thermostat.ino index 32b0bb44..476845d3 100644 --- a/code/espurna/thermostat.ino +++ b/code/espurna/thermostat.ino @@ -8,11 +8,11 @@ Copyright (C) 2017 by Dmitry Blinov #if THERMOSTAT_SUPPORT -#include #include -#include "relay.h" #include "ntp.h" +#include "relay.h" +#include "thermostat.h" #include "ws.h" @@ -603,8 +603,6 @@ void resetBurnCounters() { #if THERMOSTAT_DISPLAY_SUPPORT -#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"` - #define wifi_on_width 16 #define wifi_on_height 16 const char wifi_on_bits[] PROGMEM = { diff --git a/code/espurna/timelibshim.h b/code/espurna/timelibshim.h new file mode 100644 index 00000000..55dc4b17 --- /dev/null +++ b/code/espurna/timelibshim.h @@ -0,0 +1,120 @@ +// Shim original TimeLib functions + +#pragma once + +constexpr time_t daysPerWeek = 7; + +constexpr time_t secondsPerMinute = 60; +constexpr time_t secondsPerHour = 3600; +constexpr time_t secondsPerDay = secondsPerHour * 24; +constexpr time_t secondsPerWeek = daysPerWeek * secondsPerDay; + +constexpr time_t secondsPerYear = secondsPerWeek * 52; +constexpr time_t secondsY2K = 946684800; // the time at the start of y2k + +// wall clock values +constexpr const time_t numberOfSeconds(uint32_t ts) { + return (ts % secondsPerMinute); +} + +constexpr const time_t numberOfMinutes(uint32_t ts) { + return ((ts / secondsPerMinute) % secondsPerMinute); +} + +constexpr const time_t numberOfHours(uint32_t ts) { + return ((ts % secondsPerDay) / secondsPerHour); +} + +// week starts with sunday as number 1, monday as 2 etc. +constexpr const time_t dayOfWeek(time_t ts) { + return ((ts / secondsPerDay + 4) % daysPerWeek) + 1; +} + +// the number of days since 0 (Jan 1 1970 in case of time_t values) +constexpr const time_t elapsedDays(uint32_t ts) { + return (ts / secondsPerDay); +} + +// the number of seconds since last midnight +constexpr const time_t elapsedSecsToday(uint32_t ts) { + return (ts % secondsPerDay); +} + +// The following methods are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971 +// Always set the correct time before settting alarms + +// time at the start of the given day +constexpr const time_t previousMidnight(time_t ts) { + return ((ts / secondsPerDay) * secondsPerDay); +} + +// time at the end of the given day +constexpr const time_t nextMidnight(time_t ts) { + return previousMidnight(ts) + secondsPerDay; +} + +// note that week starts on day 1 +constexpr const time_t elapsedSecsThisWeek(uint32_t ts) { + return elapsedSecsToday(ts) + ((dayOfWeek(ts) - 1) * secondsPerDay); +} + +// time at the start of the week for the given time +constexpr const time_t previousSunday(time_t ts) { + return ts - elapsedSecsThisWeek(ts); +} + +// time at the end of the week for the given time +constexpr const time_t nextSunday(time_t ts) { + return previousSunday(ts) + secondsPerWeek; +} + + year(t), month(t), day(t), hour(t), minute(t), second(t) + +static time_t _ntp_ts = 0; +static tm _ntp_tm; + +void _ntpTmCache(time_t ts) { + if (_ntp_tm != ts) { + _ntp_ts = ts; + localtime_r(_ntp_ts, _ntp_tm); + } +} + +int hour(time_t ts) { + _ntpTmCache(ts); + return _ntp_tm.tm_hour; +} + +int minute(time_t ts) { + _ntpTmCache(ts); + return _ntp_tm.tm_min; +} + +int second(time_t ts) { + _ntpTmCache(ts); + return _ntp_tm.tm_sec; +} + +int day(time_t ts) { + _ntpTmCache(ts); + return _ntp_tm.tm_day; +} + +int weekday(time_t ts) { + _ntpTmCache(ts); + return _ntp_tm.tm_wday; +} + +int month(time_t ts) { + _ntpTmCache(ts); + return _ntp_tm.tm_mon; +} + +int year(time_t ts) { + _ntpTmCache(ts); + return _ntp_tm.tm_year; +} + +time_t now() { + return time(nullptr); +} diff --git a/code/espurna/uartmqtt.h b/code/espurna/uartmqtt.h new file mode 100644 index 00000000..797857f2 --- /dev/null +++ b/code/espurna/uartmqtt.h @@ -0,0 +1,18 @@ +/* + +UART_MQTT MODULE + +Copyright (C) 2018 by Albert Weterings +Adapted by Xose Pérez + +*/ + +#pragma once + +#if UART_MQTT_SUPPORT + +#include + +void uartmqttSetup(); + +#endif // UART_MQTT_SUPPORT == 1 diff --git a/code/espurna/uartmqtt.ino b/code/espurna/uartmqtt.ino index ed3132e6..c0424dc4 100644 --- a/code/espurna/uartmqtt.ino +++ b/code/espurna/uartmqtt.ino @@ -9,11 +9,13 @@ Adapted by Xose Pérez #if UART_MQTT_SUPPORT +#include "mqtt.h" +#include "uartmqtt.h" + char _uartmqttBuffer[UART_MQTT_BUFFER_SIZE]; bool _uartmqttNewData = false; #if UART_MQTT_USE_SOFT - #include SoftwareSerial _uart_mqtt_serial(UART_MQTT_RX_PIN, UART_MQTT_TX_PIN, false, UART_MQTT_BUFFER_SIZE); #define UART_MQTT_PORT _uart_mqtt_serial #else diff --git a/code/espurna/utils.h b/code/espurna/utils.h index f7593518..03197ff8 100644 --- a/code/espurna/utils.h +++ b/code/espurna/utils.h @@ -8,6 +8,8 @@ Copyright (C) 2017-2019 by Xose Pérez #pragma once +PROGMEM const char pstr_unknown[] = "UNKNOWN"; + #define INLINE inline __attribute__((always_inline)) extern "C" uint32_t _SPIFFS_start; diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index 9d6e6df3..c1fe6798 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -9,10 +9,35 @@ Copyright (C) 2017-2019 by Xose Pérez #include #include "config/buildtime.h" -#include "libs/HeapStats.h" + +#include "mqtt.h" #include "ntp.h" #include "utils.h" +#include "libs/HeapStats.h" + +//-------------------------------------------------------------------------------- +// Reset reasons +//-------------------------------------------------------------------------------- + +PROGMEM const char custom_reset_hardware[] = "Hardware button"; +PROGMEM const char custom_reset_web[] = "Reboot from web interface"; +PROGMEM const char custom_reset_terminal[] = "Reboot from terminal"; +PROGMEM const char custom_reset_mqtt[] = "Reboot from MQTT"; +PROGMEM const char custom_reset_rpc[] = "Reboot from RPC"; +PROGMEM const char custom_reset_ota[] = "Reboot after successful OTA update"; +PROGMEM const char custom_reset_http[] = "Reboot from HTTP"; +PROGMEM const char custom_reset_nofuss[] = "Reboot after successful NoFUSS update"; +PROGMEM const char custom_reset_upgrade[] = "Reboot after successful web update"; +PROGMEM const char custom_reset_factory[] = "Factory reset"; +PROGMEM const char* const custom_reset_string[] = { + custom_reset_hardware, custom_reset_web, custom_reset_terminal, + custom_reset_mqtt, custom_reset_rpc, custom_reset_ota, + custom_reset_http, custom_reset_nofuss, custom_reset_upgrade, + custom_reset_factory +}; + + void setDefaultHostname() { if (strlen(HOSTNAME) > 0) { setSetting("hostname", HOSTNAME); diff --git a/code/espurna/web.h b/code/espurna/web.h new file mode 100644 index 00000000..727016f1 --- /dev/null +++ b/code/espurna/web.h @@ -0,0 +1,41 @@ +/* + +WEBSERVER MODULE + +Copyright (C) 2016-2019 by Xose Pérez + +*/ + +#pragma once + +#include + +#if WEB_SUPPORT + +#include +#include +#include +#include +#include +#include + +#else + +// TODO: need these prototypes for .ino +class AsyncClient; +class AsyncWebServer; +class AsyncWebServerRequest; +class ArRequestHandlerFunction; +class AsyncWebSocketClient; +class AsyncWebSocket; +class AwsEventType; + +#endif // WEB_SUPPORT == 1 + +using web_body_callback_f = std::function; +using web_request_callback_f = std::function; + +AsyncWebServer* webServer(); + +void webBodyRegister(web_body_callback_f); +void webRequestRegister(web_request_callback_f); diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 16df0b48..476c9e17 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -8,16 +8,10 @@ Copyright (C) 2016-2019 by Xose Pérez #if WEB_SUPPORT +#include "ota.h" #include "system.h" #include "utils.h" -#include "ota.h" - -#include -#include -#include -#include -#include -#include +#include "web.h" #if WEB_EMBEDDED diff --git a/code/espurna/wifi.h b/code/espurna/wifi.h index 7e43ad52..bb83d067 100644 --- a/code/espurna/wifi.h +++ b/code/espurna/wifi.h @@ -6,6 +6,42 @@ Copyright (C) 2016-2019 by Xose Pérez */ +#pragma once + +#define LWIP_INTERNAL +#include +#undef LWIP_INTERNAL + +extern "C" { + #include + #include + #include + #include // ip_addr_t + #include // ERR_x + #include // dns_gethostbyname + #include // ip4/ip6 helpers + #include // LWIP_VERSION_MAJOR +}; + +#if LWIP_VERSION_MAJOR == 1 +#include +#else // LWIP_VERSION_MAJOR >= 2 +#include +#endif + +#include + +// ref: https://github.com/me-no-dev/ESPAsyncTCP/pull/115/files#diff-e2e636049095cc1ff920c1bfabf6dcacR8 +// This is missing with Core 2.3.0 and is sometimes missing from the build flags. Assume HIGH_BANDWIDTH version. +#ifndef TCP_MSS +#define TCP_MSS (1460) +#endif + +struct wifi_scan_info_t; + +using wifi_scan_f = std::function; +using wifi_callback_f = std::function; + uint8_t wifiState(); void wifiReconnectCheck(); bool wifiConnected(); diff --git a/code/espurna/ws.h b/code/espurna/ws.h index 9f5ef8a8..ae69f1cd 100644 --- a/code/espurna/ws.h +++ b/code/espurna/ws.h @@ -14,6 +14,7 @@ Copyright (C) 2016-2019 by Xose Pérez #include #include +#include "web.h" #include "utils.h" using ws_on_send_callback_f = std::function; diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index 0225cc19..db99190b 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -8,15 +8,13 @@ Copyright (C) 2016-2019 by Xose Pérez #if WEB_SUPPORT -#include -#include -#include -#include #include #include "system.h" +#include "web.h" #include "ws.h" #include "ws_internal.h" + #include "libs/WebSocketIncommingBuffer.h" AsyncWebSocket _ws("/ws");