ESPHome  2021.10.2
st7735.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace st7735 {
9 
10 static const uint8_t ST7735_TFTWIDTH_128 = 128; // for 1.44 and mini^M
11 static const uint8_t ST7735_TFTWIDTH_80 = 80; // for mini^M
12 static const uint8_t ST7735_TFTHEIGHT_128 = 128; // for 1.44" display^M
13 static const uint8_t ST7735_TFTHEIGHT_160 = 160; // for 1.8" and mini display^M
14 
15 // some flags for initR() :(
16 static const uint8_t INITR_GREENTAB = 0x00;
17 static const uint8_t INITR_REDTAB = 0x01;
18 static const uint8_t INITR_BLACKTAB = 0x02;
19 static const uint8_t INITR_144GREENTAB = 0x01;
20 static const uint8_t INITR_MINI_160X80 = 0x04;
21 static const uint8_t INITR_HALLOWING = 0x05;
22 static const uint8_t INITR_18GREENTAB = INITR_GREENTAB;
23 static const uint8_t INITR_18REDTAB = INITR_REDTAB;
24 static const uint8_t INITR_18BLACKTAB = INITR_BLACKTAB;
25 
27  ST7735_INITR_GREENTAB = INITR_GREENTAB,
28  ST7735_INITR_REDTAB = INITR_REDTAB,
29  ST7735_INITR_BLACKTAB = INITR_BLACKTAB,
30  ST7735_INITR_MINI_160X80 = INITR_MINI_160X80,
31  ST7735_INITR_18BLACKTAB = INITR_18BLACKTAB,
32  ST7735_INITR_18REDTAB = INITR_18REDTAB
33 };
34 
35 class ST7735 : public PollingComponent,
37  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
38  spi::DATA_RATE_8MHZ> {
39  public:
40  ST7735(ST7735Model model, int width, int height, int colstart, int rowstart, bool eightbitcolor, bool usebgr,
41  bool invert_colors);
42  void dump_config() override;
43  void setup() override;
44 
45  void display();
46 
47  void update() override;
48 
49  void set_model(ST7735Model model) { this->model_ = model; }
50  float get_setup_priority() const override { return setup_priority::PROCESSOR; }
51 
52  void set_reset_pin(GPIOPin *value) { this->reset_pin_ = value; }
53  void set_dc_pin(GPIOPin *value) { dc_pin_ = value; }
54  size_t get_buffer_length();
55 
56  protected:
57  void sendcommand_(uint8_t cmd, const uint8_t *data_bytes, uint8_t num_data_bytes);
58  void senddata_(const uint8_t *data_bytes, uint8_t num_data_bytes);
59 
60  void writecommand_(uint8_t value);
61  void writedata_(uint8_t value);
62 
63  void write_display_data_();
64 
65  void init_reset_();
66  void display_init_(const uint8_t *addr);
67  void set_addr_window_(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
68  void draw_absolute_pixel_internal(int x, int y, Color color) override;
69  void spi_master_write_addr_(uint16_t addr1, uint16_t addr2);
70  void spi_master_write_color_(uint16_t color, uint16_t size);
71 
72  int get_width_internal() override;
73  int get_height_internal() override;
74 
75  const char *model_str_();
76 
78  uint8_t colstart_ = 0, rowstart_ = 0;
79  bool eightbitcolor_ = false;
80  bool usebgr_ = false;
81  bool invert_colors_ = false;
82  int16_t width_ = 80, height_ = 80; // Watch heap size
83 
84  GPIOPin *reset_pin_{nullptr};
85  GPIOPin *dc_pin_{nullptr};
86 };
87 
88 } // namespace st7735
89 } // namespace esphome
void senddata_(const uint8_t *data_bytes, uint8_t num_data_bytes)
Definition: st7735.cpp:409
void update() override
Definition: st7735.cpp:294
void dump_config() override
Definition: st7735.cpp:375
float get_setup_priority() const override
Definition: st7735.h:50
ST7735(ST7735Model model, int width, int height, int colstart, int rowstart, bool eightbitcolor, bool usebgr, bool invert_colors)
Definition: st7735.cpp:224
This class simplifies creating components that periodically check a state.
Definition: component.h:234
GPIOPin * reset_pin_
Definition: st7735.h:84
void set_model(ST7735Model model)
Definition: st7735.h:49
const char * model_str_()
Definition: st7735.cpp:338
void spi_master_write_color_(uint16_t color, uint16_t size)
Definition: st7735.cpp:477
const float PROCESSOR
For components that use data from sensors like displays.
Definition: component.cpp:19
void spi_master_write_addr_(uint16_t addr1, uint16_t addr2)
Definition: st7735.cpp:466
void sendcommand_(uint8_t cmd, const uint8_t *data_bytes, uint8_t num_data_bytes)
Definition: st7735.cpp:404
void writedata_(uint8_t value)
Definition: st7735.cpp:397
void set_reset_pin(GPIOPin *value)
Definition: st7735.h:52
void setup() override
Definition: st7735.cpp:235
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition: st7735.cpp:310
int get_width_internal() override
Definition: st7735.cpp:301
void writecommand_(uint8_t value)
Definition: st7735.cpp:389
void set_addr_window_(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
void set_dc_pin(GPIOPin *value)
Definition: st7735.h:53
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
size_t get_buffer_length()
Definition: st7735.cpp:303
void display_init_(const uint8_t *addr)
Definition: st7735.cpp:353
int get_height_internal() override
Definition: st7735.cpp:299
ST7735Model model_
Definition: st7735.h:77
GPIOPin * dc_pin_
Definition: st7735.h:85