diff --git a/.clang-format b/.clang-format index d801df39..03091be4 100644 --- a/.clang-format +++ b/.clang-format @@ -1,12 +1,12 @@ --- -Language: Cpp +Language: Cpp # BasedOnStyle: LLVM AccessModifierOffset: -2 AlignAfterOpenBracket: Align AlignConsecutiveAssignments: true AlignConsecutiveDeclarations: false AlignEscapedNewlinesLeft: false -AlignOperands: true +AlignOperands: true AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortBlocksOnASingleLine: false @@ -21,51 +21,51 @@ AlwaysBreakTemplateDeclarations: false BinPackArguments: true BinPackParameters: true BreakBeforeBraces: Custom -BraceWrapping: - AfterClass: false +BraceWrapping: + AfterClass: false AfterControlStatement: false - AfterEnum: false - AfterFunction: true - AfterNamespace: false + AfterEnum: false + AfterFunction: true + AfterNamespace: false AfterObjCDeclaration: false - AfterStruct: true - AfterUnion: true - BeforeCatch: false - BeforeElse: false - IndentBraces: false + AfterStruct: true + AfterUnion: true + BeforeCatch: false + BeforeElse: false + IndentBraces: false SplitEmptyFunction: false BreakBeforeBinaryOperators: None BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true -ColumnLimit: 120 -CommentPragmas: '^ IWYU pragma:' +ColumnLimit: 120 +CommentPragmas: "^ IWYU pragma:" ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 Cpp11BracedListStyle: true DerivePointerAlignment: false -DisableFormat: false +DisableFormat: false ExperimentalAutoDetectBinPacking: false -ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] -IncludeCategories: - - Regex: '^"(llvm|llvm-c|clang|clang-c)/' - Priority: 2 - - Regex: '^(<|"(gtest|isl|json)/)' - Priority: 3 - - Regex: '.*' - Priority: 1 -IncludeIsMainRegex: '$' +ForEachMacros: [foreach, Q_FOREACH, BOOST_FOREACH] +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|isl|json)/)' + Priority: 3 + - Regex: ".*" + Priority: 1 +IncludeIsMainRegex: "$" IndentCaseLabels: true -IndentWidth: 4 +IndentWidth: 4 IndentWrappedFunctionNames: false # IndentPPDirectives: BeforeHash JavaScriptQuotes: Leave JavaScriptWrapImports: true KeepEmptyLinesAtTheStartOfBlocks: true -MacroBlockBegin: '' -MacroBlockEnd: '' +MacroBlockBegin: "" +MacroBlockEnd: "" MaxEmptyLinesToKeep: 1 NamespaceIndentation: None ObjCBlockIndentWidth: 2 @@ -77,21 +77,20 @@ PenaltyBreakFirstLessLess: 120 PenaltyBreakString: 1000 PenaltyExcessCharacter: 1000000 PenaltyReturnTypeOnItsOwnLine: 60 -PointerAlignment: Middle -ReflowComments: true -SortIncludes: false +PointerAlignment: Left +ReflowComments: true +SortIncludes: false SpaceAfterCStyleCast: false SpaceAfterTemplateKeyword: true SpaceBeforeAssignmentOperators: true SpaceBeforeParens: Never SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 1 -SpacesInAngles: false +SpacesInAngles: false SpacesInContainerLiterals: false SpacesInCStyleCastParentheses: false SpacesInParentheses: false SpacesInSquareBrackets: false -Standard: Cpp11 -TabWidth: 4 -UseTab: Never -... +Standard: Cpp11 +TabWidth: 4 +UseTab: Never diff --git a/include/hasp_conf.h b/include/hasp_conf.h index 215c673d..577c3d78 100644 --- a/include/hasp_conf.h +++ b/include/hasp_conf.h @@ -7,86 +7,86 @@ #define HASP_USE_APP 1 #ifndef HASP_USE_DEBUG - #define HASP_USE_DEBUG 1 +#define HASP_USE_DEBUG 1 #endif /* Network Services */ #ifndef HASP_USE_ETHERNET - #define HASP_USE_ETHERNET 0 +#define HASP_USE_ETHERNET 0 #endif #ifndef HASP_USE_WIFI - #define HASP_USE_WIFI (ARDUINO_ARCH_ESP32 > 0 || ARDUINO_ARCH_ESP8266 > 0 || HASP_USE_WIFI > 0) +#define HASP_USE_WIFI (ARDUINO_ARCH_ESP32 > 0 || ARDUINO_ARCH_ESP8266 > 0 || HASP_USE_WIFI > 0) #endif #define HASP_HAS_NETWORK \ (ARDUINO_ARCH_ESP32 > 0 || ARDUINO_ARCH_ESP8266 > 0 || HASP_USE_ETHERNET > 0 || HASP_USE_WIFI > 0) #ifndef HASP_USE_OTA - #define HASP_USE_OTA (HASP_HAS_NETWORK) +#define HASP_USE_OTA (HASP_HAS_NETWORK) #endif #ifndef HASP_USE_MQTT - #define HASP_USE_MQTT (HASP_HAS_NETWORK) +#define HASP_USE_MQTT (HASP_HAS_NETWORK) #endif #ifndef HASP_USE_HTTP - #define HASP_USE_HTTP (HASP_HAS_NETWORK) +#define HASP_USE_HTTP (HASP_HAS_NETWORK) #endif #ifndef HASP_USE_MDNS - #define HASP_USE_MDNS (HASP_HAS_NETWORK) +#define HASP_USE_MDNS (HASP_HAS_NETWORK) #endif #ifndef HASP_USE_SYSLOG - #define HASP_USE_SYSLOG (HASP_HAS_NETWORK) +#define HASP_USE_SYSLOG (HASP_HAS_NETWORK) #endif #ifndef HASP_USE_TELNET - #define HASP_USE_TELNET 0 +#define HASP_USE_TELNET 0 #endif /* Filesystem */ #define HASP_HAS_FILESYSTEM (ARDUINO_ARCH_ESP32 > 0 || ARDUINO_ARCH_ESP8266 > 0) #ifndef HASP_USE_SPIFFS - #ifndef HASP_USE_LITTLEFS - #define HASP_USE_SPIFFS (HASP_HAS_FILESYSTEM) - #else - #define HASP_USE_SPIFFS (HASP_USE_LITTLEFS <= 0) - #endif +#ifndef HASP_USE_LITTLEFS +#define HASP_USE_SPIFFS (HASP_HAS_FILESYSTEM) +#else +#define HASP_USE_SPIFFS (HASP_USE_LITTLEFS <= 0) +#endif #endif #ifndef HASP_USE_LITTLEFS - #define HASP_USE_LITTLEFS (HASP_USE_SPIFFS <= 0) +#define HASP_USE_LITTLEFS (HASP_USE_SPIFFS <= 0) #endif #ifndef HASP_USE_EEPROM - #define HASP_USE_EEPROM 1 +#define HASP_USE_EEPROM 1 #endif #ifndef HASP_USE_SDCARD - #define HASP_USE_SDCARD 0 +#define HASP_USE_SDCARD 0 #endif #ifndef HASP_USE_GPIO - #define HASP_USE_GPIO 1 +#define HASP_USE_GPIO 1 #endif #ifndef HASP_USE_QRCODE - #define HASP_USE_QRCODE 1 +#define HASP_USE_QRCODE 1 #endif #ifndef HASP_USE_PNGDECODE - #define HASP_USE_PNGDECODE 0 +#define HASP_USE_PNGDECODE 0 #endif #ifndef HASP_NUM_GPIO_CONFIG - #define HASP_NUM_GPIO_CONFIG 8 +#define HASP_NUM_GPIO_CONFIG 8 #endif #ifndef HASP_NUM_INPUTS - #define HASP_NUM_INPUTS 4 // Number of ACE Buttons +#define HASP_NUM_INPUTS 4 // Number of ACE Buttons #endif // #ifndef HASP_NUM_OUTPUTS @@ -94,188 +94,188 @@ // #endif #ifndef HASP_NUM_PAGES - #if defined(ARDUINO_ARCH_ESP8266) - #define HASP_NUM_PAGES 4 - #else - #define HASP_NUM_PAGES 12 - #endif +#if defined(ARDUINO_ARCH_ESP8266) +#define HASP_NUM_PAGES 4 +#else +#define HASP_NUM_PAGES 12 +#endif #endif #define HASP_OBJECT_NOTATION "p%ub%u" /* Includes */ #ifdef WINDOWS - #include "winsock2.h" - #include "Windows.h" +#include "winsock2.h" +#include "Windows.h" #else - #include "Arduino.h" +#include "Arduino.h" #endif #if HASP_USE_SPIFFS > 0 - // #if defined(ARDUINO_ARCH_ESP32) - // #include "SPIFFS.h" - // #endif - // #include // Include the SPIFFS library - #include "hasp_filesystem.h" +// #if defined(ARDUINO_ARCH_ESP32) +// #include "SPIFFS.h" +// #endif +// #include // Include the SPIFFS library +#include "hasp_filesystem.h" #endif #if HASP_USE_LITTLEFS > 0 - // #if defined(ARDUINO_ARCH_ESP32) - // #include "LITTLEFS.h" - // #elif defined(ARDUINO_ARCH_ESP8266) - // #include // Include the FS library - // #include - // #endif - #include "hasp_filesystem.h" +// #if defined(ARDUINO_ARCH_ESP32) +// #include "LITTLEFS.h" +// #elif defined(ARDUINO_ARCH_ESP8266) +// #include // Include the FS library +// #include +// #endif +#include "hasp_filesystem.h" #endif #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 - #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) - #include "lv_zifont.h" - #endif +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) +#include "lv_zifont.h" +#endif #endif #if HASP_USE_EEPROM > 0 - #include "hasp_eeprom.h" +#include "hasp_eeprom.h" #endif #if HASP_USE_WIFI > 0 - #include "sys/net/hasp_wifi.h" +#include "sys/net/hasp_wifi.h" - #if defined(STM32F4xx) - #include "WiFiSpi.h" +#if defined(STM32F4xx) +#include "WiFiSpi.h" static WiFiSpiClass WiFi; - #endif +#endif #endif // HASP_USE_WIFI #if HASP_USE_ETHERNET > 0 - #if defined(ARDUINO_ARCH_ESP32) - #include +#if defined(ARDUINO_ARCH_ESP32) +#include - #define ETH_ADDR 0 - #define ETH_POWER_PIN -1 - #define ETH_MDC_PIN 23 - #define ETH_MDIO_PIN 18 - #define NRST 5 - #define ETH_TYPE ETH_PHY_LAN8720 - #define ETH_CLKMODE ETH_CLOCK_GPIO17_OUT +#define ETH_ADDR 0 +#define ETH_POWER_PIN -1 +#define ETH_MDC_PIN 23 +#define ETH_MDIO_PIN 18 +#define NRST 5 +#define ETH_TYPE ETH_PHY_LAN8720 +#define ETH_CLKMODE ETH_CLOCK_GPIO17_OUT - #include "net/hasp_ethernet_esp32.h" - #warning Using ESP32 Ethernet LAN8720 +#include "net/hasp_ethernet_esp32.h" +#warning Using ESP32 Ethernet LAN8720 - #else - #if USE_BUILTIN_ETHERNET > 0 - #include - #include - #warning Use built-in STM32 Ethernet - #elif USE_UIP_ETHERNET - #include - #include - #warning Use ENC28J60 Ethernet shield - #else - #include "Ethernet.h" - #warning Use W5x00 Ethernet shield - #endif - #include "net/hasp_ethernet_stm32.h" - #endif +#else +#if USE_BUILTIN_ETHERNET > 0 +#include +#include +#warning Use built-in STM32 Ethernet +#elif USE_UIP_ETHERNET +#include +#include +#warning Use ENC28J60 Ethernet shield +#else +#include "Ethernet.h" +#warning Use W5x00 Ethernet shield +#endif +#include "net/hasp_ethernet_stm32.h" +#endif #endif #if HASP_USE_MQTT > 0 - #include "mqtt/hasp_mqtt.h" +#include "mqtt/hasp_mqtt.h" - #ifdef WINDOWS - #define USE_PAHO - #else - #define USE_PUBSUBCLIENT - #endif +#ifdef WINDOWS +#define USE_PAHO +#else +#define USE_PUBSUBCLIENT +#endif #endif #if HASP_USE_GPIO > 0 - #include "sys/gpio/hasp_gpio.h" +#include "sys/gpio/hasp_gpio.h" #endif #if HASP_USE_HTTP > 0 - #include "svc/hasp_http.h" +#include "sys/svc/hasp_http.h" #endif #if HASP_USE_TELNET > 0 - #include "svc/hasp_telnet.h" +#include "sys/svc/hasp_telnet.h" #endif #if HASP_USE_MDNS > 0 - #include "svc/hasp_mdns.h" +#include "sys/svc/hasp_mdns.h" #endif #if HASP_USE_OTA > 0 - #include "svc/hasp_ota.h" - #ifndef HASP_OTA_PORT - #if defined(ARDUINO_ARCH_ESP32) - #define HASP_OTA_PORT 3232 - #elif defined(ARDUINO_ARCH_ESP8266) - #define HASP_OTA_PORT 8266 - #endif - #endif +#include "sys/svc/hasp_ota.h" +#ifndef HASP_OTA_PORT +#if defined(ARDUINO_ARCH_ESP32) +#define HASP_OTA_PORT 3232 +#elif defined(ARDUINO_ARCH_ESP8266) +#define HASP_OTA_PORT 8266 +#endif +#endif #endif -#if HASP_USE_TASMOTA_SLAVE > 0 - #include "svc/hasp_slave.h" +#if HASP_USE_TASMOTA_CLIENT > 0 +#include "sys/svc/hasp_slave.h" #endif #ifdef WINDOWS - #ifndef FPSTR - #define FPSTR(pstr_pointer) (reinterpret_cast(pstr_pointer)) - #endif +#ifndef FPSTR +#define FPSTR(pstr_pointer) (reinterpret_cast(pstr_pointer)) +#endif - #ifndef __FlashStringHelper - #define __FlashStringHelper char - #endif +#ifndef __FlashStringHelper +#define __FlashStringHelper char +#endif - #ifndef FPSTR - #define FPSTR(pstr_pointer) (reinterpret_cast(pstr_pointer)) - #endif +#ifndef FPSTR +#define FPSTR(pstr_pointer) (reinterpret_cast(pstr_pointer)) +#endif - #ifndef PGM_P - #define PGM_P const char * - #endif +#ifndef PGM_P +#define PGM_P const char* +#endif - #ifndef F - #define F(x) (x) - #endif +#ifndef F +#define F(x) (x) +#endif - #ifndef PSTR - #define PSTR(x) x - #endif +#ifndef PSTR +#define PSTR(x) x +#endif - #ifndef PROGMEM - #define PROGMEM - #endif +#ifndef PROGMEM +#define PROGMEM +#endif #endif #ifdef WINDOWS - #include - #include - #include - #include +#include +#include +#include +#include - #define snprintf_P snprintf - #define memcpy_P memcpy - #define strcasecmp_P strcmp // TODO: should be strcasecmp - #define strcmp_P strcmp - #define strstr_P strstr - #define halRestartMcu() - #define delay Sleep - #define millis SDL_GetTicks +#define snprintf_P snprintf +#define memcpy_P memcpy +#define strcasecmp_P strcmp // TODO: should be strcasecmp +#define strcmp_P strcmp +#define strstr_P strstr +#define halRestartMcu() +#define delay Sleep +#define millis SDL_GetTicks - #define DEC 10 - #define HEX 16 - #define BIN 2 +#define DEC 10 +#define HEX 16 +#define BIN 2 - #define guiGetDim() 255 - #define guiSetDim(x) - #define guiGetBacklight() 1 - #define guiSetBacklight(x) - #define guiCalibrate() +#define guiGetDim() 255 +#define guiSetDim(x) +#define guiGetBacklight() 1 +#define guiSetBacklight(x) +//#define guiCalibrate() #endif #endif // HASP_CONF_H \ No newline at end of file diff --git a/src/hasp/hasp_dispatch.cpp b/src/hasp/hasp_dispatch.cpp index 51b4363f..c146c26b 100644 --- a/src/hasp/hasp_dispatch.cpp +++ b/src/hasp/hasp_dispatch.cpp @@ -59,11 +59,11 @@ struct moodlight_t }; moodlight_t moodlight; -static void dispatch_config(const char * topic, const char * payload); +static void dispatch_config(const char* topic, const char* payload); // void dispatch_group_value(uint8_t groupid, int16_t state, lv_obj_t * obj); -static inline void dispatch_state_msg(const __FlashStringHelper * subtopic, const char * payload); +static inline void dispatch_state_msg(const __FlashStringHelper* subtopic, const char* payload); -void dispatch_screenshot(const char *, const char * filename) +void dispatch_screenshot(const char*, const char* filename) { #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 @@ -99,16 +99,16 @@ bool dispatch_factory_reset() return formated && erased; } -void dispatch_json_error(uint8_t tag, DeserializationError & jsonError) +void dispatch_json_error(uint8_t tag, DeserializationError& jsonError) { LOG_ERROR(tag, F(D_JSON_FAILED " %s"), jsonError.c_str()); } // p[x].b[y].attr=value -static inline bool dispatch_parse_button_attribute(const char * topic_p, const char * payload) +static inline bool dispatch_parse_button_attribute(const char* topic_p, const char* payload) { long num; - char * pEnd; + char* pEnd; uint8_t pageid, objid; if(*topic_p != 'p' && *topic_p != 'P') return false; // obligated p @@ -202,7 +202,7 @@ static inline bool dispatch_parse_button_attribute(const char * topic_p, const c } // objectattribute=value -void dispatch_command(const char * topic, const char * payload) +void dispatch_command(const char* topic, const char* payload) { /* ================================= Standard payload commands ======================================= */ @@ -260,34 +260,34 @@ void dispatch_command(const char * topic, const char * payload) } // Strip command/config prefix from the topic and process the payload -void dispatch_topic_payload(const char * topic, const char * payload) +void dispatch_topic_payload(const char* topic, const char* payload) { // LOG_VERBOSE(TAG_MSGR,F("TOPIC: short topic: %s"), topic); if(!strcmp_P(topic, PSTR("command"))) { - dispatch_text_line((char *)payload); + dispatch_text_line((char*)payload); return; } if(topic == strstr_P(topic, PSTR("command/"))) { // startsWith command/ topic += 8u; - dispatch_command(topic, (char *)payload); + dispatch_command(topic, (char*)payload); return; } #if HASP_USE_CONFIG > 0 if(topic == strstr_P(topic, PSTR("config/"))) { // startsWith command/ topic += 7u; - dispatch_config(topic, (char *)payload); + dispatch_config(topic, (char*)payload); return; } #endif - dispatch_command(topic, (char *)payload); // dispatch as is + dispatch_command(topic, (char*)payload); // dispatch as is } // Parse one line of text and execute the command -void dispatch_text_line(const char * cmnd) +void dispatch_text_line(const char* cmnd) { size_t pos1 = std::string(cmnd).find("="); size_t pos2 = std::string(cmnd).find(" "); @@ -350,7 +350,7 @@ void dispatch_output_group_state(uint8_t groupid, uint16_t state) dispatch_state_msg(F("output"), payload); } -void dispatch_send_obj_attribute_str(uint8_t pageid, uint8_t btnid, const char * attribute, const char * data) +void dispatch_send_obj_attribute_str(uint8_t pageid, uint8_t btnid, const char* attribute, const char* data) { if(!attribute || !data) return; @@ -360,7 +360,7 @@ void dispatch_send_obj_attribute_str(uint8_t pageid, uint8_t btnid, const char * mqtt_send_object_state(pageid, btnid, payload); } -void dispatch_send_obj_attribute_int(uint8_t pageid, uint8_t btnid, const char * attribute, int32_t val) +void dispatch_send_obj_attribute_int(uint8_t pageid, uint8_t btnid, const char* attribute, int32_t val) { if(!attribute) return; @@ -370,7 +370,7 @@ void dispatch_send_obj_attribute_int(uint8_t pageid, uint8_t btnid, const char * mqtt_send_object_state(pageid, btnid, payload); } -void dispatch_send_obj_attribute_color(uint8_t pageid, uint8_t btnid, const char * attribute, uint8_t r, uint8_t g, +void dispatch_send_obj_attribute_color(uint8_t pageid, uint8_t btnid, const char* attribute, uint8_t r, uint8_t g, uint8_t b) { if(!attribute) return; @@ -384,7 +384,7 @@ void dispatch_send_obj_attribute_color(uint8_t pageid, uint8_t btnid, const char #if HASP_USE_CONFIG > 0 // Get or Set a part of the config.json file -static void dispatch_config(const char * topic, const char * payload) +static void dispatch_config(const char* topic, const char* payload) { DynamicJsonDocument doc(128 * 2); char buffer[128 * 2]; @@ -494,7 +494,7 @@ bool dispatch_get_event_state(uint8_t eventid) } // Map events to their description string -void dispatch_get_event_name(uint8_t eventid, char * buffer, size_t size) +void dispatch_get_event_name(uint8_t eventid, char* buffer, size_t size) { switch(eventid) { case HASP_EVENT_ON: @@ -543,7 +543,7 @@ void dispatch_gpio_input_event(uint8_t pin, uint8_t group, uint8_t eventid) } #endif -void dispatch_object_event(lv_obj_t * obj, uint8_t eventid) +void dispatch_object_event(lv_obj_t* obj, uint8_t eventid) { char topic[8]; char payload[8]; @@ -559,7 +559,7 @@ void dispatch_object_event(lv_obj_t * obj, uint8_t eventid) // dispatch_group_onoff(obj->user_data.groupid, dispatch_get_event_state(eventid), obj); } -void dispatch_object_value_changed(lv_obj_t * obj, int16_t state) +void dispatch_object_value_changed(lv_obj_t* obj, int16_t state) { char topic[4]; @@ -569,7 +569,7 @@ void dispatch_object_value_changed(lv_obj_t * obj, int16_t state) } /********************************************** Output States ******************************************/ -static inline void dispatch_state_msg(const __FlashStringHelper * subtopic, const char * payload) +static inline void dispatch_state_msg(const __FlashStringHelper* subtopic, const char* payload) { #if !defined(HASP_USE_MQTT) && !defined(HASP_USE_TASMOTA_CLIENT) LOG_TRACE(TAG_MSGR, F("%s => %s"), String(subtopic).c_str(), payload); @@ -609,7 +609,7 @@ static inline void dispatch_state_msg(const __FlashStringHelper * subtopic, cons // // dispatch_output_group_state(groupid, payload); // } -void dispatch_normalized_group_value(uint8_t groupid, uint16_t value, lv_obj_t * obj) +void dispatch_normalized_group_value(uint8_t groupid, uint16_t value, lv_obj_t* obj) { if(groupid > 0) { LOG_VERBOSE(TAG_MSGR, F("GROUP %d value %d"), groupid, value); @@ -622,7 +622,7 @@ void dispatch_normalized_group_value(uint8_t groupid, uint16_t value, lv_obj_t * /********************************************** Native Commands ****************************************/ -void dispatch_parse_json(const char *, const char * payload) +void dispatch_parse_json(const char*, const char* payload) { // Parse an incoming JSON array into individual commands /* if(strPayload.endsWith(",]")) { // Trailing null array elements are an artifact of older Home Assistant automations @@ -645,18 +645,18 @@ void dispatch_parse_json(const char *, const char * payload) JsonArray arr = json.as(); // guiStop(); for(JsonVariant command : arr) { - dispatch_text_line(command.as()); + dispatch_text_line(command.as()); } // guiStart(); } else if(json.is()) { // handle json as a jsonl uint8_t savedPage = haspGetPage(); hasp_new_object(json.as(), savedPage); - } else if(json.is()) { // handle json as a single command - dispatch_text_line(json.as()); + } else if(json.is()) { // handle json as a single command + dispatch_text_line(json.as()); - } else if(json.is()) { // handle json as a single command - dispatch_text_line(json.as()); + } else if(json.is()) { // handle json as a single command + dispatch_text_line(json.as()); // } else if(json.is()) { // handle json as a single command // dispatch_text_line(json.as().c_str()); @@ -667,9 +667,9 @@ void dispatch_parse_json(const char *, const char * payload) } #ifdef ARDUINO -void dispatch_parse_jsonl(Stream & stream) +void dispatch_parse_jsonl(Stream& stream) #else -void dispatch_parse_jsonl(std::istringstream & stream) +void dispatch_parse_jsonl(std::istringstream& stream) #endif { uint8_t savedPage = haspGetPage(); @@ -698,14 +698,14 @@ void dispatch_parse_jsonl(std::istringstream & stream) } } -void dispatch_parse_jsonl(const char *, const char * payload) +void dispatch_parse_jsonl(const char*, const char* payload) { #if HASP_USE_CONFIG > 0 - CharStream stream((char *)payload); + CharStream stream((char*)payload); // stream.setTimeout(10); dispatch_parse_jsonl(stream); #else - std::istringstream stream((char *)payload); + std::istringstream stream((char*)payload); dispatch_parse_jsonl(stream); #endif } @@ -719,7 +719,7 @@ void dispatch_output_current_page() } // Get or Set a page -void dispatch_page(const char *, const char * page) +void dispatch_page(const char*, const char* page) { if(strlen(page) > 0) { if(Utilities::is_only_digits(page)) { @@ -766,14 +766,14 @@ void dispatch_page_prev() } // Clears a page id or the current page if empty -void dispatch_clear_page(const char *, const char * page) +void dispatch_clear_page(const char*, const char* page) { uint8_t pageid = haspGetPage(); if(strlen(page) > 0) pageid = atoi(page); haspClearPage(pageid); } -void dispatch_dim(const char *, const char * level) +void dispatch_dim(const char*, const char* level) { // Set the current state if(strlen(level) != 0) haspDevice.set_backlight_level(atoi(level)); @@ -783,7 +783,7 @@ void dispatch_dim(const char *, const char * level) dispatch_state_msg(F("dim"), payload); } -void dispatch_moodlight(const char * topic, const char * payload) +void dispatch_moodlight(const char* topic, const char* payload) { // Set the current state if(strlen(payload) != 0) { @@ -800,7 +800,7 @@ void dispatch_moodlight(const char * topic, const char * payload) dispatch_json_error(TAG_MSGR, jsonError); } else { - if(!json[F("power")].isNull()) moodlight.power = Utilities::is_true(json[F("power")].as()); + if(!json[F("power")].isNull()) moodlight.power = Utilities::is_true(json[F("power")].as()); if(!json[F("r")].isNull()) moodlight.r = json[F("r")].as(); if(!json[F("g")].isNull()) moodlight.r = json[F("g")].as(); @@ -808,7 +808,7 @@ void dispatch_moodlight(const char * topic, const char * payload) if(!json[F("color")].isNull()) { lv_color32_t color; - if(Parser::haspPayloadToColor(json[F("color")].as(), color)) { + if(Parser::haspPayloadToColor(json[F("color")].as(), color)) { moodlight.r = color.ch.red; moodlight.g = color.ch.green; moodlight.b = color.ch.blue; @@ -832,7 +832,7 @@ void dispatch_moodlight(const char * topic, const char * payload) dispatch_state_msg(F("moodlight"), buffer); } -void dispatch_backlight(const char *, const char * payload) +void dispatch_backlight(const char*, const char* payload) { // Set the current state if(strlen(payload) != 0) haspDevice.set_backlight_power(Utilities::is_true(payload)); @@ -843,7 +843,7 @@ void dispatch_backlight(const char *, const char * payload) dispatch_state_msg(F("light"), buffer); } -void dispatch_web_update(const char *, const char * espOtaUrl) +void dispatch_web_update(const char*, const char* espOtaUrl) { #if HASP_USE_OTA > 0 LOG_TRACE(TAG_MSGR, F(D_OTA_CHECK_UPDATE), espOtaUrl); @@ -889,40 +889,38 @@ void dispatch_current_state() /******************************************* Command Wrapper Functions *********************************/ // Periodically publish a JSON string indicating system status -void dispatch_output_statusupdate(const char *, const char *) +void dispatch_output_statusupdate(const char*, const char*) { #if HASP_USE_MQTT > 0 char data[3 * 128]; { - /* char buffer[128]; - haspGetVersion(buffer, sizeof(buffer)); - snprintf_P(data, sizeof(data), - PSTR("{\"node\":\"%s\",\"status\":\"available\",\"version\":\"%s\",\"uptime\":%lu,"), - mqttGetNodename().c_str(), buffer, long(millis() / 1000)); + haspGetVersion(buffer, sizeof(buffer)); + snprintf_P(data, sizeof(data), + PSTR("{\"node\":\"%s\",\"status\":\"available\",\"version\":\"%s\",\"uptime\":%lu,"), + mqttGetNodename().c_str(), buffer, long(millis() / 1000)); - #if HASP_USE_WIFI > 0 - network_get_statusupdate(buffer, sizeof(buffer)); - strcat(data, buffer); - #endif - snprintf_P(buffer, sizeof(buffer), PSTR("\"heapFree\":%u,\"heapFrag\":%u,\"espCore\":\"%s\","), - halGetFreeHeap(), halGetHeapFragmentation(), halGetCoreVersion().c_str()); - strcat(data, buffer); - snprintf_P(buffer, sizeof(buffer), PSTR("\"espCanUpdate\":\"false\",\"page\":%u,\"numPages\":%u,"), - haspGetPage(), (HASP_NUM_PAGES)); - strcat(data, buffer); +#if HASP_USE_WIFI > 0 + network_get_statusupdate(buffer, sizeof(buffer)); + strcat(data, buffer); +#endif + snprintf_P(buffer, sizeof(buffer), PSTR("\"heapFree\":%u,\"heapFrag\":%u,\"espCore\":\"%s\","), + haspDevice.get_free_heap(), haspDevice.get_heap_fragmentation(), halGetCoreVersion().c_str()); + strcat(data, buffer); + snprintf_P(buffer, sizeof(buffer), PSTR("\"espCanUpdate\":\"false\",\"page\":%u,\"numPages\":%u,"), + haspGetPage(), (HASP_NUM_PAGES)); + strcat(data, buffer); - #if defined(ARDUINO_ARCH_ESP8266) - snprintf_P(buffer, sizeof(buffer), PSTR("\"espVcc\":%.2f,"), (float)ESP.getVcc() / 1000); - strcat(data, buffer); - #endif +#if defined(ARDUINO_ARCH_ESP8266) + snprintf_P(buffer, sizeof(buffer), PSTR("\"espVcc\":%.2f,"), (float)ESP.getVcc() / 1000); + strcat(data, buffer); +#endif - snprintf_P(buffer, sizeof(buffer), PSTR("\"tftDriver\":\"%s\",\"tftWidth\":%u,\"tftHeight\":%u}"), - halDisplayDriverName().c_str(), (TFT_WIDTH), (TFT_HEIGHT)); - strcat(data, buffer); - */ + snprintf_P(buffer, sizeof(buffer), PSTR("\"tftDriver\":\"%s\",\"tftWidth\":%u,\"tftHeight\":%u}"), + halDisplayDriverName().c_str(), (TFT_WIDTH), (TFT_HEIGHT)); + strcat(data, buffer); } mqtt_send_state(F("statusupdate"), data); dispatchLastMillis = millis(); @@ -937,22 +935,22 @@ void dispatch_output_statusupdate(const char *, const char *) #endif } -void dispatch_calibrate(const char * topic = NULL, const char * payload = NULL) +void dispatch_calibrate(const char* topic = NULL, const char* payload = NULL) { guiCalibrate(); } -void dispatch_wakeup(const char *, const char *) +void dispatch_wakeup(const char*, const char*) { lv_disp_trig_activity(NULL); } -void dispatch_reboot(const char *, const char *) +void dispatch_reboot(const char*, const char*) { dispatch_reboot(true); } -void dispatch_factory_reset(const char *, const char *) +void dispatch_factory_reset(const char*, const char*) { dispatch_factory_reset(); delay(500); @@ -961,7 +959,7 @@ void dispatch_factory_reset(const char *, const char *) /******************************************* Commands builder *******************************************/ -static void dispatch_add_command(const char * p_cmdstr, void (*func)(const char *, const char *)) +static void dispatch_add_command(const char* p_cmdstr, void (*func)(const char*, const char*)) { if(nCommands >= sizeof(commands) / sizeof(haspCommand_t)) { LOG_FATAL(TAG_MSGR, F("CMD_OVERFLOW %d"), nCommands); diff --git a/src/main_arduino copy.cpp b/src/main_arduino copy.cpp new file mode 100644 index 00000000..6d14b297 --- /dev/null +++ b/src/main_arduino copy.cpp @@ -0,0 +1,194 @@ +/* MIT License - Copyright (c) 2020 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#if 0 && ARDUINO + +#include +#include "lvgl.h" +#include "hasp_conf.h" // load first + +#if HASP_USE_CONFIG > 0 +#include "hasp_debug.h" +#endif + +#if HASP_USE_CONFIG > 0 +#include "hasp_config.h" +#include "hasp_gui.h" +#endif + +#include "hasp_oobe.h" + +#include "hasp/hasp_dispatch.h" +#include "hasp/hasp.h" + +#include "sys/net/hasp_network.h" + +#include "dev/device.h" + +bool isConnected; +uint8_t mainLoopCounter = 0; +unsigned long mainLastLoopTime = 0; + +void setup() +{ + hal_setup(); + + haspDevice.init(); + + /**************************** + * Storage initializations + ***************************/ +#if HASP_USE_EEPROM > 0 + eepromSetup(); // Don't start at boot, only at write +#endif + + // #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 + // filesystemSetup(); // FS mount is done in configSetup() + // #endif + + // #if HASP_USE_SDCARD > 0 + // sdcardSetup(); + // #endif + + /**************************** + * Read & Apply User Configuration + ***************************/ +#if HASP_USE_CONFIG > 0 + configSetup(); // also runs debugPreSetup(), debugSetup() and debugStart() +#endif + + dispatchSetup(); + guiSetup(); + debugSetup(); // Init the console + +#if HASP_USE_GPIO > 0 + gpioSetup(); +#endif + + /**************************** + * Apply User Configuration + ***************************/ + +#if HASP_USE_MQTT > 0 + mqttSetup(); // Load Hostname before starting WiFi +#endif + +#if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0 + networkSetup(); +#endif + +#if HASP_USE_CONFIG > 0 + if(!oobeSetup()) +#endif + { + haspSetup(); + } + +#if HASP_USE_MDNS > 0 + mdnsSetup(); +#endif + +#if HASP_USE_OTA > 0 + otaSetup(); +#endif + +#if HASP_USE_HTTP > 0 + httpSetup(); +#endif + +#if HASP_USE_TELNET > 0 + telnetSetup(); +#endif + +#if HASP_USE_TASMOTA_CLIENT > 0 + slaveSetup(); +#endif + + mainLastLoopTime = millis() - 1000; // reset loop counter + delay(250); + guiStart(); +} + +void loop() +{ + networkLoop(); + guiLoop(); + haspLoop(); + +#if HASP_USE_MQTT > 0 + mqttLoop(); +#endif // MQTT + +#if HASP_USE_TASMOTA_CLIENT > 0 + slaveLoop(); +#endif // TASMOTASLAVE + +#if HASP_USE_HTTP > 0 + httpLoop(); +#endif // HTTP + +#if HASP_USE_GPIO > 0 + gpioLoop(); +#endif // GPIO + +#if HASP_USE_OTA > 0 + otaLoop(); +#endif // OTA + +#if HASP_USE_MDNS > 0 + mdnsLoop(); +#endif // MDNS + +#if HASP_USE_TELNET > 0 + telnetLoop(); // Console +#endif // TELNET + + debugLoop(); // Console + haspDevice.loop(); + + /* Timer Loop */ + if(millis() - mainLastLoopTime >= 1000) { + /* Runs Every Second */ + haspEverySecond(); // sleep timer + debugEverySecond(); // statusupdate + +#if HASP_USE_OTA > 0 + otaEverySecond(); // progressbar +#endif + + /* Runs Every 5 Seconds */ + if(mainLoopCounter == 0 || mainLoopCounter == 5) { + isConnected = networkEvery5Seconds(); // Check connection + +#if HASP_USE_HTTP > 0 + // httpEvery5Seconds(); +#endif + +#if HASP_USE_MQTT > 0 + mqttEvery5Seconds(isConnected); +#endif + +#if HASP_USE_GPIO > 0 + // gpioEvery5Seconds(); +#endif + + haspDevice.loop_5s(); + } + + /* Reset loop counter every 10 seconds */ + if(mainLoopCounter >= 9) { + mainLoopCounter = 0; + } else { + mainLoopCounter++; + } + mainLastLoopTime += 1000; + } + +#ifdef ARDUINO_ARCH_ESP8266 + delay(2); +#else + delay(6); +#endif +} + +#endif \ No newline at end of file diff --git a/src/main_arduino.cpp b/src/main_arduino.cpp index 89b492c6..65272907 100644 --- a/src/main_arduino.cpp +++ b/src/main_arduino.cpp @@ -33,7 +33,7 @@ void setup() { // hal_setup(); - haspDevice.pre_setup(); + haspDevice.init(); /**************************** * Storage initializations @@ -100,7 +100,7 @@ void setup() telnetSetup(); #endif -#if HASP_USE_TASMOTA_SLAVE > 0 +#if HASP_USE_TASMOTA_CLINET > 0 slaveSetup(); #endif @@ -111,51 +111,24 @@ void setup() void loop() { - networkLoop(); guiLoop(); haspLoop(); + networkLoop(); #if HASP_USE_MQTT > 0 mqttLoop(); #endif // MQTT -#if HASP_USE_TASMOTA_SLAVE > 0 - slaveLoop(); -#endif // TASMOTASLAVE - -#if HASP_USE_HTTP > 0 - httpLoop(); -#endif // HTTP - -#if HASP_USE_GPIO > 0 - gpioLoop(); -#endif // GPIO - -#if HASP_USE_OTA > 0 - otaLoop(); -#endif // OTA - -#if HASP_USE_MDNS > 0 - mdnsLoop(); -#endif // MDNS - -#if HASP_USE_TELNET > 0 - telnetLoop(); // Console -#endif // TELNET - debugLoop(); // Console haspDevice.loop(); /* Timer Loop */ if(millis() - mainLastLoopTime >= 1000) { + /* Runs Every Second */ haspEverySecond(); // sleep timer debugEverySecond(); // statusupdate -#if HASP_USE_OTA > 0 - otaEverySecond(); // progressbar -#endif - /* Runs Every 5 Seconds */ if(mainLoopCounter == 0 || mainLoopCounter == 5) { isConnected = networkEvery5Seconds(); // Check connection diff --git a/src/main_windows.cpp b/src/main_windows.cpp index 56710613..c2e113ab 100644 --- a/src/main_windows.cpp +++ b/src/main_windows.cpp @@ -3,19 +3,22 @@ #ifdef WINDOWS - #include "lvgl.h" - #include "app_hal.h" +#include "hasp_conf.h" - #include "hasp_conf.h" - #include "hasp_debug.h" +#include "lvgl.h" +#include "app_hal.h" - #include "hasp/hasp_dispatch.h" - #include "hasp/hasp.h" +#include "hasp_debug.h" +#include "hasp_gui.h" - #include "dev/device.h" - #include "app_hal.h" +#include "hasp/hasp_dispatch.h" +#include "hasp/hasp.h" + +#include "dev/device.h" bool isConnected; +bool isRunning = 1; + uint8_t mainLoopCounter = 0; unsigned long mainLastLoopTime = 0; @@ -27,26 +30,26 @@ void debugLvglLogEvent(lv_log_level_t level, const char * file, uint32_t line, c void setup() { - printf("%s %d\n", __FILE__, __LINE__); - fflush(stdout); + // Load Settings + + // Init debug log + // debug_init(); + + // Initialize lvgl environment lv_init(); lv_log_register_print_cb(debugLvglLogEvent); - printf("%s %d\n", __FILE__, __LINE__); - fflush(stdout); + haspDevice.init(); hal_setup(); + guiSetup(); - printf("%s %d\n", __FILE__, __LINE__); - haspDevice.pre_setup(); - - printf("%s %d\n", __FILE__, __LINE__); dispatchSetup(); // debugSetup(); // Init the console - #if HASP_USE_MQTT > 0 +#if HASP_USE_MQTT > 0 printf("%s %d\n", __FILE__, __LINE__); mqttSetup(); // Load Hostname before starting WiFi - #endif +#endif printf("%s %d\n", __FILE__, __LINE__); haspSetup(); @@ -62,15 +65,16 @@ void loop() // debugLoop(); // Console haspDevice.loop(); + guiLoop(); /* Timer Loop */ if(millis() - mainLastLoopTime >= 1000) { /* Runs Every Second */ haspEverySecond(); // sleep timer - #if HASP_USE_OTA > 0 +#if HASP_USE_OTA > 0 otaEverySecond(); // progressbar - #endif +#endif /* Runs Every 5 Seconds */ if(mainLoopCounter == 0 || mainLoopCounter == 5) { @@ -90,32 +94,21 @@ void loop() delay(6); } - #ifdef WINDOWS +#ifdef WINDOWS int main(int argv, char ** args) { - printf("%s %d\n", __FILE__, __LINE__); - fflush(stdout); + // printf("%s %d\n", __FILE__, __LINE__); + // fflush(stdout); setup(); - std::cout << "HSetup OK\n"; - - while(1) { - SDL_Delay(5); - lv_task_handler(); - fflush(stdout); + while(isRunning) { + loop(); + // std::cout << "HSetup OK\n"; } - std::cout << "Hloop OK\n"; return 0; } - #else -void loop() -{ - delay(5); - lv_task_handler(); -} - - #endif +#endif #endif \ No newline at end of file diff --git a/src/sys/net/hasp_network.cpp b/src/sys/net/hasp_network.cpp index 02f299ad..467b6330 100644 --- a/src/sys/net/hasp_network.cpp +++ b/src/sys/net/hasp_network.cpp @@ -117,7 +117,7 @@ bool networkEverySecond(void) return true; } -void network_get_statusupdate(char * buffer, size_t len) +void network_get_statusupdate(char* buffer, size_t len) { #if HASP_USE_ETHERNET > 0 ethernet_get_statusupdate(buffer, len);