From 72171b9500c1d2e63089f5dde4c96d193e345aed Mon Sep 17 00:00:00 2001 From: TD-er Date: Sat, 18 Jul 2026 16:09:50 +0200 Subject: [PATCH] [ESPEasy Console] Simplify init of ESPEasy console + update IDF/Arduino --- lib/ESPEasySerial/ESPEasySerialConfig.cpp | 18 ++ lib/ESPEasySerial/ESPEasySerialConfig.h | 4 + platformio_core_defs.ini | 3 +- src/_P040_ID12.ino | 2 +- src/_P091_SerSwitch.ino | 2 +- src/src/ESPEasyCore/ESPEasy_Console.h | 9 +- src/src/ESPEasyCore/ESPEasy_Console_Port.cpp | 173 +++++++++++--- src/src/ESPEasyCore/ESPEasy_Console_Port.h | 16 ++ .../ESPEasy_Console_dual_serial.cpp | 212 +++++------------- .../ESPEasy_Console_serial0_only.cpp | 46 +--- .../ESPEasy_Console_single_serial.cpp | 161 ++++--------- src/src/Helpers/Improv_Helper.cpp | 5 + src/src/Helpers/Improv_Helper.h | 2 + 13 files changed, 303 insertions(+), 350 deletions(-) diff --git a/lib/ESPEasySerial/ESPEasySerialConfig.cpp b/lib/ESPEasySerial/ESPEasySerialConfig.cpp index 67ad8359c..48d6ffea0 100644 --- a/lib/ESPEasySerial/ESPEasySerialConfig.cpp +++ b/lib/ESPEasySerial/ESPEasySerialConfig.cpp @@ -2,6 +2,24 @@ #include "ESPEasySerialType.h" +bool ESPEasySerialConfig::operator==(const ESPEasySerialConfig& other) const +{ + return port == other.port + && baud == other.baud + && receivePin == other.receivePin + && transmitPin == other.transmitPin + && inverse_logic == other.inverse_logic + && rxBuffSize == other.rxBuffSize + && txBuffSize == other.txBuffSize + && forceSWserial == other.forceSWserial + && timeout_ms == other.timeout_ms + && config == other.config +#ifdef ESP8266 + && mode == other.mode +#endif + ; +} + void ESPEasySerialConfig::validate() { port = ESPeasySerialType::getSerialType(port, receivePin, transmitPin); diff --git a/lib/ESPEasySerial/ESPEasySerialConfig.h b/lib/ESPEasySerial/ESPEasySerialConfig.h index 690626537..cb9fa4a4b 100644 --- a/lib/ESPEasySerial/ESPEasySerialConfig.h +++ b/lib/ESPEasySerial/ESPEasySerialConfig.h @@ -24,6 +24,10 @@ struct ESPEasySerialConfig { ESPEasySerialConfig() = default; + bool operator==(const ESPEasySerialConfig& other) const; + + ESPEasySerialConfig& operator=(const ESPEasySerialConfig& other) = default; + void validate(); #ifdef ESP8266 diff --git a/platformio_core_defs.ini b/platformio_core_defs.ini index e46e72b69..671c03937 100644 --- a/platformio_core_defs.ini +++ b/platformio_core_defs.ini @@ -191,9 +191,10 @@ extra_scripts = ${esp82xx_common.extra_scripts} ;platform_packages = framework-arduinoespressif32 @ https://github.com/Jason2866/esp32-arduino-lib-builder/releases/download/2904-2115-5.5/framework-arduinoespressif32-release_v5.5-f2a3fa2b.tar.xz platform = https://github.com/Jason2866/platform-espressif32.git#Arduino/IDF55_gcc152 -platform_packages = +platform_packages = framework-arduinoespressif32 @ https://github.com/Jason2866/esp32-arduino-lib-builder/releases/download/1807-0842-5.5/framework-arduinoespressif32-release_v5.5-67336dba.tar.xz ;platform_packages = framework-arduinoespressif32 @ https://github.com/Jason2866/esp32-arduino-lib-builder/releases/download/2805-1324-5.5/framework-arduinoespressif32-release_v5.5-f3cdb9d0.tar.xz + custom_remove_include = true custom_component_remove = diff --git a/src/_P040_ID12.ino b/src/_P040_ID12.ino index 13dfaa868..6d1af2bfd 100644 --- a/src/_P040_ID12.ino +++ b/src/_P040_ID12.ino @@ -88,6 +88,7 @@ boolean Plugin_040(uint8_t function, struct EventStruct *event, String& string) { if (Plugin_040_init) { + success = true; uint8_t val = 0; uint8_t code[6]; uint8_t checksum = 0; @@ -187,7 +188,6 @@ boolean Plugin_040(uint8_t function, struct EventStruct *event, String& string) Scheduler.setPluginTaskTimer(500, event->TaskIndex, event->Par1); } } - success = true; } break; } diff --git a/src/_P091_SerSwitch.ino b/src/_P091_SerSwitch.ino index ebea3d752..2fe2b0c0d 100644 --- a/src/_P091_SerSwitch.ino +++ b/src/_P091_SerSwitch.ino @@ -319,6 +319,7 @@ boolean Plugin_091(uint8_t function, struct EventStruct *event, String& string) if (Plugin_091_init) { + success = true; while (ESPEASY_SERIAL_0.available() > 0) { yield(); @@ -533,7 +534,6 @@ boolean Plugin_091(uint8_t function, struct EventStruct *event, String& string) } } } // plugin initialized end - success = true; break; } diff --git a/src/src/ESPEasyCore/ESPEasy_Console.h b/src/src/ESPEasyCore/ESPEasy_Console.h index b266cc0d8..adc344d20 100644 --- a/src/src/ESPEasyCore/ESPEasy_Console.h +++ b/src/src/ESPEasyCore/ESPEasy_Console.h @@ -37,8 +37,6 @@ private: bool handledByPluginSerialIn(); - void readInput(EspEasy_Console_Port& port); - #if FEATURE_DEFINE_SERIAL_CONSOLE_PORT ESPeasySerial * getPort(); #else // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT @@ -47,18 +45,13 @@ private: void endPort(); - uint32_t _baudrate = 115200u; EspEasy_Console_Port _mainSerial; #if FEATURE_DEFINE_SERIAL_CONSOLE_PORT # if USES_ESPEASY_CONSOLE_FALLBACK_PORT + // Serial port to be always used as HW Serial0 EspEasy_Console_Port _fallbackSerial; # endif - - // Cache the used settings, so we can check whether to change the console serial - uint8_t _console_serial_port = DEFAULT_CONSOLE_PORT; - int8_t _console_serial_rxpin = DEFAULT_CONSOLE_PORT_RXPIN; - int8_t _console_serial_txpin = DEFAULT_CONSOLE_PORT_TXPIN; #endif }; diff --git a/src/src/ESPEasyCore/ESPEasy_Console_Port.cpp b/src/src/ESPEasyCore/ESPEasy_Console_Port.cpp index a179a296b..929d6425d 100644 --- a/src/src/ESPEasyCore/ESPEasy_Console_Port.cpp +++ b/src/src/ESPEasyCore/ESPEasy_Console_Port.cpp @@ -105,6 +105,35 @@ size_t EspEasy_Console_Port::available() const return res; } +void EspEasy_Console_Port::begin(uint32_t baudrate) +{ + updateActiveTaskUseSerial0(); + + if (_serial != nullptr) { + # if FEATURE_IMPROV + _improv.init(); + #endif + +#if FEATURE_DEFINE_SERIAL_CONSOLE_PORT + _config.baud = baudrate; +#endif + _serial->begin(baudrate); + addLog(LOG_LEVEL_INFO, F("ESPEasy console using ESPEasySerial")); + + +# ifdef ESP32 + // Allow to flush data from the serial buffers + // When not opening the USB serial port, the ESP may hang at boot. + delay(10); + _serial->end(); + delay(10); + _serial->begin(baudrate); + _serial->flush(); +# endif // ifdef ESP32 + + } +} + void EspEasy_Console_Port::endPort() { if (_serial != nullptr) { @@ -112,38 +141,70 @@ void EspEasy_Console_Port::endPort() } } -bool EspEasy_Console_Port::process_serialWriteBuffer() +void EspEasy_Console_Port::readInput() { - if (_serial != nullptr) { -#ifdef ESP32 + if (_serial == nullptr) return; + size_t bytesToRead = available(); - if (!xPortCanYield()) { return false; } -#endif // ifdef ESP32 - size_t availableForWrite = _serial->availableForWrite(); + while (bytesToRead > 0) + { + --bytesToRead; + delay(0); + const int SerialInByte = read(); - if (availableForWrite == 0) { return false; } - - if (availableForWrite == 1) { - // For only a single byte, just write it directly - return _serialWriteBuffer.process(_serial, availableForWrite); - } - - if (availableForWrite > 64) { - // Set to max. of 64 bytes as this is the optimum 'chunk size' for most - // serial ports, like the CDC ports and I2C to UART. - // Also it is relatively fast to allocate. - availableForWrite = 64; - } - - PrintToString str; - str.reserve(availableForWrite); - - if (_serialWriteBuffer.process(&str, availableForWrite)) { - _serial->write(str.get().c_str(), str.length()); - return true; + if (SerialInByte >= 0) { + if (process_consoleInput(SerialInByte)) { + // Processed a full line + return; + } } } - return false; +} + +ESPEasySerialPort EspEasy_Console_Port::getPortType() const +{ + #if FEATURE_DEFINE_SERIAL_CONSOLE_PORT + + if (_serial == nullptr) { return ESPEasySerialPort::not_set; } + return _config.port; + #else // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT + + // TODO TD-er: Should I also try to see if we're using serial0_swapped? + return ESPEasySerialPort::serial0; + #endif // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT +} + +bool EspEasy_Console_Port::process_serialWriteBuffer() +{ + if (_serial == nullptr) { return false; } +#ifdef ESP32 + + if (!xPortCanYield()) { return false; } +#endif // ifdef ESP32 + size_t availableForWrite = _serial->availableForWrite(); + + if (availableForWrite == 0) { return false; } + + if (availableForWrite == 1) { + // For only a single byte, just write it directly + return _serialWriteBuffer.process(_serial, availableForWrite); + } + + if (availableForWrite > 64) { + // Set to max. of 64 bytes as this is the optimum 'chunk size' for most + // serial ports, like the CDC ports and I2C to UART. + // Also it is relatively fast to allocate. + availableForWrite = 64; + } + + PrintToString str; + str.reserve(availableForWrite); + + if (!_serialWriteBuffer.process(&str, availableForWrite)) { + return false; + } + _serial->write(str.get().c_str(), str.length()); + return true; } bool EspEasy_Console_Port::process_consoleInput(uint8_t SerialInByte) @@ -199,3 +260,61 @@ String EspEasy_Console_Port::getPortDescription() const return F("-"); } + +#if FEATURE_DEFINE_SERIAL_CONSOLE_PORT + +bool EspEasy_Console_Port::updateSerialPort( + const ESPEasySerialConfig& config) +{ + updateActiveTaskUseSerial0(); + bool somethingChanged = false; + + const bool consoleUseSerial0 = ( +# ifdef ESP8266 + (config.port == ESPEasySerialPort::serial0_swap) || +# endif // ifdef ESP8266 + config.port == ESPEasySerialPort::serial0); + + const bool canUseSerial0 = !activeTaskUseSerial0() && !log_to_serial_disabled; + + + bool mustHaveSerial = Settings.UseSerial && (!consoleUseSerial0 || canUseSerial0); + + if (!(_config == config) || + !mustHaveSerial) { + if (_serial != nullptr) { + delete _serial; + _serial = nullptr; + somethingChanged = true; + } + _config = config; + } + + if ((_serial == nullptr) && mustHaveSerial) { + # ifdef USE_SECOND_HEAP + HeapSelectDram ephemeral; + # endif // ifdef USE_SECOND_HEAP + + { + # ifdef USE_SECOND_HEAP + HeapSelectDram ephemeral; + # endif // ifdef USE_SECOND_HEAP + _serial = new (std::nothrow) ESPeasySerial(_config); + } + + somethingChanged = true; + } + + + if (_serial == nullptr) { + _serialWriteBuffer.clear(); + } + + if (somethingChanged) { + begin(_config.baud); + } + + return somethingChanged; +} + +#endif // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT diff --git a/src/src/ESPEasyCore/ESPEasy_Console_Port.h b/src/src/ESPEasyCore/ESPEasy_Console_Port.h index ec7db6fda..37b00f82a 100644 --- a/src/src/ESPEasyCore/ESPEasy_Console_Port.h +++ b/src/src/ESPEasyCore/ESPEasy_Console_Port.h @@ -27,14 +27,30 @@ struct EspEasy_Console_Port { int read(); size_t available() const; + void begin(uint32_t baudrate); + void endPort(); + void readInput(); + + ESPEasySerialPort getPortType() const; + bool process_serialWriteBuffer(); bool process_consoleInput(uint8_t SerialInByte); String getPortDescription() const; +#if FEATURE_DEFINE_SERIAL_CONSOLE_PORT + bool updateSerialPort(const ESPEasySerialConfig& config); + +private: + // Cache the used settings, so we can check whether to change the console serial + ESPEasySerialConfig _config; + +public: +#endif + int SerialInByteCounter{}; char *InputBuffer_Serial{}; SerialWriteBuffer_t _serialWriteBuffer; diff --git a/src/src/ESPEasyCore/ESPEasy_Console_dual_serial.cpp b/src/src/ESPEasyCore/ESPEasy_Console_dual_serial.cpp index 12fc8f416..c482bf88c 100644 --- a/src/src/ESPEasyCore/ESPEasy_Console_dual_serial.cpp +++ b/src/src/ESPEasyCore/ESPEasy_Console_dual_serial.cpp @@ -31,152 +31,96 @@ EspEasy_Console_t::EspEasy_Console_t() : _mainSerial(LOG_TO_SERIAL) , _fallbackSerial(LOG_TO_SERIAL_EXTRA) { - const ESPEasySerialPort port = static_cast(_console_serial_port); - # if USES_USBCDC /* if (port == ESPEasySerialPort::usb_cdc_0 || - port == ESPEasySerialPort::usb_cdc_1) + port == ESPEasySerialPort::usb_cdc_1) { - USB.manufacturerName(F("ESPEasy")); - USB.productName() + USB.manufacturerName(F("ESPEasy")); + USB.productName() } */ # endif // if USES_USBCDC - - // Ideal buffer size is a trade-off between bootspeed - // and not missing data when the ESP is busy processing stuff. - // Since we do have a separate buffer in the console, - // it may just take less time in the background tasks to dump - // any logs as larger chunks can be transferred at once. - constexpr size_t buffSize = 512; - ESPEasySerialConfig config; - config.port = port; + config.port = static_cast(DEFAULT_CONSOLE_PORT); config.baud = DEFAULT_SERIAL_BAUD; - config.receivePin = _console_serial_rxpin; - config.transmitPin = _console_serial_txpin; + config.receivePin = DEFAULT_CONSOLE_PORT_RXPIN; + config.transmitPin = DEFAULT_CONSOLE_PORT_TXPIN; config.inverse_logic = false; config.rxBuffSize = 256; - config.txBuffSize = buffSize; +# if USES_HWCDC - _mainSerial._serial = new (std::nothrow) ESPeasySerial(config); + if (config.port == ESPEasySerialPort::usb_hw_cdc) { + config.txBuffSize = 2048; + } else { + config.txBuffSize = 512; + } +# else // if USES_HWCDC + config.txBuffSize = 512; +# endif // if USES_HWCDC - if (Settings.console_serial0_fallback && (port != ESPEasySerialPort::serial0)) { + if (config.port != ESPEasySerialPort::serial0) { + // We only use the _mainSerial for ports other than HW Serial0 + _mainSerial.updateSerialPort(config); + } + + if (Settings.console_serial0_fallback || + (config.port == ESPEasySerialPort::serial0)) + { config.port = ESPEasySerialPort::serial0; config.receivePin = SOC_RX0; config.transmitPin = SOC_TX0; - - _fallbackSerial._serial = new (std::nothrow) ESPeasySerial(config); + config.txBuffSize = 512; + _fallbackSerial.updateSerialPort(config); } } void EspEasy_Console_t::reInit() { - updateActiveTaskUseSerial0(); - bool somethingChanged = false; - const ESPEasySerialPort port = static_cast(Settings.console_serial_port); - - const bool consoleUseSerial0 = port == ESPEasySerialPort::serial0; - - const bool canUseSerial0 = !activeTaskUseSerial0() && !log_to_serial_disabled; - - - bool mustHaveSerial = Settings.UseSerial && (!consoleUseSerial0 || canUseSerial0); - bool mustHaveFallback = false; - - if (Settings.UseSerial) { - if (consoleUseSerial0 && canUseSerial0 && (_fallbackSerial._serial != nullptr)) { - // Should not destruct an already running fallback serial port - mustHaveFallback = true; - mustHaveSerial = false; - } else { - if (Settings.console_serial0_fallback && !consoleUseSerial0 && canUseSerial0) - { - mustHaveFallback = true; - } - } - } - - if (!mustHaveFallback) { - if (_fallbackSerial._serial != nullptr) { - delete _fallbackSerial._serial; - _fallbackSerial._serial = nullptr; - somethingChanged = true; - } - } - - if ((_console_serial_port != Settings.console_serial_port) || - (_console_serial_rxpin != Settings.console_serial_rxpin) || - (_console_serial_txpin != Settings.console_serial_txpin) || - !mustHaveSerial) { - if (_mainSerial._serial != nullptr) { - delete _mainSerial._serial; - _mainSerial._serial = nullptr; - somethingChanged = true; - } - - _console_serial_port = Settings.console_serial_port; - _console_serial_rxpin = Settings.console_serial_rxpin; - _console_serial_txpin = Settings.console_serial_txpin; - } - - if ((_mainSerial._serial == nullptr) && mustHaveSerial) { - unsigned int buffsize = 128; - - const ESPEasySerialPort mainSerialPort = static_cast(_console_serial_port); + ESPEasySerialConfig config; + config.port = static_cast(Settings.console_serial_port); + config.baud = Settings.BaudRate; + config.receivePin = Settings.console_serial_rxpin; + config.transmitPin = Settings.console_serial_txpin; + config.inverse_logic = false; + config.rxBuffSize = 256; # if USES_HWCDC - if (mainSerialPort == ESPEasySerialPort::usb_hw_cdc) { - buffsize = 2048; - } + if (config.port == ESPEasySerialPort::usb_hw_cdc) { + config.txBuffSize = 2048; + } else { + config.txBuffSize = 512; + } +# else // if USES_HWCDC + config.txBuffSize = 512; # endif // if USES_HWCDC - _mainSerial._serial = new (std::nothrow) ESPeasySerial( - mainSerialPort, - _console_serial_rxpin, - _console_serial_txpin, - false, - buffsize); - somethingChanged = true; + + if (config.port != ESPEasySerialPort::serial0) { + // We only use the _mainSerial for ports other than HW Serial0 + _mainSerial.updateSerialPort(config); } - if ((_fallbackSerial._serial == nullptr) && mustHaveFallback) { - _fallbackSerial._serial = new (std::nothrow) ESPeasySerial( - ESPEasySerialPort::serial0, - SOC_RX0, - SOC_TX0); - somethingChanged = true; - } - - if (_fallbackSerial._serial == nullptr) { - _fallbackSerial._serialWriteBuffer.clear(); - } - - if (_mainSerial._serial == nullptr) { - _mainSerial._serialWriteBuffer.clear(); - } - - if (somethingChanged) { - begin(Settings.BaudRate); + if (Settings.console_serial0_fallback || + (config.port == ESPEasySerialPort::serial0)) + { + config.port = ESPEasySerialPort::serial0; + config.receivePin = SOC_RX0; + config.transmitPin = SOC_TX0; + config.txBuffSize = 512; + _fallbackSerial.updateSerialPort(config); } } void EspEasy_Console_t::begin(uint32_t baudrate) { - updateActiveTaskUseSerial0(); - _baudrate = baudrate; - - if (_mainSerial._serial != nullptr) { - _mainSerial._serial->begin(baudrate); - addLog(LOG_LEVEL_INFO, F("ESPEasy console using ESPEasySerial")); - } + _mainSerial.begin(baudrate); + _fallbackSerial.begin(baudrate); if (_fallbackSerial._serial != nullptr) { - _fallbackSerial._serial->begin(baudrate); // Need to have this string as C-string, not F-string perimanSetPinBusExtraType(SOC_RX0, "Console"); @@ -206,22 +150,13 @@ void EspEasy_Console_t::loop() START_TIMER; - const bool consoleUsesSerial0 = - (static_cast(_console_serial_port) == ESPEasySerialPort::serial0 - ); + _mainSerial.readInput(); - if (handledByPluginSerialIn()) - { + if (!handledByPluginSerialIn()) { // Any serial0 data is already dealt with - if (!consoleUsesSerial0 && (_mainSerial._serial != nullptr)) { - readInput(_mainSerial); - } - return; + _fallbackSerial.readInput(); } - readInput(_mainSerial); - readInput(_fallbackSerial); - STOP_TIMER(CONSOLE_LOOP); } @@ -245,10 +180,12 @@ bool EspEasy_Console_t::process_serialWriteBuffer() { void EspEasy_Console_t::setDebugOutput(bool enable) { - auto port = getPort(); + if (_mainSerial._serial != nullptr) { + _mainSerial._serial->setDebugOutput(enable); + } - if (port != nullptr) { - port->setDebugOutput(enable); + if (_fallbackSerial._serial != nullptr) { + _fallbackSerial._serial->setDebugOutput(enable); } } @@ -272,36 +209,9 @@ bool EspEasy_Console_t::handledByPluginSerialIn() return PluginCall(PLUGIN_SERIAL_IN, 0, dummy); } - if ((_mainSerial._serial != nullptr) && _mainSerial._serial->available() && - (static_cast(_console_serial_port) == ESPEasySerialPort::serial0 - )) - { - String dummy; - - return PluginCall(PLUGIN_SERIAL_IN, 0, dummy); - } return false; } -void EspEasy_Console_t::readInput(EspEasy_Console_Port& port) -{ - size_t bytesToRead = port.available(); - - while (bytesToRead > 0) - { - --bytesToRead; - delay(0); - const int SerialInByte = port.read(); - - if (SerialInByte >= 0) { - if (port.process_consoleInput(SerialInByte)) { - // Processed a full line - return; - } - } - } -} - ESPeasySerial * EspEasy_Console_t::getPort() { if (_mainSerial._serial != nullptr) { diff --git a/src/src/ESPEasyCore/ESPEasy_Console_serial0_only.cpp b/src/src/ESPEasyCore/ESPEasy_Console_serial0_only.cpp index 4e669b3f8..63cda8fb4 100644 --- a/src/src/ESPEasyCore/ESPEasy_Console_serial0_only.cpp +++ b/src/src/ESPEasyCore/ESPEasy_Console_serial0_only.cpp @@ -30,6 +30,9 @@ EspEasy_Console_t::EspEasy_Console_t() : void EspEasy_Console_t::reInit() { updateActiveTaskUseSerial0(); + + // TODO TD-er: Must check whether a task uses serial 0 and then stop serial console + //activeTaskUseSerial0() bool somethingChanged = false; if (somethingChanged) { @@ -39,27 +42,7 @@ void EspEasy_Console_t::reInit() void EspEasy_Console_t::begin(uint32_t baudrate) { - updateActiveTaskUseSerial0(); - _baudrate = baudrate; - - if (_mainSerial._serial != nullptr) { -# ifdef ESP8266 - _mainSerial._serial->begin(baudrate); -# ifndef BUILD_NO_DEBUG - addLog(LOG_LEVEL_INFO, F("ESPEasy console using HW Serial")); -# endif -# endif // ifdef ESP8266 -# ifdef ESP32 - - // Allow to flush data from the serial buffers - // When not opening the USB serial port, the ESP may hang at boot. - delay(10); - _mainSerial._serial->end(); - delay(10); - _mainSerial._serial->begin(baudrate); - _mainSerial._serial->flush(); -# endif // ifdef ESP32 - } + _mainSerial.begin(baudrate); } void EspEasy_Console_t::init() { @@ -89,7 +72,7 @@ void EspEasy_Console_t::loop() return; } - readInput(_mainSerial); + _mainSerial.readInput(); STOP_TIMER(CONSOLE_LOOP); } @@ -130,25 +113,6 @@ bool EspEasy_Console_t::handledByPluginSerialIn() return PluginCall(PLUGIN_SERIAL_IN, 0, dummy); } -void EspEasy_Console_t::readInput(EspEasy_Console_Port& port) -{ - size_t bytesToRead = port.available(); - - while (bytesToRead > 0) - { - --bytesToRead; - delay(0); - const int SerialInByte = port.read(); - - if (SerialInByte >= 0) { - if (port.process_consoleInput(SerialInByte)) { - // Processed a full line - return; - } - } - } -} - HardwareSerial * EspEasy_Console_t::getPort() { if (_mainSerial._serial != nullptr) { diff --git a/src/src/ESPEasyCore/ESPEasy_Console_single_serial.cpp b/src/src/ESPEasyCore/ESPEasy_Console_single_serial.cpp index aefce3543..6b88fb81c 100644 --- a/src/src/ESPEasyCore/ESPEasy_Console_single_serial.cpp +++ b/src/src/ESPEasyCore/ESPEasy_Console_single_serial.cpp @@ -27,9 +27,7 @@ EspEasy_Console_t::EspEasy_Console_t() : _mainSerial(LOG_TO_SERIAL) { - const ESPEasySerialPort port = static_cast(_console_serial_port); - -# if USES_USBCDC +# if USES_USBCDC /* if (port == ESPEasySerialPort::usb_cdc_0 || @@ -39,12 +37,12 @@ EspEasy_Console_t::EspEasy_Console_t() : USB.productName() } */ -# endif // if USES_USBCDC +# endif // if USES_USBCDC -# ifdef ESP8266 +# ifdef ESP8266 constexpr size_t buffSize = 256; -# endif // ifdef ESP8266 -# ifdef ESP32 +# endif // ifdef ESP8266 +# ifdef ESP32 // Ideal buffer size is a trade-off between bootspeed // and not missing data when the ESP is busy processing stuff. @@ -52,104 +50,48 @@ EspEasy_Console_t::EspEasy_Console_t() : // it may just take less time in the background tasks to dump // any logs as larger chunks can be transferred at once. constexpr size_t buffSize = 512; -# endif // ifdef ESP32 +# endif // ifdef ESP32 ESPEasySerialConfig config; - config.port = port; + config.port = static_cast(DEFAULT_CONSOLE_PORT); config.baud = DEFAULT_SERIAL_BAUD; - config.receivePin = _console_serial_rxpin; - config.transmitPin = _console_serial_txpin; + config.receivePin = DEFAULT_CONSOLE_PORT_RXPIN; + config.transmitPin = DEFAULT_CONSOLE_PORT_TXPIN; config.inverse_logic = false; config.rxBuffSize = 256; config.txBuffSize = buffSize; - { - # ifdef USE_SECOND_HEAP - HeapSelectDram ephemeral; - # endif // ifdef USE_SECOND_HEAP - - _mainSerial._serial = new (std::nothrow) ESPeasySerial(config); - } + _mainSerial.updateSerialPort(config); } void EspEasy_Console_t::reInit() { - updateActiveTaskUseSerial0(); - bool somethingChanged = false; - const ESPEasySerialPort port = static_cast(Settings.console_serial_port); +# ifdef ESP8266 + constexpr size_t buffSize = 256; +# endif // ifdef ESP8266 +# ifdef ESP32 - const bool consoleUseSerial0 = ( -# ifdef ESP8266 - (port == ESPEasySerialPort::serial0_swap) || -# endif // ifdef ESP8266 - port == ESPEasySerialPort::serial0); + // Ideal buffer size is a trade-off between bootspeed + // and not missing data when the ESP is busy processing stuff. + // Since we do have a separate buffer in the console, + // it may just take less time in the background tasks to dump + // any logs as larger chunks can be transferred at once. + constexpr size_t buffSize = 512; +# endif // ifdef ESP32 - const bool canUseSerial0 = !activeTaskUseSerial0() && !log_to_serial_disabled; + ESPEasySerialConfig config; + config.port = static_cast(Settings.console_serial_port); + config.baud = Settings.BaudRate; + config.receivePin = Settings.console_serial_rxpin; + config.transmitPin = Settings.console_serial_txpin; + config.inverse_logic = false; + config.rxBuffSize = 256; + config.txBuffSize = buffSize; - - bool mustHaveSerial = Settings.UseSerial && (!consoleUseSerial0 || canUseSerial0); - - - if ((_console_serial_port != Settings.console_serial_port) || - (_console_serial_rxpin != Settings.console_serial_rxpin) || - (_console_serial_txpin != Settings.console_serial_txpin) || - !mustHaveSerial) { - if (_mainSerial._serial != nullptr) { - delete _mainSerial._serial; - _mainSerial._serial = nullptr; - somethingChanged = true; - } - - _console_serial_port = Settings.console_serial_port; - _console_serial_rxpin = Settings.console_serial_rxpin; - _console_serial_txpin = Settings.console_serial_txpin; - } - - if ((_mainSerial._serial == nullptr) && mustHaveSerial) { - # ifdef USE_SECOND_HEAP - HeapSelectDram ephemeral; - # endif // ifdef USE_SECOND_HEAP - - unsigned int buffsize = 128; - - const ESPEasySerialPort mainSerialPort = static_cast(_console_serial_port); - -# if USES_HWCDC - - if (mainSerialPort == ESPEasySerialPort::usb_hw_cdc) { - buffsize = 2048; - } -# endif // if USES_HWCDC - - _mainSerial._serial = new (std::nothrow) ESPeasySerial( - mainSerialPort, - _console_serial_rxpin, - _console_serial_txpin, - false, - buffsize); - somethingChanged = true; - } - - - if (_mainSerial._serial == nullptr) { - _mainSerial._serialWriteBuffer.clear(); - } - - if (somethingChanged) { - begin(Settings.BaudRate); - } + _mainSerial.updateSerialPort(config); } -void EspEasy_Console_t::begin(uint32_t baudrate) -{ - updateActiveTaskUseSerial0(); - _baudrate = baudrate; - - if (_mainSerial._serial != nullptr) { - _mainSerial._serial->begin(baudrate); - addLog(LOG_LEVEL_INFO, F("ESPEasy console using ESPEasySerial")); - } -} +void EspEasy_Console_t::begin(uint32_t baudrate) { _mainSerial.begin(baudrate); } void EspEasy_Console_t::init() { # if FEATURE_IMPROV @@ -171,22 +113,22 @@ void EspEasy_Console_t::loop() START_TIMER; const bool consoleUsesSerial0 = - (static_cast(_console_serial_port) == ESPEasySerialPort::serial0 -# ifdef ESP8266 - || static_cast(_console_serial_port) == ESPEasySerialPort::serial0_swap -# endif // ifdef ESP8266 + (_mainSerial.getPortType() == ESPEasySerialPort::serial0 +# ifdef ESP8266 + || _mainSerial.getPortType() == ESPEasySerialPort::serial0_swap +# endif // ifdef ESP8266 ); if (handledByPluginSerialIn()) { // Any serial0 data is already dealt with if (!consoleUsesSerial0 && (_mainSerial._serial != nullptr)) { - readInput(_mainSerial); + _mainSerial.readInput(); } return; } - readInput(_mainSerial); + _mainSerial.readInput(); STOP_TIMER(CONSOLE_LOOP); } @@ -220,14 +162,13 @@ String EspEasy_Console_t::getPortDescription() const return _mainSerial.getPortDescription(); } - bool EspEasy_Console_t::handledByPluginSerialIn() { if ((_mainSerial._serial != nullptr) && _mainSerial._serial->available() && - (static_cast(_console_serial_port) == ESPEasySerialPort::serial0 -# ifdef ESP8266 - || static_cast(_console_serial_port) == ESPEasySerialPort::serial0_swap -# endif // ifdef ESP8266 + (_mainSerial.getPortType() == ESPEasySerialPort::serial0 +# ifdef ESP8266 + || _mainSerial.getPortType() == ESPEasySerialPort::serial0_swap +# endif // ifdef ESP8266 )) { String dummy; @@ -237,26 +178,6 @@ bool EspEasy_Console_t::handledByPluginSerialIn() return false; } -void EspEasy_Console_t::readInput(EspEasy_Console_Port& port) -{ - size_t bytesToRead = port.available(); - - while (bytesToRead > 0) - { - --bytesToRead; - delay(0); - const int SerialInByte = port.read(); - - if (SerialInByte >= 0) { - if (port.process_consoleInput(SerialInByte)) { - // Processed a full line - return; - } - } - } -} - - ESPeasySerial * EspEasy_Console_t::getPort() { if (_mainSerial._serial != nullptr) { diff --git a/src/src/Helpers/Improv_Helper.cpp b/src/src/Helpers/Improv_Helper.cpp index 477f8e34c..d487c9ea1 100644 --- a/src/src/Helpers/Improv_Helper.cpp +++ b/src/src/Helpers/Improv_Helper.cpp @@ -71,6 +71,11 @@ void Improv_Helper_t::init() // FIXME TD-er: Implement callback to use ESPEasy functions to connect to WiFi // _improv.setCustomTryConnectToWiFi(OnImprovESPEasyConnectWiFi); + update(); +} + +void Improv_Helper_t::update() +{ String firmwareName = get_binary_filename(); const String buildString = getSystemBuildString(); diff --git a/src/src/Helpers/Improv_Helper.h b/src/src/Helpers/Improv_Helper.h index fc9265458..c2a3fe080 100644 --- a/src/src/Helpers/Improv_Helper.h +++ b/src/src/Helpers/Improv_Helper.h @@ -17,6 +17,8 @@ public: void init(); + void update(); + bool handle(uint8_t b, Stream *serialForWrite); bool getFromBuffer(uint8_t& b);