From 91e25ea8ae34571383f49f82c77fe9e3677316bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Wed, 18 Apr 2018 13:35:47 +0200 Subject: [PATCH] Decouple Serial object from SerialInjector (#787) --- code/espurna/debug.ino | 14 ++++++++- code/espurna/espurna.ino | 7 ++++- code/espurna/libs/StreamInjector.h | 50 ++++++++++++++++-------------- code/espurna/settings.ino | 43 ++++++++++++------------- code/espurna/system.ino | 7 ----- 5 files changed, 65 insertions(+), 56 deletions(-) diff --git a/code/espurna/debug.ino b/code/espurna/debug.ino index f442ab56..e7659693 100644 --- a/code/espurna/debug.ino +++ b/code/espurna/debug.ino @@ -119,7 +119,7 @@ void debugSend_P(PGM_P format_P, ...) { #if DEBUG_WEB_SUPPORT -void debugSetup() { +void debugWebSetup() { wsOnSendRegister([](JsonObject& root) { root["dbgVisible"] = 1; @@ -145,6 +145,17 @@ void debugSetup() { #endif // DEBUG_WEB_SUPPORT +void debugSetup() { + + #if DEBUG_SERIAL_SUPPORT + DEBUG_PORT.begin(SERIAL_BAUDRATE); + #if DEBUG_ESP_WIFI + DEBUG_PORT.setDebugOutput(true); + #endif + #endif + +} + // ----------------------------------------------------------------------------- // Save crash info // Taken from krzychb EspSaveCrash @@ -274,4 +285,5 @@ void debugDumpCrashInfo() { DEBUG_MSG_P(PSTR("<< writeCallback; - StreamInjector(Stream& serial, size_t buflen = 128) : _stream(serial), _buffer_size(buflen) { + StreamInjector(size_t buflen = 128) : _buffer_size(buflen) { _buffer = new char[buflen]; } @@ -20,28 +20,42 @@ class StreamInjector : public Stream { delete[] _buffer; } + // --------------------------------------------------------------------- + + virtual uint8_t inject(char ch) { + _buffer[_buffer_write] = ch; + _buffer_write = (_buffer_write + 1) % _buffer_size; + return 1; + } + + virtual uint8_t inject(char *data, size_t len) { + for (int i=0; i _buffer_write) { bytes += (_buffer_write - _buffer_read + _buffer_size); } else if (_buffer_read < _buffer_write) { @@ -51,35 +65,23 @@ class StreamInjector : public Stream { } virtual int peek() { - int ch = _stream.peek(); - if (ch == -1) { - if (_buffer_read != _buffer_write) { - ch = _buffer[_buffer_read]; - } + int ch = -1; + if (_buffer_read != _buffer_write) { + ch = _buffer[_buffer_read]; } return ch; } virtual void flush() { - _stream.flush(); _buffer_read = _buffer_write; } - virtual void inject(char *data, size_t len) { - for (int i=0; i #include #include "libs/EmbedisWrap.h" #include +#include "libs/StreamInjector.h" -#ifdef DEBUG_PORT - #define EMBEDIS_PORT DEBUG_PORT -#else - #define EMBEDIS_PORT Serial -#endif - -#if TELNET_SUPPORT - #include "libs/StreamInjector.h" - StreamInjector _serial = StreamInjector(EMBEDIS_PORT, TERMINAL_BUFFER_SIZE); - #undef EMBEDIS_PORT - #define EMBEDIS_PORT _serial -#endif - -EmbedisWrap embedis(EMBEDIS_PORT, TERMINAL_BUFFER_SIZE); +StreamInjector _serial = StreamInjector(TERMINAL_BUFFER_SIZE); +EmbedisWrap embedis(_serial, TERMINAL_BUFFER_SIZE); #if TERMINAL_SUPPORT #if SERIAL_RX_ENABLED @@ -360,11 +349,9 @@ void resetSettings() { // Settings // ----------------------------------------------------------------------------- -#if TELNET_SUPPORT - void settingsInject(void *data, size_t len) { - _serial.inject((char *) data, len); - } -#endif +void settingsInject(void *data, size_t len) { + _serial.inject((char *) data, len); +} size_t settingsMaxSize() { size_t size = EEPROM_SIZE; @@ -420,11 +407,14 @@ void settingsSetup() { EEPROM.begin(SPI_FLASH_SEC_SIZE); - #if TELNET_SUPPORT - _serial.callback([](uint8_t ch) { + _serial.callback([](uint8_t ch) { + #if TELNET_SUPPORT telnetWrite(ch); - }); - #endif + #endif + #if DEBUG_SERIAL_SUPPORT + DEBUG_PORT.write(ch); + #endif + }); Embedis::dictionary( F("EEPROM"), SPI_FLASH_SEC_SIZE, @@ -457,8 +447,15 @@ void settingsLoop() { _settings_save = false; } + #if TERMINAL_SUPPORT + #if DEBUG_SERIAL_SUPPORT + while (DEBUG_PORT.available()) { + _serial.inject(DEBUG_PORT.read()); + } + #endif + embedis.process(); #if SERIAL_RX_ENABLED diff --git a/code/espurna/system.ino b/code/espurna/system.ino index e6b5b9c5..fb1d1ed0 100644 --- a/code/espurna/system.ino +++ b/code/espurna/system.ino @@ -150,13 +150,6 @@ void systemSetup() { EEPROM.begin(EEPROM_SIZE); - #if DEBUG_SERIAL_SUPPORT - DEBUG_PORT.begin(SERIAL_BAUDRATE); - #if DEBUG_ESP_WIFI - DEBUG_PORT.setDebugOutput(true); - #endif - #endif - #if SPIFFS_SUPPORT SPIFFS.begin(); #endif