display.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """ESPHome display platform driving a GxEPD2 panel via a native chunked
  2. refresh engine (see cycle_engine.h) instead of GxEPD2's own C++ classes.
  3. Panels are selected via `model:`, each backed by a hand-written (later,
  4. extracted) EpdPanelDescriptor - see panel_213_z98c.h / panel_426_gdeq0426t82.h
  5. / panel_583_gdeq0583t31.h. catalog.json/vendor/arduino_compat stay in the
  6. tree as reference material for panels not yet ported to this engine.
  7. """
  8. import esphome.codegen as cg
  9. import esphome.config_validation as cv
  10. from esphome import pins
  11. from esphome.components import display, spi
  12. from esphome.const import (
  13. CONF_BUSY_PIN,
  14. CONF_CS_PIN,
  15. CONF_DC_PIN,
  16. CONF_ID,
  17. CONF_LAMBDA,
  18. CONF_MODEL,
  19. CONF_RESET_PIN,
  20. CONF_SPI_ID,
  21. Framework,
  22. )
  23. from esphome.types import ConfigType
  24. CODEOWNERS = ["@zib"]
  25. DEPENDENCIES = ["spi"]
  26. # When true, the 1bpp (mono / BW+accent) draw path treats a *bright* color as
  27. # ink and a *dark* color as background, instead of the literal RGB mapping
  28. # (bright = white = blank). This exists only to ease conversion from
  29. # ESPHome's LCD-rooted color model: its drawing primitives default to
  30. # COLOR_ON = white, so with the literal mapping a default-color it.print()
  31. # draws nothing. We can't tell a defaulted COLOR_ON from an explicit white
  32. # (the default is compiled into the user's lambda as a constant), so the only
  33. # lever is this wholesale mono inversion - the same thing waveshare_epaper
  34. # bakes in. Default false (native, literal RGB); a future waveshare model
  35. # alias will flip the *default* to true, but an explicit setting always wins.
  36. # No effect on 2bpp/4bpp packed panels - there the palette maps literal RGB.
  37. CONF_INVERT_MONO = "invert_mono"
  38. gxepd2_ns = cg.esphome_ns.namespace("gxepd2")
  39. GxEPD2Display = gxepd2_ns.class_("GxEPD2Display", display.DisplayBuffer)
  40. # model name -> C++ descriptor global (EpdPanelDescriptor) declared in the
  41. # matching panel_*.h header, which gxepd2_display.h includes unconditionally
  42. # - so any of these names can be selected without extra include wiring here.
  43. MODELS = {
  44. "GxEPD2_213_Z98C": "PANEL_213_Z98C",
  45. "GxEPD2_426_GDEQ0426T82": "PANEL_426_GDEQ0426T82",
  46. "GxEPD2_583_GDEQ0583T31": "PANEL_583_GDEQ0583T31",
  47. "GxEPD2_290c_GDEY029F51": "PANEL_290C_GDEY029F51",
  48. "GxEPD2_579c_GDEY0579F51": "PANEL_579C_GDEY0579F51",
  49. # Debug variant: master half only, as if it were a self-contained
  50. # 396x272 panel - see panel_579c_gdey0579f51_master_only.h. Kept
  51. # around as a diagnostic tool, not the panel's real mode.
  52. "GxEPD2_579c_GDEY0579F51_MASTER_ONLY": "PANEL_579C_GDEY0579F51_MASTER_ONLY",
  53. }
  54. CONFIG_SCHEMA = cv.All(
  55. display.FULL_DISPLAY_SCHEMA.extend(
  56. {
  57. cv.GenerateID(): cv.declare_id(GxEPD2Display),
  58. cv.GenerateID(CONF_SPI_ID): cv.use_id(spi.SPIComponent),
  59. cv.Required(CONF_MODEL): cv.one_of(*MODELS),
  60. cv.Required(CONF_CS_PIN): pins.gpio_output_pin_schema,
  61. cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
  62. cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
  63. cv.Optional(CONF_BUSY_PIN): pins.gpio_input_pin_schema,
  64. # No schema default: absence falls to the alias-derived default in
  65. # to_code() (just False today; true for a waveshare alias once that
  66. # lands), while an explicit value always wins.
  67. cv.Optional(CONF_INVERT_MONO): cv.boolean,
  68. }
  69. ),
  70. # IDF-first, but the runtime C++ uses only framework-portable ESPHome
  71. # abstractions (spi::SPIDevice, GPIOPin, hal.h, RAMAllocator, ESP_LOG*) -
  72. # the old IDF-only gate was a leftover from the superseded FreeRTOS-task
  73. # design. ESP32-Arduino is a supported accommodation; ESP8266-Arduino
  74. # compiles too but is resolution-restricted at 1bpp and lacks the PSRAM
  75. # that >1bpp (color) panels need, so those fail allocation at runtime.
  76. cv.only_with_framework([Framework.ESP_IDF, Framework.ARDUINO]),
  77. )
  78. async def to_code(config: ConfigType) -> None:
  79. var = cg.new_Pvariable(config[CONF_ID])
  80. cg.add(var.set_descriptor(cg.RawExpression(f"&esphome::gxepd2::{MODELS[config[CONF_MODEL]]}")))
  81. cg.add(var.set_cs_pin(await cg.gpio_pin_expression(config[CONF_CS_PIN])))
  82. cg.add(var.set_dc_pin(await cg.gpio_pin_expression(config[CONF_DC_PIN])))
  83. if reset_pin := config.get(CONF_RESET_PIN):
  84. cg.add(var.set_reset_pin(await cg.gpio_pin_expression(reset_pin)))
  85. if busy_pin := config.get(CONF_BUSY_PIN):
  86. cg.add(var.set_busy_pin(await cg.gpio_pin_expression(busy_pin)))
  87. spi_parent = await cg.get_variable(config[CONF_SPI_ID])
  88. cg.add(var.set_spi_parent(spi_parent))
  89. # Alias-derived default is just False for now; the waveshare-compat layer
  90. # will compute it from the (aliased) model. An explicit invert_mono wins.
  91. cg.add(var.set_invert_mono(config.get(CONF_INVERT_MONO, False)))
  92. # display.register_display() only wires up `pages:` - a bare top-level
  93. # `lambda:` (what our CONFIG_SCHEMA actually supports) has to be
  94. # processed and attached via set_writer() by the platform itself, same
  95. # as every other display component's to_code() (see e.g. epaper_spi's
  96. # display.py). Missing this meant the configured lambda never ran at
  97. # all - the only thing ever drawn was auto_clear's default
  98. # fill(COLOR_OFF), which is why every real-hardware run came out solid
  99. # black regardless of any protocol-level change.
  100. if CONF_LAMBDA in config:
  101. lambda_ = await cg.process_lambda(config[CONF_LAMBDA], [(display.DisplayRef, "it")], return_type=cg.void)
  102. cg.add(var.set_writer(lambda_))
  103. await display.register_display(var, config)