ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mipi_dsi.h
Go to the documentation of this file.
1//
2// Created by Clyde Stubbs on 29/10/2023.
3//
4#pragma once
5
6// only applicable on ESP32-P4
7#ifdef USE_ESP32_VARIANT_ESP32P4
11#include "esphome/core/log.h"
12#include "esphome/core/gpio.h"
13
16#include "esp_lcd_panel_ops.h"
17#include "esp_lcd_panel_io.h"
18
19#include "esp_lcd_mipi_dsi.h"
20
21#include <utility>
22
24
25ESPHOME_LOG_TAG(TAG, "display.mipi_dsi");
26const uint8_t SW_RESET_CMD = 0x01;
27const uint8_t SLEEP_OUT = 0x11;
28const uint8_t SDIR_CMD = 0xC7;
29const uint8_t MADCTL_CMD = 0x36;
30const uint8_t INVERT_OFF = 0x20;
31const uint8_t INVERT_ON = 0x21;
32const uint8_t DISPLAY_ON = 0x29;
33const uint8_t CMD2_BKSEL = 0xFF;
34const uint8_t DELAY_FLAG = 0xFF;
35const uint8_t MADCTL_BGR = 0x08;
36const uint8_t MADCTL_MX = 0x40;
37const uint8_t MADCTL_MY = 0x80;
38const uint8_t MADCTL_MV = 0x20; // row/column swap
39const uint8_t MADCTL_XFLIP = 0x02; // Mirror the display horizontally
40const uint8_t MADCTL_YFLIP = 0x01; // Mirror the display vertically
41
42class MipiDsi final : public display::Display {
43 public:
44 MipiDsi(size_t width, size_t height, display::ColorBitness color_depth, uint8_t pixel_mode)
45 : width_(width), height_(height), color_depth_(color_depth), pixel_mode_(pixel_mode) {}
47 void set_invert_colors(bool invert_colors) { this->invert_colors_ = invert_colors; }
49
50 void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
51 void set_enable_pins(std::vector<GPIOPin *> enable_pins) { this->enable_pins_ = std::move(enable_pins); }
52 void set_pclk_frequency(float pclk_frequency) { this->pclk_frequency_ = pclk_frequency; }
53 int get_width_internal() override { return this->width_; }
54 int get_height_internal() override { return this->height_; }
55 void set_hsync_back_porch(uint16_t hsync_back_porch) { this->hsync_back_porch_ = hsync_back_porch; }
56 void set_hsync_front_porch(uint16_t hsync_front_porch) { this->hsync_front_porch_ = hsync_front_porch; }
57 void set_hsync_pulse_width(uint16_t hsync_pulse_width) { this->hsync_pulse_width_ = hsync_pulse_width; }
58 void set_vsync_pulse_width(uint16_t vsync_pulse_width) { this->vsync_pulse_width_ = vsync_pulse_width; }
59 void set_vsync_back_porch(uint16_t vsync_back_porch) { this->vsync_back_porch_ = vsync_back_porch; }
60 void set_vsync_front_porch(uint16_t vsync_front_porch) { this->vsync_front_porch_ = vsync_front_porch; }
61 void set_init_sequence(const uint8_t *init_sequence, size_t len) {
62 this->init_sequence_ = init_sequence;
63 this->init_sequence_len_ = len;
64 }
65 void set_model(const char *model) { this->model_ = model; }
66 void set_lane_bit_rate(float lane_bit_rate) { this->lane_bit_rate_ = lane_bit_rate; }
67 void set_lanes(uint8_t lanes) { this->lanes_ = lanes; }
68
69 void smark_failed(const LogString *message, esp_err_t err);
70
71 void update() override;
72
73 void setup() override;
74
75 void draw_pixel_at(int x, int y, Color color) override;
76 void fill(Color color) override;
77 int get_width() override;
78 int get_height() override;
79
80 void dump_config() override;
81
82 protected:
83 void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness,
84 bool big_endian, int x_offset, int y_offset, int x_pad) override;
85
86 void write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset,
87 int x_pad);
91 // Maps drawn coordinates to the buffer the way draw_pixel_at() stores them
92 void rotate_(int &x, int &y) const {
93 switch (this->rotation_) {
95 break;
97 std::swap(x, y);
98 x = this->width_ - x - 1;
99 break;
101 x = this->width_ - x - 1;
102 y = this->height_ - y - 1;
103 break;
105 std::swap(x, y);
106 y = this->height_ - y - 1;
107 break;
108 }
109 }
110 void mark_dirty_(int x_start, int y_start, int w, int h);
112 uint16_t to_panel_565_(Color color) const {
113 const uint16_t rgb565 = display::ColorUtil::color_to_565(color);
114 return this->big_endian_ ? convert_big_endian(rgb565) : rgb565;
115 }
117 std::vector<GPIOPin *> enable_pins_{};
118 size_t width_{};
119 size_t height_{};
120 uint16_t hsync_pulse_width_ = 10;
121 uint16_t hsync_back_porch_ = 10;
122 uint16_t hsync_front_porch_ = 20;
123 uint16_t vsync_pulse_width_ = 10;
124 uint16_t vsync_back_porch_ = 10;
125 uint16_t vsync_front_porch_ = 10;
126 const char *model_{"Unknown"};
127 // Shared PROGMEM table
128 const uint8_t *init_sequence_{nullptr};
130 float pclk_frequency_ = 16; // in MHz
131 float lane_bit_rate_{1500}; // in Mbps
132 uint8_t lanes_{2}; // 1, 2, 3 or 4 lanes
133
136 bool big_endian_{false};
137 bool updating_{false};
138 uint8_t pixel_mode_{};
139
140 esp_lcd_panel_handle_t handle_{};
141 esp_lcd_dsi_bus_handle_t bus_handle_{};
142 esp_lcd_panel_io_handle_t io_handle_{};
143 SemaphoreHandle_t io_lock_{};
144 uint8_t *buffer_{nullptr};
145 uint16_t x_low_{1};
146 uint16_t y_low_{1};
147 uint16_t x_high_{0};
148 uint16_t y_high_{0};
149};
150
151} // namespace esphome::mipi_dsi
152#endif
int int int const uint8_t ColorOrder ColorBitness bitness
Definition display.h:392
int int int const uint8_t ColorOrder ColorBitness bool int int y_offset
Definition display.h:392
int int int const uint8_t ColorOrder ColorBitness bool int int int x_pad
Definition display.h:392
int int int const uint8_t * ptr
Definition display.h:391
int int int const uint8_t ColorOrder ColorBitness bool big_endian
Definition display.h:392
int int int const uint8_t ColorOrder ColorBitness bool int x_offset
Definition display.h:392
DisplayRotation rotation_
Definition display.h:863
void mark_dirty_(int x_start, int y_start, int w, int h)
display::ColorBitness color_depth_
Definition mipi_dsi.h:135
esp_lcd_panel_handle_t handle_
Definition mipi_dsi.h:140
int get_height_internal() override
Definition mipi_dsi.h:54
esp_lcd_panel_io_handle_t io_handle_
Definition mipi_dsi.h:142
uint16_t to_panel_565_(Color color) const
Pack a colour as the 565 value the panel's byte order expects in memory.
Definition mipi_dsi.h:112
int get_width_internal() override
Definition mipi_dsi.h:53
void draw_pixel_at(int x, int y, Color color) override
void set_init_sequence(const uint8_t *init_sequence, size_t len)
Definition mipi_dsi.h:61
void set_vsync_front_porch(uint16_t vsync_front_porch)
Definition mipi_dsi.h:60
std::vector< GPIOPin * > enable_pins_
Definition mipi_dsi.h:117
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)
void set_model(const char *model)
Definition mipi_dsi.h:65
void set_reset_pin(GPIOPin *reset_pin)
Definition mipi_dsi.h:50
void set_big_endian(bool big_endian)
Definition mipi_dsi.h:46
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
SemaphoreHandle_t io_lock_
Definition mipi_dsi.h:143
void smark_failed(const LogString *message, esp_err_t err)
void set_vsync_pulse_width(uint16_t vsync_pulse_width)
Definition mipi_dsi.h:58
void set_pclk_frequency(float pclk_frequency)
Definition mipi_dsi.h:52
esp_lcd_dsi_bus_handle_t bus_handle_
Definition mipi_dsi.h:141
void fill(Color color) override
void set_vsync_back_porch(uint16_t vsync_back_porch)
Definition mipi_dsi.h:59
const uint8_t * init_sequence_
Definition mipi_dsi.h:128
void set_hsync_front_porch(uint16_t hsync_front_porch)
Definition mipi_dsi.h:56
void set_enable_pins(std::vector< GPIOPin * > enable_pins)
Definition mipi_dsi.h:51
void set_invert_colors(bool invert_colors)
Definition mipi_dsi.h:47
void flush_dirty_()
Send the region touched since the last flush to the panel.
void dump_config() override
void set_hsync_pulse_width(uint16_t hsync_pulse_width)
Definition mipi_dsi.h:57
display::DisplayType get_display_type() override
Definition mipi_dsi.h:48
void rotate_(int &x, int &y) const
Definition mipi_dsi.h:92
void set_lanes(uint8_t lanes)
Definition mipi_dsi.h:67
MipiDsi(size_t width, size_t height, display::ColorBitness color_depth, uint8_t pixel_mode)
Definition mipi_dsi.h:44
void set_hsync_back_porch(uint16_t hsync_back_porch)
Definition mipi_dsi.h:55
void set_lane_bit_rate(float lane_bit_rate)
Definition mipi_dsi.h:66
const LogString * message
Definition component.cpp:35
@ DISPLAY_ROTATION_0_DEGREES
Definition display.h:136
@ DISPLAY_ROTATION_270_DEGREES
Definition display.h:139
@ DISPLAY_ROTATION_180_DEGREES
Definition display.h:138
@ DISPLAY_ROTATION_90_DEGREES
Definition display.h:137
const uint8_t MADCTL_CMD
Definition mipi_dsi.h:29
const uint8_t MADCTL_YFLIP
Definition mipi_dsi.h:40
const uint8_t INVERT_ON
Definition mipi_dsi.h:31
ESPHOME_LOG_TAG(TAG, "display.mipi_dsi")
const uint8_t SDIR_CMD
Definition mipi_dsi.h:28
const uint8_t DISPLAY_ON
Definition mipi_dsi.h:32
const uint8_t MADCTL_MV
Definition mipi_dsi.h:38
const uint8_t MADCTL_MX
Definition mipi_dsi.h:36
const uint8_t CMD2_BKSEL
Definition mipi_dsi.h:33
const uint8_t MADCTL_MY
Definition mipi_dsi.h:37
const uint8_t MADCTL_XFLIP
Definition mipi_dsi.h:39
const uint8_t INVERT_OFF
Definition mipi_dsi.h:30
const uint8_t DELAY_FLAG
Definition mipi_dsi.h:34
const uint8_t SLEEP_OUT
Definition mipi_dsi.h:27
const uint8_t SW_RESET_CMD
Definition mipi_dsi.h:26
const uint8_t MADCTL_BGR
Definition mipi_dsi.h:35
constexpr T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order.
Definition helpers.h:1098
const void size_t len
Definition hal.h:68
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6