ESPHome  2024.11.1
st7789v.cpp
Go to the documentation of this file.
1 #include "st7789v.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace st7789v {
6 
7 static const char *const TAG = "st7789v";
8 static const size_t TEMP_BUFFER_SIZE = 128;
9 
11  ESP_LOGCONFIG(TAG, "Setting up SPI ST7789V...");
12 #ifdef USE_POWER_SUPPLY
13  this->power_.request();
14  // the PowerSupply component takes care of post turn-on delay
15 #endif
16  this->spi_setup();
17  this->dc_pin_->setup(); // OUTPUT
18 
19  this->init_reset_();
20 
21  this->write_command_(ST7789_SLPOUT); // Sleep out
22  delay(120); // NOLINT
23 
24  this->write_command_(ST7789_NORON); // Normal display mode on
25 
26  // *** display and color format setting ***
27  this->write_command_(ST7789_MADCTL);
28  this->write_data_(ST7789_MADCTL_COLOR_ORDER);
29 
30  // JLX240 display datasheet
31  this->write_command_(0xB6);
32  this->write_data_(0x0A);
33  this->write_data_(0x82);
34 
35  this->write_command_(ST7789_COLMOD);
36  this->write_data_(0x55);
37  delay(10);
38 
39  // *** ST7789V Frame rate setting ***
40  this->write_command_(ST7789_PORCTRL);
41  this->write_data_(0x0c);
42  this->write_data_(0x0c);
43  this->write_data_(0x00);
44  this->write_data_(0x33);
45  this->write_data_(0x33);
46 
47  this->write_command_(ST7789_GCTRL); // Voltages: VGH / VGL
48  this->write_data_(0x35);
49 
50  // *** ST7789V Power setting ***
51  this->write_command_(ST7789_VCOMS);
52  this->write_data_(0x28); // JLX240 display datasheet
53 
54  this->write_command_(ST7789_LCMCTRL);
55  this->write_data_(0x0C);
56 
57  this->write_command_(ST7789_VDVVRHEN);
58  this->write_data_(0x01);
59  this->write_data_(0xFF);
60 
61  this->write_command_(ST7789_VRHS); // voltage VRHS
62  this->write_data_(0x10);
63 
64  this->write_command_(ST7789_VDVS);
65  this->write_data_(0x20);
66 
67  this->write_command_(ST7789_FRCTRL2);
68  this->write_data_(0x0f);
69 
70  this->write_command_(ST7789_PWCTRL1);
71  this->write_data_(0xa4);
72  this->write_data_(0xa1);
73 
74  // *** ST7789V gamma setting ***
75  this->write_command_(ST7789_PVGAMCTRL);
76  this->write_data_(0xd0);
77  this->write_data_(0x00);
78  this->write_data_(0x02);
79  this->write_data_(0x07);
80  this->write_data_(0x0a);
81  this->write_data_(0x28);
82  this->write_data_(0x32);
83  this->write_data_(0x44);
84  this->write_data_(0x42);
85  this->write_data_(0x06);
86  this->write_data_(0x0e);
87  this->write_data_(0x12);
88  this->write_data_(0x14);
89  this->write_data_(0x17);
90 
91  this->write_command_(ST7789_NVGAMCTRL);
92  this->write_data_(0xd0);
93  this->write_data_(0x00);
94  this->write_data_(0x02);
95  this->write_data_(0x07);
96  this->write_data_(0x0a);
97  this->write_data_(0x28);
98  this->write_data_(0x31);
99  this->write_data_(0x54);
100  this->write_data_(0x47);
101  this->write_data_(0x0e);
102  this->write_data_(0x1c);
103  this->write_data_(0x17);
104  this->write_data_(0x1b);
105  this->write_data_(0x1e);
106 
107  this->write_command_(ST7789_INVON);
108 
109  // Clear display - ensures we do not see garbage at power-on
110  this->draw_filled_rect_(0, 0, this->get_width_internal(), this->get_height_internal(), 0x0000);
111 
112  delay(120); // NOLINT
113 
114  this->write_command_(ST7789_DISPON); // Display on
115  delay(120); // NOLINT
116 
117  backlight_(true);
118 
119  this->init_internal_(this->get_buffer_length_());
120  memset(this->buffer_, 0x00, this->get_buffer_length_());
121 }
122 
124  LOG_DISPLAY("", "SPI ST7789V", this);
125  ESP_LOGCONFIG(TAG, " Model: %s", this->model_str_);
126  ESP_LOGCONFIG(TAG, " Height: %u", this->height_);
127  ESP_LOGCONFIG(TAG, " Width: %u", this->width_);
128  ESP_LOGCONFIG(TAG, " Height Offset: %u", this->offset_height_);
129  ESP_LOGCONFIG(TAG, " Width Offset: %u", this->offset_width_);
130  ESP_LOGCONFIG(TAG, " 8-bit color mode: %s", YESNO(this->eightbitcolor_));
131  LOG_PIN(" CS Pin: ", this->cs_);
132  LOG_PIN(" DC Pin: ", this->dc_pin_);
133  LOG_PIN(" Reset Pin: ", this->reset_pin_);
134  LOG_PIN(" B/L Pin: ", this->backlight_pin_);
135  LOG_UPDATE_INTERVAL(this);
136  ESP_LOGCONFIG(TAG, " Data rate: %dMHz", (unsigned) (this->data_rate_ / 1000000));
137 #ifdef USE_POWER_SUPPLY
138  ESP_LOGCONFIG(TAG, " Power Supply Configured: yes");
139 #endif
140 }
141 
143 
145  this->do_update_();
146  this->write_display_data();
147 }
148 
149 void ST7789V::set_model_str(const char *model_str) { this->model_str_ = model_str; }
150 
152  uint16_t x1 = this->offset_height_;
153  uint16_t x2 = x1 + get_width_internal() - 1;
154  uint16_t y1 = this->offset_width_;
155  uint16_t y2 = y1 + get_height_internal() - 1;
156 
157  this->enable();
158 
159  // set column(x) address
160  this->dc_pin_->digital_write(false);
161  this->write_byte(ST7789_CASET);
162  this->dc_pin_->digital_write(true);
163  this->write_addr_(x1, x2);
164  // set page(y) address
165  this->dc_pin_->digital_write(false);
166  this->write_byte(ST7789_RASET);
167  this->dc_pin_->digital_write(true);
168  this->write_addr_(y1, y2);
169  // write display memory
170  this->dc_pin_->digital_write(false);
171  this->write_byte(ST7789_RAMWR);
172  this->dc_pin_->digital_write(true);
173 
174  if (this->eightbitcolor_) {
175  uint8_t temp_buffer[TEMP_BUFFER_SIZE];
176  size_t temp_index = 0;
177  for (int line = 0; line < this->get_buffer_length_(); line = line + this->get_width_internal()) {
178  for (int index = 0; index < this->get_width_internal(); ++index) {
182  temp_buffer[temp_index++] = (uint8_t) (color >> 8);
183  temp_buffer[temp_index++] = (uint8_t) color;
184  if (temp_index == TEMP_BUFFER_SIZE) {
185  this->write_array(temp_buffer, TEMP_BUFFER_SIZE);
186  temp_index = 0;
187  }
188  }
189  }
190  if (temp_index != 0)
191  this->write_array(temp_buffer, temp_index);
192  } else {
193  this->write_array(this->buffer_, this->get_buffer_length_());
194  }
195 
196  this->disable();
197 }
198 
200  if (this->reset_pin_ != nullptr) {
201  this->reset_pin_->setup();
202  this->reset_pin_->digital_write(true);
203  delay(1);
204  // Trigger Reset
205  this->reset_pin_->digital_write(false);
206  delay(1);
207  // Wake up
208  this->reset_pin_->digital_write(true);
209  delay(5);
210  }
211 }
212 
213 void ST7789V::backlight_(bool onoff) {
214  if (this->backlight_pin_ != nullptr) {
215  this->backlight_pin_->setup();
216  this->backlight_pin_->digital_write(onoff);
217  }
218 }
219 
220 void ST7789V::write_command_(uint8_t value) {
221  this->enable();
222  this->dc_pin_->digital_write(false);
223  this->write_byte(value);
224  this->dc_pin_->digital_write(true);
225  this->disable();
226 }
227 
228 void ST7789V::write_data_(uint8_t value) {
229  this->dc_pin_->digital_write(true);
230  this->enable();
231  this->write_byte(value);
232  this->disable();
233 }
234 
235 void ST7789V::write_addr_(uint16_t addr1, uint16_t addr2) {
236  static uint8_t byte[4];
237  byte[0] = (addr1 >> 8) & 0xFF;
238  byte[1] = addr1 & 0xFF;
239  byte[2] = (addr2 >> 8) & 0xFF;
240  byte[3] = addr2 & 0xFF;
241 
242  this->dc_pin_->digital_write(true);
243  this->write_array(byte, 4);
244 }
245 
246 void ST7789V::write_color_(uint16_t color, uint16_t size) {
247  static uint8_t byte[1024];
248  int index = 0;
249  for (int i = 0; i < size; i++) {
250  byte[index++] = (color >> 8) & 0xFF;
251  byte[index++] = color & 0xFF;
252  }
253 
254  this->dc_pin_->digital_write(true);
255  return write_array(byte, size * 2);
256 }
257 
259  if (this->eightbitcolor_) {
260  return size_t(this->get_width_internal()) * size_t(this->get_height_internal());
261  }
262  return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * 2;
263 }
264 
265 // Draw a filled rectangle
266 // x1: Start X coordinate
267 // y1: Start Y coordinate
268 // x2: End X coordinate
269 // y2: End Y coordinate
270 // color: color
271 void ST7789V::draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
272  this->enable();
273  this->dc_pin_->digital_write(false);
274  this->write_byte(ST7789_CASET); // set column(x) address
275  this->dc_pin_->digital_write(true);
276  this->write_addr_(x1, x2);
277 
278  this->dc_pin_->digital_write(false);
279  this->write_byte(ST7789_RASET); // set Page(y) address
280  this->dc_pin_->digital_write(true);
281  this->write_addr_(y1, y2);
282  this->dc_pin_->digital_write(false);
283  this->write_byte(ST7789_RAMWR); // begin a write to memory
284  this->dc_pin_->digital_write(true);
285  for (int i = x1; i <= x2; i++) {
286  uint16_t size = y2 - y1 + 1;
287  this->write_color_(color, size);
288  }
289  this->disable();
290 }
291 
292 void HOT ST7789V::draw_absolute_pixel_internal(int x, int y, Color color) {
293  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
294  return;
295 
296  if (this->eightbitcolor_) {
297  auto color332 = display::ColorUtil::color_to_332(color);
298  uint32_t pos = (x + y * this->get_width_internal());
299  this->buffer_[pos] = color332;
300  } else {
301  auto color565 = display::ColorUtil::color_to_565(color);
302  uint32_t pos = (x + y * this->get_width_internal()) * 2;
303  this->buffer_[pos++] = (color565 >> 8) & 0xff;
304  this->buffer_[pos] = color565 & 0xff;
305  }
306 }
307 
308 } // namespace st7789v
309 } // namespace esphome
virtual void digital_write(bool value)=0
void setup() override
Definition: st7789v.cpp:10
float get_setup_priority() const override
Definition: st7789v.cpp:142
void dump_config() override
Definition: st7789v.cpp:123
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
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
GPIOPin * cs_
Definition: spi.h:378
virtual void setup()=0
uint16_t y
Definition: tt21100.cpp:18
void init_internal_(uint32_t buffer_length)
int get_width_internal() override
Definition: st7789v.h:161
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
static uint8_t color_to_332(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void line(int x1, int y1, int x2, int y2, Color color=COLOR_ON)
Draw a straight line from the point [x1,y1] to [x2,y2] with the given color.
Definition: display.cpp:18
const float PROCESSOR
For components that use data from sensors like displays.
Definition: component.cpp:20
uint32_t data_rate_
Definition: spi.h:376
void write_addr_(uint16_t addr1, uint16_t addr2)
Definition: st7789v.cpp:235
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
static Color to_color(uint32_t colorcode, ColorOrder color_order, ColorBitness color_bitness=ColorBitness::COLOR_BITNESS_888, bool right_bit_aligned=true)
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
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26