ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
max7219.cpp
Go to the documentation of this file.
1#include "max7219.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace max7219 {
8
9static const char *const TAG = "max7219";
10
11static const uint8_t MAX7219_REGISTER_NOOP = 0x00;
12static const uint8_t MAX7219_REGISTER_DECODE_MODE = 0x09;
13static const uint8_t MAX7219_REGISTER_INTENSITY = 0x0A;
14static const uint8_t MAX7219_REGISTER_SCAN_LIMIT = 0x0B;
15static const uint8_t MAX7219_REGISTER_SHUTDOWN = 0x0C;
16static const uint8_t MAX7219_REGISTER_TEST = 0x0F;
17static const uint8_t MAX7219_UNKNOWN_CHAR = 0b11111111;
18
19const uint8_t MAX7219_ASCII_TO_RAW[95] PROGMEM = {
20 0b00000000, // ' ', ord 0x20
21 0b10110000, // '!', ord 0x21
22 0b00100010, // '"', ord 0x22
23 MAX7219_UNKNOWN_CHAR, // '#', ord 0x23
24 MAX7219_UNKNOWN_CHAR, // '$', ord 0x24
25 0b01001001, // '%', ord 0x25
26 MAX7219_UNKNOWN_CHAR, // '&', ord 0x26
27 0b00000010, // ''', ord 0x27
28 0b01001110, // '(', ord 0x28
29 0b01111000, // ')', ord 0x29
30 0b01000000, // '*', ord 0x2A
31 MAX7219_UNKNOWN_CHAR, // '+', ord 0x2B
32 0b00010000, // ',', ord 0x2C
33 0b00000001, // '-', ord 0x2D
34 0b10000000, // '.', ord 0x2E
35 MAX7219_UNKNOWN_CHAR, // '/', ord 0x2F
36 0b01111110, // '0', ord 0x30
37 0b00110000, // '1', ord 0x31
38 0b01101101, // '2', ord 0x32
39 0b01111001, // '3', ord 0x33
40 0b00110011, // '4', ord 0x34
41 0b01011011, // '5', ord 0x35
42 0b01011111, // '6', ord 0x36
43 0b01110000, // '7', ord 0x37
44 0b01111111, // '8', ord 0x38
45 0b01111011, // '9', ord 0x39
46 0b01001000, // ':', ord 0x3A
47 0b01011000, // ';', ord 0x3B
48 MAX7219_UNKNOWN_CHAR, // '<', ord 0x3C
49 0b00001001, // '=', ord 0x3D
50 MAX7219_UNKNOWN_CHAR, // '>', ord 0x3E
51 0b01100101, // '?', ord 0x3F
52 0b01101111, // '@', ord 0x40
53 0b01110111, // 'A', ord 0x41
54 0b00011111, // 'B', ord 0x42
55 0b01001110, // 'C', ord 0x43
56 0b00111101, // 'D', ord 0x44
57 0b01001111, // 'E', ord 0x45
58 0b01000111, // 'F', ord 0x46
59 0b01011110, // 'G', ord 0x47
60 0b00110111, // 'H', ord 0x48
61 0b00110000, // 'I', ord 0x49
62 0b00111100, // 'J', ord 0x4A
63 MAX7219_UNKNOWN_CHAR, // 'K', ord 0x4B
64 0b00001110, // 'L', ord 0x4C
65 MAX7219_UNKNOWN_CHAR, // 'M', ord 0x4D
66 0b00010101, // 'N', ord 0x4E
67 0b01111110, // 'O', ord 0x4F
68 0b01100111, // 'P', ord 0x50
69 0b11111110, // 'Q', ord 0x51
70 0b00000101, // 'R', ord 0x52
71 0b01011011, // 'S', ord 0x53
72 0b00000111, // 'T', ord 0x54
73 0b00111110, // 'U', ord 0x55
74 0b00111110, // 'V', ord 0x56
75 0b00111111, // 'W', ord 0x57
76 MAX7219_UNKNOWN_CHAR, // 'X', ord 0x58
77 0b00100111, // 'Y', ord 0x59
78 0b01101101, // 'Z', ord 0x5A
79 0b01001110, // '[', ord 0x5B
80 MAX7219_UNKNOWN_CHAR, // '\', ord 0x5C
81 0b01111000, // ']', ord 0x5D
82 MAX7219_UNKNOWN_CHAR, // '^', ord 0x5E
83 0b00001000, // '_', ord 0x5F
84 0b00100000, // '`', ord 0x60
85 0b01110111, // 'a', ord 0x61
86 0b00011111, // 'b', ord 0x62
87 0b00001101, // 'c', ord 0x63
88 0b00111101, // 'd', ord 0x64
89 0b01001111, // 'e', ord 0x65
90 0b01000111, // 'f', ord 0x66
91 0b01011110, // 'g', ord 0x67
92 0b00010111, // 'h', ord 0x68
93 0b00010000, // 'i', ord 0x69
94 0b00111100, // 'j', ord 0x6A
95 MAX7219_UNKNOWN_CHAR, // 'k', ord 0x6B
96 0b00001110, // 'l', ord 0x6C
97 MAX7219_UNKNOWN_CHAR, // 'm', ord 0x6D
98 0b00010101, // 'n', ord 0x6E
99 0b00011101, // 'o', ord 0x6F
100 0b01100111, // 'p', ord 0x70
101 MAX7219_UNKNOWN_CHAR, // 'q', ord 0x71
102 0b00000101, // 'r', ord 0x72
103 0b01011011, // 's', ord 0x73
104 0b00000111, // 't', ord 0x74
105 0b00011100, // 'u', ord 0x75
106 0b00011100, // 'v', ord 0x76
107 MAX7219_UNKNOWN_CHAR, // 'w', ord 0x77
108 MAX7219_UNKNOWN_CHAR, // 'x', ord 0x78
109 0b00100111, // 'y', ord 0x79
110 MAX7219_UNKNOWN_CHAR, // 'z', ord 0x7A
111 0b00110001, // '{', ord 0x7B
112 0b00000110, // '|', ord 0x7C
113 0b00000111, // '}', ord 0x7D
114 0b01100011, // '~', ord 0x7E (degree symbol)
115};
116
119 this->spi_setup();
120 this->buffer_ = new uint8_t[this->num_chips_ * 8]; // NOLINT
121 for (uint8_t i = 0; i < this->num_chips_ * 8; i++)
122 this->buffer_[i] = 0;
123
124 // let's assume the user has all 8 digits connected, only important in daisy chained setups anyway
125 this->send_to_all_(MAX7219_REGISTER_SCAN_LIMIT, 7);
126 // let's use our own ASCII -> led pattern encoding
127 this->send_to_all_(MAX7219_REGISTER_DECODE_MODE, 0);
128 this->send_to_all_(MAX7219_REGISTER_INTENSITY, this->intensity_);
129 this->display();
130 // power up
131 this->send_to_all_(MAX7219_REGISTER_TEST, 0);
132 this->send_to_all_(MAX7219_REGISTER_SHUTDOWN, 1);
133}
135 ESP_LOGCONFIG(TAG,
136 "MAX7219:\n"
137 " Number of Chips: %u\n"
138 " Intensity: %u",
139 this->num_chips_, this->intensity_);
140 LOG_PIN(" CS Pin: ", this->cs_);
141 LOG_UPDATE_INTERVAL(this);
142}
143
145 for (uint8_t i = 0; i < 8; i++) {
146 this->enable();
147 for (uint8_t j = 0; j < this->num_chips_; j++) {
148 if (reverse_) {
149 this->send_byte_(8 - i, buffer_[(num_chips_ - j - 1) * 8 + i]);
150 } else {
151 this->send_byte_(8 - i, buffer_[j * 8 + i]);
152 }
153 }
154 this->disable();
155 }
156}
157void MAX7219Component::send_byte_(uint8_t a_register, uint8_t data) {
158 this->write_byte(a_register);
159 this->write_byte(data);
160}
161void MAX7219Component::send_to_all_(uint8_t a_register, uint8_t data) {
162 this->enable();
163 for (uint8_t i = 0; i < this->num_chips_; i++)
164 this->send_byte_(a_register, data);
165 this->disable();
166}
168 if (this->intensity_changed_) {
169 this->send_to_all_(MAX7219_REGISTER_INTENSITY, this->intensity_);
170 this->intensity_changed_ = false;
171 }
172 for (uint8_t i = 0; i < this->num_chips_ * 8; i++)
173 this->buffer_[i] = 0;
174 if (this->writer_.has_value())
175 (*this->writer_)(*this);
176 this->display();
177}
178uint8_t MAX7219Component::print(uint8_t start_pos, const char *str) {
179 uint8_t pos = start_pos;
180 for (; *str != '\0'; str++) {
181 uint8_t data = MAX7219_UNKNOWN_CHAR;
182 if (*str >= ' ' && *str <= '~')
183 data = progmem_read_byte(&MAX7219_ASCII_TO_RAW[*str - ' ']);
184
185 if (data == MAX7219_UNKNOWN_CHAR) {
186 ESP_LOGW(TAG, "Encountered character '%c' with no MAX7219 representation while translating string!", *str);
187 }
188 if (*str == '.') {
189 if (pos != start_pos)
190 pos--;
191 this->buffer_[pos] |= 0b10000000;
192 } else {
193 if (pos >= this->num_chips_ * 8) {
194 ESP_LOGE(TAG, "MAX7219 String is too long for the display!");
195 break;
196 }
197 this->buffer_[pos] = data;
198 }
199 pos++;
200 }
201 return pos - start_pos;
202}
203uint8_t MAX7219Component::print(const char *str) { return this->print(0, str); }
204uint8_t MAX7219Component::printf(uint8_t pos, const char *format, ...) {
205 va_list arg;
206 va_start(arg, format);
207 char buffer[64];
208 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
209 va_end(arg);
210 if (ret > 0)
211 return this->print(pos, buffer);
212 return 0;
213}
214uint8_t MAX7219Component::printf(const char *format, ...) {
215 va_list arg;
216 va_start(arg, format);
217 char buffer[64];
218 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
219 va_end(arg);
220 if (ret > 0)
221 return this->print(buffer);
222 return 0;
223}
224void MAX7219Component::set_writer(max7219_writer_t &&writer) { this->writer_ = writer; }
225void MAX7219Component::set_intensity(uint8_t intensity) {
226 intensity &= 0xF;
227 if (intensity != this->intensity_) {
228 this->intensity_changed_ = true;
229 this->intensity_ = intensity;
230 }
231}
232void MAX7219Component::set_num_chips(uint8_t num_chips) { this->num_chips_ = num_chips; }
233
234uint8_t MAX7219Component::strftime(uint8_t pos, const char *format, ESPTime time) {
235 char buffer[64];
236 size_t ret = time.strftime(buffer, sizeof(buffer), format);
237 if (ret > 0)
238 return this->print(pos, buffer);
239 return 0;
240}
241uint8_t MAX7219Component::strftime(const char *format, ESPTime time) { return this->strftime(0, format, time); }
242
243} // namespace max7219
244} // namespace esphome
uint8_t printf(uint8_t pos, const char *format,...) __attribute__((format(printf
Evaluate the printf-format and print the result at the given position.
Definition max7219.cpp:204
optional< max7219_writer_t > writer_
Definition max7219.h:60
void set_intensity(uint8_t intensity)
Definition max7219.cpp:225
uint8_t strftime(uint8_t pos, const char *format, ESPTime time) __attribute__((format(strftime
Evaluate the strftime-format and print the result at the given position.
Definition max7219.cpp:234
void send_to_all_(uint8_t a_register, uint8_t data)
Definition max7219.cpp:161
uint8_t uint8_t uint8_t print(uint8_t pos, const char *str)
Print str at the given position.
Definition max7219.cpp:178
void set_writer(max7219_writer_t &&writer)
Definition max7219.cpp:224
float get_setup_priority() const override
Definition max7219.cpp:117
void send_byte_(uint8_t a_register, uint8_t data)
Definition max7219.cpp:157
void set_num_chips(uint8_t num_chips)
Definition max7219.cpp:232
bool has_value() const
Definition optional.h:92
const uint8_t MAX7219_ASCII_TO_RAW[95] PROGMEM
Definition max7219.cpp:19
std::function< void(MAX7219Component &)> max7219_writer_t
Definition max7219.h:13
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
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:58
A more user-friendly version of struct tm from time.h.
Definition time.h:15
size_t strftime(char *buffer, size_t buffer_len, const char *format)
Convert this ESPTime struct to a null-terminated c string buffer as specified by the format argument.
Definition time.cpp:15
std::string print()