Quellcode durchsuchen

Route GxEPD2's Serial.print/println output to the ESPHome logger

The Arduino.h shim's Serial stub was a complete no-op, silently
discarding everything written to it - including GxEPD2_EPD's
unconditional "Busy Timeout!" warning from _waitWhileBusy(), the one
signal that the busy pin never released. Buffer print() calls and
flush the accumulated line to ESP_LOGW on println(), so that warning
(and any other direct Serial usage in a vendored driver) actually
reaches the log instead of vanishing.
ZiB vor 1 Monat
Ursprung
Commit
40c637833e
1 geänderte Dateien mit 26 neuen und 5 gelöschten Zeilen
  1. 26 5
      components/gxepd2/arduino_compat/Arduino.h

+ 26 - 5
components/gxepd2/arduino_compat/Arduino.h

@@ -7,8 +7,11 @@
 // PinRegistry, so GxEPD2 compiles completely unmodified against it.
 // PinRegistry, so GxEPD2 compiles completely unmodified against it.
 
 
 #include <cstdint>
 #include <cstdint>
+#include <sstream>
+#include <string>
 
 
 #include "esphome/core/hal.h"
 #include "esphome/core/hal.h"
+#include "esphome/core/log.h"
 #include "esphome/components/gxepd2/pin_registry.h"
 #include "esphome/components/gxepd2/pin_registry.h"
 
 
 #ifndef ESP32
 #ifndef ESP32
@@ -39,13 +42,31 @@ inline void yield() { esphome::yield(); }
 
 
 #define pgm_read_byte(addr) (*reinterpret_cast<const uint8_t *>(addr))
 #define pgm_read_byte(addr) (*reinterpret_cast<const uint8_t *>(addr))
 
 
-// Stub: GxEPD2's serial diagnostics are opt-in and this component never
-// enables them.
+// GxEPD2's per-transfer timing diagnostics are opt-in (this component never
+// enables them, by passing serial_diag_bitrate=0 to init()) and stay
+// discarded here. But a handful of unconditional warnings - notably
+// GxEPD2_EPD::_waitWhileBusy()'s "Busy Timeout!" - also go through Serial,
+// and those matter: they're the only signal that the busy pin never
+// released. Buffer print() calls and flush to the logger on println(), so
+// that warning actually surfaces instead of vanishing silently.
 class GxEPD2SerialShim {
 class GxEPD2SerialShim {
  public:
  public:
   void begin(uint32_t baud) {}
   void begin(uint32_t baud) {}
-  template<typename T> void print(T) {}
-  template<typename T> void println(T) {}
-  void println() {}
+  template<typename T> void print(T value) { this->buffer_ << value; }
+  template<typename T> void println(T value) {
+    this->buffer_ << value;
+    this->flush_();
+  }
+  void println() { this->flush_(); }
+
+ private:
+  void flush_() {
+    std::string line = this->buffer_.str();
+    if (!line.empty())
+      ESP_LOGW("gxepd2", "%s", line.c_str());
+    this->buffer_.str("");
+    this->buffer_.clear();
+  }
+  std::ostringstream buffer_;
 };
 };
 inline GxEPD2SerialShim Serial;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
 inline GxEPD2SerialShim Serial;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)