ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
st7567_base.cpp
Go to the documentation of this file.
1#include "st7567_base.h"
3#include "esphome/core/log.h"
4
6
7static const char *const TAG = "st7567";
8
11 this->display_init_();
12}
13
15 ESP_LOGD(TAG, "Initializing display");
17 this->clear();
18 this->write_display_data();
19 this->command(ST7567_DISPLAY_ON);
20}
21
23 this->command(ST7567_BIAS_9);
24 this->command(this->mirror_x_ ? ST7567_SEG_REVERSE : ST7567_SEG_NORMAL);
25 this->command(this->mirror_y_ ? ST7567_COM_NORMAL : ST7567_COM_REMAP);
26 this->command(ST7567_POWER_CTL | 0x4);
27 this->command(ST7567_POWER_CTL | 0x6);
28 this->command(ST7567_POWER_CTL | 0x7);
29
30 this->set_brightness(this->brightness_);
31 this->set_contrast(this->contrast_);
32
33 this->command(ST7567_INVERT_OFF | this->invert_colors_);
34
35 this->command(ST7567_BOOSTER_ON);
36 this->command(ST7567_REGULATOR_ON);
37 this->command(ST7567_POWER_ON);
38
39 this->command(ST7567_SCAN_START_LINE);
40 this->command(ST7567_PIXELS_NORMAL | this->all_pixels_on_);
41}
42
44 ESP_LOGD(TAG, "Performing refresh sequence");
45 this->command(ST7567_SW_REFRESH);
47}
48
50 // as per datasheet: It is recommended to use the refresh sequence regularly in a specified interval.
51 this->refresh_requested_ = true;
52}
53
55 this->do_update_();
56 if (this->refresh_requested_) {
57 this->refresh_requested_ = false;
58 this->display_sw_refresh_();
59 }
60 this->write_display_data();
61}
62
63void ST7567::set_all_pixels_on(bool enable) {
64 this->all_pixels_on_ = enable;
65 this->command(ST7567_PIXELS_NORMAL | this->all_pixels_on_);
66}
67
68void ST7567::set_invert_colors(bool invert_colors) {
69 this->invert_colors_ = invert_colors;
70 this->command(ST7567_INVERT_OFF | this->invert_colors_);
71}
72
74 this->contrast_ = val & 0b111111;
75 // 0..63, 26 is normal
76
77 // two byte command
78 // first byte 0x81
79 // second byte 0-63
80
81 this->command(ST7567_SET_EV_CMD);
82 this->command(this->contrast_);
83}
84
86 this->brightness_ = val & 0b111;
87 // 0..7, 5 normal
88
89 //********Adjust display brightness********
90 // 0x20-0x27 is the internal Rb/Ra resistance
91 // adjustment setting of V5 voltage RR=4.5V
92
93 this->command(ST7567_RESISTOR_RATIO | this->brightness_);
94}
95
96bool ST7567::is_on() { return this->is_on_; }
97
99 this->command(ST7567_DISPLAY_ON);
100 this->is_on_ = true;
101}
102
104 this->command(ST7567_DISPLAY_OFF);
105 this->is_on_ = false;
106}
107
108void ST7567::set_scroll(uint8_t line) { this->start_line_ = line % this->get_height_internal(); }
109
110int ST7567::get_width_internal() { return 128; }
111
112int ST7567::get_height_internal() { return 64; }
113
114// 128x64, but memory size 132x64, line starts from 0, but if mirrored then it starts from 131, not 127
116 return size_t(this->get_width_internal() + 4) * size_t(this->get_height_internal()) / 8u;
117}
118
120 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
121 return;
122 }
123
124 uint16_t pos = x + (y / 8) * this->get_width_internal();
125 uint8_t subpos = y & 0x07;
126 if (color.is_on()) {
127 this->buffer_[pos] |= (1 << subpos);
128 } else {
129 this->buffer_[pos] &= ~(1 << subpos);
130 }
131}
132
133void ST7567::fill(Color color) {
134 // If clipping is active, fall back to base implementation
135 if (this->get_clipping().is_set()) {
136 Display::fill(color);
137 return;
138 }
139
140 uint8_t fill = color.is_on() ? 0xFF : 0x00;
141 memset(buffer_, fill, this->get_buffer_length_());
142}
143
145 if (this->reset_pin_ != nullptr) {
146 this->reset_pin_->setup();
147 this->reset_pin_->digital_write(true);
148 delay(1);
149 // Trigger Reset
150 this->reset_pin_->digital_write(false);
151 delay(10);
152 // Wake up
153 this->reset_pin_->digital_write(true);
154 }
155}
156
157const char *ST7567::model_str_() { return "ST7567 128x64"; }
158
159} // namespace esphome::st7567_base
virtual void setup()=0
virtual void digital_write(bool value)=0
void init_internal_(uint32_t buffer_length)
virtual void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:15
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:766
void draw_absolute_pixel_internal(int x, int y, Color color) override
void set_invert_colors(bool invert_colors)
void set_scroll(uint8_t line)
void set_all_pixels_on(bool enable)
virtual void command(uint8_t value)=0
void fill(Color color) override
void set_brightness(uint8_t val)
void set_contrast(uint8_t val)
virtual void write_display_data()=0
mopeka_std_values val[3]
const char int line
Definition log.h:74
size_t size_t pos
Definition helpers.h:1038
void HOT delay(uint32_t ms)
Definition hal.cpp:85
bool is_on() ESPHOME_ALWAYS_INLINE
Definition color.h:70
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6