From 254522dd9341c9c6cbbb6903a42d787856d68e90 Mon Sep 17 00:00:00 2001 From: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:26:50 +1100 Subject: [PATCH] [qspi_dbi] Rename from qspi_amoled, add features (#7594) Co-authored-by: clydeps --- CODEOWNERS | 2 +- esphome/components/ili9xxx/display.py | 2 +- esphome/components/qspi_amoled/display.py | 142 +------------- .../{qspi_amoled => qspi_dbi}/__init__.py | 0 esphome/components/qspi_dbi/display.py | 185 ++++++++++++++++++ esphome/components/qspi_dbi/models.py | 64 ++++++ .../qspi_amoled.cpp => qspi_dbi/qspi_dbi.cpp} | 139 ++++++++----- .../qspi_amoled.h => qspi_dbi/qspi_dbi.h} | 41 ++-- esphome/components/st7701s/display.py | 2 +- esphome/const.py | 1 + .../{qspi_amoled => qspi_dbi}/common.yaml | 14 +- .../test.esp32-s3-idf.yaml | 0 12 files changed, 385 insertions(+), 207 deletions(-) rename esphome/components/{qspi_amoled => qspi_dbi}/__init__.py (100%) create mode 100644 esphome/components/qspi_dbi/display.py create mode 100644 esphome/components/qspi_dbi/models.py rename esphome/components/{qspi_amoled/qspi_amoled.cpp => qspi_dbi/qspi_dbi.cpp} (51%) rename esphome/components/{qspi_amoled/qspi_amoled.h => qspi_dbi/qspi_dbi.h} (80%) rename tests/components/{qspi_amoled => qspi_dbi}/common.yaml (73%) rename tests/components/{qspi_amoled => qspi_dbi}/test.esp32-s3-idf.yaml (100%) diff --git a/CODEOWNERS b/CODEOWNERS index a19f3d75ed..7dd18d2c51 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -324,7 +324,7 @@ esphome/components/pvvx_mithermometer/* @pasiz esphome/components/pylontech/* @functionpointer esphome/components/qmp6988/* @andrewpc esphome/components/qr_code/* @wjtje -esphome/components/qspi_amoled/* @clydebarrow +esphome/components/qspi_dbi/* @clydebarrow esphome/components/qwiic_pir/* @kahrendt esphome/components/radon_eye_ble/* @jeffeb3 esphome/components/radon_eye_rd200/* @jeffeb3 diff --git a/esphome/components/ili9xxx/display.py b/esphome/components/ili9xxx/display.py index 2182ca9a6d..68e3aa953d 100644 --- a/esphome/components/ili9xxx/display.py +++ b/esphome/components/ili9xxx/display.py @@ -10,6 +10,7 @@ from esphome.const import ( CONF_DIMENSIONS, CONF_HEIGHT, CONF_ID, + CONF_INIT_SEQUENCE, CONF_INVERT_COLORS, CONF_LAMBDA, CONF_MIRROR_X, @@ -89,7 +90,6 @@ CONF_LED_PIN = "led_pin" CONF_COLOR_PALETTE_IMAGES = "color_palette_images" CONF_INVERT_DISPLAY = "invert_display" CONF_PIXEL_MODE = "pixel_mode" -CONF_INIT_SEQUENCE = "init_sequence" def cmd(c, *args): diff --git a/esphome/components/qspi_amoled/display.py b/esphome/components/qspi_amoled/display.py index 77d1e3d095..00773b516a 100644 --- a/esphome/components/qspi_amoled/display.py +++ b/esphome/components/qspi_amoled/display.py @@ -1,143 +1,3 @@ -import esphome.codegen as cg import esphome.config_validation as cv -from esphome import pins -from esphome.components import ( - spi, - display, -) -from esphome.const import ( - CONF_RESET_PIN, - CONF_ID, - CONF_DIMENSIONS, - CONF_WIDTH, - CONF_HEIGHT, - CONF_LAMBDA, - CONF_BRIGHTNESS, - CONF_ENABLE_PIN, - CONF_MODEL, - CONF_OFFSET_HEIGHT, - CONF_OFFSET_WIDTH, - CONF_INVERT_COLORS, - CONF_MIRROR_X, - CONF_MIRROR_Y, - CONF_SWAP_XY, - CONF_COLOR_ORDER, - CONF_TRANSFORM, -) -DEPENDENCIES = ["spi"] - -qspi_amoled_ns = cg.esphome_ns.namespace("qspi_amoled") -QSPI_AMOLED = qspi_amoled_ns.class_( - "QspiAmoLed", display.Display, display.DisplayBuffer, cg.Component, spi.SPIDevice -) -ColorOrder = display.display_ns.enum("ColorMode") -Model = qspi_amoled_ns.enum("Model") - -MODELS = {"RM690B0": Model.RM690B0, "RM67162": Model.RM67162} - -COLOR_ORDERS = { - "RGB": ColorOrder.COLOR_ORDER_RGB, - "BGR": ColorOrder.COLOR_ORDER_BGR, -} -DATA_PIN_SCHEMA = pins.internal_gpio_output_pin_schema - - -def validate_dimension(value): - value = cv.positive_int(value) - if value % 2 != 0: - raise cv.Invalid("Width/height/offset must be divisible by 2") - return value - - -CONFIG_SCHEMA = cv.All( - display.FULL_DISPLAY_SCHEMA.extend( - cv.Schema( - { - cv.GenerateID(): cv.declare_id(QSPI_AMOLED), - cv.Required(CONF_MODEL): cv.enum(MODELS, upper=True), - cv.Required(CONF_DIMENSIONS): cv.Any( - cv.dimensions, - cv.Schema( - { - cv.Required(CONF_WIDTH): validate_dimension, - cv.Required(CONF_HEIGHT): validate_dimension, - cv.Optional( - CONF_OFFSET_HEIGHT, default=0 - ): validate_dimension, - cv.Optional( - CONF_OFFSET_WIDTH, default=0 - ): validate_dimension, - } - ), - ), - cv.Optional(CONF_TRANSFORM): cv.Schema( - { - cv.Optional(CONF_MIRROR_X, default=False): cv.boolean, - cv.Optional(CONF_MIRROR_Y, default=False): cv.boolean, - cv.Optional(CONF_SWAP_XY, default=False): cv.boolean, - } - ), - cv.Optional(CONF_COLOR_ORDER, default="RGB"): cv.enum( - COLOR_ORDERS, upper=True - ), - cv.Optional(CONF_INVERT_COLORS, default=False): cv.boolean, - cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema, - cv.Optional(CONF_ENABLE_PIN): pins.gpio_output_pin_schema, - cv.Optional(CONF_BRIGHTNESS, default=0xD0): cv.int_range( - 0, 0xFF, min_included=True, max_included=True - ), - } - ).extend( - spi.spi_device_schema( - cs_pin_required=False, - default_mode="MODE0", - default_data_rate=10e6, - quad=True, - ) - ) - ), - cv.only_with_esp_idf, -) - - -async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) - await display.register_display(var, config) - await spi.register_spi_device(var, config) - - cg.add(var.set_color_mode(config[CONF_COLOR_ORDER])) - cg.add(var.set_invert_colors(config[CONF_INVERT_COLORS])) - cg.add(var.set_brightness(config[CONF_BRIGHTNESS])) - cg.add(var.set_model(config[CONF_MODEL])) - if enable_pin := config.get(CONF_ENABLE_PIN): - enable = await cg.gpio_pin_expression(enable_pin) - cg.add(var.set_enable_pin(enable)) - - if reset_pin := config.get(CONF_RESET_PIN): - reset = await cg.gpio_pin_expression(reset_pin) - cg.add(var.set_reset_pin(reset)) - - if transform := config.get(CONF_TRANSFORM): - cg.add(var.set_mirror_x(transform[CONF_MIRROR_X])) - cg.add(var.set_mirror_y(transform[CONF_MIRROR_Y])) - cg.add(var.set_swap_xy(transform[CONF_SWAP_XY])) - - if CONF_DIMENSIONS in config: - dimensions = config[CONF_DIMENSIONS] - if isinstance(dimensions, dict): - cg.add(var.set_dimensions(dimensions[CONF_WIDTH], dimensions[CONF_HEIGHT])) - cg.add( - var.set_offsets( - dimensions[CONF_OFFSET_WIDTH], dimensions[CONF_OFFSET_HEIGHT] - ) - ) - else: - (width, height) = dimensions - cg.add(var.set_dimensions(width, height)) - - if lamb := config.get(CONF_LAMBDA): - lambda_ = await cg.process_lambda( - lamb, [(display.DisplayRef, "it")], return_type=cg.void - ) - cg.add(var.set_writer(lambda_)) +CONFIG_SCHEMA = cv.invalid("The qspi_amoled component has been renamed to qspi_dbi") diff --git a/esphome/components/qspi_amoled/__init__.py b/esphome/components/qspi_dbi/__init__.py similarity index 100% rename from esphome/components/qspi_amoled/__init__.py rename to esphome/components/qspi_dbi/__init__.py diff --git a/esphome/components/qspi_dbi/display.py b/esphome/components/qspi_dbi/display.py new file mode 100644 index 0000000000..71ae31f182 --- /dev/null +++ b/esphome/components/qspi_dbi/display.py @@ -0,0 +1,185 @@ +from esphome import pins +import esphome.codegen as cg +from esphome.components import display, spi +import esphome.config_validation as cv +from esphome.const import ( + CONF_BRIGHTNESS, + CONF_COLOR_ORDER, + CONF_DIMENSIONS, + CONF_ENABLE_PIN, + CONF_HEIGHT, + CONF_ID, + CONF_INIT_SEQUENCE, + CONF_INVERT_COLORS, + CONF_LAMBDA, + CONF_MIRROR_X, + CONF_MIRROR_Y, + CONF_MODEL, + CONF_OFFSET_HEIGHT, + CONF_OFFSET_WIDTH, + CONF_RESET_PIN, + CONF_SWAP_XY, + CONF_TRANSFORM, + CONF_WIDTH, +) +from esphome.core import TimePeriod + +from .models import DriverChip + +DEPENDENCIES = ["spi"] + +qspi_dbi_ns = cg.esphome_ns.namespace("qspi_dbi") +QSPI_DBI = qspi_dbi_ns.class_( + "QspiDbi", display.Display, display.DisplayBuffer, cg.Component, spi.SPIDevice +) +ColorOrder = display.display_ns.enum("ColorMode") +Model = qspi_dbi_ns.enum("Model") + +COLOR_ORDERS = { + "RGB": ColorOrder.COLOR_ORDER_RGB, + "BGR": ColorOrder.COLOR_ORDER_BGR, +} +DATA_PIN_SCHEMA = pins.internal_gpio_output_pin_schema + +CONF_DRAW_FROM_ORIGIN = "draw_from_origin" +DELAY_FLAG = 0xFF + + +def validate_dimension(value): + value = cv.positive_int(value) + if value % 2 != 0: + raise cv.Invalid("Width/height/offset must be divisible by 2") + return value + + +def map_sequence(value): + """ + The format is a repeated sequence of [CMD, ] where is s a sequence of bytes. The length is inferred + from the length of the sequence and should not be explicit. + A delay can be inserted by specifying "- delay N" where N is in ms + """ + if isinstance(value, str) and value.lower().startswith("delay "): + value = value.lower()[6:] + delay = cv.All( + cv.positive_time_period_milliseconds, + cv.Range(TimePeriod(milliseconds=1), TimePeriod(milliseconds=255)), + )(value) + return [delay, DELAY_FLAG] + value = cv.Length(min=1, max=254)(value) + params = value[1:] + return [value[0], len(params)] + list(params) + + +def _validate(config): + chip = DriverChip.chips[config[CONF_MODEL]] + if not chip.initsequence: + if CONF_INIT_SEQUENCE not in config: + raise cv.Invalid(f"{chip.name} model requires init_sequence") + return config + + +CONFIG_SCHEMA = cv.All( + display.FULL_DISPLAY_SCHEMA.extend( + cv.Schema( + { + cv.GenerateID(): cv.declare_id(QSPI_DBI), + cv.Required(CONF_MODEL): cv.one_of( + *DriverChip.chips.keys(), upper=True + ), + cv.Optional(CONF_INIT_SEQUENCE): cv.ensure_list(map_sequence), + cv.Required(CONF_DIMENSIONS): cv.Any( + cv.dimensions, + cv.Schema( + { + cv.Required(CONF_WIDTH): validate_dimension, + cv.Required(CONF_HEIGHT): validate_dimension, + cv.Optional( + CONF_OFFSET_HEIGHT, default=0 + ): validate_dimension, + cv.Optional( + CONF_OFFSET_WIDTH, default=0 + ): validate_dimension, + } + ), + ), + cv.Optional(CONF_TRANSFORM): cv.Schema( + { + cv.Optional(CONF_MIRROR_X, default=False): cv.boolean, + cv.Optional(CONF_MIRROR_Y, default=False): cv.boolean, + cv.Optional(CONF_SWAP_XY, default=False): cv.boolean, + } + ), + cv.Optional(CONF_COLOR_ORDER, default="RGB"): cv.enum( + COLOR_ORDERS, upper=True + ), + cv.Optional(CONF_INVERT_COLORS, default=False): cv.boolean, + cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema, + cv.Optional(CONF_ENABLE_PIN): pins.gpio_output_pin_schema, + cv.Optional(CONF_BRIGHTNESS, default=0xD0): cv.int_range( + 0, 0xFF, min_included=True, max_included=True + ), + cv.Optional(CONF_DRAW_FROM_ORIGIN, default=False): cv.boolean, + } + ).extend( + spi.spi_device_schema( + cs_pin_required=False, + default_mode="MODE0", + default_data_rate=10e6, + quad=True, + ) + ) + ), + cv.only_with_esp_idf, +) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + await display.register_display(var, config) + await spi.register_spi_device(var, config) + + chip = DriverChip.chips[config[CONF_MODEL]] + if chip.initsequence: + cg.add(var.add_init_sequence(chip.initsequence)) + if init_sequences := config.get(CONF_INIT_SEQUENCE): + sequence = [] + for seq in init_sequences: + sequence.extend(seq) + cg.add(var.add_init_sequence(sequence)) + + cg.add(var.set_color_mode(config[CONF_COLOR_ORDER])) + cg.add(var.set_invert_colors(config[CONF_INVERT_COLORS])) + cg.add(var.set_brightness(config[CONF_BRIGHTNESS])) + cg.add(var.set_model(config[CONF_MODEL])) + cg.add(var.set_draw_from_origin(config[CONF_DRAW_FROM_ORIGIN])) + if enable_pin := config.get(CONF_ENABLE_PIN): + enable = await cg.gpio_pin_expression(enable_pin) + cg.add(var.set_enable_pin(enable)) + + if reset_pin := config.get(CONF_RESET_PIN): + reset = await cg.gpio_pin_expression(reset_pin) + cg.add(var.set_reset_pin(reset)) + + if transform := config.get(CONF_TRANSFORM): + cg.add(var.set_mirror_x(transform[CONF_MIRROR_X])) + cg.add(var.set_mirror_y(transform[CONF_MIRROR_Y])) + cg.add(var.set_swap_xy(transform[CONF_SWAP_XY])) + + if CONF_DIMENSIONS in config: + dimensions = config[CONF_DIMENSIONS] + if isinstance(dimensions, dict): + cg.add(var.set_dimensions(dimensions[CONF_WIDTH], dimensions[CONF_HEIGHT])) + cg.add( + var.set_offsets( + dimensions[CONF_OFFSET_WIDTH], dimensions[CONF_OFFSET_HEIGHT] + ) + ) + else: + (width, height) = dimensions + cg.add(var.set_dimensions(width, height)) + + if lamb := config.get(CONF_LAMBDA): + lambda_ = await cg.process_lambda( + lamb, [(display.DisplayRef, "it")], return_type=cg.void + ) + cg.add(var.set_writer(lambda_)) diff --git a/esphome/components/qspi_dbi/models.py b/esphome/components/qspi_dbi/models.py new file mode 100644 index 0000000000..071ea72d73 --- /dev/null +++ b/esphome/components/qspi_dbi/models.py @@ -0,0 +1,64 @@ +# Commands +SW_RESET_CMD = 0x01 +SLEEP_OUT = 0x11 +INVERT_OFF = 0x20 +INVERT_ON = 0x21 +ALL_ON = 0x23 +WRAM = 0x24 +MIPI = 0x26 +DISPLAY_OFF = 0x28 +DISPLAY_ON = 0x29 +RASET = 0x2B +CASET = 0x2A +WDATA = 0x2C +TEON = 0x35 +MADCTL_CMD = 0x36 +PIXFMT = 0x3A +BRIGHTNESS = 0x51 +SWIRE1 = 0x5A +SWIRE2 = 0x5B +PAGESEL = 0xFE + + +class DriverChip: + chips = {} + + def __init__(self, name: str): + name = name.upper() + self.name = name + self.chips[name] = self + self.initsequence = [] + + def cmd(self, c, *args): + """ + Add a command sequence to the init sequence + :param c: The command (8 bit) + :param args: zero or more arguments (8 bit values) + """ + self.initsequence.extend([c, len(args)] + list(args)) + + def delay(self, ms): + self.initsequence.extend([ms, 0xFF]) + + +chip = DriverChip("RM67162") +chip.cmd(PIXFMT, 0x55) +chip.cmd(BRIGHTNESS, 0) + +chip = DriverChip("RM690B0") +chip.cmd(PAGESEL, 0x20) +chip.cmd(MIPI, 0x0A) +chip.cmd(WRAM, 0x80) +chip.cmd(SWIRE1, 0x51) +chip.cmd(SWIRE2, 0x2E) +chip.cmd(PAGESEL, 0x00) +chip.cmd(0xC2, 0x00) +chip.delay(10) +chip.cmd(TEON, 0x00) + +chip = DriverChip("AXS15231") +chip.cmd(0xBB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5A, 0xA5) +chip.cmd(0xC1, 0x33) +chip.cmd(0xBB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00) + +DriverChip("Custom") diff --git a/esphome/components/qspi_amoled/qspi_amoled.cpp b/esphome/components/qspi_dbi/qspi_dbi.cpp similarity index 51% rename from esphome/components/qspi_amoled/qspi_amoled.cpp rename to esphome/components/qspi_dbi/qspi_dbi.cpp index b1f651025a..a649a25ea6 100644 --- a/esphome/components/qspi_amoled/qspi_amoled.cpp +++ b/esphome/components/qspi_dbi/qspi_dbi.cpp @@ -1,12 +1,12 @@ #ifdef USE_ESP_IDF -#include "qspi_amoled.h" +#include "qspi_dbi.h" #include "esphome/core/log.h" namespace esphome { -namespace qspi_amoled { +namespace qspi_dbi { -void QspiAmoLed::setup() { - esph_log_config(TAG, "Setting up QSPI_AMOLED"); +void QspiDbi::setup() { + ESP_LOGCONFIG(TAG, "Setting up QSPI_DBI"); this->spi_setup(); if (this->enable_pin_ != nullptr) { this->enable_pin_->setup(); @@ -22,13 +22,17 @@ void QspiAmoLed::setup() { } this->set_timeout(120, [this] { this->write_command_(SLEEP_OUT); }); this->set_timeout(240, [this] { this->write_init_sequence_(); }); + if (this->draw_from_origin_) + check_buffer_(); } -void QspiAmoLed::update() { +void QspiDbi::update() { if (!this->setup_complete_) { return; } this->do_update_(); + if (this->buffer_ == nullptr || this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_) + return; // Start addresses and widths/heights must be divisible by 2 (CASET/RASET restriction in datasheet) if (this->x_low_ % 2 == 1) { this->x_low_--; @@ -42,10 +46,15 @@ void QspiAmoLed::update() { if (this->y_high_ % 2 == 0) { this->y_high_++; } + if (this->draw_from_origin_) { + this->x_low_ = 0; + this->y_low_ = 0; + this->x_high_ = this->width_ - 1; + } int w = this->x_high_ - this->x_low_ + 1; int h = this->y_high_ - this->y_low_ + 1; - this->draw_pixels_at(this->x_low_, this->y_low_, w, h, this->buffer_, this->color_mode_, display::COLOR_BITNESS_565, - true, this->x_low_, this->y_low_, this->get_width_internal() - w - this->x_low_); + this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_, this->y_low_, + this->width_ - w - this->x_low_); // invalidate watermarks this->x_low_ = this->width_; this->y_low_ = this->height_; @@ -53,21 +62,19 @@ void QspiAmoLed::update() { this->y_high_ = 0; } -void QspiAmoLed::draw_absolute_pixel_internal(int x, int y, Color color) { +void QspiDbi::draw_absolute_pixel_internal(int x, int y, Color color) { if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) { return; } - if (this->buffer_ == nullptr) - this->init_internal_(this->width_ * this->height_ * 2); if (this->is_failed()) return; + check_buffer_(); uint32_t pos = (y * this->width_) + x; - uint16_t new_color; bool updated = false; pos = pos * 2; - new_color = display::ColorUtil::color_to_565(color, display::ColorOrder::COLOR_ORDER_RGB); - if (this->buffer_[pos] != (uint8_t) (new_color >> 8)) { - this->buffer_[pos] = (uint8_t) (new_color >> 8); + uint16_t new_color = display::ColorUtil::color_to_565(color, display::ColorOrder::COLOR_ORDER_RGB); + if (this->buffer_[pos] != static_cast(new_color >> 8)) { + this->buffer_[pos] = static_cast(new_color >> 8); updated = true; } pos = pos + 1; @@ -90,7 +97,7 @@ void QspiAmoLed::draw_absolute_pixel_internal(int x, int y, Color color) { } } -void QspiAmoLed::reset_params_(bool ready) { +void QspiDbi::reset_params_(bool ready) { if (!ready && !this->is_ready()) return; this->write_command_(this->invert_colors_ ? INVERT_ON : INVERT_OFF); @@ -102,55 +109,64 @@ void QspiAmoLed::reset_params_(bool ready) { mad |= MADCTL_MX; if (this->mirror_y_) mad |= MADCTL_MY; - this->write_command_(MADCTL_CMD, &mad, 1); - this->write_command_(BRIGHTNESS, &this->brightness_, 1); + this->write_command_(MADCTL_CMD, mad); + this->write_command_(BRIGHTNESS, this->brightness_); + this->write_command_(NORON); + this->write_command_(DISPLAY_ON); } -void QspiAmoLed::write_init_sequence_() { - if (this->model_ == RM690B0) { - this->write_command_(PAGESEL, 0x20); - this->write_command_(MIPI, 0x0A); - this->write_command_(WRAM, 0x80); - this->write_command_(SWIRE1, 0x51); - this->write_command_(SWIRE2, 0x2E); - this->write_command_(PAGESEL, 0x00); - this->write_command_(0xC2, 0x00); - delay(10); - this->write_command_(TEON, 0x00); +void QspiDbi::write_init_sequence_() { + for (const auto &seq : this->init_sequences_) { + this->write_sequence_(seq); } - this->write_command_(PIXFMT, 0x55); - this->write_command_(BRIGHTNESS, 0); - this->write_command_(DISPLAY_ON); this->reset_params_(true); this->setup_complete_ = true; - esph_log_config(TAG, "QSPI_AMOLED setup complete"); + ESP_LOGCONFIG(TAG, "QSPI_DBI setup complete"); } -void QspiAmoLed::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { +void QspiDbi::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { + ESP_LOGVV(TAG, "Set addr %d/%d, %d/%d", x1, y1, x2, y2); uint8_t buf[4]; x1 += this->offset_x_; x2 += this->offset_x_; y1 += this->offset_y_; y2 += this->offset_y_; - put16_be(buf, x1); - put16_be(buf + 2, x2); - this->write_command_(CASET, buf, sizeof buf); put16_be(buf, y1); put16_be(buf + 2, y2); this->write_command_(RASET, buf, sizeof buf); + put16_be(buf, x1); + put16_be(buf + 2, x2); + this->write_command_(CASET, buf, sizeof buf); } -void QspiAmoLed::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order, - display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) { +void QspiDbi::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order, + display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) { if (!this->setup_complete_ || this->is_failed()) return; if (w <= 0 || h <= 0) return; if (bitness != display::COLOR_BITNESS_565 || order != this->color_mode_ || big_endian != (this->bit_order_ == spi::BIT_ORDER_MSB_FIRST)) { - return display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset, - x_pad); + return Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad); + } else if (this->draw_from_origin_) { + auto stride = x_offset + w + x_pad; + for (int y = 0; y != h; y++) { + memcpy(this->buffer_ + ((y + y_start) * this->width_ + x_start) * 2, + ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2); + } + ptr = this->buffer_; + w = this->width_; + h += y_start; + x_start = 0; + y_start = 0; + x_offset = 0; + y_offset = 0; } + this->write_to_display_(x_start, y_start, w, h, ptr, x_offset, y_offset, x_pad); +} + +void QspiDbi::write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset, + int x_pad) { this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1); this->enable(); // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display. @@ -158,17 +174,50 @@ void QspiAmoLed::draw_pixels_at(int x_start, int y_start, int w, int h, const ui // we could deal here with a non-zero y_offset, but if x_offset is zero, y_offset probably will be so don't bother this->write_cmd_addr_data(8, 0x32, 24, 0x2C00, ptr, w * h * 2, 4); } else { - this->write_cmd_addr_data(8, 0x32, 24, 0x2C00, nullptr, 0, 4); auto stride = x_offset + w + x_pad; + uint16_t cmd = 0x2C00; for (int y = 0; y != h; y++) { - this->write_cmd_addr_data(0, 0, 0, 0, ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2, 4); + this->write_cmd_addr_data(8, 0x32, 24, cmd, ptr + ((y + y_offset) * stride + x_offset) * 2, w * 2, 4); + cmd = 0x3C00; } } this->disable(); } +void QspiDbi::write_command_(uint8_t cmd, const uint8_t *bytes, size_t len) { + ESP_LOGV(TAG, "Command %02X, length %d, bytes %s", cmd, len, format_hex_pretty(bytes, len).c_str()); + this->enable(); + this->write_cmd_addr_data(8, 0x02, 24, cmd << 8, bytes, len); + this->disable(); +} -void QspiAmoLed::dump_config() { - ESP_LOGCONFIG("", "QSPI AMOLED"); +void QspiDbi::write_sequence_(const std::vector &vec) { + size_t index = 0; + while (index != vec.size()) { + if (vec.size() - index < 2) { + ESP_LOGE(TAG, "Malformed init sequence"); + return; + } + uint8_t cmd = vec[index++]; + uint8_t x = vec[index++]; + if (x == DELAY_FLAG) { + ESP_LOGV(TAG, "Delay %dms", cmd); + delay(cmd); + } else { + uint8_t num_args = x & 0x7F; + if (vec.size() - index < num_args) { + ESP_LOGE(TAG, "Malformed init sequence"); + return; + } + const auto *ptr = vec.data() + index; + this->write_command_(cmd, ptr, num_args); + index += num_args; + } + } +} + +void QspiDbi::dump_config() { + ESP_LOGCONFIG("", "QSPI_DBI Display"); + ESP_LOGCONFIG("", "Model: %s", this->model_); ESP_LOGCONFIG(TAG, " Height: %u", this->height_); ESP_LOGCONFIG(TAG, " Width: %u", this->width_); LOG_PIN(" CS Pin: ", this->cs_); @@ -176,6 +225,6 @@ void QspiAmoLed::dump_config() { ESP_LOGCONFIG(TAG, " SPI Data rate: %dMHz", (unsigned) (this->data_rate_ / 1000000)); } -} // namespace qspi_amoled +} // namespace qspi_dbi } // namespace esphome #endif diff --git a/esphome/components/qspi_amoled/qspi_amoled.h b/esphome/components/qspi_dbi/qspi_dbi.h similarity index 80% rename from esphome/components/qspi_amoled/qspi_amoled.h rename to esphome/components/qspi_dbi/qspi_dbi.h index c766b4e685..ebb65a8a05 100644 --- a/esphome/components/qspi_amoled/qspi_amoled.h +++ b/esphome/components/qspi_dbi/qspi_dbi.h @@ -14,11 +14,12 @@ #include "esp_lcd_panel_rgb.h" namespace esphome { -namespace qspi_amoled { +namespace qspi_dbi { -constexpr static const char *const TAG = "display.qspi_amoled"; +constexpr static const char *const TAG = "display.qspi_dbi"; static const uint8_t SW_RESET_CMD = 0x01; static const uint8_t SLEEP_OUT = 0x11; +static const uint8_t NORON = 0x13; static const uint8_t INVERT_OFF = 0x20; static const uint8_t INVERT_ON = 0x21; static const uint8_t ALL_ON = 0x23; @@ -42,6 +43,7 @@ static const uint8_t MADCTL_MV = 0x20; ///< Bit 5 Reverse Mode static const uint8_t MADCTL_RGB = 0x00; ///< Bit 3 Red-Green-Blue pixel order static const uint8_t MADCTL_BGR = 0x08; ///< Bit 3 Blue-Green-Red pixel order +static const uint8_t DELAY_FLAG = 0xFF; // store a 16 bit value in a buffer, big endian. static inline void put16_be(uint8_t *buf, uint16_t value) { buf[0] = value >> 8; @@ -49,15 +51,16 @@ static inline void put16_be(uint8_t *buf, uint16_t value) { } enum Model { + CUSTOM, RM690B0, RM67162, }; -class QspiAmoLed : public display::DisplayBuffer, - public spi::SPIDevice { +class QspiDbi : public display::DisplayBuffer, + public spi::SPIDevice { public: - void set_model(Model model) { this->model_ = model; } + void set_model(const char *model) { this->model_ = model; } void update() override; void setup() override; display::ColorOrder get_color_mode() { return this->color_mode_; } @@ -93,17 +96,27 @@ class QspiAmoLed : public display::DisplayBuffer, this->offset_x_ = offset_x; this->offset_y_ = offset_y; } + + void set_draw_from_origin(bool draw_from_origin) { this->draw_from_origin_ = draw_from_origin; } display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; } void dump_config() override; int get_width_internal() override { return this->width_; } int get_height_internal() override { return this->height_; } bool can_proceed() override { return this->setup_complete_; } + void add_init_sequence(const std::vector &sequence) { this->init_sequences_.push_back(sequence); } protected: + void check_buffer_() { + if (this->buffer_ == nullptr) + this->init_internal_(this->width_ * this->height_ * 2); + } + void write_sequence_(const std::vector &vec); void draw_absolute_pixel_internal(int x, int y, Color color) override; void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override; + void write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset, + int x_pad); /** * the RM67162 in quad SPI mode seems to work like this (not in the datasheet, this is deduced from the * sample code.) @@ -122,11 +135,7 @@ class QspiAmoLed : public display::DisplayBuffer, * @param bytes * @param len */ - void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len) { - this->enable(); - this->write_cmd_addr_data(8, 0x02, 24, cmd << 8, bytes, len); - this->disable(); - } + void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len); void write_command_(uint8_t cmd, uint8_t data) { this->write_command_(cmd, &data, 1); } void write_command_(uint8_t cmd) { this->write_command_(cmd, &cmd, 0); } @@ -136,8 +145,8 @@ class QspiAmoLed : public display::DisplayBuffer, GPIOPin *reset_pin_{nullptr}; GPIOPin *enable_pin_{nullptr}; - uint16_t x_low_{0}; - uint16_t y_low_{0}; + uint16_t x_low_{1}; + uint16_t y_low_{1}; uint16_t x_high_{0}; uint16_t y_high_{0}; bool setup_complete_{}; @@ -151,12 +160,14 @@ class QspiAmoLed : public display::DisplayBuffer, bool swap_xy_{}; bool mirror_x_{}; bool mirror_y_{}; + bool draw_from_origin_{false}; uint8_t brightness_{0xD0}; - Model model_{RM690B0}; + const char *model_{"Unknown"}; + std::vector> init_sequences_{}; esp_lcd_panel_handle_t handle_{}; }; -} // namespace qspi_amoled +} // namespace qspi_dbi } // namespace esphome #endif diff --git a/esphome/components/st7701s/display.py b/esphome/components/st7701s/display.py index 9310e9d760..e73c2467da 100644 --- a/esphome/components/st7701s/display.py +++ b/esphome/components/st7701s/display.py @@ -18,6 +18,7 @@ from esphome.const import ( CONF_HSYNC_PIN, CONF_ID, CONF_IGNORE_STRAPPING_WARNING, + CONF_INIT_SEQUENCE, CONF_INVERT_COLORS, CONF_LAMBDA, CONF_MIRROR_X, @@ -35,7 +36,6 @@ from esphome.core import TimePeriod from .init_sequences import ST7701S_INITS, cmd -CONF_INIT_SEQUENCE = "init_sequence" CONF_DE_PIN = "de_pin" CONF_PCLK_PIN = "pclk_pin" diff --git a/esphome/const.py b/esphome/const.py index 7665c77a32..a3a4318d69 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -398,6 +398,7 @@ CONF_INCLUDES = "includes" CONF_INDEX = "index" CONF_INDOOR = "indoor" CONF_INFRARED = "infrared" +CONF_INIT_SEQUENCE = "init_sequence" CONF_INITIAL_MODE = "initial_mode" CONF_INITIAL_OPTION = "initial_option" CONF_INITIAL_STATE = "initial_state" diff --git a/tests/components/qspi_amoled/common.yaml b/tests/components/qspi_dbi/common.yaml similarity index 73% rename from tests/components/qspi_amoled/common.yaml rename to tests/components/qspi_dbi/common.yaml index 01d1a63bcb..655af304af 100644 --- a/tests/components/qspi_amoled/common.yaml +++ b/tests/components/qspi_dbi/common.yaml @@ -5,7 +5,7 @@ spi: data_pins: [14, 10, 16, 12] display: - - platform: qspi_amoled + - platform: qspi_dbi model: RM690B0 data_rate: 80MHz spi_mode: mode0 @@ -20,9 +20,10 @@ display: reset_pin: 13 enable_pin: 9 - - platform: qspi_amoled - model: RM67162 + - platform: qspi_dbi + model: CUSTOM id: main_lcd + draw_from_origin: true dimensions: height: 240 width: 536 @@ -34,3 +35,10 @@ display: cs_pin: 6 reset_pin: 17 enable_pin: 38 + init_sequence: + - [0x3A, 0x66] + - [0x11] + - delay 120ms + - [0x29] + - delay 20ms + diff --git a/tests/components/qspi_amoled/test.esp32-s3-idf.yaml b/tests/components/qspi_dbi/test.esp32-s3-idf.yaml similarity index 100% rename from tests/components/qspi_amoled/test.esp32-s3-idf.yaml rename to tests/components/qspi_dbi/test.esp32-s3-idf.yaml