ESPHome 2025.5.0
Loading...
Searching...
No Matches
ssd1351_base.cpp
Go to the documentation of this file.
1#include "ssd1351_base.h"
2#include "esphome/core/log.h"
4
5namespace esphome {
6namespace ssd1351_base {
7
8static const char *const TAG = "ssd1351";
9
10static const uint16_t SSD1351_COLORMASK = 0xffff;
11static const uint8_t SSD1351_MAX_CONTRAST = 15;
12static const uint8_t SSD1351_BYTESPERPIXEL = 2;
13// SSD1351 commands
14static const uint8_t SSD1351_SETCOLUMN = 0x15;
15static const uint8_t SSD1351_SETROW = 0x75;
16static const uint8_t SSD1351_SETREMAP = 0xA0;
17static const uint8_t SSD1351_STARTLINE = 0xA1;
18static const uint8_t SSD1351_DISPLAYOFFSET = 0xA2;
19static const uint8_t SSD1351_DISPLAYOFF = 0xAE;
20static const uint8_t SSD1351_DISPLAYON = 0xAF;
21static const uint8_t SSD1351_PRECHARGE = 0xB1;
22static const uint8_t SSD1351_CLOCKDIV = 0xB3;
23static const uint8_t SSD1351_PRECHARGELEVEL = 0xBB;
24static const uint8_t SSD1351_VCOMH = 0xBE;
25// display controls
26static const uint8_t SSD1351_DISPLAYALLOFF = 0xA4;
27static const uint8_t SSD1351_DISPLAYALLON = 0xA5;
28static const uint8_t SSD1351_NORMALDISPLAY = 0xA6;
29static const uint8_t SSD1351_INVERTDISPLAY = 0xA7;
30// contrast controls
31static const uint8_t SSD1351_CONTRASTABC = 0xC1;
32static const uint8_t SSD1351_CONTRASTMASTER = 0xC7;
33// memory functions
34static const uint8_t SSD1351_WRITERAM = 0x5C;
35static const uint8_t SSD1351_READRAM = 0x5D;
36// other functions
37static const uint8_t SSD1351_FUNCTIONSELECT = 0xAB;
38static const uint8_t SSD1351_DISPLAYENHANCE = 0xB2;
39static const uint8_t SSD1351_SETVSL = 0xB4;
40static const uint8_t SSD1351_SETGPIO = 0xB5;
41static const uint8_t SSD1351_PRECHARGE2 = 0xB6;
42static const uint8_t SSD1351_SETGRAY = 0xB8;
43static const uint8_t SSD1351_USELUT = 0xB9;
44static const uint8_t SSD1351_MUXRATIO = 0xCA;
45static const uint8_t SSD1351_COMMANDLOCK = 0xFD;
46static const uint8_t SSD1351_HORIZSCROLL = 0x96;
47static const uint8_t SSD1351_STOPSCROLL = 0x9E;
48static const uint8_t SSD1351_STARTSCROLL = 0x9F;
49
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}
106void 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}
122bool 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}
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}
162void 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}
184const 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
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)
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:28
static const Color BLACK
Definition color.h:168
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6