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