ESPHome  2024.10.2
st7789v.h
Go to the documentation of this file.
1 #pragma once
2 
6 #ifdef USE_POWER_SUPPLY
8 #endif
9 
10 namespace esphome {
11 namespace st7789v {
12 
13 static const uint8_t ST7789_NOP = 0x00; // No Operation
14 static const uint8_t ST7789_SWRESET = 0x01; // Software Reset
15 static const uint8_t ST7789_RDDID = 0x04; // Read Display ID
16 static const uint8_t ST7789_RDDST = 0x09; // Read Display Status
17 static const uint8_t ST7789_RDDPM = 0x0A; // Read Display Power Mode
18 static const uint8_t ST7789_RDDMADCTL = 0x0B; // Read Display MADCTL
19 static const uint8_t ST7789_RDDCOLMOD = 0x0C; // Read Display Pixel Format
20 static const uint8_t ST7789_RDDIM = 0x0D; // Read Display Image Mode
21 static const uint8_t ST7789_RDDSM = 0x0E; // Read Display Signal Mod
22 static const uint8_t ST7789_RDDSDR = 0x0F; // Read Display Self-Diagnostic Result
23 static const uint8_t ST7789_SLPIN = 0x10; // Sleep in
24 static const uint8_t ST7789_SLPOUT = 0x11; // Sleep Out
25 static const uint8_t ST7789_PTLON = 0x12; // Partial Display Mode On
26 static const uint8_t ST7789_NORON = 0x13; // Normal Display Mode On
27 static const uint8_t ST7789_INVOFF = 0x20; // Display Inversion Off
28 static const uint8_t ST7789_INVON = 0x21; // Display Inversion On
29 static const uint8_t ST7789_GAMSET = 0x26; // Gamma Set
30 static const uint8_t ST7789_DISPOFF = 0x28; // Display Off
31 static const uint8_t ST7789_DISPON = 0x29; // Display On
32 static const uint8_t ST7789_CASET = 0x2A; // Column Address Set
33 static const uint8_t ST7789_RASET = 0x2B; // Row Address Set
34 static const uint8_t ST7789_RAMWR = 0x2C; // Memory Write
35 static const uint8_t ST7789_RAMRD = 0x2E; // Memory Read
36 static const uint8_t ST7789_PTLAR = 0x30; // Partial Area
37 static const uint8_t ST7789_VSCRDEF = 0x33; // Vertical Scrolling Definition
38 static const uint8_t ST7789_TEOFF = 0x34; // Tearing Effect Line Off
39 static const uint8_t ST7789_TEON = 0x35; // Tearing Effect Line On
40 static const uint8_t ST7789_MADCTL = 0x36; // Memory Data Access Control
41 static const uint8_t ST7789_VSCSAD = 0x37; // Vertical Scroll Start Address of RAM
42 static const uint8_t ST7789_IDMOFF = 0x38; // Idle Mode Off
43 static const uint8_t ST7789_IDMON = 0x39; // Idle Mode On
44 static const uint8_t ST7789_COLMOD = 0x3A; // Interface Pixel Format
45 static const uint8_t ST7789_WRMEMC = 0x3C; // Write Memory Continue
46 static const uint8_t ST7789_RDMEMC = 0x3E; // Read Memory Continue
47 static const uint8_t ST7789_STE = 0x44; // Set Tear Scanline
48 static const uint8_t ST7789_GSCAN = 0x45; // Get Scanline
49 static const uint8_t ST7789_WRDISBV = 0x51; // Write Display Brightness
50 static const uint8_t ST7789_RDDISBV = 0x52; // Read Display Brightness Value
51 static const uint8_t ST7789_WRCTRLD = 0x53; // Write CTRL Display
52 static const uint8_t ST7789_RDCTRLD = 0x54; // Read CTRL Value Display
53 static const uint8_t ST7789_WRCACE = 0x55; // Write Content Adaptive Brightness Control and Color Enhancement
54 static const uint8_t ST7789_RDCABC = 0x56; // Read Content Adaptive Brightness Control
55 static const uint8_t ST7789_WRCABCMB = 0x5E; // Write CABC Minimum Brightnes
56 static const uint8_t ST7789_RDCABCMB = 0x5F; // Read CABC Minimum Brightnes
57 static const uint8_t ST7789_RDABCSDR = 0x68; // Read Automatic Brightness Control Self-Diagnostic Result
58 static const uint8_t ST7789_RDID1 = 0xDA; // Read ID1
59 static const uint8_t ST7789_RDID2 = 0xDB; // Read ID2
60 static const uint8_t ST7789_RDID3 = 0xDC; // Read ID3
61 static const uint8_t ST7789_RAMCTRL = 0xB0; // RAM Control
62 static const uint8_t ST7789_RGBCTRL = 0xB1; // RGB Interface Control
63 static const uint8_t ST7789_PORCTRL = 0xB2; // Porch Setting
64 static const uint8_t ST7789_FRCTRL1 = 0xB3; // Frame Rate Control 1 (In partial mode/ idle colors)
65 static const uint8_t ST7789_PARCTRL = 0xB5; // Partial mode Control
66 static const uint8_t ST7789_GCTRL = 0xB7; // Gate Control
67 static const uint8_t ST7789_GTADJ = 0xB8; // Gate On Timing Adjustment
68 static const uint8_t ST7789_DGMEN = 0xBA; // Digital Gamma Enable
69 static const uint8_t ST7789_VCOMS = 0xBB; // VCOMS Setting
70 static const uint8_t ST7789_LCMCTRL = 0xC0; // LCM Control
71 static const uint8_t ST7789_IDSET = 0xC1; // ID Code Setting
72 static const uint8_t ST7789_VDVVRHEN = 0xC2; // VDV and VRH Command Enable
73 static const uint8_t ST7789_VRHS = 0xC3; // VRH Set
74 static const uint8_t ST7789_VDVS = 0xC4; // VDV Set
75 static const uint8_t ST7789_VCMOFSET = 0xC5; // VCOMS Offset Set
76 static const uint8_t ST7789_FRCTRL2 = 0xC6; // Frame Rate Control in Normal Mode
77 static const uint8_t ST7789_CABCCTRL = 0xC7; // CABC Control
78 static const uint8_t ST7789_REGSEL1 = 0xC8; // Register Value Selection 1
79 static const uint8_t ST7789_REGSEL2 = 0xCA; // Register Value Selection
80 static const uint8_t ST7789_PWMFRSEL = 0xCC; // PWM Frequency Selection
81 static const uint8_t ST7789_PWCTRL1 = 0xD0; // Power Control 1
82 static const uint8_t ST7789_VAPVANEN = 0xD2; // Enable VAP/VAN signal output
83 static const uint8_t ST7789_CMD2EN = 0xDF; // Command 2 Enable
84 static const uint8_t ST7789_PVGAMCTRL = 0xE0; // Positive Voltage Gamma Control
85 static const uint8_t ST7789_NVGAMCTRL = 0xE1; // Negative Voltage Gamma Control
86 static const uint8_t ST7789_DGMLUTR = 0xE2; // Digital Gamma Look-up Table for Red
87 static const uint8_t ST7789_DGMLUTB = 0xE3; // Digital Gamma Look-up Table for Blue
88 static const uint8_t ST7789_GATECTRL = 0xE4; // Gate Control
89 static const uint8_t ST7789_SPI2EN = 0xE7; // SPI2 Enable
90 static const uint8_t ST7789_PWCTRL2 = 0xE8; // Power Control 2
91 static const uint8_t ST7789_EQCTRL = 0xE9; // Equalize time control
92 static const uint8_t ST7789_PROMCTRL = 0xEC; // Program Mode Control
93 static const uint8_t ST7789_PROMEN = 0xFA; // Program Mode Enable
94 static const uint8_t ST7789_NVMSET = 0xFC; // NVM Setting
95 static const uint8_t ST7789_PROMACT = 0xFE; // Program action
96 
97 // Flags for ST7789_MADCTL
98 static const uint8_t ST7789_MADCTL_MY = 0x80;
99 static const uint8_t ST7789_MADCTL_MX = 0x40;
100 static const uint8_t ST7789_MADCTL_MV = 0x20;
101 static const uint8_t ST7789_MADCTL_ML = 0x10;
102 static const uint8_t ST7789_MADCTL_RGB = 0x00;
103 static const uint8_t ST7789_MADCTL_BGR = 0x08;
104 static const uint8_t ST7789_MADCTL_MH = 0x04;
105 static const uint8_t ST7789_MADCTL_SS = 0x02;
106 static const uint8_t ST7789_MADCTL_GS = 0x01;
107 
108 static const uint8_t ST7789_MADCTL_COLOR_ORDER = ST7789_MADCTL_BGR;
109 
111  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
112  spi::DATA_RATE_20MHZ> {
113  public:
114  void set_model_str(const char *model_str);
115  void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; }
116  void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
117  void set_backlight_pin(GPIOPin *backlight_pin) { this->backlight_pin_ = backlight_pin; }
118 #ifdef USE_POWER_SUPPLY
119  void set_power_supply(power_supply::PowerSupply *power_supply) { this->power_.set_parent(power_supply); }
120 #endif
121 
122  void set_eightbitcolor(bool eightbitcolor) { this->eightbitcolor_ = eightbitcolor; }
123  void set_height(uint32_t height) { this->height_ = height; }
124  void set_width(uint16_t width) { this->width_ = width; }
125  void set_offset_height(uint32_t offset_height) { this->offset_height_ = offset_height; }
126  void set_offset_width(uint16_t offset_width) { this->offset_width_ = offset_width; }
127 
128  // ========== INTERNAL METHODS ==========
129  // (In most use cases you won't need these)
130  void setup() override;
131  void dump_config() override;
132  float get_setup_priority() const override;
133  void update() override;
134 
135  void write_display_data();
136 
138 
139  protected:
140  GPIOPin *dc_pin_{nullptr};
141  GPIOPin *reset_pin_{nullptr};
143 #ifdef USE_POWER_SUPPLY
145 #endif
146 
147  bool eightbitcolor_{false};
148  uint16_t height_{0};
149  uint16_t width_{0};
150  uint16_t offset_height_{0};
151  uint16_t offset_width_{0};
152 
153  void init_reset_();
154  void backlight_(bool onoff);
155  void write_command_(uint8_t value);
156  void write_data_(uint8_t value);
157  void write_addr_(uint16_t addr1, uint16_t addr2);
158  void write_color_(uint16_t color, uint16_t size);
159 
160  int get_height_internal() override { return this->height_; }
161  int get_width_internal() override { return this->width_; }
162  size_t get_buffer_length_();
163 
164  void draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
165 
166  void draw_absolute_pixel_internal(int x, int y, Color color) override;
167 
168  const char *model_str_;
169 };
170 
171 } // namespace st7789v
172 } // namespace esphome
void setup() override
Definition: st7789v.cpp:10
display::DisplayType get_display_type() override
Definition: st7789v.h:137
void set_offset_width(uint16_t offset_width)
Definition: st7789v.h:126
float get_setup_priority() const override
Definition: st7789v.cpp:142
void dump_config() override
Definition: st7789v.cpp:123
const char * model_str_
Definition: st7789v.h:168
void update() override
Definition: st7789v.cpp:144
uint16_t x
Definition: tt21100.cpp:17
void write_color_(uint16_t color, uint16_t size)
Definition: st7789v.cpp:246
void set_model_str(const char *model_str)
Definition: st7789v.cpp:149
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition: st7789v.cpp:292
GPIOPin * backlight_pin_
Definition: st7789v.h:142
void set_height(uint32_t height)
Definition: st7789v.h:123
uint16_t y
Definition: tt21100.cpp:18
int get_width_internal() override
Definition: st7789v.h:161
The SPIDevice is what components using the SPI will create.
Definition: spi.h:391
void set_offset_height(uint32_t offset_height)
Definition: st7789v.h:125
void draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Definition: st7789v.cpp:271
void write_command_(uint8_t value)
Definition: st7789v.cpp:220
void write_data_(uint8_t value)
Definition: st7789v.cpp:228
void set_parent(PowerSupply *parent)
Definition: power_supply.h:47
void set_backlight_pin(GPIOPin *backlight_pin)
Definition: st7789v.h:117
void set_width(uint16_t width)
Definition: st7789v.h:124
void write_addr_(uint16_t addr1, uint16_t addr2)
Definition: st7789v.cpp:235
void set_power_supply(power_supply::PowerSupply *power_supply)
Definition: st7789v.h:119
void set_eightbitcolor(bool eightbitcolor)
Definition: st7789v.h:122
void set_dc_pin(GPIOPin *dc_pin)
Definition: st7789v.h:115
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_reset_pin(GPIOPin *reset_pin)
Definition: st7789v.h:116
power_supply::PowerSupplyRequester power_
Definition: st7789v.h:144
void backlight_(bool onoff)
Definition: st7789v.cpp:213
int get_height_internal() override
Definition: st7789v.h:160