From 8fc42694f69743561ee5d4474d768aeb2be9d35b Mon Sep 17 00:00:00 2001 From: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:42:25 +1000 Subject: [PATCH] [ili9xxx] Rework delay handling (#7115) --- esphome/components/ili9xxx/ili9xxx_defines.h | 4 ++- .../components/ili9xxx/ili9xxx_display.cpp | 31 ++++++------------- esphome/components/ili9xxx/ili9xxx_display.h | 12 +++---- esphome/components/ili9xxx/ili9xxx_init.h | 8 ++--- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/esphome/components/ili9xxx/ili9xxx_defines.h b/esphome/components/ili9xxx/ili9xxx_defines.h index 744013db3d..f4c5aad957 100644 --- a/esphome/components/ili9xxx/ili9xxx_defines.h +++ b/esphome/components/ili9xxx/ili9xxx_defines.h @@ -92,7 +92,9 @@ static const uint8_t ILI9XXX_GMCTRN1 = 0xE1; static const uint8_t ILI9XXX_CSCON = 0xF0; static const uint8_t ILI9XXX_ADJCTL3 = 0xF7; -static const uint8_t ILI9XXX_DELAY = 0xFF; // followed by one byte of delay time in ms +static const uint8_t ILI9XXX_DELAY_FLAG = 0xFF; +// special marker for delay - command byte reprents ms, length byte is an impossible value +#define ILI9XXX_DELAY(ms) ((uint8_t) ((ms) | 0x80)), ILI9XXX_DELAY_FLAG } // namespace ili9xxx } // namespace esphome diff --git a/esphome/components/ili9xxx/ili9xxx_display.cpp b/esphome/components/ili9xxx/ili9xxx_display.cpp index de03df5d41..4f035edbb0 100644 --- a/esphome/components/ili9xxx/ili9xxx_display.cpp +++ b/esphome/components/ili9xxx/ili9xxx_display.cpp @@ -34,8 +34,8 @@ void ILI9XXXDisplay::setup() { ESP_LOGD(TAG, "Setting up ILI9xxx"); this->setup_pins_(); - this->init_lcd(this->init_sequence_); - this->init_lcd(this->extra_init_sequence_.data()); + this->init_lcd_(this->init_sequence_); + this->init_lcd_(this->extra_init_sequence_.data()); switch (this->pixel_mode_) { case PIXEL_MODE_16: if (this->is_18bitdisplay_) { @@ -405,42 +405,29 @@ void ILI9XXXDisplay::reset_() { } } -void ILI9XXXDisplay::init_lcd(const uint8_t *addr) { +void ILI9XXXDisplay::init_lcd_(const uint8_t *addr) { if (addr == nullptr) return; uint8_t cmd, x, num_args; while ((cmd = *addr++) != 0) { x = *addr++; - if (cmd == ILI9XXX_DELAY) { - ESP_LOGD(TAG, "Delay %dms", x); - delay(x); + if (x == ILI9XXX_DELAY_FLAG) { + cmd &= 0x7F; + ESP_LOGV(TAG, "Delay %dms", cmd); + delay(cmd); } else { num_args = x & 0x7F; - ESP_LOGD(TAG, "Command %02X, length %d, bits %02X", cmd, num_args, *addr); + ESP_LOGV(TAG, "Command %02X, length %d, bits %02X", cmd, num_args, *addr); this->send_command(cmd, addr, num_args); addr += num_args; if (x & 0x80) { - ESP_LOGD(TAG, "Delay 150ms"); + ESP_LOGV(TAG, "Delay 150ms"); delay(150); // NOLINT } } } } -void ILI9XXXGC9A01A::init_lcd(const uint8_t *addr) { - if (addr == nullptr) - return; - uint8_t cmd, x, num_args; - while ((cmd = *addr++) != 0) { - x = *addr++; - num_args = x & 0x7F; - this->send_command(cmd, addr, num_args); - addr += num_args; - if (x & 0x80) - delay(150); // NOLINT - } -} - // Tell the display controller where we want to draw pixels. void ILI9XXXDisplay::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { x1 += this->offset_x_; diff --git a/esphome/components/ili9xxx/ili9xxx_display.h b/esphome/components/ili9xxx/ili9xxx_display.h index b60047a8c3..6121488d15 100644 --- a/esphome/components/ili9xxx/ili9xxx_display.h +++ b/esphome/components/ili9xxx/ili9xxx_display.h @@ -33,7 +33,9 @@ class ILI9XXXDisplay : public display::DisplayBuffer, uint8_t cmd, num_args, bits; const uint8_t *addr = init_sequence; while ((cmd = *addr++) != 0) { - num_args = *addr++ & 0x7F; + num_args = *addr++; + if (num_args == ILI9XXX_DELAY_FLAG) + continue; bits = *addr; switch (cmd) { case ILI9XXX_MADCTL: { @@ -50,13 +52,10 @@ class ILI9XXXDisplay : public display::DisplayBuffer, break; } - case ILI9XXX_DELAY: - continue; // no args to skip - default: break; } - addr += num_args; + addr += (num_args & 0x7F); } } @@ -109,7 +108,7 @@ class ILI9XXXDisplay : public display::DisplayBuffer, virtual void set_madctl(); void display_(); - virtual void init_lcd(const uint8_t *addr); + void init_lcd_(const uint8_t *addr); void set_addr_window_(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2); void reset_(); @@ -269,7 +268,6 @@ class ILI9XXXS3BoxLite : public ILI9XXXDisplay { class ILI9XXXGC9A01A : public ILI9XXXDisplay { public: ILI9XXXGC9A01A() : ILI9XXXDisplay(INITCMD_GC9A01A, 240, 240, true) {} - void init_lcd(const uint8_t *addr) override; }; //----------- ILI9XXX_24_TFT display -------------- diff --git a/esphome/components/ili9xxx/ili9xxx_init.h b/esphome/components/ili9xxx/ili9xxx_init.h index 260bde4c80..5a67812bc1 100644 --- a/esphome/components/ili9xxx/ili9xxx_init.h +++ b/esphome/components/ili9xxx/ili9xxx_init.h @@ -372,9 +372,9 @@ static const uint8_t PROGMEM INITCMD_GC9A01A[] = { static const uint8_t PROGMEM INITCMD_ST7735[] = { ILI9XXX_SWRESET, 0, // Soft reset, then delay 10ms - ILI9XXX_DELAY, 10, + ILI9XXX_DELAY(10), ILI9XXX_SLPOUT , 0, // Exit Sleep, delay - ILI9XXX_DELAY, 10, + ILI9XXX_DELAY(10), ILI9XXX_PIXFMT , 1, 0x05, ILI9XXX_FRMCTR1, 3, // 4: Frame rate control, 3 args + delay: 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D) @@ -415,9 +415,9 @@ static const uint8_t PROGMEM INITCMD_ST7735[] = { 0x00, 0x00, 0x02, 0x10, ILI9XXX_MADCTL , 1, 0x00, // Memory Access Control, BGR ILI9XXX_NORON , 0, - ILI9XXX_DELAY, 10, + ILI9XXX_DELAY(10), ILI9XXX_DISPON , 0, // Display on - ILI9XXX_DELAY, 10, + ILI9XXX_DELAY(10), 00, // endo of list };