ESPHome 2025.5.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 ESP_LOGCONFIG(TAG, "Setting up ST7920...");
36 this->dump_config();
37 this->spi_setup();
40}
41
42void ST7920::command_(uint8_t value) {
43 this->enable();
44 this->send_(LCD_COMMAND, value);
45 this->disable();
46}
47
48void ST7920::data_(uint8_t value) {
49 this->enable();
50 this->send_(LCD_DATA, value);
51 this->disable();
52}
53
54void ST7920::send_(uint8_t type, uint8_t value) {
55 this->write_byte(type);
56 this->write_byte(value & 0xF0);
57 this->write_byte(value << 4);
58}
59
60void ST7920::goto_xy_(uint16_t x, uint16_t y) {
61 if (y >= 32 && y < 64) {
62 y -= 32;
63 x += 8;
64 } else if (y >= 64 && y < 64 + 32) {
65 y -= 32;
66 x += 0;
67 } else if (y >= 64 + 32 && y < 64 + 64) {
68 y -= 64;
69 x += 8;
70 }
71 this->command_(LCD_ADDR | y); // 6-bit (0..63)
72 this->command_(LCD_ADDR | x); // 4-bit (0..15)
73}
74
76 uint8_t i, j, b;
77 for (j = 0; j < (uint8_t) (this->get_height_internal() / 2); j++) {
78 this->goto_xy_(0, j);
79 this->enable();
80 for (i = 0; i < 16; i++) { // 16 bytes from line #0+
81 b = this->buffer_[i + j * 16];
82 this->send_(LCD_DATA, b);
83 }
84 for (i = 0; i < 16; i++) { // 16 bytes from line #32+
85 b = this->buffer_[i + (j + 32) * 16];
86 this->send_(LCD_DATA, b);
87 }
88 this->disable();
89 App.feed_wdt();
90 }
91}
92
93void ST7920::fill(Color color) { memset(this->buffer_, color.is_on() ? 0xFF : 0x00, this->get_buffer_length_()); }
94
96 LOG_DISPLAY("", "ST7920", this);
97 LOG_PIN(" CS Pin: ", this->cs_);
98 ESP_LOGCONFIG(TAG, " Height: %d", this->height_);
99 ESP_LOGCONFIG(TAG, " Width: %d", this->width_);
100}
101
103
105 this->clear();
106 if (this->writer_local_.has_value()) // call lambda function if available
107 (*this->writer_local_)(*this);
108 this->write_display_data();
109}
110
111int ST7920::get_width_internal() { return this->width_; }
112
114
116 return size_t(this->get_width_internal()) * 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 int width = this->get_width_internal() / 8u;
124 if (color.is_on()) {
125 this->buffer_[y * width + x / 8] |= (0x80 >> (x & 7));
126 } else {
127 this->buffer_[y * width + x / 8] &= ~(0x80 >> (x & 7));
128 }
129}
130
132 ESP_LOGD(TAG, "Initializing display...");
133 this->command_(LCD_BASIC); // 8bit mode
134 this->command_(LCD_BASIC); // 8bit mode
135 this->command_(LCD_CLS); // clear screen
136 delay(12); // >10 ms delay
137 this->command_(LCD_ADDRINC); // cursor increment right no shift
138 this->command_(LCD_DISPLAYON); // D=1, C=0, B=0
139 this->command_(LCD_EXTEND); // LCD_EXTEND);
140 this->command_(LCD_GFXMODE); // LCD_GFXMODE);
141 this->write_display_data();
142}
143
144} // namespace st7920
145} // namespace esphome
void feed_wdt(uint32_t time=0)
void init_internal_(uint32_t buffer_length)
void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:16
bool has_value() const
Definition optional.h:87
optional< st7920_writer_t > writer_local_
Definition st7920.h:47
void send_(uint8_t type, uint8_t value)
Definition st7920.cpp:54
void dump_config() override
Definition st7920.cpp:95
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition st7920.cpp:119
void command_(uint8_t value)
Definition st7920.cpp:42
void goto_xy_(uint16_t x, uint16_t y)
Definition st7920.cpp:60
int get_height_internal() override
Definition st7920.cpp:113
float get_setup_priority() const override
Definition st7920.cpp:102
void fill(Color color) override
Definition st7920.cpp:93
void setup() override
Definition st7920.cpp:34
void update() override
Definition st7920.cpp:104
void data_(uint8_t value)
Definition st7920.cpp:48
size_t get_buffer_length_()
Definition st7920.cpp:115
int get_width_internal() override
Definition st7920.cpp:111
uint8_t type
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR 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:46
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6