Arduino.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. // Minimal Arduino.h substitute for compiling GxEPD2 under ESP-IDF. GxEPD2's
  3. // vendored source calls down into a small, fixed set of Arduino primitives
  4. // (digitalWrite/pinMode/digitalRead on plain int16_t pin numbers, plus
  5. // delay/micros/yield) - this shim backs them with esphome::GPIOPin via
  6. // PinRegistry, so GxEPD2 compiles completely unmodified against it.
  7. #include <cstdint>
  8. #include <sstream>
  9. #include <string>
  10. #include "esphome/core/application.h"
  11. #include "esphome/core/hal.h"
  12. #include "esphome/core/log.h"
  13. #include "esphome/components/gxepd2/pin_registry.h"
  14. #ifndef ESP32
  15. #define ESP32
  16. #endif
  17. #define HIGH 1
  18. #define LOW 0
  19. #define INPUT 0
  20. #define OUTPUT 1
  21. #define INPUT_PULLUP 2
  22. inline void pinMode(int16_t pin, uint8_t mode) {}
  23. inline void digitalWrite(int16_t pin, uint8_t value) {
  24. if (auto *p = esphome::gxepd2::PinRegistry::get(pin))
  25. p->digital_write(value != 0);
  26. }
  27. inline int digitalRead(int16_t pin) {
  28. auto *p = esphome::gxepd2::PinRegistry::get(pin);
  29. return (p != nullptr && p->digital_read()) ? HIGH : LOW;
  30. }
  31. // GxEPD2_EPD::_waitWhileBusy() spins on these two (delay() in its main
  32. // branch, yield() right after) for as long as the busy pin stays asserted -
  33. // deliberately up to tens of seconds (_busy_timeout), since a real 3/4-color
  34. // refresh can legitimately take that long. Neither esphome::delay() nor
  35. // esphome::yield() resets the ESP-IDF task watchdog on their own, so
  36. // without an explicit feed here, a real (or a misconfigured-polarity, thus
  37. // permanently "busy") wait outlasts the watchdog and the whole device
  38. // reboots mid-refresh instead of the loop's own timeout ever getting a
  39. // chance to fire.
  40. inline void delay(uint32_t ms) {
  41. esphome::App.feed_wdt();
  42. esphome::delay(ms);
  43. }
  44. inline uint32_t micros() { return esphome::micros(); }
  45. inline void yield() {
  46. esphome::App.feed_wdt();
  47. esphome::yield();
  48. }
  49. #define pgm_read_byte(addr) (*reinterpret_cast<const uint8_t *>(addr))
  50. // GxEPD2's per-transfer timing diagnostics are opt-in (this component never
  51. // enables them, by passing serial_diag_bitrate=0 to init()) and stay
  52. // discarded here. But a handful of unconditional warnings - notably
  53. // GxEPD2_EPD::_waitWhileBusy()'s "Busy Timeout!" - also go through Serial,
  54. // and those matter: they're the only signal that the busy pin never
  55. // released. Buffer print() calls and flush to the logger on println(), so
  56. // that warning actually surfaces instead of vanishing silently.
  57. class GxEPD2SerialShim {
  58. public:
  59. void begin(uint32_t baud) {}
  60. template<typename T> void print(T value) { this->buffer_ << value; }
  61. template<typename T> void println(T value) {
  62. this->buffer_ << value;
  63. this->flush_();
  64. }
  65. void println() { this->flush_(); }
  66. private:
  67. void flush_() {
  68. std::string line = this->buffer_.str();
  69. if (!line.empty())
  70. ESP_LOGW("gxepd2", "%s", line.c_str());
  71. this->buffer_.str("");
  72. this->buffer_.clear();
  73. }
  74. std::ostringstream buffer_;
  75. };
  76. inline GxEPD2SerialShim Serial; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)