ESPHome  2022.6.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, 239, 319, 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  LOG_PIN(" CS Pin: ", this->cs_);
121  LOG_PIN(" DC Pin: ", this->dc_pin_);
122  LOG_PIN(" Reset Pin: ", this->reset_pin_);
123  LOG_PIN(" B/L Pin: ", this->backlight_pin_);
124  LOG_UPDATE_INTERVAL(this);
125 }
126 
128 
130  this->do_update_();
131  this->write_display_data();
132 }
133 
134 void ST7789V::loop() {}
135 
137  uint16_t x1 = 52; // _offsetx
138  uint16_t x2 = 186; // _offsetx
139  uint16_t y1 = 40; // _offsety
140  uint16_t y2 = 279; // _offsety
141 
142  this->enable();
143 
144  // set column(x) address
145  this->dc_pin_->digital_write(false);
146  this->write_byte(ST7789_CASET);
147  this->dc_pin_->digital_write(true);
148  this->write_addr_(x1, x2);
149  // set page(y) address
150  this->dc_pin_->digital_write(false);
151  this->write_byte(ST7789_RASET);
152  this->dc_pin_->digital_write(true);
153  this->write_addr_(y1, y2);
154  // write display memory
155  this->dc_pin_->digital_write(false);
156  this->write_byte(ST7789_RAMWR);
157  this->dc_pin_->digital_write(true);
158 
159  this->write_array(this->buffer_, this->get_buffer_length_());
160 
161  this->disable();
162 }
163 
165  if (this->reset_pin_ != nullptr) {
166  this->reset_pin_->setup();
167  this->reset_pin_->digital_write(true);
168  delay(1);
169  // Trigger Reset
170  this->reset_pin_->digital_write(false);
171  delay(10);
172  // Wake up
173  this->reset_pin_->digital_write(true);
174  }
175 }
176 
177 void ST7789V::backlight_(bool onoff) {
178  if (this->backlight_pin_ != nullptr) {
179  this->backlight_pin_->setup();
180  this->backlight_pin_->digital_write(onoff);
181  }
182 }
183 
184 void ST7789V::write_command_(uint8_t value) {
185  this->enable();
186  this->dc_pin_->digital_write(false);
187  this->write_byte(value);
188  this->dc_pin_->digital_write(true);
189  this->disable();
190 }
191 
192 void ST7789V::write_data_(uint8_t value) {
193  this->dc_pin_->digital_write(true);
194  this->enable();
195  this->write_byte(value);
196  this->disable();
197 }
198 
199 void ST7789V::write_addr_(uint16_t addr1, uint16_t addr2) {
200  static uint8_t byte[4];
201  byte[0] = (addr1 >> 8) & 0xFF;
202  byte[1] = addr1 & 0xFF;
203  byte[2] = (addr2 >> 8) & 0xFF;
204  byte[3] = addr2 & 0xFF;
205 
206  this->dc_pin_->digital_write(true);
207  this->write_array(byte, 4);
208 }
209 
210 void ST7789V::write_color_(uint16_t color, uint16_t size) {
211  static uint8_t byte[1024];
212  int index = 0;
213  for (int i = 0; i < size; i++) {
214  byte[index++] = (color >> 8) & 0xFF;
215  byte[index++] = color & 0xFF;
216  }
217 
218  this->dc_pin_->digital_write(true);
219  return write_array(byte, size * 2);
220 }
221 
223  return 240; // 320;
224 }
225 
227  return 135; // 240;
228 }
229 
231  return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * 2;
232 }
233 
234 // Draw a filled rectangle
235 // x1: Start X coordinate
236 // y1: Start Y coordinate
237 // x2: End X coordinate
238 // y2: End Y coordinate
239 // color: color
240 void ST7789V::draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
241  // ESP_LOGD(TAG,"offset(x)=%d offset(y)=%d",dev->_offsetx,dev->_offsety);
242  this->enable();
243  this->dc_pin_->digital_write(false);
244  this->write_byte(ST7789_CASET); // set column(x) address
245  this->dc_pin_->digital_write(true);
246  this->write_addr_(x1, x2);
247 
248  this->dc_pin_->digital_write(false);
249  this->write_byte(ST7789_RASET); // set Page(y) address
250  this->dc_pin_->digital_write(true);
251  this->write_addr_(y1, y2);
252  this->dc_pin_->digital_write(false);
253  this->write_byte(ST7789_RAMWR); // begin a write to memory
254  this->dc_pin_->digital_write(true);
255  for (int i = x1; i <= x2; i++) {
256  uint16_t size = y2 - y1 + 1;
257  this->write_color_(color, size);
258  }
259  this->disable();
260 }
261 
262 void HOT ST7789V::draw_absolute_pixel_internal(int x, int y, Color color) {
263  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
264  return;
265 
266  auto color565 = display::ColorUtil::color_to_565(color);
267 
268  uint16_t pos = (x + y * this->get_width_internal()) * 2;
269  this->buffer_[pos++] = (color565 >> 8) & 0xff;
270  this->buffer_[pos] = color565 & 0xff;
271 }
272 
273 } // namespace st7789v
274 } // 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:127
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:129
void write_color_(uint16_t color, uint16_t size)
Definition: st7789v.cpp:210
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition: st7789v.cpp:262
GPIOPin * backlight_pin_
Definition: st7789v.h:134
virtual void setup()=0
void init_internal_(uint32_t buffer_length)
int get_width_internal() override
Definition: st7789v.cpp:226
void draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Definition: st7789v.cpp:240
void write_command_(uint8_t value)
Definition: st7789v.cpp:184
void write_data_(uint8_t value)
Definition: st7789v.cpp:192
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:199
void loop() override
Definition: st7789v.cpp:134
Definition: a4988.cpp:4
void backlight_(bool onoff)
Definition: st7789v.cpp:177
int get_height_internal() override
Definition: st7789v.cpp:222
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27