[ESPEasy Console] Simplify init of ESPEasy console + update IDF/Arduino

This commit is contained in:
TD-er
2026-07-18 16:09:50 +02:00
parent 943b95a955
commit 72171b9500
13 changed files with 303 additions and 350 deletions
+18
View File
@@ -2,6 +2,24 @@
#include "ESPEasySerialType.h" #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() void ESPEasySerialConfig::validate()
{ {
port = ESPeasySerialType::getSerialType(port, receivePin, transmitPin); port = ESPeasySerialType::getSerialType(port, receivePin, transmitPin);
+4
View File
@@ -24,6 +24,10 @@
struct ESPEasySerialConfig { struct ESPEasySerialConfig {
ESPEasySerialConfig() = default; ESPEasySerialConfig() = default;
bool operator==(const ESPEasySerialConfig& other) const;
ESPEasySerialConfig& operator=(const ESPEasySerialConfig& other) = default;
void validate(); void validate();
#ifdef ESP8266 #ifdef ESP8266
+2 -1
View File
@@ -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_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 = 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 ;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_remove_include = true
custom_component_remove = custom_component_remove =
+1 -1
View File
@@ -88,6 +88,7 @@ boolean Plugin_040(uint8_t function, struct EventStruct *event, String& string)
{ {
if (Plugin_040_init) if (Plugin_040_init)
{ {
success = true;
uint8_t val = 0; uint8_t val = 0;
uint8_t code[6]; uint8_t code[6];
uint8_t checksum = 0; 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); Scheduler.setPluginTaskTimer(500, event->TaskIndex, event->Par1);
} }
} }
success = true;
} }
break; break;
} }
+1 -1
View File
@@ -319,6 +319,7 @@ boolean Plugin_091(uint8_t function, struct EventStruct *event, String& string)
if (Plugin_091_init) if (Plugin_091_init)
{ {
success = true;
while (ESPEASY_SERIAL_0.available() > 0) { while (ESPEASY_SERIAL_0.available() > 0) {
yield(); yield();
@@ -533,7 +534,6 @@ boolean Plugin_091(uint8_t function, struct EventStruct *event, String& string)
} }
} }
} // plugin initialized end } // plugin initialized end
success = true;
break; break;
} }
+1 -8
View File
@@ -37,8 +37,6 @@ private:
bool handledByPluginSerialIn(); bool handledByPluginSerialIn();
void readInput(EspEasy_Console_Port& port);
#if FEATURE_DEFINE_SERIAL_CONSOLE_PORT #if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
ESPeasySerial * getPort(); ESPeasySerial * getPort();
#else // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT #else // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
@@ -47,18 +45,13 @@ private:
void endPort(); void endPort();
uint32_t _baudrate = 115200u;
EspEasy_Console_Port _mainSerial; EspEasy_Console_Port _mainSerial;
#if FEATURE_DEFINE_SERIAL_CONSOLE_PORT #if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
# if USES_ESPEASY_CONSOLE_FALLBACK_PORT # if USES_ESPEASY_CONSOLE_FALLBACK_PORT
// Serial port to be always used as HW Serial0
EspEasy_Console_Port _fallbackSerial; EspEasy_Console_Port _fallbackSerial;
# endif # 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 #endif
}; };
+146 -27
View File
@@ -105,6 +105,35 @@ size_t EspEasy_Console_Port::available() const
return res; 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() void EspEasy_Console_Port::endPort()
{ {
if (_serial != nullptr) { 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) { if (_serial == nullptr) return;
#ifdef ESP32 size_t bytesToRead = available();
if (!xPortCanYield()) { return false; } while (bytesToRead > 0)
#endif // ifdef ESP32 {
size_t availableForWrite = _serial->availableForWrite(); --bytesToRead;
delay(0);
const int SerialInByte = read();
if (availableForWrite == 0) { return false; } if (SerialInByte >= 0) {
if (process_consoleInput(SerialInByte)) {
if (availableForWrite == 1) { // Processed a full line
// For only a single byte, just write it directly return;
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;
} }
} }
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) bool EspEasy_Console_Port::process_consoleInput(uint8_t SerialInByte)
@@ -199,3 +260,61 @@ String EspEasy_Console_Port::getPortDescription() const
return F("-"); 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
@@ -27,14 +27,30 @@ struct EspEasy_Console_Port {
int read(); int read();
size_t available() const; size_t available() const;
void begin(uint32_t baudrate);
void endPort(); void endPort();
void readInput();
ESPEasySerialPort getPortType() const;
bool process_serialWriteBuffer(); bool process_serialWriteBuffer();
bool process_consoleInput(uint8_t SerialInByte); bool process_consoleInput(uint8_t SerialInByte);
String getPortDescription() const; 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{}; int SerialInByteCounter{};
char *InputBuffer_Serial{}; char *InputBuffer_Serial{};
SerialWriteBuffer_t _serialWriteBuffer; SerialWriteBuffer_t _serialWriteBuffer;
@@ -31,152 +31,96 @@ EspEasy_Console_t::EspEasy_Console_t() :
_mainSerial(LOG_TO_SERIAL) _mainSerial(LOG_TO_SERIAL)
, _fallbackSerial(LOG_TO_SERIAL_EXTRA) , _fallbackSerial(LOG_TO_SERIAL_EXTRA)
{ {
const ESPEasySerialPort port = static_cast<ESPEasySerialPort>(_console_serial_port);
# if USES_USBCDC # if USES_USBCDC
/* /*
if (port == ESPEasySerialPort::usb_cdc_0 || if (port == ESPEasySerialPort::usb_cdc_0 ||
port == ESPEasySerialPort::usb_cdc_1) port == ESPEasySerialPort::usb_cdc_1)
{ {
USB.manufacturerName(F("ESPEasy")); USB.manufacturerName(F("ESPEasy"));
USB.productName() USB.productName()
} }
*/ */
# endif // if USES_USBCDC # 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; ESPEasySerialConfig config;
config.port = port; config.port = static_cast<ESPEasySerialPort>(DEFAULT_CONSOLE_PORT);
config.baud = DEFAULT_SERIAL_BAUD; config.baud = DEFAULT_SERIAL_BAUD;
config.receivePin = _console_serial_rxpin; config.receivePin = DEFAULT_CONSOLE_PORT_RXPIN;
config.transmitPin = _console_serial_txpin; config.transmitPin = DEFAULT_CONSOLE_PORT_TXPIN;
config.inverse_logic = false; config.inverse_logic = false;
config.rxBuffSize = 256; 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.port = ESPEasySerialPort::serial0;
config.receivePin = SOC_RX0; config.receivePin = SOC_RX0;
config.transmitPin = SOC_TX0; config.transmitPin = SOC_TX0;
config.txBuffSize = 512;
_fallbackSerial._serial = new (std::nothrow) ESPeasySerial(config); _fallbackSerial.updateSerialPort(config);
} }
} }
void EspEasy_Console_t::reInit() void EspEasy_Console_t::reInit()
{ {
updateActiveTaskUseSerial0(); ESPEasySerialConfig config;
bool somethingChanged = false;
const ESPEasySerialPort port = static_cast<ESPEasySerialPort>(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<ESPEasySerialPort>(_console_serial_port);
config.port = static_cast<ESPEasySerialPort>(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 USES_HWCDC
if (mainSerialPort == ESPEasySerialPort::usb_hw_cdc) { if (config.port == ESPEasySerialPort::usb_hw_cdc) {
buffsize = 2048; config.txBuffSize = 2048;
} } else {
config.txBuffSize = 512;
}
# else // if USES_HWCDC
config.txBuffSize = 512;
# endif // if USES_HWCDC # endif // if USES_HWCDC
_mainSerial._serial = new (std::nothrow) ESPeasySerial(
mainSerialPort, if (config.port != ESPEasySerialPort::serial0) {
_console_serial_rxpin, // We only use the _mainSerial for ports other than HW Serial0
_console_serial_txpin, _mainSerial.updateSerialPort(config);
false,
buffsize);
somethingChanged = true;
} }
if ((_fallbackSerial._serial == nullptr) && mustHaveFallback) { if (Settings.console_serial0_fallback ||
_fallbackSerial._serial = new (std::nothrow) ESPeasySerial( (config.port == ESPEasySerialPort::serial0))
ESPEasySerialPort::serial0, {
SOC_RX0, config.port = ESPEasySerialPort::serial0;
SOC_TX0); config.receivePin = SOC_RX0;
somethingChanged = true; config.transmitPin = SOC_TX0;
} config.txBuffSize = 512;
_fallbackSerial.updateSerialPort(config);
if (_fallbackSerial._serial == nullptr) {
_fallbackSerial._serialWriteBuffer.clear();
}
if (_mainSerial._serial == nullptr) {
_mainSerial._serialWriteBuffer.clear();
}
if (somethingChanged) {
begin(Settings.BaudRate);
} }
} }
void EspEasy_Console_t::begin(uint32_t baudrate) void EspEasy_Console_t::begin(uint32_t baudrate)
{ {
updateActiveTaskUseSerial0(); _mainSerial.begin(baudrate);
_baudrate = baudrate; _fallbackSerial.begin(baudrate);
if (_mainSerial._serial != nullptr) {
_mainSerial._serial->begin(baudrate);
addLog(LOG_LEVEL_INFO, F("ESPEasy console using ESPEasySerial"));
}
if (_fallbackSerial._serial != nullptr) { if (_fallbackSerial._serial != nullptr) {
_fallbackSerial._serial->begin(baudrate);
// Need to have this string as C-string, not F-string // Need to have this string as C-string, not F-string
perimanSetPinBusExtraType(SOC_RX0, "Console"); perimanSetPinBusExtraType(SOC_RX0, "Console");
@@ -206,22 +150,13 @@ void EspEasy_Console_t::loop()
START_TIMER; START_TIMER;
const bool consoleUsesSerial0 = _mainSerial.readInput();
(static_cast<ESPEasySerialPort>(_console_serial_port) == ESPEasySerialPort::serial0
);
if (handledByPluginSerialIn()) if (!handledByPluginSerialIn()) {
{
// Any serial0 data is already dealt with // Any serial0 data is already dealt with
if (!consoleUsesSerial0 && (_mainSerial._serial != nullptr)) { _fallbackSerial.readInput();
readInput(_mainSerial);
}
return;
} }
readInput(_mainSerial);
readInput(_fallbackSerial);
STOP_TIMER(CONSOLE_LOOP); STOP_TIMER(CONSOLE_LOOP);
} }
@@ -245,10 +180,12 @@ bool EspEasy_Console_t::process_serialWriteBuffer() {
void EspEasy_Console_t::setDebugOutput(bool enable) void EspEasy_Console_t::setDebugOutput(bool enable)
{ {
auto port = getPort(); if (_mainSerial._serial != nullptr) {
_mainSerial._serial->setDebugOutput(enable);
}
if (port != nullptr) { if (_fallbackSerial._serial != nullptr) {
port->setDebugOutput(enable); _fallbackSerial._serial->setDebugOutput(enable);
} }
} }
@@ -272,36 +209,9 @@ bool EspEasy_Console_t::handledByPluginSerialIn()
return PluginCall(PLUGIN_SERIAL_IN, 0, dummy); return PluginCall(PLUGIN_SERIAL_IN, 0, dummy);
} }
if ((_mainSerial._serial != nullptr) && _mainSerial._serial->available() &&
(static_cast<ESPEasySerialPort>(_console_serial_port) == ESPEasySerialPort::serial0
))
{
String dummy;
return PluginCall(PLUGIN_SERIAL_IN, 0, dummy);
}
return false; 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() ESPeasySerial * EspEasy_Console_t::getPort()
{ {
if (_mainSerial._serial != nullptr) { if (_mainSerial._serial != nullptr) {
@@ -30,6 +30,9 @@ EspEasy_Console_t::EspEasy_Console_t() :
void EspEasy_Console_t::reInit() void EspEasy_Console_t::reInit()
{ {
updateActiveTaskUseSerial0(); updateActiveTaskUseSerial0();
// TODO TD-er: Must check whether a task uses serial 0 and then stop serial console
//activeTaskUseSerial0()
bool somethingChanged = false; bool somethingChanged = false;
if (somethingChanged) { if (somethingChanged) {
@@ -39,27 +42,7 @@ void EspEasy_Console_t::reInit()
void EspEasy_Console_t::begin(uint32_t baudrate) void EspEasy_Console_t::begin(uint32_t baudrate)
{ {
updateActiveTaskUseSerial0(); _mainSerial.begin(baudrate);
_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
}
} }
void EspEasy_Console_t::init() { void EspEasy_Console_t::init() {
@@ -89,7 +72,7 @@ void EspEasy_Console_t::loop()
return; return;
} }
readInput(_mainSerial); _mainSerial.readInput();
STOP_TIMER(CONSOLE_LOOP); STOP_TIMER(CONSOLE_LOOP);
} }
@@ -130,25 +113,6 @@ bool EspEasy_Console_t::handledByPluginSerialIn()
return PluginCall(PLUGIN_SERIAL_IN, 0, dummy); 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() HardwareSerial * EspEasy_Console_t::getPort()
{ {
if (_mainSerial._serial != nullptr) { if (_mainSerial._serial != nullptr) {
@@ -27,9 +27,7 @@
EspEasy_Console_t::EspEasy_Console_t() : EspEasy_Console_t::EspEasy_Console_t() :
_mainSerial(LOG_TO_SERIAL) _mainSerial(LOG_TO_SERIAL)
{ {
const ESPEasySerialPort port = static_cast<ESPEasySerialPort>(_console_serial_port); # if USES_USBCDC
# if USES_USBCDC
/* /*
if (port == ESPEasySerialPort::usb_cdc_0 || if (port == ESPEasySerialPort::usb_cdc_0 ||
@@ -39,12 +37,12 @@ EspEasy_Console_t::EspEasy_Console_t() :
USB.productName() USB.productName()
} }
*/ */
# endif // if USES_USBCDC # endif // if USES_USBCDC
# ifdef ESP8266 # ifdef ESP8266
constexpr size_t buffSize = 256; constexpr size_t buffSize = 256;
# endif // ifdef ESP8266 # endif // ifdef ESP8266
# ifdef ESP32 # ifdef ESP32
// Ideal buffer size is a trade-off between bootspeed // Ideal buffer size is a trade-off between bootspeed
// and not missing data when the ESP is busy processing stuff. // 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 // it may just take less time in the background tasks to dump
// any logs as larger chunks can be transferred at once. // any logs as larger chunks can be transferred at once.
constexpr size_t buffSize = 512; constexpr size_t buffSize = 512;
# endif // ifdef ESP32 # endif // ifdef ESP32
ESPEasySerialConfig config; ESPEasySerialConfig config;
config.port = port; config.port = static_cast<ESPEasySerialPort>(DEFAULT_CONSOLE_PORT);
config.baud = DEFAULT_SERIAL_BAUD; config.baud = DEFAULT_SERIAL_BAUD;
config.receivePin = _console_serial_rxpin; config.receivePin = DEFAULT_CONSOLE_PORT_RXPIN;
config.transmitPin = _console_serial_txpin; config.transmitPin = DEFAULT_CONSOLE_PORT_TXPIN;
config.inverse_logic = false; config.inverse_logic = false;
config.rxBuffSize = 256; config.rxBuffSize = 256;
config.txBuffSize = buffSize; config.txBuffSize = buffSize;
{ _mainSerial.updateSerialPort(config);
# ifdef USE_SECOND_HEAP
HeapSelectDram ephemeral;
# endif // ifdef USE_SECOND_HEAP
_mainSerial._serial = new (std::nothrow) ESPeasySerial(config);
}
} }
void EspEasy_Console_t::reInit() void EspEasy_Console_t::reInit()
{ {
updateActiveTaskUseSerial0(); # ifdef ESP8266
bool somethingChanged = false; constexpr size_t buffSize = 256;
const ESPEasySerialPort port = static_cast<ESPEasySerialPort>(Settings.console_serial_port); # endif // ifdef ESP8266
# ifdef ESP32
const bool consoleUseSerial0 = ( // Ideal buffer size is a trade-off between bootspeed
# ifdef ESP8266 // and not missing data when the ESP is busy processing stuff.
(port == ESPEasySerialPort::serial0_swap) || // Since we do have a separate buffer in the console,
# endif // ifdef ESP8266 // it may just take less time in the background tasks to dump
port == ESPEasySerialPort::serial0); // 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<ESPEasySerialPort>(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;
_mainSerial.updateSerialPort(config);
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<ESPEasySerialPort>(_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);
}
} }
void EspEasy_Console_t::begin(uint32_t baudrate) void EspEasy_Console_t::begin(uint32_t baudrate) { _mainSerial.begin(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::init() { void EspEasy_Console_t::init() {
# if FEATURE_IMPROV # if FEATURE_IMPROV
@@ -171,22 +113,22 @@ void EspEasy_Console_t::loop()
START_TIMER; START_TIMER;
const bool consoleUsesSerial0 = const bool consoleUsesSerial0 =
(static_cast<ESPEasySerialPort>(_console_serial_port) == ESPEasySerialPort::serial0 (_mainSerial.getPortType() == ESPEasySerialPort::serial0
# ifdef ESP8266 # ifdef ESP8266
|| static_cast<ESPEasySerialPort>(_console_serial_port) == ESPEasySerialPort::serial0_swap || _mainSerial.getPortType() == ESPEasySerialPort::serial0_swap
# endif // ifdef ESP8266 # endif // ifdef ESP8266
); );
if (handledByPluginSerialIn()) if (handledByPluginSerialIn())
{ {
// Any serial0 data is already dealt with // Any serial0 data is already dealt with
if (!consoleUsesSerial0 && (_mainSerial._serial != nullptr)) { if (!consoleUsesSerial0 && (_mainSerial._serial != nullptr)) {
readInput(_mainSerial); _mainSerial.readInput();
} }
return; return;
} }
readInput(_mainSerial); _mainSerial.readInput();
STOP_TIMER(CONSOLE_LOOP); STOP_TIMER(CONSOLE_LOOP);
} }
@@ -220,14 +162,13 @@ String EspEasy_Console_t::getPortDescription() const
return _mainSerial.getPortDescription(); return _mainSerial.getPortDescription();
} }
bool EspEasy_Console_t::handledByPluginSerialIn() bool EspEasy_Console_t::handledByPluginSerialIn()
{ {
if ((_mainSerial._serial != nullptr) && _mainSerial._serial->available() && if ((_mainSerial._serial != nullptr) && _mainSerial._serial->available() &&
(static_cast<ESPEasySerialPort>(_console_serial_port) == ESPEasySerialPort::serial0 (_mainSerial.getPortType() == ESPEasySerialPort::serial0
# ifdef ESP8266 # ifdef ESP8266
|| static_cast<ESPEasySerialPort>(_console_serial_port) == ESPEasySerialPort::serial0_swap || _mainSerial.getPortType() == ESPEasySerialPort::serial0_swap
# endif // ifdef ESP8266 # endif // ifdef ESP8266
)) ))
{ {
String dummy; String dummy;
@@ -237,26 +178,6 @@ bool EspEasy_Console_t::handledByPluginSerialIn()
return false; 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() ESPeasySerial * EspEasy_Console_t::getPort()
{ {
if (_mainSerial._serial != nullptr) { if (_mainSerial._serial != nullptr) {
+5
View File
@@ -71,6 +71,11 @@ void Improv_Helper_t::init()
// FIXME TD-er: Implement callback to use ESPEasy functions to connect to WiFi // FIXME TD-er: Implement callback to use ESPEasy functions to connect to WiFi
// _improv.setCustomTryConnectToWiFi(OnImprovESPEasyConnectWiFi); // _improv.setCustomTryConnectToWiFi(OnImprovESPEasyConnectWiFi);
update();
}
void Improv_Helper_t::update()
{
String firmwareName = get_binary_filename(); String firmwareName = get_binary_filename();
const String buildString = getSystemBuildString(); const String buildString = getSystemBuildString();
+2
View File
@@ -17,6 +17,8 @@ public:
void init(); void init();
void update();
bool handle(uint8_t b, Stream *serialForWrite); bool handle(uint8_t b, Stream *serialForWrite);
bool getFromBuffer(uint8_t& b); bool getFromBuffer(uint8_t& b);