ESPHome 2026.5.1
Loading...
Searching...
No Matches
ssd1322_base.cpp
Go to the documentation of this file.
1#include "ssd1322_base.h"
3#include "esphome/core/log.h"
4
6
7static const char *const TAG = "ssd1322";
8
9static const uint8_t SSD1322_MAX_CONTRAST = 255;
10static const uint8_t SSD1322_COLORMASK = 0x0f;
11static const uint8_t SSD1322_COLORSHIFT = 4;
12static const uint8_t SSD1322_PIXELSPERBYTE = 2;
13
14static const uint8_t SSD1322_ENABLEGRAYSCALETABLE = 0x00;
15static const uint8_t SSD1322_SETCOLUMNADDRESS = 0x15;
16static const uint8_t SSD1322_WRITERAM = 0x5C;
17static const uint8_t SSD1322_READRAM = 0x5D;
18static const uint8_t SSD1322_SETROWADDRESS = 0x75;
19static const uint8_t SSD1322_SETREMAP = 0xA0;
20static const uint8_t SSD1322_SETSTARTLINE = 0xA1;
21static const uint8_t SSD1322_SETOFFSET = 0xA2;
22static const uint8_t SSD1322_SETMODEALLOFF = 0xA4;
23static const uint8_t SSD1322_SETMODEALLON = 0xA5;
24static const uint8_t SSD1322_SETMODENORMAL = 0xA6;
25static const uint8_t SSD1322_SETMODEINVERTED = 0xA7;
26static const uint8_t SSD1322_ENABLEPARTIALDISPLAY = 0xA8;
27static const uint8_t SSD1322_EXITPARTIALDISPLAY = 0xA9;
28static const uint8_t SSD1322_SETFUNCTIONSELECTION = 0xAB;
29static const uint8_t SSD1322_SETDISPLAYOFF = 0xAE;
30static const uint8_t SSD1322_SETDISPLAYON = 0xAF;
31static const uint8_t SSD1322_SETPHASELENGTH = 0xB1;
32static const uint8_t SSD1322_SETFRONTCLOCKDIVIDER = 0xB3;
33static const uint8_t SSD1322_DISPLAYENHANCEMENTA = 0xB4;
34static const uint8_t SSD1322_SETGPIO = 0xB5;
35static const uint8_t SSD1322_SETSECONDPRECHARGEPERIOD = 0xB6;
36static const uint8_t SSD1322_SETGRAYSCALETABLE = 0xB8;
37static const uint8_t SSD1322_SELECTDEFAULTLINEARGRAYSCALETABLE = 0xB9;
38static const uint8_t SSD1322_SETPRECHARGEVOLTAGE = 0xBB;
39static const uint8_t SSD1322_SETVCOMHVOLTAGE = 0xBE;
40static const uint8_t SSD1322_SETCONTRAST = 0xC1;
41static const uint8_t SSD1322_MASTERCURRENTCONTROL = 0xC7;
42static const uint8_t SSD1322_SETMULTIPLEXRATIO = 0xCA;
43static const uint8_t SSD1322_DISPLAYENHANCEMENTB = 0xD1;
44static const uint8_t SSD1322_SETCOMMANDLOCK = 0xFD;
45
46static const uint8_t SSD1322_SETCOMMANDLOCK_UNLOCK = 0x12;
47static const uint8_t SSD1322_SETCOMMANDLOCK_LOCK = 0x16;
48
51
52 this->command(SSD1322_SETCOMMANDLOCK);
53 this->data(SSD1322_SETCOMMANDLOCK_UNLOCK);
54 this->turn_off();
55 this->command(SSD1322_SETFRONTCLOCKDIVIDER);
56 this->data(0x91);
57 this->command(SSD1322_SETMULTIPLEXRATIO);
58 this->data(0x3F);
59 this->command(SSD1322_SETOFFSET);
60 this->data(0x00);
61 this->command(SSD1322_SETSTARTLINE);
62 this->data(0x00);
63 this->command(SSD1322_SETREMAP);
64 this->data(0x14);
65 this->data(0x11);
66 this->command(SSD1322_SETGPIO);
67 this->data(0x00);
68 this->command(SSD1322_SETFUNCTIONSELECTION);
69 this->data(0x01);
70 this->command(SSD1322_DISPLAYENHANCEMENTA);
71 this->data(0xA0);
72 this->data(0xFD);
73 this->command(SSD1322_MASTERCURRENTCONTROL);
74 this->data(0x0F);
75 this->command(SSD1322_SETPHASELENGTH);
76 this->data(0xE2);
77 this->command(SSD1322_DISPLAYENHANCEMENTB);
78 this->data(0x82);
79 this->data(0x20);
80 this->command(SSD1322_SETPRECHARGEVOLTAGE);
81 this->data(0x1F);
82 this->command(SSD1322_SETSECONDPRECHARGEPERIOD);
83 this->data(0x08);
84 this->command(SSD1322_SETVCOMHVOLTAGE);
85 this->data(0x07);
86 this->command(SSD1322_SETMODENORMAL);
87 this->command(SSD1322_EXITPARTIALDISPLAY);
88 // this->command(SSD1322_SELECTDEFAULTLINEARGRAYSCALETABLE);
89 this->command(SSD1322_SETGRAYSCALETABLE);
90 // gamma ~2.2
91 this->data(24);
92 this->data(29);
93 this->data(36);
94 this->data(43);
95 this->data(51);
96 this->data(60);
97 this->data(70);
98 this->data(81);
99 this->data(93);
100 this->data(105);
101 this->data(118);
102 this->data(132);
103 this->data(147);
104 this->data(163);
105 this->data(180);
106 this->command(SSD1322_ENABLEGRAYSCALETABLE);
108 this->fill(Color::BLACK); // clear display - ensures we do not see garbage at power-on
109 this->display(); // ...write buffer, which actually clears the display's memory
110 this->turn_on(); // display ON
111}
113 this->command(SSD1322_SETCOLUMNADDRESS); // set column address
114 this->data(0x1C); // set column start address
115 this->data(0x5B); // set column end address
116 this->command(SSD1322_SETROWADDRESS); // set row address
117 this->data(0x00); // set row start address
118 this->data(0x3F); // set last row
119 this->command(SSD1322_WRITERAM); // write
120
121 this->write_display_data();
122}
124 this->do_update_();
125 this->display();
126}
127void SSD1322::set_brightness(float brightness) {
128 this->brightness_ = clamp(brightness, 0.0F, 1.0F);
129 // now write the new brightness level to the display
130 this->command(SSD1322_SETCONTRAST);
131 this->data(int(SSD1322_MAX_CONTRAST * (this->brightness_)));
132}
133bool SSD1322::is_on() { return this->is_on_; }
135 this->command(SSD1322_SETDISPLAYON);
136 this->is_on_ = true;
137}
139 this->command(SSD1322_SETDISPLAYOFF);
140 this->is_on_ = false;
141}
143 switch (this->model_) {
145 return 64;
146 default:
147 return 0;
148 }
149}
151 switch (this->model_) {
153 return 256;
154 default:
155 return 0;
156 }
157}
159 return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / SSD1322_PIXELSPERBYTE;
160}
162 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
163 return;
165 // where should the bits go in the big buffer array? math...
166 uint16_t pos = (x / SSD1322_PIXELSPERBYTE) + (y * this->get_width_internal() / SSD1322_PIXELSPERBYTE);
167 uint8_t shift = (1u - (x % SSD1322_PIXELSPERBYTE)) * SSD1322_COLORSHIFT;
168 // ensure 'color4' is valid (only 4 bits aka 1 nibble) and shift the bits left when necessary
169 color4 = (color4 & SSD1322_COLORMASK) << shift;
170 // first mask off the nibble we must change...
171 this->buffer_[pos] &= (static_cast<uint8_t>(~SSD1322_COLORMASK) >> shift);
172 // ...then lay the new nibble back on top. done!
173 this->buffer_[pos] |= color4;
174}
175void SSD1322::fill(Color color) {
176 // If clipping is active, fall back to base implementation
177 if (this->get_clipping().is_set()) {
178 Display::fill(color);
179 return;
180 }
181
183 uint8_t fill = (color4 & SSD1322_COLORMASK) | ((color4 & SSD1322_COLORMASK) << SSD1322_COLORSHIFT);
184 for (uint32_t i = 0; i < this->get_buffer_length_(); i++)
185 this->buffer_[i] = fill;
186}
188 if (this->reset_pin_ != nullptr) {
189 this->reset_pin_->setup();
190 this->reset_pin_->digital_write(true);
191 delay(1);
192 // Trigger Reset
193 this->reset_pin_->digital_write(false);
194 delay(10);
195 // Wake up
196 this->reset_pin_->digital_write(true);
197 }
198}
199const char *SSD1322::model_str_() {
200 switch (this->model_) {
202 return "SSD1322 256x64";
203 default:
204 return "Unknown";
205 }
206}
207
208} // namespace esphome::ssd1322_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)
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:765
virtual void data(uint8_t value)=0
virtual void command(uint8_t value)=0
void fill(Color color) override
void set_brightness(float brightness)
virtual void write_display_data()=0
void draw_absolute_pixel_internal(int x, int y, Color color) override
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