ESPHome  2022.6.3
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  // now write the new brightness level to the display
116  this->command(SSD1351_CONTRASTMASTER);
117  this->data(int(SSD1351_MAX_CONTRAST * (this->brightness_)));
118 }
119 bool SSD1351::is_on() { return this->is_on_; }
121  this->command(SSD1351_DISPLAYON);
122  this->is_on_ = true;
123 }
125  this->command(SSD1351_DISPLAYOFF);
126  this->is_on_ = false;
127 }
129  switch (this->model_) {
131  return 96;
133  return 128;
134  default:
135  return 0;
136  }
137 }
139  switch (this->model_) {
142  return 128;
143  default:
144  return 0;
145  }
146 }
148  return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * size_t(SSD1351_BYTESPERPIXEL);
149 }
150 void HOT SSD1351::draw_absolute_pixel_internal(int x, int y, Color color) {
151  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
152  return;
153  const uint32_t color565 = display::ColorUtil::color_to_565(color);
154  // where should the bits go in the big buffer array? math...
155  uint16_t pos = (x + y * this->get_width_internal()) * SSD1351_BYTESPERPIXEL;
156  this->buffer_[pos++] = (color565 >> 8) & 0xff;
157  this->buffer_[pos] = color565 & 0xff;
158 }
159 void SSD1351::fill(Color color) {
160  const uint32_t color565 = display::ColorUtil::color_to_565(color);
161  for (uint32_t i = 0; i < this->get_buffer_length_(); i++) {
162  if (i & 1) {
163  this->buffer_[i] = color565 & 0xff;
164  } else {
165  this->buffer_[i] = (color565 >> 8) & 0xff;
166  }
167  }
168 }
170  if (this->reset_pin_ != nullptr) {
171  this->reset_pin_->setup();
172  this->reset_pin_->digital_write(true);
173  delay(1);
174  // Trigger Reset
175  this->reset_pin_->digital_write(false);
176  delay(10);
177  // Wake up
178  this->reset_pin_->digital_write(true);
179  }
180 }
181 const char *SSD1351::model_str_() {
182  switch (this->model_) {
184  return "SSD1351 128x96";
186  return "SSD1351 128x128";
187  default:
188  return "Unknown";
189  }
190 }
191 
192 } // namespace ssd1351_base
193 } // 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)
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
void init_internal_(uint32_t buffer_length)
virtual void command(uint8_t value)=0
static const Color BLACK
Definition: color.h:147
Definition: a4988.cpp:4
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27