Add separate hasp_console

This commit is contained in:
fvanroie 2021-05-06 21:26:11 +02:00
parent 8fbab678b3
commit 76ae156018
10 changed files with 160 additions and 48 deletions

View File

@ -63,6 +63,10 @@
#define HASP_USE_TELNET 0 #define HASP_USE_TELNET 0
#endif #endif
#ifndef HASP_USE_CONSOLE
#define HASP_USE_CONSOLE 1
#endif
/* Filesystem */ /* Filesystem */
#define HASP_HAS_FILESYSTEM (ARDUINO_ARCH_ESP32 > 0 || ARDUINO_ARCH_ESP8266 > 0) #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" #include "sys/svc/hasp_http.h"
#endif #endif
#if HASP_USE_CONSOLE > 0
#include "sys/svc/hasp_console.h"
#endif
#if HASP_USE_TELNET > 0 #if HASP_USE_TELNET > 0
#include "sys/svc/hasp_telnet.h" #include "sys/svc/hasp_telnet.h"
#endif #endif

View File

@ -76,7 +76,7 @@ bool configSet(uint16_t& value, const JsonVariant& setting, const __FlashStringH
void configSetupDebug(JsonDocument& settings) 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 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); configRestorePasswords(settings, wifiPass, mqttPass, httpPass);
LOG_INFO(TAG_CONF, F(D_FILE_LOADED), configFile.c_str()); LOG_INFO(TAG_CONF, F(D_FILE_LOADED), configFile.c_str());
if(setupdebug) debugSetup(); // if(setupdebug) debugSetup();
return error; return error;
} }

View File

