19static constexpr size_t MIPI_SPI_MAX_CMD_LOG_BYTES = 64;
22static constexpr uint8_t SW_RESET_CMD = 0x01;
23static constexpr uint8_t SLEEP_OUT = 0x11;
24static constexpr uint8_t NORON = 0x13;
25static constexpr uint8_t INVERT_OFF = 0x20;
26static constexpr uint8_t INVERT_ON = 0x21;
27static constexpr uint8_t ALL_ON = 0x23;
28static constexpr uint8_t WRAM = 0x24;
29static constexpr uint8_t MIPI = 0x26;
30static constexpr uint8_t DISPLAY_ON = 0x29;
31static constexpr uint8_t RASET = 0x2B;
32static constexpr uint8_t CASET = 0x2A;
33static constexpr uint8_t WDATA = 0x2C;
34static constexpr uint8_t TEON = 0x35;
35static constexpr uint8_t MADCTL_CMD = 0x36;
36static constexpr uint8_t PIXFMT = 0x3A;
37static constexpr uint8_t BRIGHTNESS = 0x51;
38static constexpr uint8_t SWIRE1 = 0x5A;
39static constexpr uint8_t SWIRE2 = 0x5B;
40static constexpr uint8_t PAGESEL = 0xFE;
42static constexpr uint8_t MADCTL_MY = 0x80;
43static constexpr uint8_t MADCTL_MX = 0x40;
44static constexpr uint8_t MADCTL_MV = 0x20;
45static constexpr uint8_t MADCTL_RGB = 0x00;
46static constexpr uint8_t MADCTL_BGR = 0x08;
47static constexpr uint8_t MADCTL_XFLIP = 0x02;
48static constexpr uint8_t MADCTL_YFLIP = 0x01;
49static constexpr uint16_t MADCTL_FLIP_FLAG = 0x100;
51static constexpr uint8_t DELAY_FLAG = 0xFF;
68void internal_dump_config(
const char *model,
int width,
int height,
int offset_width,
int offset_height, uint8_t madctl,
69 bool invert_colors,
int display_bits,
bool is_big_endian,
const optional<uint8_t> &brightness,
71 bool has_hardware_rotation);
98 int WIDTH,
int HEIGHT,
int OFFSET_WIDTH,
int OFFSET_HEIGHT,
int PAD_WIDTH,
int PAD_HEIGHT, uint16_t MADCTL,
99 bool HAS_HARDWARE_ROTATION>
102 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
103 spi::DATA_RATE_1MHZ> {
124 if constexpr (HAS_HARDWARE_ROTATION) {
141 if (this->
dc_pin_ !=
nullptr) {
147 pin->digital_write(
true);
153 auto when =
millis() + 120;
157 while (index !=
len) {
158 if (
len - index < 2) {
159 esph_log_e(TAG,
"Malformed init sequence");
165 if (
x == DELAY_FLAG) {
169 esph_log_d(TAG,
"Delay %dms", cmd);
172 uint8_t num_args =
x & 0x7F;
173 if (
len - index < num_args) {
174 esph_log_e(TAG,
"Malformed init sequence");
194 this->
data_rate_, BUS_TYPE, HAS_HARDWARE_ROTATION);
204 if (w <= 0 ||
h <= 0)
217 if constexpr (HAS_HARDWARE_ROTATION)
222 if constexpr (HAS_HARDWARE_ROTATION)
273 for (
size_t i = 0; i !=
len; i++) {
292 uint8_t madctl = (uint8_t) MADCTL;
293 constexpr bool use_flips = (MADCTL & MADCTL_FLIP_FLAG) != 0;
294 constexpr uint8_t x_mask = use_flips ? MADCTL_XFLIP : MADCTL_MX;
295 constexpr uint8_t y_mask = use_flips ? MADCTL_YFLIP : MADCTL_MY;
296 if constexpr (HAS_HARDWARE_ROTATION) {
314 esph_log_d(TAG,
"Setting MADCTL for rotation %d, value %X", this->
rotation_, madctl);
319 if constexpr (HAS_HARDWARE_ROTATION) {
322 return OFFSET_HEIGHT;
335 if constexpr (HAS_HARDWARE_ROTATION) {
347 return OFFSET_HEIGHT;
352 esph_log_v(TAG,
"Set addr %d/%d, %d/%d", x1, y1, x2, y2);
398 for (
size_t y = 0;
y !=
h;
y++) {
420 esph_log_e(TAG,
"Unsupported color depth, bit order or rotation");
434 if constexpr (BUFFERPIXEL == DISPLAYPIXEL) {
439 uint8_t dbuffer[DISPLAYPIXEL * 48];
440 uint8_t *dptr = dbuffer;
442 for (
size_t y = 0;
y !=
static_cast<size_t>(
h);
y++) {
443 for (
size_t x = 0;
x !=
static_cast<size_t>(
w);
x++) {
444 auto color_val =
ptr[
y * stride +
x];
447 if constexpr (IS_BIG_ENDIAN) {
448 *dptr++ = color_val & 0xF8;
449 *dptr++ = ((color_val & 0x7) << 5) | (color_val & 0xE000) >> 11;
450 *dptr++ = (color_val >> 5) & 0xF8;
452 *dptr++ = (color_val >> 8) & 0xF8;
453 *dptr++ = (color_val & 0x7E0) >> 3;
454 *dptr++ = color_val << 3;
458 *dptr++ = color_val << 6;
459 *dptr++ = (color_val & 0x1C) << 3;
460 *dptr++ = (color_val & 0xE0);
462 if constexpr (IS_BIG_ENDIAN) {
463 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
464 *dptr++ = (color_val & 3) << 3;
466 *dptr++ = (color_val & 3) << 3;
467 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
471 if (dptr == dbuffer +
sizeof(dbuffer)) {
478 if (dptr != dbuffer) {
520 uint16_t WIDTH, uint16_t HEIGHT,
int OFFSET_WIDTH,
int OFFSET_HEIGHT,
int PAD_WIDTH,
int PAD_HEIGHT,
521 uint16_t MADCTL,
bool HAS_HARDWARE_ROTATION,
int FRACTION,
unsigned ROUNDING>
523 :
public MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH,
524 OFFSET_HEIGHT, PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION> {
534 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
535 PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION>
::dump_config();
538 " Buffer pixels: %d bits\n"
539 " Buffer fraction: 1/%d\n"
540 " Buffer bytes: %zu\n"
541 " Draw rounding: %u",
542 this->
rotation_, BUFFERPIXEL * 8, FRACTION,
547 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
548 PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION>
::setup();
551 if (this->
buffer_ ==
nullptr) {
552 this->
mark_failed(LOG_STR(
"Buffer allocation failed"));
557#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
567#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
577 }
else if (this->
page_ !=
nullptr) {
583#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
584 esph_log_v(TAG,
"Drawing from line %d took %dms", this->
start_line_,
millis() - lap);
589 esph_log_v(TAG,
"x_low %d, y_low %d, x_high %d, y_high %d", this->
x_low_, this->
y_low_, this->
x_high_,
606#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
607 esph_log_v(TAG,
"Write to display took %dms",
millis() - lap);
611#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
612 esph_log_v(TAG,
"Total update took %dms",
millis() - now);
623 if constexpr (not HAS_HARDWARE_ROTATION) {
688 const int first_col = std::max(0, -x_start);
690 const int first_row = std::max<int>(0, this->
start_line_ - y_start);
691 const int end_row = std::min<int>(
h, this->
end_line_ - y_start);
692 if (first_col >= end_col || first_row >= end_row)
696 for (
int y = first_row;
y != end_row;
y++) {
697 memcpy(this->
buffer_ + (y_start +
y - this->
start_line_) * buffer_stride + x_start + first_col,
699 (end_col - first_col) *
sizeof(BUFFERTYPE));
701 this->
x_low_ = std::min<int>(this->
x_low_, x_start + first_col);
702 this->
y_low_ = std::min<int>(this->
y_low_, y_start + first_row);
703 this->
x_high_ = std::max<int>(this->
x_high_, x_start + end_col - 1);
704 this->
y_high_ = std::max<int>(this->
y_high_, y_start + end_row - 1);
714 return (color.
red & 0xE0) | (color.
g & 0xE0) >> 3 | color.
b >> 6;
716 if constexpr (IS_BIG_ENDIAN) {
717 return (color.
r & 0xF8) | color.
g >> 5 | (color.
g & 0x1C) << 11 | (color.
b & 0xF8) << 5;
719 return (color.
r & 0xF8) << 8 | (color.
g & 0xFC) << 3 | color.
b >> 3;
722 return static_cast<BUFFERTYPE
>(0);
void mark_failed()
Mark this component as failed.
virtual void digital_write(bool value)=0
An STL allocator that uses SPI or internal RAM.
virtual bool pixel_axes_swapped() const
True when draw_pixel_at() swaps x and y before writing, so pixels that are neighbours along x land a ...
int int int const uint8_t ColorOrder ColorBitness bitness
virtual void clear()
Clear the entire screen by filling it with OFF pixels.
int int int const uint8_t ColorOrder ColorBitness bool int int y_offset
virtual void fill(Color color)
Fill the entire screen with the given color.
int int int const uint8_t ColorOrder ColorBitness bool int int int x_pad
int int int const uint8_t * ptr
bool ESPHOME_ALWAYS_INLINE is_point_clipped(int x, int y) const
Whether (x, y) falls outside the active clipping rectangle.
int int int const uint8_t ColorOrder ColorBitness bool big_endian
Rect get_clipping() const
Get the current the clipping rectangle.
int int int const uint8_t ColorOrder ColorBitness bool int x_offset
DisplayRotation rotation_
virtual void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Draws a block for draw_pixels_at(); the parameters are the same.
const display_writer_t & get_writer() const
virtual void set_brightness(uint8_t brightness)=0
Class for MIPI SPI displays with a buffer.
void draw_pixel_at(int x, int y, Color color) override
static constexpr size_t round_buffer(size_t size)
void draw_pixels_slow(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
bool pixel_axes_swapped() const override
void fill(Color color) override
static BUFFERTYPE convert_color(const Color &color)
void dump_config() override
Base class for MIPI SPI displays.
uint16_t get_offset_height_() const
void write_to_display_(int x_start, int y_start, int w, int h, const BUFFERTYPE *ptr, int x_offset, int y_offset, int x_pad)
Writes a buffer to the display.
int get_width_internal() override
void set_init_sequence(const uint8_t *sequence, size_t len)
const uint8_t * init_sequence_
void set_brightness(uint8_t brightness) override
static PixelMode get_pixel_mode(display::ColorBitness bitness)
void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len)
void set_reset_pin(GPIOPin *reset_pin)
optional< uint8_t > brightness_
void set_invert_colors(bool invert_colors)
int get_height_internal() override
void dump_config() override
uint16_t get_offset_width_() const
int get_height() override
void draw_pixel_at(int x, int y, Color color) override
void write_command_(uint8_t cmd)
void write_display_data_(const uint8_t *ptr, size_t w, size_t h, size_t stride)
Writes a buffer to the display.
display::DisplayType get_display_type() override
void set_dc_pin(GPIOPin *dc_pin)
void set_rotation(display::DisplayRotation rotation) override
void write_command_(uint8_t cmd, uint8_t data)
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
bool is_native_block_(display::ColorBitness bitness, bool big_endian) const
size_t init_sequence_len_
void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
void set_model(const char *model)
virtual void draw_pixels_slow(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
std::vector< GPIOPin * > enable_pins_
void set_enable_pins(std::vector< GPIOPin * > enable_pins)
The SPIDevice is what components using the SPI will create.
void spi_setup() override
void write_byte(uint8_t data)
void write_array(const uint8_t *data, size_t length)
void write_cmd_addr_data(size_t cmd_bits, uint32_t cmd, size_t addr_bits, uint32_t address, const uint8_t *data, size_t length, uint8_t bus_width=1)
@ DISPLAY_ROTATION_0_DEGREES
@ DISPLAY_ROTATION_270_DEGREES
@ DISPLAY_ROTATION_180_DEGREES
@ DISPLAY_ROTATION_90_DEGREES
constexpr bool rotation_swaps_axes(DisplayRotation rotation)
Whether a rotation swaps the x and y axes.
void internal_dump_config(const char *model, int width, int height, int offset_width, int offset_height, uint8_t madctl, bool invert_colors, int display_bits, bool is_big_endian, const optional< uint8_t > &brightness, GPIOPin *cs, GPIOPin *reset, GPIOPin *dc, int spi_mode, uint32_t data_rate, int bus_width, bool has_hardware_rotation)
ESPHOME_LOG_TAG(TAG, "display.mipi_spi")
T clamp_at_most(T value, U max)
void progmem_memcpy(void *dst, const void *src, size_t len)
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
T clamp_at_least(T value, U min)
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.
uint8_t progmem_read_byte(const uint8_t *addr)