diff --git a/include/hasp_conf.h b/include/hasp_conf.h index 6501e974..af8c8473 100644 --- a/include/hasp_conf.h +++ b/include/hasp_conf.h @@ -63,6 +63,10 @@ #define HASP_USE_TELNET 0 #endif +#ifndef HASP_USE_CONSOLE +#define HASP_USE_CONSOLE 1 +#endif + /* Filesystem */ #define HASP_HAS_FILESYSTEM (ARDUINO_ARCH_ESP32 > 0 || ARDUINO_ARCH_ESP8266 > 0) @@ -217,6 +221,10 @@ static WiFiSpiClass WiFi; #include "sys/svc/hasp_http.h" #endif +#if HASP_USE_CONSOLE > 0 +#include "sys/svc/hasp_console.h" +#endif + #if HASP_USE_TELNET > 0 #include "sys/svc/hasp_telnet.h" #endif diff --git a/src/hasp_config.cpp b/src/hasp_config.cpp index 2e3d678b..3b315507 100644 --- a/src/hasp_config.cpp +++ b/src/hasp_config.cpp @@ -76,7 +76,7 @@ bool configSet(uint16_t& value, const JsonVariant& setting, const __FlashStringH void configSetupDebug(JsonDocument& settings) { - debugSetupWithoutLogging(settings[FPSTR(FP_DEBUG)]); + debugSetup(settings[FPSTR(FP_DEBUG)]); debugStart(); // Debug started, now we can use it; HASP header sent } @@ -158,7 +158,7 @@ DeserializationError configRead(JsonDocument& settings, bool setupdebug = false) configRestorePasswords(settings, wifiPass, mqttPass, httpPass); LOG_INFO(TAG_CONF, F(D_FILE_LOADED), configFile.c_str()); - if(setupdebug) debugSetup(); + // if(setupdebug) debugSetup(); return error; } diff --git a/src/hasp_debug.cpp b/src/hasp_debug.cpp index 12fe6258..1fbc44a8 100644 --- a/src/hasp_debug.cpp +++ b/src/hasp_debug.cpp @@ -86,8 +86,6 @@ static void debugPrintTimestamp(int level, Print* _logOutput) } /* ===== Default Event Processors ===== */ -// void debugSetupWithoutLogging(JsonObject settings); -// void debugSetup(); static inline void debug_flush() { @@ -243,6 +241,9 @@ void debug_get_tag(uint8_t tag, char* buffer) memcpy_P(buffer, PSTR("HAL "), 5); break; + case TAG_CONS: + memcpy_P(buffer, PSTR("CONS"), 5); + break; case TAG_DEBG: memcpy_P(buffer, PSTR("DBUG"), 5); break; diff --git a/src/hasp_debug.h b/src/hasp_debug.h index 1e5e4055..c60ffc13 100644 --- a/src/hasp_debug.h +++ b/src/hasp_debug.h @@ -14,8 +14,7 @@ #include "ArduinoLog.h" /* ===== Default Event Processors ===== */ -void debugSetupWithoutLogging(JsonObject settings); -void debugSetup(); +void debugSetup(JsonObject settings); /* ===== Special Event Processors ===== */ @@ -163,9 +162,10 @@ enum { TAG_EVENT = 8, TAG_DEBG = 10, - TAG_TELN = 11, - TAG_SYSL = 12, - TAG_TASM = 13, + TAG_CONS = 11, + TAG_TELN = 12, + TAG_SYSL = 13, + TAG_TASM = 14, TAG_CONF = 20, TAG_GUI = 21, diff --git a/src/log/hasp_debug.cpp b/src/log/hasp_debug.cpp index 522c18a0..3e41b88b 100644 --- a/src/log/hasp_debug.cpp +++ b/src/log/hasp_debug.cpp @@ -39,10 +39,6 @@ #include "hasp/hasp_dispatch.h" #include "hasp/hasp.h" -// #ifdef USE_CONFIG_OVERRIDE -// #include "user_config_override.h" -// #endif - #ifndef SERIAL_SPEED #define SERIAL_SPEED 115200 #endif @@ -66,7 +62,6 @@ // static String debugOutput((char *)0); // static StringStream debugStream((String &)debugOutput); -// extern char mqttNodeName[16]; // const char* syslogAppName = APP_NAME; char debugSyslogHost[32] = SYSLOG_SERVER; uint16_t debugSyslogPort = SYSLOG_PORT; @@ -92,9 +87,6 @@ uint16_t debugSerialBaud = SERIAL_SPEED / 10; // Multiplied by 10 extern bool debugSerialStarted; extern bool debugAnsiCodes; -ConsoleInput debugConsole(&Serial, HASP_CONSOLE_BUFFER); - -unsigned long debugLastMillis = 0; extern dispatch_conf_t dispatch_setings; // #if HASP_USE_SYSLOG > 0 @@ -106,14 +98,6 @@ extern dispatch_conf_t dispatch_setings; // } // #endif -void debugSetup() -{ - // memset(serialInputBuffer, 0, sizeof(serialInputBuffer)); - // serialInputIndex = 0; - LOG_TRACE(TAG_DEBG, F(D_SERVICE_STARTING)); // Starting console - debugConsole.setLineCallback(dispatch_text_line); -} - void debugStartSyslog() { @@ -293,13 +277,14 @@ void debugPrintSuffix(uint8_t tag, int level, Print* _logOutput) _logOutput->println(); if(_logOutput == &Serial) { - debugConsole.update(); + console_update_prompt(); } else { _logOutput->print("hasp > "); } } -void debugSetupWithoutLogging(JsonObject settings) +// Do NOT call Log function before debugSetup is called +void debugSetup(JsonObject settings) { Log.begin(LOG_LEVEL_WARNING, true); Log.setPrefix(debugPrintPrefix); // Uncomment to get timestamps as prefix @@ -335,23 +320,23 @@ void debugSetupWithoutLogging(JsonObject settings) IRAM_ATTR void debugLoop(void) { - int16_t keypress; - do { - switch(keypress = debugConsole.readKey()) { + /* int16_t keypress; + do { + switch(keypress = debugConsole.readKey()) { - case ConsoleInput::KEY_PAGE_UP: - dispatch_page_next(LV_SCR_LOAD_ANIM_NONE); - break; + case ConsoleInput::KEY_PAGE_UP: + dispatch_page_next(LV_SCR_LOAD_ANIM_NONE); + break; - case ConsoleInput::KEY_PAGE_DOWN: - dispatch_page_prev(LV_SCR_LOAD_ANIM_NONE); - break; + case ConsoleInput::KEY_PAGE_DOWN: + dispatch_page_prev(LV_SCR_LOAD_ANIM_NONE); + break; - case(ConsoleInput::KEY_FN)...(ConsoleInput::KEY_FN + 12): - dispatch_set_page(keypress - ConsoleInput::KEY_FN, LV_SCR_LOAD_ANIM_NONE); - break; - } - } while(keypress != 0); + case(ConsoleInput::KEY_FN)...(ConsoleInput::KEY_FN + 12): + dispatch_set_page(keypress - ConsoleInput::KEY_FN, LV_SCR_LOAD_ANIM_NONE); + break; + } + } while(keypress != 0); */ } void printLocalTime() diff --git a/src/main_arduino copy.cpp b/src/main_arduino copy.cpp index dcc98250..0e2b192d 100644 --- a/src/main_arduino copy.cpp +++ b/src/main_arduino copy.cpp @@ -54,12 +54,11 @@ void setup() * Read & Apply User Configuration ***************************/ #if HASP_USE_CONFIG > 0 - configSetup(); // also runs debugSetupWithoutLogging(), debugSetup() and debugStart() + configSetup(); // also runs debugSetup() and debugStart() #endif dispatchSetup(); guiSetup(); - debugSetup(); // Init the console #if HASP_USE_GPIO > 0 gpioSetup(); diff --git a/src/main_arduino.cpp b/src/main_arduino.cpp index 4d3c23f6..b990b941 100644 --- a/src/main_arduino.cpp +++ b/src/main_arduino.cpp @@ -54,12 +54,11 @@ void setup() * Read & Apply User Configuration ***************************/ #if HASP_USE_CONFIG > 0 - configSetup(); // also runs debugSetupWithoutLogging(), debugSetup() and debugStart() + configSetup(); // also runs debugSetup() and debugStart() #endif dispatchSetup(); // before hasp and oobe, asap after logging starts guiSetup(); - debugSetup(); // Init the console #if HASP_USE_CONFIG > 0 if(!oobeSetup()) @@ -96,6 +95,10 @@ void setup() httpSetup(); #endif +#if HASP_USE_CONSOLE > 0 + consoleSetup(); +#endif + #if HASP_USE_TELNET > 0 telnetSetup(); #endif @@ -123,9 +126,13 @@ IRAM_ATTR void loop() mqttLoop(); #endif // MQTT - debugLoop(); // Console + // debugLoop(); haspDevice.loop(); +#if HASP_USE_CONSOLE > 0 + consoleLoop(); +#endif + /* Timer Loop */ if(millis() - mainLastLoopTime >= 1000) { diff --git a/src/main_sdl2.cpp b/src/main_sdl2.cpp index 75ce1ae9..78fdf2e1 100644 --- a/src/main_sdl2.cpp +++ b/src/main_sdl2.cpp @@ -120,8 +120,6 @@ void setup() // hal_setup(); guiSetup(); - // debugSetup(); // Init the console - printf("%s %d\n", __FILE__, __LINE__); dispatchSetup(); // for hasp and oobe haspSetup(); diff --git a/src/sys/svc/hasp_console.cpp b/src/sys/svc/hasp_console.cpp new file mode 100644 index 00000000..e6ab31ce --- /dev/null +++ b/src/sys/svc/hasp_console.cpp @@ -0,0 +1,78 @@ +/* MIT License - Copyright (c) 2019-2021 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#include "hasplib.h" + +#if HASP_USE_CONSOLE > 0 + +#include "ConsoleInput.h" + +#include "hasp_debug.h" +#include "hasp_config.h" +#include "hasp_console.h" + +#include "../../hasp/hasp_dispatch.h" + +uint8_t consoleInputEnabled = true; +ConsoleInput debugConsole(&Serial, HASP_CONSOLE_BUFFER); + +void console_update_prompt() +{ + debugConsole.update(); +} + +void consoleSetup() +{ + LOG_TRACE(TAG_MSGR, F(D_SERVICE_STARTING)); + debugConsole.setLineCallback(dispatch_text_line); + LOG_INFO(TAG_CONS, F(D_SERVICE_STARTED)); +} + +IRAM_ATTR void consoleLoop() +{ + if(!consoleInputEnabled) return; + + int16_t keypress; + do { + switch(keypress = debugConsole.readKey()) { + + case ConsoleInput::KEY_PAGE_UP: + dispatch_page_next(LV_SCR_LOAD_ANIM_NONE); + break; + + case ConsoleInput::KEY_PAGE_DOWN: + dispatch_page_prev(LV_SCR_LOAD_ANIM_NONE); + break; + + case(ConsoleInput::KEY_FN)...(ConsoleInput::KEY_FN + 12): + dispatch_set_page(keypress - ConsoleInput::KEY_FN, LV_SCR_LOAD_ANIM_NONE); + break; + } + } while(keypress != 0); +} + +#if HASP_USE_CONFIG > 0 +bool consoleGetConfig(const JsonObject& settings) +{ + bool changed = false; + + if(changed) configOutput(settings, TAG_CONS); + return changed; +} + +/** Set console Configuration. + * + * Read the settings from json and sets the application variables. + * + * @param[in] settings JsonObject with the config settings. + **/ +bool consoleSetConfig(const JsonObject& settings) +{ + configOutput(settings, TAG_CONS); + bool changed = false; + + return changed; +} +#endif // HASP_USE_CONFIG + +#endif \ No newline at end of file diff --git a/src/sys/svc/hasp_console.h b/src/sys/svc/hasp_console.h new file mode 100644 index 00000000..e0ed31c9 --- /dev/null +++ b/src/sys/svc/hasp_console.h @@ -0,0 +1,36 @@ +/* MIT License - Copyright (c) 2019-2021 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#ifndef HASP_CONSOLE_H +#define HASP_CONSOLE_H + +#if HASP_USE_CONSOLE > 0 + +#include "hasplib.h" + +/* ===== Default Event Processors ===== */ +void consoleSetup(); +IRAM_ATTR void consoleLoop(void); +void consoleEvery5Seconds(void); +void consoleEverySecond(void); +void consoleStart(void); +void consoleStop(void); + +/* ===== Special Event Processors ===== */ +void console_update_prompt(); + +/* ===== Getter and Setter Functions ===== */ + +/* ===== Read/Write Configuration ===== */ +#if HASP_USE_CONFIG > 0 +bool consoleSetConfig(const JsonObject& settings); +bool consoleGetConfig(const JsonObject& settings); +#endif + +#define CONSOLE_UNAUTHENTICATED 0 +#define CONSOLE_USERNAME_OK 10 +#define CONSOLE_USERNAME_NOK 99 +#define CONSOLE_AUTHENTICATED 255 + +#endif +#endif