ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ssd1325_base.cpp
Go to the documentation of this file.
1#include "ssd1325_base.h"
3#include "esphome/core/log.h"
4
6
7static const char *const TAG = "ssd1325";
8
9static const uint8_t SSD1325_MAX_CONTRAST = 127;
10static const uint8_t SSD1325_COLORMASK = 0x0f;
11static const uint8_t SSD1325_COLORSHIFT = 4;
12static const uint8_t SSD1325_PIXELSPERBYTE = 2;
13
14static const uint8_t SSD1325_SETCOLADDR = 0x15;
15static const uint8_t SSD1325_SETROWADDR = 0x75;
16static const uint8_t SSD1325_SETCONTRAST = 0x81;
17static const uint8_t SSD1325_SETCURRENT = 0x84;
18
19static const uint8_t SSD1325_SETREMAP = 0xA0;
20static const uint8_t SSD1325_SETSTARTLINE = 0xA1;
21static const uint8_t SSD1325_SETOFFSET = 0xA2;
22static const uint8_t SSD1325_NORMALDISPLAY = 0xA4;
23static const uint8_t SSD1325_DISPLAYALLON = 0xA5;
24static const uint8_t SSD1325_DISPLAYALLOFF = 0xA6;
25static const uint8_t SSD1325_INVERTDISPLAY = 0xA7;
26static const uint8_t SSD1325_SETMULTIPLEX = 0xA8;
27static const uint8_t SSD1325_MASTERCONFIG = 0xAD;
28static const uint8_t SSD1325_DISPLAYOFF = 0xAE;
29static const uint8_t SSD1325_DISPLAYON = 0xAF;
30
31static const uint8_t SSD1325_SETPRECHARGECOMPENABLE = 0xB0;
32static const uint8_t SSD1325_SETPHASELEN = 0xB1;
33static const uint8_t SSD1325_SETROWPERIOD = 0xB2;
34static const uint8_t SSD1325_SETCLOCK = 0xB3;
35static const uint8_t SSD1325_SETPRECHARGECOMP = 0xB4;
36static const uint8_t SSD1325_SETGRAYTABLE = 0xB8;
37static const uint8_t SSD1325_SETDEFAULTGRAYTABLE = 0xB9;
38static const uint8_t SSD1325_SETPRECHARGEVOLTAGE = 0xBC;
39static const uint8_t SSD1325_SETVCOMLEVEL = 0xBE;
40static const uint8_t SSD1325_SETVSL = 0xBF;
41
42static const uint8_t SSD1325_GFXACCEL = 0x23;
43static const uint8_t SSD1325_DRAWRECT = 0x24;
44static const uint8_t SSD1325_COPY = 0x25;
45
48
49 this->command(SSD1325_DISPLAYOFF); // display off
50 this->command(SSD1325_SETCLOCK); // set osc division
51 this->command(0xF1); // 145
52 this->command(SSD1325_SETMULTIPLEX); // multiplex ratio
53 if (this->model_ == SSD1327_MODEL_128_128) {
54 this->command(0x7f); // duty = height - 1
55 } else {
56 this->command(0x3f); // duty = 1/64
57 }
58 this->command(SSD1325_SETOFFSET); // set display offset
59 if (this->model_ == SSD1327_MODEL_128_128) {
60 this->command(0x00); // 0
61 } else {
62 this->command(0x4C); // 76
63 }
64 this->command(SSD1325_SETSTARTLINE); // set start line
65 this->command(0x00); // ...
66 this->command(SSD1325_MASTERCONFIG); // Set Master Config DC/DC Converter
67 this->command(0x02);
68 this->command(SSD1325_SETREMAP); // set segment remapping
69 if (this->model_ == SSD1327_MODEL_128_128) {
70 this->command(0x53); // COM bottom-up, split odd/even, enable column and nibble remapping
71 } else {
72 this->command(0x50); // COM bottom-up, split odd/even
73 }
74 this->command(SSD1325_SETCURRENT + 0x2); // Set Full Current Range
75 this->command(SSD1325_SETGRAYTABLE);
76 // gamma ~2.2
77 if (this->model_ == SSD1327_MODEL_128_128) {
78 this->command(0);
79 this->command(1);
80 this->command(2);
81 this->command(3);
82 this->command(6);
83 this->command(8);
84 this->command(12);
85 this->command(16);
86 this->command(20);
87 this->command(26);
88 this->command(32);
89 this->command(39);
90 this->command(46);
91 this->command(54);
92 this->command(63);
93 } else {
94 this->command(0x01);
95 this->command(0x11);
96 this->command(0x22);
97 this->command(0x32);
98 this->command(0x43);
99 this->command(0x54);
100 this->command(0x65);
101 this->command(0x76);
102 }
103 this->command(SSD1325_SETROWPERIOD);
104 this->command(0x51);
105 this->command(SSD1325_SETPHASELEN);
106 this->command(0x55);
107 this->command(SSD1325_SETPRECHARGECOMP);
108 this->command(0x02);
109 this->command(SSD1325_SETPRECHARGECOMPENABLE);
110 this->command(0x28);
111 this->command(SSD1325_SETVCOMLEVEL); // Set High Voltage Level of COM Pin
112 this->command(0x1C);
113 this->command(SSD1325_SETVSL); // set Low Voltage Level of SEG Pin
114 this->command(0x0D | 0x02);
115 this->command(SSD1325_NORMALDISPLAY); // set display mode
117 this->fill(Color::BLACK); // clear display - ensures we do not see garbage at power-on
118 this->display(); // ...write buffer, which actually clears the display's memory
119 this->turn_on(); // display ON
120}
122 this->command(SSD1325_SETCOLADDR); // set column address
123 this->command(0x00); // set column start address
124 this->command(0x3F); // set column end address
125 this->command(SSD1325_SETROWADDR); // set row address
126 this->command(0x00); // set row start address
127 if (this->model_ == SSD1327_MODEL_128_128) {
128 this->command(127); // set last row
129 } else {
130 this->command(63); // set last row
131 }
132
133 this->write_display_data();
134}
136 this->do_update_();
137 this->display();
138}
139void SSD1325::set_brightness(float brightness) {
140 // validation
141 if (brightness > 1) {
142 this->brightness_ = 1.0;
143 } else if (brightness < 0) {
144 this->brightness_ = 0;
145 } else {
146 this->brightness_ = brightness;
147 }
148 // now write the new brightness level to the display
149 this->command(SSD1325_SETCONTRAST);
150 this->command(int(SSD1325_MAX_CONTRAST * (this->brightness_)));
151}
152bool SSD1325::is_on() { return this->is_on_; }
154 this->command(SSD1325_DISPLAYON);
155 this->is_on_ = true;
156}
158 this->command(SSD1325_DISPLAYOFF);
159 this->is_on_ = false;
160}
162 switch (this->model_) {
164 return 32;
166 return 64;
168 return 16;
170 return 48;
172 return 128;
173 default:
174 return 0;
175 }
176}
178 switch (this->model_) {
182 return 128;
184 return 96;
186 return 64;
187 default:
188 return 0;
189 }
190}
192 return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / SSD1325_PIXELSPERBYTE;
193}
195 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
196 return;
198 // where should the bits go in the big buffer array? math...
199 uint16_t pos = (x / SSD1325_PIXELSPERBYTE) + (y * this->get_width_internal() / SSD1325_PIXELSPERBYTE);
200 uint8_t shift = (x % SSD1325_PIXELSPERBYTE) * SSD1325_COLORSHIFT;
201 // ensure 'color4' is valid (only 4 bits aka 1 nibble) and shift the bits left when necessary
202 color4 = (color4 & SSD1325_COLORMASK) << shift;
203 // first mask off the nibble we must change...
204 this->buffer_[pos] &= (static_cast<uint8_t>(~SSD1325_COLORMASK) >> shift);
205 // ...then lay the new nibble back on top. done!
206 this->buffer_[pos] |= color4;
207}
208void SSD1325::fill(Color color) {
210 uint8_t fill = (color4 & SSD1325_COLORMASK) | ((color4 & SSD1325_COLORMASK) << SSD1325_COLORSHIFT);
211 for (uint32_t i = 0; i < this->get_buffer_length_(); i++)
212 this->buffer_[i] = fill;
213}
215 if (this->reset_pin_ != nullptr) {
216 this->reset_pin_->setup();
217 this->reset_pin_->digital_write(true);
218 delay(1);
219 // Trigger Reset
220 this->reset_pin_->digital_write(false);
221 delay(10);
222 // Wake up
223 this->reset_pin_->digital_write(true);
224 }
225}
226const char *SSD1325::model_str_() {
227 switch (this->model_) {
229 return "SSD1325 128x32";
231 return "SSD1325 128x64";
233 return "SSD1325 96x16";
235 return "SSD1325 64x48";
237 return "SSD1327 128x128";
238 default:
239 return "Unknown";
240 }
241}
242
243} // namespace esphome::ssd1325_base
virtual void setup()=0
virtual void digital_write(bool value)=0
static uint32_t color_to_grayscale4(Color color)
void init_internal_(uint32_t buffer_length)
void fill(Color color) override
void draw_absolute_pixel_internal(int x, int y, Color color) override
void set_brightness(float brightness)
virtual void command(uint8_t value)=0
virtual void write_display_data()=0
size_t size_t pos
Definition helpers.h:1038
void HOT delay(uint32_t ms)
Definition hal.cpp:82
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