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