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