ESPHome 2025.9.0-dev
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 uint8_t i, j, b;
76 for (j = 0; j < (uint8_t) (this->get_height_internal() / 2); j++) {
77 this->goto_xy_(0, j);
78 this->enable();
79 for (i = 0; i < 16; i++) { // 16 bytes from line #0+
80 b = this->buffer_[i + j * 16];
81 this->send_(LCD_DATA, b);
82 }
83 for (i = 0; i < 16; i++) { // 16 bytes from line #32+
84 b = this->buffer_[i + (j + 32) * 16];
85 this->send_(LCD_DATA, b);
86 }
87 this->disable();
88 App.feed_wdt();
89 }
90}
91
92void ST7920::fill(Color color) { memset(this->buffer_, color.is_on() ? 0xFF : 0x00, this->get_buffer_length_()); }
93
95 LOG_DISPLAY("", "ST7920", this);
96 LOG_PIN(" CS Pin: ", this->cs_);
97 ESP_LOGCONFIG(TAG,
98 " Height: %d\n"
99 " Width: %d",
100 this->height_, this->width_);
101}
102
104
106 this->clear();
107 if (this->writer_local_.has_value()) // call lambda function if available
108 (*this->writer_local_)(*this);
109 this->write_display_data();
110}
111
112int ST7920::get_width_internal() { return this->width_; }
113
115
117 return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / 8u;
118}
119
121 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
122 return;
123 }
124 int width = this->get_width_internal() / 8u;
125 if (color.is_on()) {
126 this->buffer_[y * width + x / 8] |= (0x80 >> (x & 7));
127 } else {
128 this->buffer_[y * width + x / 8] &= ~(0x80 >> (x & 7));
129 }
130}
131
133 ESP_LOGD(TAG, "Initializing display");
134 this->command_(LCD_BASIC); // 8bit mode
135 this->command_(LCD_BASIC); // 8bit mode
136 this->command_(LCD_CLS); // clear screen
137 delay(12); // >10 ms delay
138 this->command_(LCD_ADDRINC); // cursor increment right no shift
139 this->command_(LCD_DISPLAYON); // D=1, C=0, B=0
140 this->command_(LCD_EXTEND); // LCD_EXTEND);
141 this->command_(LCD_GFXMODE); // LCD_GFXMODE);
142 this->write_display_data();
143}
144
145} // namespace st7920
146} // 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:17
bool has_value() const
Definition optional.h:92
optional< st7920_writer_t > writer_local_
Definition st7920.h:47
void send_(uint8_t type, uint8_t value)
Definition st7920.cpp:53
void dump_config() override
Definition st7920.cpp:94
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition st7920.cpp:120
void command_(uint8_t value)
Definition st7920.cpp:41
void goto_xy_(uint16_t x, uint16_t y)
Definition st7920.cpp:59
int get_height_internal() override
Definition st7920.cpp:114
float get_setup_priority() const override
Definition st7920.cpp:103
void fill(Color color) override
Definition st7920.cpp:92
void setup() override
Definition st7920.cpp:34
void update() override
Definition st7920.cpp:105
void data_(uint8_t value)
Definition st7920.cpp:47
size_t get_buffer_length_()
Definition st7920.cpp:116
int get_width_internal() override
Definition st7920.cpp:112
uint8_t type
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:51
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:29
Application App
Global storage of Application pointer - only one Application can exist.
bool is_on() ESPHOME_ALWAYS_INLINE
Definition color.h:61
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6