ESPHome  2024.3.2
ssd1351_base.cpp
Go to the documentation of this file.
1 #include "ssd1351_base.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 namespace ssd1351_base {
7 
8 static const char *const TAG = "ssd1351";
9 
10 static const uint16_t SSD1351_COLORMASK = 0xffff;
11 static const uint8_t SSD1351_MAX_CONTRAST = 15;
12 static const uint8_t SSD1351_BYTESPERPIXEL = 2;
13 // SSD1351 commands
14 static const uint8_t SSD1351_SETCOLUMN = 0x15;
15 static const uint8_t SSD1351_SETROW = 0x75;
16 static const uint8_t SSD1351_SETREMAP = 0xA0;
17 static const uint8_t SSD1351_STARTLINE = 0xA1;
18 static const uint8_t SSD1351_DISPLAYOFFSET = 0xA2;
19 static const uint8_t SSD1351_DISPLAYOFF = 0xAE;
20 static const uint8_t SSD1351_DISPLAYON = 0xAF;
21 static const uint8_t SSD1351_PRECHARGE = 0xB1;
22 static const uint8_t SSD1351_CLOCKDIV = 0xB3;
23 static const uint8_t SSD1351_PRECHARGELEVEL = 0xBB;
24 static const uint8_t SSD1351_VCOMH = 0xBE;
25 // display controls
26 static const uint8_t SSD1351_DISPLAYALLOFF = 0xA4;
27 static const uint8_t SSD1351_DISPLAYALLON = 0xA5;
28 static const uint8_t SSD1351_NORMALDISPLAY = 0xA6;
29 static const uint8_t SSD1351_INVERTDISPLAY = 0xA7;
30 // contrast controls
31 static const uint8_t SSD1351_CONTRASTABC = 0xC1;
32 static const uint8_t SSD1351_CONTRASTMASTER = 0xC7;
33 // memory functions
34 static const uint8_t SSD1351_WRITERAM = 0x5C;
35 static const uint8_t SSD1351_READRAM = 0x5D;
36 // other functions
37 static const uint8_t SSD1351_FUNCTIONSELECT = 0xAB;
38 static const uint8_t SSD1351_DISPLAYENHANCE = 0xB2;
39 static const uint8_t SSD1351_SETVSL = 0xB4;
40 static const uint8_t SSD1351_SETGPIO = 0xB5;
41 static const uint8_t SSD1351_PRECHARGE2 = 0xB6;
42 static const uint8_t SSD1351_SETGRAY = 0xB8;
43 static const uint8_t SSD1351_USELUT = 0xB9;
44 static const uint8_t SSD1351_MUXRATIO = 0xCA;
45 static const uint8_t SSD1351_COMMANDLOCK = 0xFD;
46 static const uint8_t SSD1351_HORIZSCROLL = 0x96;
47 static const uint8_t SSD1351_STOPSCROLL = 0x9E;
48 static const uint8_t SSD1351_STARTSCROLL = 0x9F;
49 
51  this->init_internal_(this->get_buffer_length_());
52 
53  this->command(SSD1351_COMMANDLOCK);
54  this->data(0x12);
55  this->command(SSD1351_COMMANDLOCK);
56  this->data(0xB1);
57  this->command(SSD1351_DISPLAYOFF);
58  this->command(SSD1351_CLOCKDIV);
59  this->data(0xF1); // 7:4 = Oscillator Freq, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
60  this->command(SSD1351_MUXRATIO);
61  this->data(127);
62  this->command(SSD1351_DISPLAYOFFSET);
63  this->data(0x00);
64  this->command(SSD1351_SETGPIO);
65  this->data(0x00);
66  this->command(SSD1351_FUNCTIONSELECT);
67  this->data(0x01); // internal (diode drop)
68  this->command(SSD1351_PRECHARGE);
69  this->data(0x32);
70  this->command(SSD1351_VCOMH);
71  this->data(0x05);
72  this->command(SSD1351_NORMALDISPLAY);
73  this->command(SSD1351_SETVSL);
74  this->data(0xA0);
75  this->data(0xB5);
76  this->data(0x55);
77  this->command(SSD1351_PRECHARGE2);
78  this->data(0x01);
79  this->command(SSD1351_SETREMAP);
80  this->data(0x34);
81  this->command(SSD1351_STARTLINE);
82  this->data(0x00);
83  this->command(SSD1351_CONTRASTABC);
84  this->data(0xC8);
85  this->data(0x80);
86  this->data(0xC8);
88  this->fill(Color::BLACK); // clear display - ensures we do not see garbage at power-on
89  this->display(); // ...write buffer, which actually clears the display's memory
90  this->turn_on(); // display ON
91 }
93  this->command(SSD1351_SETCOLUMN); // set column address
94  this->data(0x00); // set column start address
95  this->data(0x7F); // set column end address
96  this->command(SSD1351_SETROW); // set row address
97  this->data(0x00); // set row start address
98  this->data(0x7F); // set last row
99  this->command(SSD1351_WRITERAM);
100  this->write_display_data();
101 }
103  this->do_update_();
104  this->display();
105 }
106 void SSD1351::set_brightness(float brightness) {
107  // validation
108  if (brightness > 1) {
109  this->brightness_ = 1.0;
110  } else if (brightness < 0) {
111  this->brightness_ = 0;
112  } else {
113  this->brightness_ = brightness;
114  }
115  if (!this->is_ready()) {
116  return; // Component is not yet setup skip the command
117  }
118  // now write the new brightness level to the display
119  this->command(SSD1351_CONTRASTMASTER);
120  this->data(int(SSD1351_MAX_CONTRAST * (this->brightness_)));
121 }
122 bool SSD1351::is_on() { return this->is_on_; }
124  this->command(SSD1351_DISPLAYON);
125  this->is_on_ = true;
126 }
128  this->command(SSD1351_DISPLAYOFF);
129  this->is_on_ = false;
130 }
132  switch (this->model_) {
134  return 96;
136  return 128;
137  default:
138  return 0;
139  }
140 }
142  switch (this->model_) {
145  return 128;
146  default:
147  return 0;
148  }
149 }
151  return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * size_t(SSD1351_BYTESPERPIXEL);
152 }
153 void HOT SSD1351::draw_absolute_pixel_internal(int x, int y, Color color) {
154  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
155  return;
156  const uint32_t color565 = display::ColorUtil::color_to_565(color);
157  // where should the bits go in the big buffer array? math...
158  uint16_t pos = (x + y * this->get_width_internal()) * SSD1351_BYTESPERPIXEL;
159  this->buffer_[pos++] = (color565 >> 8) & 0xff;
160  this->buffer_[pos] = color565 & 0xff;
161 }
162 void SSD1351::fill(Color color) {
163  const uint32_t color565 = display::ColorUtil::color_to_565(color);
164  for (uint32_t i = 0; i < this->get_buffer_length_(); i++) {
165  if (i & 1) {
166  this->buffer_[i] = color565 & 0xff;
167  } else {
168  this->buffer_[i] = (color565 >> 8) & 0xff;
169  }
170  }
171 }
173  if (this->reset_pin_ != nullptr) {
174  this->reset_pin_->setup();
175  this->reset_pin_->digital_write(true);
176  delay(1);
177  // Trigger Reset
178  this->reset_pin_->digital_write(false);
179  delay(10);
180  // Wake up
181  this->reset_pin_->digital_write(true);
182  }
183 }
184 const char *SSD1351::model_str_() {
185  switch (this->model_) {
187  return "SSD1351 128x96";
189  return "SSD1351 128x128";
190  default:
191  return "Unknown";
192  }
193 }
194 
195 } // namespace ssd1351_base
196 } // namespace esphome
virtual void digital_write(bool value)=0
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void set_brightness(float brightness)
uint16_t x
Definition: tt21100.cpp:17
void draw_absolute_pixel_internal(int x, int y, Color color) override
virtual void write_display_data()=0
virtual void setup()=0
virtual void data(uint8_t value)=0
void fill(Color color) override
uint16_t y
Definition: tt21100.cpp:18
void init_internal_(uint32_t buffer_length)
virtual void command(uint8_t value)=0
static const Color BLACK
Definition: color.h:157
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26