ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
sh1122_base.cpp
Go to the documentation of this file.
1#include "sh1122_base.h"
3#include "esphome/core/log.h"
4
5#include <cstring>
6
8
9ESPHOME_LOG_TAG(TAG, "sh1122");
10
11static constexpr uint8_t SH1122_MAX_CONTRAST = 255;
12static constexpr uint8_t SH1122_COLORMASK = 0x0f;
13static constexpr uint8_t SH1122_COLORSHIFT = 4;
14static constexpr uint8_t SH1122_PIXELSPERBYTE = 2;
15
16static constexpr uint8_t SH1122_SETLOWCOLUMNADDRESS = 0x00;
17static constexpr uint8_t SH1122_SETHIGHCOLUMNADDRESS = 0x10;
18static constexpr uint8_t SH1122_SETDISCHARGEVSLLEVEL = 0x30;
19static constexpr uint8_t SH1122_SETDISPLAYSTARTLINE = 0x40;
20static constexpr uint8_t SH1122_SETCONTRASTCURRENT = 0x81;
21static constexpr uint8_t SH1122_SETSEGMENTREMAP = 0xA0;
22static constexpr uint8_t SH1122_SETDISPLAYOFFON = 0xA4;
23static constexpr uint8_t SH1122_SETNORMALDISPLAY = 0xA6;
24static constexpr uint8_t SH1122_SETMULTIPLEXRATIO = 0xA8;
25static constexpr uint8_t SH1122_DCDCSETTING = 0xAD;
26static constexpr uint8_t SH1122_SETDISPLAYOFF = 0xAE;
27static constexpr uint8_t SH1122_SETDISPLAYON = 0xAF;
28static constexpr uint8_t SH1122_SETROWADDRESS = 0xB0;
29static constexpr uint8_t SH1122_SETSCANDIRECTION = 0xC0;
30static constexpr uint8_t SH1122_SETDISPLAYOFFSET = 0xD3;
31static constexpr uint8_t SH1122_SETCLOCKDIVIDER = 0xD5;
32static constexpr uint8_t SH1122_SETDISCHARGEPRECHARGEPERIOD = 0xD9;
33static constexpr uint8_t SH1122_SETVCOMDESELECTLEVEL = 0xDB;
34static constexpr uint8_t SH1122_SETVSEGMLEVEL = 0xDC;
35
38 if (this->buffer_ == nullptr) {
39 this->mark_failed();
40 return;
41 }
42
43 this->turn_off();
44 this->command2_(SH1122_SETCLOCKDIVIDER, 0x50);
45 this->command2_(SH1122_SETMULTIPLEXRATIO, 0x3F);
46 this->command2_(SH1122_SETDISPLAYOFFSET, 0x00);
47 this->command2_(SH1122_SETROWADDRESS, 0x00);
48 this->command_(SH1122_SETDISPLAYSTARTLINE | 32); // the panel's rows start half way down the 128 line RAM
49 this->command_(SH1122_SETDISCHARGEVSLLEVEL);
50 this->command2_(SH1122_DCDCSETTING, 0x81);
51 this->command_(SH1122_SETSEGMENTREMAP | 0x01); // column 0 on the right
52 this->command_(SH1122_SETSCANDIRECTION | 0x08); // rows scanned bottom up
53 this->command2_(SH1122_SETDISCHARGEPRECHARGEPERIOD, 0x28);
54 this->command2_(SH1122_SETVCOMDESELECTLEVEL, 0x35);
55 this->command2_(SH1122_SETVSEGMLEVEL, 0x35);
56 this->command_(SH1122_SETNORMALDISPLAY);
57 this->command_(SH1122_SETHIGHCOLUMNADDRESS);
58 this->command_(SH1122_SETLOWCOLUMNADDRESS);
59 this->command_(SH1122_SETDISPLAYOFFON);
60 this->set_brightness(this->brightness_);
61 this->display(); // the buffer starts cleared; push it so no power-on garbage shows
62 this->turn_on();
63}
65 // Return the RAM pointer to the top left corner in one transfer, then stream the frame
66 const uint8_t set_address[] = {SH1122_SETHIGHCOLUMNADDRESS, SH1122_SETLOWCOLUMNADDRESS, SH1122_SETROWADDRESS, 0x00};
67 this->write_command(set_address, sizeof(set_address));
68 this->write_display_data();
69}
71 this->do_update_();
72 this->display();
73}
74void SH1122::set_brightness(float brightness) {
75 this->brightness_ = clamp(brightness, 0.0F, 1.0F);
76 this->command2_(SH1122_SETCONTRASTCURRENT, int(SH1122_MAX_CONTRAST * (this->brightness_)));
77}
79 this->command_(SH1122_SETDISPLAYON);
80 this->is_on_ = true;
81}
83 this->command_(SH1122_SETDISPLAYOFF);
84 this->is_on_ = false;
85}
87 switch (this->model_) {
89 return 64;
90 default:
91 return 0;
92 }
93}
95 switch (this->model_) {
97 return 256;
98 default:
99 return 0;
100 }
101}
103 return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / SH1122_PIXELSPERBYTE;
104}
106 const int width = this->get_width_internal();
107 if (x >= width || x < 0 || y >= this->get_height_internal() || y < 0)
108 return;
109 // Two pixels per byte, the left one in the high nibble
110 const uint32_t color4 = display::ColorUtil::color_to_grayscale4(color) & SH1122_COLORMASK;
111 const uint16_t pos = (x / SH1122_PIXELSPERBYTE) + (y * width / SH1122_PIXELSPERBYTE);
112 const uint8_t shift = (1u - (x % SH1122_PIXELSPERBYTE)) * SH1122_COLORSHIFT;
113 this->buffer_[pos] = (this->buffer_[pos] & (static_cast<uint8_t>(~SH1122_COLORMASK) >> shift)) | (color4 << shift);
114}
115void SH1122::fill(Color color) {
116 // If clipping is active, fall back to base implementation
117 if (this->get_clipping().is_set()) {
118 Display::fill(color);
119 return;
120 }
121 const uint32_t color4 = display::ColorUtil::color_to_grayscale4(color);
122 const uint8_t fill = (color4 & SH1122_COLORMASK) | ((color4 & SH1122_COLORMASK) << SH1122_COLORSHIFT);
123 memset(this->buffer_, fill, this->get_buffer_length_());
124}
126 if (this->reset_pin_ != nullptr) {
127 this->reset_pin_->setup();
128 this->reset_pin_->digital_write(true);
129 delay(1);
130 // Trigger Reset
131 this->reset_pin_->digital_write(false);
132 delay(10);
133 // Wake up
134 this->reset_pin_->digital_write(true);
135 }
136}
137const LogString *SH1122::model_str_() {
138 switch (this->model_) {
140 return LOG_STR("SH1122 256x64");
141 default:
142 return LOG_STR("Unknown");
143 }
144}
145
146} // namespace esphome::sh1122_base
void mark_failed()
Mark this component as failed.
virtual void setup()=0
virtual void digital_write(bool value)=0
void init_internal_(uint32_t buffer_length)
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:826
void command_(uint8_t value)
Definition sh1122_base.h:39
void draw_absolute_pixel_internal(int x, int y, Color color) override
void fill(Color color) override
virtual void write_display_data()=0
void command2_(uint8_t value, uint8_t data)
Definition sh1122_base.h:40
int get_width_internal() override
int get_height_internal() override
virtual void write_command(const uint8_t *bytes, size_t len)=0
Sends a command byte and its arguments; the SH1122 takes command arguments with D/C low as well.
const LogString * model_str_()
void set_brightness(float brightness)
ESPHOME_LOG_TAG(TAG, "sh1122")
size_t size_t pos
Definition helpers.h:1179
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6