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