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