@ -86,8 +86,6 @@ static void debugPrintTimestamp(int level, Print* _logOutput)
} }
/* ===== Default Event Processors ===== */ /* ===== Default Event Processors ===== */
// void debugSetupWithoutLogging(JsonObject settings);
// void debugSetup();
static inline void debug_flush() static inline void debug_flush()
{ {
@ -243,6 +241,9 @@ void debug_get_tag(uint8_t tag, char* buffer)
memcpy_P(buffer, PSTR("HAL "), 5); memcpy_P(buffer, PSTR("HAL "), 5);
break; break;
case TAG_CONS:
memcpy_P(buffer, PSTR("CONS"), 5);
break;
case TAG_DEBG: case TAG_DEBG:
memcpy_P(buffer, PSTR("DBUG"), 5); memcpy_P(buffer, PSTR("DBUG"), 5);
break; break;

View File

@ -14,8 +14,7 @@
#include "ArduinoLog.h" #include "ArduinoLog.h"
/* ===== Default Event Processors ===== */ /* ===== Default Event Processors ===== */
void debugSetupWithoutLogging(JsonObject settings); void debugSetup(JsonObject settings);
void debugSetup();
/* ===== Special Event Processors ===== */ /* ===== Special Event Processors ===== */
@ -163,9 +162,10 @@ enum {
TAG_EVENT = 8, TAG_EVENT = 8,
TAG_DEBG = 10, TAG_DEBG = 10,
TAG_TELN = 11, TAG_CONS = 11,
TAG_SYSL = 12, TAG_TELN = 12,
TAG_TASM = 13, TAG_SYSL = 13,
TAG_TASM = 14,
TAG_CONF = 20, TAG_CONF = 20,
TAG_GUI = 21, TAG_GUI = 21,

View File

@ -39,10 +39,6 @@
#include "hasp/hasp_dispatch.h" #include "hasp/hasp_dispatch.h"
#include "hasp/hasp.h" #include "hasp/hasp.h"
// #ifdef USE_CONFIG_OVERRIDE
// #include "user_config_override.h"
// #endif
#ifndef SERIAL_SPEED #ifndef SERIAL_SPEED
#define SERIAL_SPEED 115200 #define SERIAL_SPEED 115200
#endif #endif
@ -66,7 +62,6 @@
// static String debugOutput((char *)0); // static String debugOutput((char *)0);
// static StringStream debugStream((String &)debugOutput); // static StringStream debugStream((String &)debugOutput);
// extern char mqttNodeName[16];
// const char* syslogAppName = APP_NAME; // const char* syslogAppName = APP_NAME;
char debugSyslogHost[32] = SYSLOG_SERVER; char debugSyslogHost[32] = SYSLOG_SERVER;
uint16_t debugSyslogPort = SYSLOG_PORT; uint16_t debugSyslogPort = SYSLOG_PORT;
@ -92,9 +87,6 @@ uint16_t debugSerialBaud = SERIAL_SPEED / 10; // Multiplied by 10
extern bool debugSerialStarted; extern bool debugSerialStarted;
extern bool debugAnsiCodes; extern bool debugAnsiCodes;
ConsoleInput debugConsole(&Serial, HASP_CONSOLE_BUFFER);
unsigned long debugLastMillis = 0;
extern dispatch_conf_t dispatch_setings; extern dispatch_conf_t dispatch_setings;
// #if HASP_USE_SYSLOG > 0 // #if HASP_USE_SYSLOG > 0
@ -106,14 +98,6 @@ extern dispatch_conf_t dispatch_setings;
// } // }
// #endif // #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() void debugStartSyslog()
{ {
@ -293,13 +277,14 @@ void debugPrintSuffix(uint8_t tag, int level, Print* _logOutput)
_logOutput->println(); _logOutput->println();
if(_logOutput == &Serial) { if(_logOutput == &Serial) {
debugConsole.update(); console_update_prompt();
} else { } else {
_logOutput->print("hasp > "); _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.begin(LOG_LEVEL_WARNING, true);
Log.setPrefix(debugPrintPrefix); // Uncomment to get timestamps as prefix Log.setPrefix(debugPrintPrefix); // Uncomment to get timestamps as prefix
@ -335,23 +320,23 @@ void debugSetupWithoutLogging(JsonObject settings)
IRAM_ATTR void debugLoop(void) IRAM_ATTR void debugLoop(void)
{ {
int16_t keypress; /* int16_t keypress;
do { do {
switch(keypress = debugConsole.readKey()) { switch(keypress = debugConsole.readKey()) {
case ConsoleInput::KEY_PAGE_UP: case ConsoleInput::KEY_PAGE_UP:
dispatch_page_next(LV_SCR_LOAD_ANIM_NONE); dispatch_page_next(LV_SCR_LOAD_ANIM_NONE);
break; break;
case ConsoleInput::KEY_PAGE_DOWN: case ConsoleInput::KEY_PAGE_DOWN:
dispatch_page_prev(LV_SCR_LOAD_ANIM_NONE); dispatch_page_prev(LV_SCR_LOAD_ANIM_NONE);
break; break;
case(ConsoleInput::KEY_FN)...(ConsoleInput::KEY_FN + 12): case(ConsoleInput::KEY_FN)...(ConsoleInput::KEY_FN + 12):
dispatch_set_page(keypress - ConsoleInput::KEY_FN, LV_SCR_LOAD_ANIM_NONE); dispatch_set_page(keypress - ConsoleInput::KEY_FN, LV_SCR_LOAD_ANIM_NONE);
break; break;
} }
} while(keypress != 0); } while(keypress != 0); */
} }
void printLocalTime() void printLocalTime()

View File

@ -54,12 +54,11 @@ void setup()
* Read & Apply User Configuration * Read & Apply User Configuration
***************************/ ***************************/
#if HASP_USE_CONFIG > 0 #if HASP_USE_CONFIG > 0
configSetup(); // also runs debugSetupWithoutLogging(), debugSetup() and debugStart() configSetup(); // also runs debugSetup() and debugStart()
#endif #endif
dispatchSetup(); dispatchSetup();
guiSetup(); guiSetup();
debugSetup(); // Init the console
#if HASP_USE_GPIO > 0 #if HASP_USE_GPIO > 0
gpioSetup(); gpioSetup();

View File

@ -54,12 +54,11 @@ void setup()
* Read & Apply User Configuration * Read & Apply User Configuration
***************************/ ***************************/
#if HASP_USE_CONFIG > 0 #if HASP_USE_CONFIG > 0
configSetup(); // also runs debugSetupWithoutLogging(), debugSetup() and debugStart() configSetup(); // also runs debugSetup() and debugStart()
#endif #endif
dispatchSetup(); // before hasp and oobe, asap after logging starts dispatchSetup(); // before hasp and oobe, asap after logging starts
guiSetup(); guiSetup();
debugSetup(); // Init the console
#if HASP_USE_CONFIG > 0 #if HASP_USE_CONFIG > 0
if(!oobeSetup()) if(!oobeSetup())
@ -96,6 +95,10 @@ void setup()
httpSetup(); httpSetup();
#endif #endif
#if HASP_USE_CONSOLE > 0
consoleSetup();
#endif
#if HASP_USE_TELNET > 0 #if HASP_USE_TELNET > 0
telnetSetup(); telnetSetup();
#endif #endif
@ -123,9 +126,13 @@ IRAM_ATTR void loop()
mqttLoop(); mqttLoop();
#endif // MQTT #endif // MQTT
debugLoop(); // Console // debugLoop();
haspDevice.loop(); haspDevice.loop();
#if HASP_USE_CONSOLE > 0
consoleLoop();
#endif
/* Timer Loop */ /* Timer Loop */
if(millis() - mainLastLoopTime >= 1000) { if(millis() - mainLastLoopTime >= 1000) {

View File

@ -120,8 +120,6 @@ void setup()
// hal_setup(); // hal_setup();
guiSetup(); guiSetup();
// debugSetup(); // Init the console
printf("%s %d\n", __FILE__, __LINE__); printf("%s %d\n", __FILE__, __LINE__);
dispatchSetup(); // for hasp and oobe dispatchSetup(); // for hasp and oobe
haspSetup(); haspSetup();

View File

@ -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

View File

@ -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