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