ESPHome 2026.5.3
Loading...
Searching...
No Matches
st7920.cpp
Go to the documentation of this file.
1#include "st7920.h"
4#include "esphome/core/log.h"
5
6namespace esphome::st7920 {
7
8static const char *const TAG = "st7920";
9
10// ST7920 COMMANDS
11static const uint8_t LCD_DATA = 0xFA;
12static const uint8_t LCD_COMMAND = 0xF8;
13static const uint8_t LCD_CLS = 0x01;
14static const uint8_t LCD_HOME = 0x02;
15static const uint8_t LCD_ADDRINC = 0x06;
16static const uint8_t LCD_DISPLAYON = 0x0C;
17static const uint8_t LCD_DISPLAYOFF = 0x08;
18static const uint8_t LCD_CURSORON = 0x0E;
19static const uint8_t LCD_CURSORBLINK = 0x0F;
20static const uint8_t LCD_BASIC = 0x30;
21static const uint8_t LCD_GFXMODE = 0x36;
22static const uint8_t LCD_EXTEND = 0x34;
23static const uint8_t LCD_TXTMODE = 0x34;
24static const uint8_t LCD_STANDBY = 0x01;
25static const uint8_t LCD_SCROLL = 0x03;
26static const uint8_t LCD_SCROLLADDR = 0x40;
27static const uint8_t LCD_ADDR = 0x80;
28static const uint8_t LCD_LINE0 = 0x80;
29static const uint8_t LCD_LINE1 = 0x90;
30static const uint8_t LCD_LINE2 = 0x88;
31static const uint8_t LCD_LINE3 = 0x98;
32
34 this->dump_config();
35 this->spi_setup();
38}
39
40void ST7920::command_(uint8_t value) {
41 this->enable();
42 this->send_(LCD_COMMAND, value);
43 this->disable();
44}
45
46void ST7920::data_(uint8_t value) {
47 this->enable();
48 this->send_(LCD_DATA, value);
49 this->disable();
50}
51
52void ST7920::send_(uint8_t type, uint8_t value) {
53 this->write_byte(type);
54 this->write_byte(value & 0xF0);
55 this->write_byte(value << 4);
56}
57
58void ST7920::goto_xy_(uint16_t x, uint16_t y) {
59 if (y >= 32 && y < 64) {
60 y -= 32;
61 x += 8;
62 } else if (y >= 64 && y < 64 + 32) {
63 y -= 32;
64 x += 0;
65 } else if (y >= 64 + 32 && y < 64 + 64) {
66 y -= 64;
67 x += 8;
68 }
69 this->command_(LCD_ADDR | y); // 6-bit (0..63)
70 this->command_(LCD_ADDR | x); // 4-bit (0..15)
71}
72
74 int i, j;
75 uint8_t b;
76 int width_bytes = this->get_width_internal() / 8;
77 int half_height = this->get_height_internal() / 2;
78 for (j = 0; j < half_height; j++) {
79 this->goto_xy_(0, j);
80 this->enable();
81 for (i = 0; i < width_bytes; i++) {
82 b = this->buffer_[i + j * width_bytes];
83 this->send_(LCD_DATA, b);
84 }
85 for (i = 0; i < width_bytes; i++) {
86 b = this->buffer_[i + (j + half_height) * width_bytes];
87 this->send_(LCD_DATA, b);
88 }
89 this->disable();
90 App.feed_wdt();
91 }
92}
93
94void ST7920::fill(Color color) {
95 // If clipping is active, fall back to base implementation
96 if (this->get_clipping().is_set()) {
97 Display::fill(color);
98 return;
99 }
100
101 uint8_t fill = color.is_on() ? 0xFF : 0x00;
102 memset(this->buffer_, fill, this->get_buffer_length_());
103}
104
106 LOG_DISPLAY("", "ST7920", this);
107 LOG_PIN(" CS Pin: ", this->cs_);
108 ESP_LOGCONFIG(TAG,
109 " Height: %d\n"
110 " Width: %d",
111 this->height_, this->width_);
112}
113
115
117 this->clear();
118 if (this->writer_local_.has_value()) // call lambda function if available
119 (*this->writer_local_)(*this);
120 this->write_display_data();
121}
122
123int ST7920::get_width_internal() { return this->width_; }
124
126
128 return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / 8u;
129}
130
132 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
133 return;
134 }
135 int width = this->get_width_internal() / 8u;
136 if (color.is_on()) {
137 this->buffer_[y * width + x / 8] |= (0x80 >> (x & 7));
138 } else {
139 this->buffer_[y * width + x / 8] &= ~(0x80 >> (x & 7));
140 }
141}
142
144 ESP_LOGD(TAG, "Initializing display");
145 this->command_(LCD_BASIC); // 8bit mode
146 this->command_(LCD_BASIC); // 8bit mode
147 this->command_(LCD_CLS); // clear screen
148 delay(12); // >10 ms delay
149 this->command_(LCD_ADDRINC); // cursor increment right no shift
150 this->command_(LCD_DISPLAYON); // D=1, C=0, B=0
151 this->command_(LCD_EXTEND); // LCD_EXTEND);
152 this->command_(LCD_GFXMODE); // LCD_GFXMODE);
153 this->write_display_data();
154}
155
156} // namespace esphome::st7920
void feed_wdt()
Feed the task watchdog.
void init_internal_(uint32_t buffer_length)
virtual void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:14
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:765
void send_(uint8_t type, uint8_t value)
Definition st7920.cpp:52
void dump_config() override
Definition st7920.cpp:105
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition st7920.cpp:131
void command_(uint8_t value)
Definition st7920.cpp:40
st7920_writer_t writer_local_
Definition st7920.h:46
void goto_xy_(uint16_t x, uint16_t y)
Definition st7920.cpp:58
int get_height_internal() override
Definition st7920.cpp:125
float get_setup_priority() const override
Definition st7920.cpp:114
void fill(Color color) override
Definition st7920.cpp:94
void setup() override
Definition st7920.cpp:33
void update() override
Definition st7920.cpp:116
void data_(uint8_t value)
Definition st7920.cpp:46
size_t get_buffer_length_()
Definition st7920.cpp:127
int get_width_internal() override
Definition st7920.cpp:123
uint16_t type
constexpr float PROCESSOR
For components that use data from sensors like displays.
Definition component.h:45
void HOT delay(uint32_t ms)
Definition hal.cpp:85
Application App
Global storage of Application pointer - only one Application can exist.
bool is_on() ESPHOME_ALWAYS_INLINE
Definition color.h:70
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6