ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
st7735.cpp
Go to the documentation of this file.
1#include "st7735.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::st7735 {
7
8static const uint8_t ST_CMD_DELAY = 0x80; // special signifier for command lists
9
10static const uint8_t ST77XX_NOP = 0x00;
11static const uint8_t ST77XX_SWRESET = 0x01;
12static const uint8_t ST77XX_RDDID = 0x04;
13static const uint8_t ST77XX_RDDST = 0x09;
14
15static const uint8_t ST77XX_SLPIN = 0x10;
16static const uint8_t ST77XX_SLPOUT = 0x11;
17static const uint8_t ST77XX_PTLON = 0x12;
18static const uint8_t ST77XX_NORON = 0x13;
19
20static const uint8_t ST77XX_INVOFF = 0x20;
21static const uint8_t ST77XX_INVON = 0x21;
22static const uint8_t ST77XX_DISPOFF = 0x28;
23static const uint8_t ST77XX_DISPON = 0x29;
24static const uint8_t ST77XX_CASET = 0x2A;
25static const uint8_t ST77XX_RASET = 0x2B;
26static const uint8_t ST77XX_RAMWR = 0x2C;
27static const uint8_t ST77XX_RAMRD = 0x2E;
28
29static const uint8_t ST77XX_PTLAR = 0x30;
30static const uint8_t ST77XX_TEOFF = 0x34;
31static const uint8_t ST77XX_TEON = 0x35;
32static const uint8_t ST77XX_MADCTL = 0x36;
33static const uint8_t ST77XX_COLMOD = 0x3A;
34
35static const uint8_t ST77XX_MADCTL_MY = 0x80;
36static const uint8_t ST77XX_MADCTL_MX = 0x40;
37static const uint8_t ST77XX_MADCTL_MV = 0x20;
38static const uint8_t ST77XX_MADCTL_ML = 0x10;
39static const uint8_t ST77XX_MADCTL_RGB = 0x00;
40
41static const uint8_t ST77XX_RDID1 = 0xDA;
42static const uint8_t ST77XX_RDID2 = 0xDB;
43static const uint8_t ST77XX_RDID3 = 0xDC;
44static const uint8_t ST77XX_RDID4 = 0xDD;
45
46// Some register settings
47static const uint8_t ST7735_MADCTL_BGR = 0x08;
48
49static const uint8_t ST7735_MADCTL_MH = 0x04;
50
51static const uint8_t ST7735_FRMCTR1 = 0xB1;
52static const uint8_t ST7735_FRMCTR2 = 0xB2;
53static const uint8_t ST7735_FRMCTR3 = 0xB3;
54static const uint8_t ST7735_INVCTR = 0xB4;
55static const uint8_t ST7735_DISSET5 = 0xB6;
56
57static const uint8_t ST7735_PWCTR1 = 0xC0;
58static const uint8_t ST7735_PWCTR2 = 0xC1;
59static const uint8_t ST7735_PWCTR3 = 0xC2;
60static const uint8_t ST7735_PWCTR4 = 0xC3;
61static const uint8_t ST7735_PWCTR5 = 0xC4;
62static const uint8_t ST7735_VMCTR1 = 0xC5;
63
64static const uint8_t ST7735_PWCTR6 = 0xFC;
65
66static const uint8_t ST7735_GMCTRP1 = 0xE0;
67static const uint8_t ST7735_GMCTRN1 = 0xE1;
68
69// clang-format off
70static constexpr uint8_t PROGMEM
71 BCMD[] = { // Init commands for 7735B screens
72 18, // 18 commands in list:
73 ST77XX_SWRESET, ST_CMD_DELAY, // 1: Software reset, no args, w/delay
74 50, // 50 ms delay
75 ST77XX_SLPOUT, ST_CMD_DELAY, // 2: Out of sleep mode, no args, w/delay
76 255, // 255 = max (500 ms) delay
77 ST77XX_COLMOD, 1+ST_CMD_DELAY, // 3: Set color mode, 1 arg + delay:
78 0x05, // 16-bit color
79 10, // 10 ms delay
80 ST7735_FRMCTR1, 3+ST_CMD_DELAY, // 4: Frame rate control, 3 args + delay:
81 0x00, // fastest refresh
82 0x06, // 6 lines front porch
83 0x03, // 3 lines back porch
84 10, // 10 ms delay
85 ST77XX_MADCTL, 1, // 5: Mem access ctl (directions), 1 arg:
86 0x08, // Row/col addr, bottom-top refresh
87 ST7735_DISSET5, 2, // 6: Display settings #5, 2 args:
88 0x15, // 1 clk cycle nonoverlap, 2 cycle gate
89 // rise, 3 cycle osc equalize
90 0x02, // Fix on VTL
91 ST7735_INVCTR, 1, // 7: Display inversion control, 1 arg:
92 0x0, // Line inversion
93 ST7735_PWCTR1, 2+ST_CMD_DELAY, // 8: Power control, 2 args + delay:
94 0x02, // GVDD = 4.7V
95 0x70, // 1.0uA
96 10, // 10 ms delay
97 ST7735_PWCTR2, 1, // 9: Power control, 1 arg, no delay:
98 0x05, // VGH = 14.7V, VGL = -7.35V
99 ST7735_PWCTR3, 2, // 10: Power control, 2 args, no delay:
100 0x01, // Opamp current small
101 0x02, // Boost frequency
102 ST7735_VMCTR1, 2+ST_CMD_DELAY, // 11: Power control, 2 args + delay:
103 0x3C, // VCOMH = 4V
104 0x38, // VCOML = -1.1V
105 10, // 10 ms delay
106 ST7735_PWCTR6, 2, // 12: Power control, 2 args, no delay:
107 0x11, 0x15,
108 ST7735_GMCTRP1,16, // 13: Gamma Adjustments (pos. polarity), 16 args + delay:
109 0x09, 0x16, 0x09, 0x20, // (Not entirely necessary, but provides
110 0x21, 0x1B, 0x13, 0x19, // accurate colors)
111 0x17, 0x15, 0x1E, 0x2B,
112 0x04, 0x05, 0x02, 0x0E,
113 ST7735_GMCTRN1,16+ST_CMD_DELAY, // 14: Gamma Adjustments (neg. polarity), 16 args + delay:
114 0x0B, 0x14, 0x08, 0x1E, // (Not entirely necessary, but provides
115 0x22, 0x1D, 0x18, 0x1E, // accurate colors)
116 0x1B, 0x1A, 0x24, 0x2B,
117 0x06, 0x06, 0x02, 0x0F,
118 10, // 10 ms delay
119 ST77XX_CASET, 4, // 15: Column addr set, 4 args, no delay:
120 0x00, 0x02, // XSTART = 2
121 0x00, 0x81, // XEND = 129
122 ST77XX_RASET, 4, // 16: Row addr set, 4 args, no delay:
123 0x00, 0x02, // XSTART = 1
124 0x00, 0x81, // XEND = 160
125 ST77XX_NORON, ST_CMD_DELAY, // 17: Normal display on, no args, w/delay
126 10, // 10 ms delay
127 ST77XX_DISPON, ST_CMD_DELAY, // 18: Main screen turn on, no args, delay
128 255 }, // 255 = max (500 ms) delay
129
130 RCMD1[] = { // 7735R init, part 1 (red or green tab)
131 15, // 15 commands in list:
132 ST77XX_SWRESET, ST_CMD_DELAY, // 1: Software reset, 0 args, w/delay
133 150, // 150 ms delay
134 ST77XX_SLPOUT, ST_CMD_DELAY, // 2: Out of sleep mode, 0 args, w/delay
135 255, // 500 ms delay
136 ST7735_FRMCTR1, 3, // 3: Framerate ctrl - normal mode, 3 arg:
137 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
138 ST7735_FRMCTR2, 3, // 4: Framerate ctrl - idle mode, 3 args:
139 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
140 ST7735_FRMCTR3, 6, // 5: Framerate - partial mode, 6 args:
141 0x01, 0x2C, 0x2D, // Dot inversion mode
142 0x01, 0x2C, 0x2D, // Line inversion mode
143 ST7735_INVCTR, 1, // 6: Display inversion ctrl, 1 arg:
144 0x07, // No inversion
145 ST7735_PWCTR1, 3, // 7: Power control, 3 args, no delay:
146 0xA2,
147 0x02, // -4.6V
148 0x84, // AUTO mode
149 ST7735_PWCTR2, 1, // 8: Power control, 1 arg, no delay:
150 0xC5, // VGH25=2.4C VGSEL=-10 VGH=3 * AVDD
151 ST7735_PWCTR3, 2, // 9: Power control, 2 args, no delay:
152 0x0A, // Opamp current small
153 0x00, // Boost frequency
154 ST7735_PWCTR4, 2, // 10: Power control, 2 args, no delay:
155 0x8A, // BCLK/2,
156 0x2A, // opamp current small & medium low
157 ST7735_PWCTR5, 2, // 11: Power control, 2 args, no delay:
158 0x8A, 0xEE,
159 ST7735_VMCTR1, 1, // 12: Power control, 1 arg, no delay:
160 0x0E,
161 ST77XX_INVOFF, 0, // 13: Don't invert display, no args
162 ST77XX_MADCTL, 1, // 14: Mem access ctl (directions), 1 arg:
163 0xC8, // row/col addr, bottom-top refresh
164 ST77XX_COLMOD, 1, // 15: set color mode, 1 arg, no delay:
165 0x05 }, // 16-bit color
166
167 RCMD2GREEN[] = { // 7735R init, part 2 (green tab only)
168 2, // 2 commands in list:
169 ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay:
170 0x00, 0x02, // XSTART = 0
171 0x00, 0x7F+0x02, // XEND = 127
172 ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay:
173 0x00, 0x01, // XSTART = 0
174 0x00, 0x9F+0x01 }, // XEND = 159
175
176 RCMD2RED[] = { // 7735R init, part 2 (red tab only)
177 2, // 2 commands in list:
178 ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay:
179 0x00, 0x00, // XSTART = 0
180 0x00, 0x7F, // XEND = 127
181 ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay:
182 0x00, 0x00, // XSTART = 0
183 0x00, 0x9F }, // XEND = 159
184
185 RCMD2GREEN144[] = { // 7735R init, part 2 (green 1.44 tab)
186 2, // 2 commands in list:
187 ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay:
188 0x00, 0x00, // XSTART = 0
189 0x00, 0x7F, // XEND = 127
190 ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay:
191 0x00, 0x00, // XSTART = 0
192 0x00, 0x7F }, // XEND = 127
193
194 RCMD2GREEN160X80[] = { // 7735R init, part 2 (mini 160x80)
195 2, // 2 commands in list:
196 ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay:
197 0x00, 0x00, // XSTART = 0
198 0x00, 0x4F, // XEND = 79
199 ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay:
200 0x00, 0x00, // XSTART = 0
201 0x00, 0x9F }, // XEND = 159
202
203 RCMD3[] = { // 7735R init, part 3 (red or green tab)
204 4, // 4 commands in list:
205 ST7735_GMCTRP1, 16 , // 1: Gamma Adjustments (pos. polarity), 16 args + delay:
206 0x02, 0x1c, 0x07, 0x12, // (Not entirely necessary, but provides
207 0x37, 0x32, 0x29, 0x2d, // accurate colors)
208 0x29, 0x25, 0x2B, 0x39,
209 0x00, 0x01, 0x03, 0x10,
210 ST7735_GMCTRN1, 16 , // 2: Gamma Adjustments (neg. polarity), 16 args + delay:
211 0x03, 0x1d, 0x07, 0x06, // (Not entirely necessary, but provides
212 0x2E, 0x2C, 0x29, 0x2D, // accurate colors)
213 0x2E, 0x2E, 0x37, 0x3F,
214 0x00, 0x00, 0x02, 0x10,
215 ST77XX_NORON, ST_CMD_DELAY, // 3: Normal display on, no args, w/delay
216 10, // 10 ms delay
217 ST77XX_DISPON, ST_CMD_DELAY, // 4: Main screen turn on, no args w/delay
218 100 }; // 100 ms delay
219
220// clang-format on
221static const char *const TAG = "st7735";
222
223ST7735::ST7735(ST7735Model model, int width, int height, int colstart, int rowstart, bool eightbitcolor, bool usebgr,
224 bool invert_colors)
225 : model_(model),
226 colstart_(colstart),
227 rowstart_(rowstart),
228 eightbitcolor_(eightbitcolor),
229 usebgr_(usebgr),
230 invert_colors_(invert_colors),
231 width_(width),
232 height_(height) {}
233
235 this->spi_setup();
236
237 this->dc_pin_->setup(); // OUTPUT
238 this->cs_->setup(); // OUTPUT
239
240 this->dc_pin_->digital_write(true);
241 this->cs_->digital_write(true);
242
243 this->init_reset_();
244 delay(100); // NOLINT
245
246 ESP_LOGD(TAG, " START");
247 dump_config();
248 ESP_LOGD(TAG, " END");
249
250 display_init_(RCMD1);
251
252 if (this->model_ == INITR_GREENTAB) {
253 display_init_(RCMD2GREEN);
254 colstart_ == 0 ? colstart_ = 2 : colstart_;
255 rowstart_ == 0 ? rowstart_ = 1 : rowstart_;
256 } else if ((this->model_ == INITR_144GREENTAB) || (this->model_ == INITR_HALLOWING)) {
257 height_ == 0 ? height_ = ST7735_TFTHEIGHT_128 : height_;
258 width_ == 0 ? width_ = ST7735_TFTWIDTH_128 : width_;
259 display_init_(RCMD2GREEN144);
260 colstart_ == 0 ? colstart_ = 2 : colstart_;
261 rowstart_ == 0 ? rowstart_ = 3 : rowstart_;
262 } else if (this->model_ == INITR_MINI_160X80) {
263 height_ == 0 ? height_ = ST7735_TFTHEIGHT_160 : height_;
264 width_ == 0 ? width_ = ST7735_TFTWIDTH_80 : width_;
265 display_init_(RCMD2GREEN160X80);
266 colstart_ == 0 ? colstart_ = 24 : colstart_;
267 rowstart_ == 0 ? rowstart_ = 0 : rowstart_;
268 } else {
269 // colstart, rowstart left at default '0' values
270 display_init_(RCMD2RED);
271 }
272 display_init_(RCMD3);
273
274 uint8_t data = 0;
275 if (this->model_ != INITR_HALLOWING) {
276 data = ST77XX_MADCTL_MX | ST77XX_MADCTL_MY;
277 }
278 if (this->usebgr_) {
279 data = data | ST7735_MADCTL_BGR;
280 } else {
281 data = data | ST77XX_MADCTL_RGB;
282 }
283 sendcommand_(ST77XX_MADCTL, &data, 1);
284
285 if (this->invert_colors_)
286 sendcommand_(ST77XX_INVON, nullptr, 0);
287
288 this->init_internal_(this->get_buffer_length());
289 memset(this->buffer_, 0x00, this->get_buffer_length());
290}
291
293 this->do_update_();
294 this->write_display_data_();
295}
296
298
300
302 if (this->eightbitcolor_) {
303 return size_t(this->get_width_internal()) * size_t(this->get_height_internal());
304 }
305 return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * 2;
306}
307
309 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
310 return;
311
312 if (this->eightbitcolor_) {
313 const uint32_t color332 = display::ColorUtil::color_to_332(color);
314 uint16_t pos = (x + y * this->get_width_internal());
315 this->buffer_[pos] = color332;
316 } else {
317 const uint32_t color565 = display::ColorUtil::color_to_565(color);
318 uint16_t pos = (x + y * this->get_width_internal()) * 2;
319 this->buffer_[pos++] = (color565 >> 8) & 0xff;
320 this->buffer_[pos] = color565 & 0xff;
321 }
322}
323
325 if (this->reset_pin_ != nullptr) {
326 this->reset_pin_->setup();
327 this->reset_pin_->digital_write(true);
328 delay(1);
329 // Trigger Reset
330 this->reset_pin_->digital_write(false);
331 delay(10);
332 // Wake up
333 this->reset_pin_->digital_write(true);
334 }
335}
336const char *ST7735::model_str_() {
337 switch (this->model_) {
338 case INITR_GREENTAB:
339 return "ST7735 GREENTAB";
340 case INITR_REDTAB:
341 return "ST7735 REDTAB";
342 case INITR_BLACKTAB:
343 return "ST7735 BLACKTAB";
344 case INITR_MINI_160X80:
345 return "ST7735 MINI160x80";
346 default:
347 return "Unknown";
348 }
349}
350
351void ST7735::display_init_(const uint8_t *addr) {
352 uint8_t num_commands, cmd, num_args;
353 uint16_t ms;
354
355 num_commands = progmem_read_byte(addr++); // Number of commands to follow
356 while (num_commands--) { // For each command...
357 cmd = progmem_read_byte(addr++); // Read command
358 num_args = progmem_read_byte(addr++); // Number of args to follow
359 ms = num_args & ST_CMD_DELAY; // If hibit set, delay follows args
360 num_args &= ~ST_CMD_DELAY; // Mask out delay bit
361 this->sendcommand_(cmd, addr, num_args);
362 addr += num_args;
363
364 if (ms) {
365 ms = progmem_read_byte(addr++); // Read post-command delay time (ms)
366 if (ms == 255)
367 ms = 500; // If 255, delay for 500 ms
368 delay(ms);
369 }
370 }
371}
372
374 LOG_DISPLAY("", "ST7735", this);
375 LOG_PIN(" CS Pin: ", this->cs_);
376 LOG_PIN(" DC Pin: ", this->dc_pin_);
377 LOG_PIN(" Reset Pin: ", this->reset_pin_);
378 ESP_LOGCONFIG(TAG,
379 " Model: %s\n"
380 " Buffer Size: %zu\n"
381 " Height: %d\n"
382 " Width: %d\n"
383 " ColStart: %d\n"
384 " RowStart: %d",
385 this->model_str_(), this->get_buffer_length(), this->height_, this->width_, this->colstart_,
386 this->rowstart_);
387 LOG_UPDATE_INTERVAL(this);
388}
389
390void HOT ST7735::writecommand_(uint8_t value) {
391 this->enable();
392 this->dc_pin_->digital_write(false);
393 this->write_byte(value);
394 this->dc_pin_->digital_write(true);
395 this->disable();
396}
397
398void HOT ST7735::writedata_(uint8_t value) {
399 this->dc_pin_->digital_write(true);
400 this->enable();
401 this->write_byte(value);
402 this->disable();
403}
404
405void HOT ST7735::sendcommand_(uint8_t cmd, const uint8_t *data_bytes, uint8_t num_data_bytes) {
406 this->writecommand_(cmd);
407 this->senddata_(data_bytes, num_data_bytes);
408}
409
410void HOT ST7735::senddata_(const uint8_t *data_bytes, uint8_t num_data_bytes) {
411 this->dc_pin_->digital_write(true); // pull DC high to indicate data
412 this->cs_->digital_write(false);
413 this->enable();
414 for (uint8_t i = 0; i < num_data_bytes; i++) {
415 this->write_byte(progmem_read_byte(data_bytes++)); // write byte - SPI library
416 }
417 this->cs_->digital_write(true);
418 this->disable();
419}
420
422 uint16_t offsetx = colstart_;
423 uint16_t offsety = rowstart_;
424
425 uint16_t x1 = offsetx;
426 uint16_t x2 = x1 + get_width_internal() - 1;
427 uint16_t y1 = offsety;
428 uint16_t y2 = y1 + get_height_internal() - 1;
429
430 this->enable();
431
432 // set column(x) address
433 this->dc_pin_->digital_write(false);
434 this->write_byte(ST77XX_CASET);
435 this->dc_pin_->digital_write(true);
436 this->spi_master_write_addr_(x1, x2);
437
438 // set Page(y) address
439 this->dc_pin_->digital_write(false);
440 this->write_byte(ST77XX_RASET);
441 this->dc_pin_->digital_write(true);
442 this->spi_master_write_addr_(y1, y2);
443
444 // Memory Write
445 this->dc_pin_->digital_write(false);
446 this->write_byte(ST77XX_RAMWR);
447 this->dc_pin_->digital_write(true);
448
449 if (this->eightbitcolor_) {
450 for (size_t line = 0; line < this->get_buffer_length(); line = line + this->get_width_internal()) {
451 for (int index = 0; index < this->get_width_internal(); ++index) {
454
455 auto color = display::ColorUtil::color_to_565(color332);
456
457 this->write_byte((color >> 8) & 0xff);
458 this->write_byte(color & 0xff);
459 }
460 }
461 } else {
462 this->write_array(this->buffer_, this->get_buffer_length());
463 }
464 this->disable();
465}
466
467void ST7735::spi_master_write_addr_(uint16_t addr1, uint16_t addr2) {
468 uint8_t byte[4];
469 byte[0] = (addr1 >> 8) & 0xFF;
470 byte[1] = addr1 & 0xFF;
471 byte[2] = (addr2 >> 8) & 0xFF;
472 byte[3] = addr2 & 0xFF;
473
474 this->dc_pin_->digital_write(true);
475 this->write_array(byte, 4);
476}
477
478} // namespace esphome::st7735
virtual void setup()=0
virtual void digital_write(bool value)=0
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
static uint8_t color_to_332(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
static Color to_color(uint32_t colorcode, ColorOrder color_order, ColorBitness color_bitness=ColorBitness::COLOR_BITNESS_888, bool right_bit_aligned=true)
void init_internal_(uint32_t buffer_length)
const char * model_str_()
Definition st7735.cpp:336
void senddata_(const uint8_t *data_bytes, uint8_t num_data_bytes)
Definition st7735.cpp:410
int get_height_internal() override
Definition st7735.cpp:297
GPIOPin * reset_pin_
Definition st7735.h:83
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition st7735.cpp:308
ST7735Model model_
Definition st7735.h:76
void display_init_(const uint8_t *addr)
Definition st7735.cpp:351
void writecommand_(uint8_t value)
Definition st7735.cpp:390
void writedata_(uint8_t value)
Definition st7735.cpp:398
int get_width_internal() override
Definition st7735.cpp:299
void update() override
Definition st7735.cpp:292
void dump_config() override
Definition st7735.cpp:373
ST7735(ST7735Model model, int width, int height, int colstart, int rowstart, bool eightbitcolor, bool usebgr, bool invert_colors)
Definition st7735.cpp:223
void setup() override
Definition st7735.cpp:234
void spi_master_write_addr_(uint16_t addr1, uint16_t addr2)
Definition st7735.cpp:467
void sendcommand_(uint8_t cmd, const uint8_t *data_bytes, uint8_t num_data_bytes)
Definition st7735.cpp:405
const char int line
Definition log.h:74
size_t size_t pos
Definition helpers.h:1038
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:43
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6
const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM
Definition web_server.h:28