ESPHome 2026.5.1
Loading...
Searching...
No Matches
ssd1327_base.cpp
Go to the documentation of this file.
1#include "ssd1327_base.h"
3#include "esphome/core/log.h"
4
6
7static const char *const TAG = "ssd1327";
8
9static const uint8_t SSD1327_MAX_CONTRAST = 127;
10static const uint8_t SSD1327_COLORMASK = 0x0f;
11static const uint8_t SSD1327_COLORSHIFT = 4;
12static const uint8_t SSD1327_PIXELSPERBYTE = 2;
13
14static const uint8_t SSD1327_SETCOLUMNADDRESS = 0x15;
15static const uint8_t SSD1327_SETROWADDRESS = 0x75;
16static const uint8_t SSD1327_SETCONTRAST = 0x81;
17static const uint8_t SSD1327_SETREMAP = 0xA0;
18static const uint8_t SSD1327_SETSTARTLINE = 0xA1;
19static const uint8_t SSD1327_SETOFFSET = 0xA2;
20static const uint8_t SSD1327_NORMALDISPLAY = 0xA4;
21static const uint8_t SSD1327_DISPLAYALLON = 0xA5;
22static const uint8_t SSD1327_DISPLAYALLOFF = 0xA6;
23static const uint8_t SSD1327_INVERTDISPLAY = 0xA7;
24static const uint8_t SSD1327_SETMULTIPLEX = 0xA8;
25static const uint8_t SSD1327_FUNCTIONSELECTIONA = 0xAB;
26static const uint8_t SSD1327_DISPLAYOFF = 0xAE;
27static const uint8_t SSD1327_DISPLAYON = 0xAF;
28static const uint8_t SSD1327_SETPHASELENGTH = 0xB1;
29static const uint8_t SSD1327_SETFRONTCLOCKDIVIDER = 0xB3;
30static const uint8_t SSD1327_SETGPIO = 0xB5;
31static const uint8_t SSD1327_SETSECONDPRECHARGEPERIOD = 0xB6;
32static const uint8_t SSD1327_SETGRAYSCALETABLE = 0xB8;
33static const uint8_t SSD1327_SELECTDEFAULTLINEARGRAYSCALETABLE = 0xB9;
34static const uint8_t SSD1327_SETPRECHARGEVOLTAGE = 0xBC;
35static const uint8_t SSD1327_SETVCOMHVOLTAGE = 0xBE;
36static const uint8_t SSD1327_FUNCTIONSELECTIONB = 0xD5;
37static const uint8_t SSD1327_SETCOMMANDLOCK = 0xFD;
38static const uint8_t SSD1327_HORIZONTALSCROLLRIGHTSETUP = 0x26;
39static const uint8_t SSD1327_HORIZONTALSCROLLLEFTSETUP = 0x27;
40static const uint8_t SSD1327_DEACTIVATESCROLL = 0x2E;
41static const uint8_t SSD1327_ACTIVATESCROLL = 0x2F;
42
45
46 this->turn_off(); // display OFF
47 this->command(SSD1327_SETFRONTCLOCKDIVIDER); // set osc division
48 this->command(0xF1); // 145
49 this->command(SSD1327_SETMULTIPLEX); // multiplex ratio
50 this->command(0x7f); // duty = height - 1
51 this->command(SSD1327_SETOFFSET); // set display offset
52 this->command(0x00); // 0
53 this->command(SSD1327_SETSTARTLINE); // set start line
54 this->command(0x00); // ...
55 this->command(SSD1327_SETREMAP); // set segment remapping
56 this->command(0x53); // COM bottom-up, split odd/even, enable column and nibble remapping
57 this->command(SSD1327_SETGRAYSCALETABLE);
58 // gamma ~2.2
59 this->command(0);
60 this->command(1);
61 this->command(2);
62 this->command(3);
63 this->command(6);
64 this->command(8);
65 this->command(12);
66 this->command(16);
67 this->command(20);
68 this->command(26);
69 this->command(32);
70 this->command(39);
71 this->command(46);
72 this->command(54);
73 this->command(63);
74 this->command(SSD1327_SETPHASELENGTH);
75 this->command(0x55);
76 this->command(SSD1327_SETVCOMHVOLTAGE); // Set High Voltage Level of COM Pin
77 this->command(0x1C);
78 this->command(SSD1327_SETGPIO); // Switch voltage converter on (for Aliexpress display)
79 this->command(0x03);
80 this->command(SSD1327_NORMALDISPLAY); // set display mode
82 this->fill(Color::BLACK); // clear display - ensures we do not see garbage at power-on
83 this->display(); // ...write buffer, which actually clears the display's memory
84 this->turn_on(); // display ON
85}
87 this->command(SSD1327_SETCOLUMNADDRESS); // set column address
88 this->command(0x00); // set column start address
89 this->command(0x3F); // set column end address
90 this->command(SSD1327_SETROWADDRESS); // set row address
91 this->command(0x00); // set row start address
92 this->command(127); // set last row
93
94 this->write_display_data();
95}
97 if (!this->is_failed()) {
98 this->do_update_();
99 this->display();
100 }
101}
102void SSD1327::set_brightness(float brightness) {
103 // validation
104 this->brightness_ = clamp(brightness, 0.0F, 1.0F);
105 // now write the new brightness level to the display
106 this->command(SSD1327_SETCONTRAST);
107 this->command(int(SSD1327_MAX_CONTRAST * (this->brightness_)));
108}
109bool SSD1327::is_on() { return this->is_on_; }
111 this->command(SSD1327_DISPLAYON);
112 this->is_on_ = true;
113}
115 this->command(SSD1327_DISPLAYOFF);
116 this->is_on_ = false;
117}
119 switch (this->model_) {
121 return 128;
122 default:
123 return 0;
124 }
125}
127 switch (this->model_) {
129 return 128;
130 default:
131 return 0;
132 }
133}
135 return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / SSD1327_PIXELSPERBYTE;
136}
138 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
139 return;
141 // where should the bits go in the big buffer array? math...
142 uint16_t pos = (x / SSD1327_PIXELSPERBYTE) + (y * this->get_width_internal() / SSD1327_PIXELSPERBYTE);
143 uint8_t shift = (x % SSD1327_PIXELSPERBYTE) * SSD1327_COLORSHIFT;
144 // ensure 'color4' is valid (only 4 bits aka 1 nibble) and shift the bits left when necessary
145 color4 = (color4 & SSD1327_COLORMASK) << shift;
146 // first mask off the nibble we must change...
147 this->buffer_[pos] &= (static_cast<uint8_t>(~SSD1327_COLORMASK) >> shift);
148 // ...then lay the new nibble back on top. done!
149 this->buffer_[pos] |= color4;
150}
151void SSD1327::fill(Color color) {
152 // If clipping is active, fall back to base implementation
153 if (this->get_clipping().is_set()) {
154 Display::fill(color);
155 return;
156 }
157
159 uint8_t fill = (color4 & SSD1327_COLORMASK) | ((color4 & SSD1327_COLORMASK) << SSD1327_COLORSHIFT);
160 for (uint32_t i = 0; i < this->get_buffer_length_(); i++)
161 this->buffer_[i] = fill;
162}
164 if (this->reset_pin_ != nullptr) {
165 this->reset_pin_->setup();
166 this->reset_pin_->digital_write(true);
167 delay(1);
168 // Trigger Reset
169 this->reset_pin_->digital_write(false);
170 delay(10);
171 // Wake up
172 this->reset_pin_->digital_write(true);
173 }
174}
175const char *SSD1327::model_str_() {
176 switch (this->model_) {
178 return "SSD1327 128x128";
179 default:
180 return "Unknown";
181 }
182}
183
184} // namespace esphome::ssd1327_base
bool is_failed() const
Definition component.h:284
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 write_display_data()=0
void set_brightness(float brightness)
void draw_absolute_pixel_internal(int x, int y, Color color) override
virtual void command(uint8_t value)=0
void fill(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