[ESPEasy console] reduce load when USB HWCDC is not connected

This commit is contained in:
TD-er
2026-07-19 22:50:51 +02:00
parent 39f43f14f8
commit 50c52c3b95
5 changed files with 54 additions and 29 deletions
@@ -48,6 +48,20 @@ static void hwcdcEventCallback(void *arg, esp_event_base_t event_base, int32_t e
}
}
bool Port_ESPEasySerial_USB_HWCDC_t::isConnected() const
{
static bool connected_cache{};
static uint32_t lastChecked{};
if (!connected_cache) {
const int32_t timePassedSince = (int32_t) (millis() - lastChecked);
if (timePassedSince < 1000) return false;
lastChecked = millis();
}
connected_cache = _hwcdc_serial != nullptr && _hwcdc_serial->isConnected();
return connected_cache;
}
Port_ESPEasySerial_USB_HWCDC_t::Port_ESPEasySerial_USB_HWCDC_t(const ESPEasySerialConfig& config)
# if ARDUINO_USB_CDC_ON_BOOT // Serial used for USB CDC
@@ -71,7 +85,7 @@ Port_ESPEasySerial_USB_HWCDC_t::Port_ESPEasySerial_USB_HWCDC_t(const ESPEasySeri
// _hwcdc_serial->begin();
// _hwcdc_serial->onEvent(hwcdcEventCallback);
// _hwcdc_serial->onEvent(hwcdcEventCallback);
}
}
@@ -106,7 +120,7 @@ void Port_ESPEasySerial_USB_HWCDC_t::end() {
int Port_ESPEasySerial_USB_HWCDC_t::available(void)
{
if (_hwcdc_serial != nullptr && _hwcdc_serial->isConnected()) {
if (isConnected()) {
return _hwcdc_serial->available();
}
return 0;
@@ -114,7 +128,7 @@ int Port_ESPEasySerial_USB_HWCDC_t::available(void)
int Port_ESPEasySerial_USB_HWCDC_t::availableForWrite(void)
{
if (_hwcdc_serial != nullptr && _hwcdc_serial->isConnected()) {
if (isConnected()) {
return _hwcdc_serial->availableForWrite();
}
return 0;
@@ -122,7 +136,7 @@ int Port_ESPEasySerial_USB_HWCDC_t::availableForWrite(void)
int Port_ESPEasySerial_USB_HWCDC_t::peek(void)
{
if (_hwcdc_serial != nullptr && _hwcdc_serial->isConnected()) {
if (isConnected()) {
return _hwcdc_serial->peek();
}
return 0;
@@ -130,7 +144,7 @@ int Port_ESPEasySerial_USB_HWCDC_t::peek(void)
int Port_ESPEasySerial_USB_HWCDC_t::read(void)
{
if (_hwcdc_serial != nullptr && _hwcdc_serial->isConnected()) {
if (isConnected()) {
return _hwcdc_serial->read();
}
return 0;
@@ -139,7 +153,7 @@ int Port_ESPEasySerial_USB_HWCDC_t::read(void)
size_t Port_ESPEasySerial_USB_HWCDC_t::read(uint8_t *buffer,
size_t size)
{
if (_hwcdc_serial != nullptr && _hwcdc_serial->isConnected()) {
if (isConnected()) {
return _hwcdc_serial->read(buffer, size);
}
return 0;
@@ -147,7 +161,7 @@ size_t Port_ESPEasySerial_USB_HWCDC_t::read(uint8_t *buffer,
void Port_ESPEasySerial_USB_HWCDC_t::flush(void)
{
if (_hwcdc_serial != nullptr && _hwcdc_serial->isConnected()) {
if (isConnected()) {
return _hwcdc_serial->flush();
}
}
@@ -176,7 +190,7 @@ size_t Port_ESPEasySerial_USB_HWCDC_t::write(const uint8_t *buffer,
Port_ESPEasySerial_USB_HWCDC_t::operator bool() const
{
if (_hwcdc_serial != nullptr && _hwcdc_serial->isConnected()) {
if (isConnected()) {
// return usbActive;
return true;
}
@@ -212,4 +226,5 @@ bool Port_ESPEasySerial_USB_HWCDC_t::setRS485Mode(int8_t rtsPin, bool enableColl
return false;
}
#endif // if USES_HWCDC
@@ -43,6 +43,9 @@ public:
bool setRS485Mode(int8_t rtsPin, bool enableCollisionDetection = false);
private:
bool isConnected() const;
#if !ARDUINO_USB_CDC_ON_BOOT
HWCDC myUsbSerial;
#endif
+19 -9
View File
@@ -217,6 +217,7 @@ ESPEasySerialPort EspEasy_Console_Port::getPortType() const
bool EspEasy_Console_Port::process_serialWriteBuffer()
{
if (_serialWriteBuffer.getNrMessages() == 0) { return false; }
if (_serial == nullptr) { return false; }
#ifdef ESP32
@@ -234,35 +235,44 @@ bool EspEasy_Console_Port::process_serialWriteBuffer()
PrintToString str;
str.reserve(CONSOLE_MAX_WRITE_CHUNKSIZE);
size_t totalWritten = 0;
do
{
if (availableForWrite == 0) { return false; }
if (availableForWrite == 0) { return totalWritten > 0; }
if (availableForWrite == 1) {
// For only a single byte, just write it directly
return _serialWriteBuffer.process(_serial, availableForWrite);
if (_serialWriteBuffer.process(_serial, availableForWrite)) {
++totalWritten;
}
return totalWritten > 0;
}
size_t chunkSize = availableForWrite;
const size_t chunkSize = availableForWrite > CONSOLE_MAX_WRITE_CHUNKSIZE
? CONSOLE_MAX_WRITE_CHUNKSIZE
: availableForWrite;
if (chunkSize > CONSOLE_MAX_WRITE_CHUNKSIZE) {
chunkSize = CONSOLE_MAX_WRITE_CHUNKSIZE;
}
availableForWrite -= chunkSize;
if (!_serialWriteBuffer.process(&str, chunkSize)) {
// Nothing left to write
return false;
return totalWritten > 0;
}
if (_serial->write(str.get().c_str(), str.length()) < chunkSize) {
const size_t lastWritten = _serial->write(str.get().c_str(), str.length());
totalWritten += lastWritten;
if (lastWritten < chunkSize) {
// Try to end on a full log line to lower the chance of SDK debug logs
// interfering with console output.
++nrLinesWritten;
}
str.clear();
} while (availableForWrite > 0 && nrLinesWritten < 3);
return true;
return totalWritten > 0;
}
bool EspEasy_Console_Port::process_consoleInput(uint8_t SerialInByte)
@@ -74,21 +74,19 @@ void EspEasy_Console_t::loop()
}
bool EspEasy_Console_t::process_serialWriteBuffer() {
# if FEATURE_TIMING_STATS
# if FEATURE_TIMING_STATS
START_TIMER;
bool res = false;
if (_mainSerial.process_serialWriteBuffer()) {
res = true;
STOP_TIMER(CONSOLE_WRITE_SERIAL);
return true;
}
if (res) { STOP_TIMER(CONSOLE_WRITE_SERIAL); }
return res;
return false;
# else // if FEATURE_TIMING_STATS
# else // if FEATURE_TIMING_STATS
return _mainSerial.process_serialWriteBuffer();
# endif // if FEATURE_TIMING_STATS
# endif // if FEATURE_TIMING_STATS
}
void EspEasy_Console_t::setDebugOutput(bool enable)
@@ -101,14 +101,13 @@ void EspEasy_Console_t::loop()
bool EspEasy_Console_t::process_serialWriteBuffer() {
# if FEATURE_TIMING_STATS
START_TIMER;
bool res = false;
if (_mainSerial.process_serialWriteBuffer()) {
res = true;
STOP_TIMER(CONSOLE_WRITE_SERIAL);
return true;
}
if (res) { STOP_TIMER(CONSOLE_WRITE_SERIAL); }
return res;
return false;
# else // if FEATURE_TIMING_STATS
return _mainSerial.process_serialWriteBuffer();