ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
max7219digit.cpp
Go to the documentation of this file.
1#include "max7219digit.h"
3#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6#include "max7219font.h"
7
8#include <algorithm>
9
10namespace esphome {
11namespace max7219digit {
12
13static const char *const TAG = "max7219DIGIT";
14
15static const uint8_t MAX7219_REGISTER_NOOP = 0x00;
16static const uint8_t MAX7219_REGISTER_DECODE_MODE = 0x09;
17static const uint8_t MAX7219_REGISTER_INTENSITY = 0x0A;
18static const uint8_t MAX7219_REGISTER_SCAN_LIMIT = 0x0B;
19static const uint8_t MAX7219_REGISTER_SHUTDOWN = 0x0C;
20static const uint8_t MAX7219_REGISTER_DISPLAY_TEST = 0x0F;
21constexpr uint8_t MAX7219_NO_SHUTDOWN = 0x00;
22constexpr uint8_t MAX7219_SHUTDOWN = 0x01;
23constexpr uint8_t MAX7219_NO_DISPLAY_TEST = 0x00;
24constexpr uint8_t MAX7219_DISPLAY_TEST = 0x01;
25
27
29 this->spi_setup();
30 this->stepsleft_ = 0;
31 for (int chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
32 std::vector<uint8_t> vec(1);
33 this->max_displaybuffer_.push_back(vec);
34 // Initialize buffer with 0 for display so all non written pixels are blank
35 this->max_displaybuffer_[chip_line].resize(get_width_internal(), 0);
36 }
37 // let's assume the user has all 8 digits connected, only important in daisy chained setups anyway
38 this->send_to_all_(MAX7219_REGISTER_SCAN_LIMIT, 7);
39 // let's use our own ASCII -> led pattern encoding
40 this->send_to_all_(MAX7219_REGISTER_DECODE_MODE, 0);
41 // No display test with all the pixels on
42 this->send_to_all_(MAX7219_REGISTER_DISPLAY_TEST, MAX7219_NO_DISPLAY_TEST);
43 // SET Intsity of display
44 this->send_to_all_(MAX7219_REGISTER_INTENSITY, this->intensity_);
45 // this->send_to_all_(MAX7219_REGISTER_INTENSITY, 1);
46 this->display();
47 // power up
48 this->send_to_all_(MAX7219_REGISTER_SHUTDOWN, 1);
49}
50
52 ESP_LOGCONFIG(TAG,
53 "MAX7219DIGIT:\n"
54 " Number of Chips: %u\n"
55 " Number of Chips Lines: %u\n"
56 " Chips Lines Style : %u\n"
57 " Intensity: %u\n"
58 " Scroll Mode: %u\n"
59 " Scroll Speed: %u\n"
60 " Scroll Dwell: %u\n"
61 " Scroll Delay: %u",
62 this->num_chips_, this->num_chip_lines_, this->chip_lines_style_, this->intensity_, this->scroll_mode_,
63 this->scroll_speed_, this->scroll_dwell_, this->scroll_delay_);
64 LOG_PIN(" CS Pin: ", this->cs_);
65 LOG_UPDATE_INTERVAL(this);
66}
67
69 const uint32_t now = App.get_loop_component_start_time();
70 const uint32_t millis_since_last_scroll = now - this->last_scroll_;
71 const size_t first_line_size = this->max_displaybuffer_[0].size();
72 // check if the buffer has shrunk past the current position since last update
73 if ((first_line_size >= this->old_buffer_size_ + 3) || (first_line_size <= this->old_buffer_size_ - 3)) {
74 ESP_LOGV(TAG, "Buffer size changed %d to %d", this->old_buffer_size_, first_line_size);
75 this->stepsleft_ = 0;
76 this->display();
77 this->old_buffer_size_ = first_line_size;
78 }
79
80 if (!this->scroll_ || (first_line_size <= (size_t) get_width_internal())) {
81 ESP_LOGVV(TAG, "Return if there is no need to scroll or scroll is off.");
82 this->display();
83 return;
84 }
85
86 if ((this->stepsleft_ == 0) && (millis_since_last_scroll < this->scroll_delay_)) {
87 ESP_LOGVV(TAG, "At first step. Waiting for scroll delay");
88 this->display();
89 return;
90 }
91
92 if (this->scroll_mode_ == ScrollMode::STOP) {
93 if (this->stepsleft_ + get_width_internal() == first_line_size + 1) {
94 if (millis_since_last_scroll < this->scroll_dwell_) {
95 ESP_LOGVV(TAG, "Dwell time at end of string in case of stop at end. Step %d, since last scroll %d, dwell %d.",
96 this->stepsleft_, millis_since_last_scroll, this->scroll_dwell_);
97 return;
98 }
99 ESP_LOGV(TAG, "Dwell time passed. Continue scrolling.");
100 }
101 }
102
103 if (millis_since_last_scroll >= this->scroll_speed_) {
104 ESP_LOGVV(TAG, "Call to scroll left action");
105 this->last_scroll_ = now;
106 this->scroll_left();
107 this->display();
108 }
109}
110
112 uint8_t pixels[8];
113 // Run this loop for every MAX CHIP (GRID OF 64 leds)
114 // Run this routine for the rows of every chip 8x row 0 top to 7 bottom
115 // Fill the pixel parameter with display data
116 // Send the data to the chip
117 for (uint8_t chip = 0; chip < this->num_chips_ / this->num_chip_lines_; chip++) {
118 for (uint8_t chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
119 for (uint8_t j = 0; j < 8; j++) {
120 bool reverse =
121 chip_line % 2 != 0 && this->chip_lines_style_ == ChipLinesStyle::SNAKE ? !this->reverse_ : this->reverse_;
122 if (reverse) {
123 pixels[j] =
124 this->max_displaybuffer_[chip_line][(this->num_chips_ / this->num_chip_lines_ - chip - 1) * 8 + j];
125 } else {
126 pixels[j] = this->max_displaybuffer_[chip_line][chip * 8 + j];
127 }
128 }
129 if (chip_line % 2 != 0 && this->chip_lines_style_ == ChipLinesStyle::SNAKE)
131 this->send64pixels(chip_line * this->num_chips_ / this->num_chip_lines_ + chip, pixels);
132 if (chip_line % 2 != 0 && this->chip_lines_style_ == ChipLinesStyle::SNAKE)
134 }
135 }
136}
137
139 switch (this->orientation_) {
140 case 0:
141 return 2;
142 case 1:
143 return 3;
144 case 2:
145 return 0;
146 case 3:
147 return 1;
148 default:
149 return 0;
150 }
151}
152
154 return 8 * this->num_chip_lines_; // TO BE DONE -> CREATE Virtual size of screen and scroll
155}
156
158
160 if (x + 1 > (int) this->max_displaybuffer_[0].size()) { // Extend the display buffer in case required
161 for (int chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
162 this->max_displaybuffer_[chip_line].resize(x + 1, this->bckgrnd_);
163 }
164 }
165
166 if ((y >= this->get_height_internal()) || (y < 0) || (x < 0)) // If pixel is outside display then dont draw
167 return;
168
169 uint16_t pos = x; // X is starting at 0 top left
170 uint8_t subpos = y; // Y is starting at 0 top left
171
172 if (color.is_on()) {
173 this->max_displaybuffer_[subpos / 8][pos] |= (1 << subpos % 8);
174 } else {
175 this->max_displaybuffer_[subpos / 8][pos] &= ~(1 << subpos % 8);
176 }
177}
178
179void MAX7219Component::send_byte_(uint8_t a_register, uint8_t data) {
180 this->write_byte(a_register); // Write register value to MAX
181 this->write_byte(data); // Followed by actual data
182}
183void MAX7219Component::send_to_all_(uint8_t a_register, uint8_t data) {
184 this->enable(); // Enable SPI
185 for (uint8_t i = 0; i < this->num_chips_; i++) // Run the loop for every MAX chip in the stack
186 this->send_byte_(a_register, data); // Send the data to the chips
187 this->disable(); // Disable SPI
188}
190 this->update_ = true;
191 for (int chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
192 this->max_displaybuffer_[chip_line].clear();
193 this->max_displaybuffer_[chip_line].resize(get_width_internal(), this->bckgrnd_);
194 }
195 if (this->writer_local_.has_value()) // insert Labda function if available
196 (*this->writer_local_)(*this);
197}
198
199void MAX7219Component::invert_on_off(bool on_off) { this->invert_ = on_off; };
201
203 if (on_off) {
204 this->send_to_all_(MAX7219_REGISTER_SHUTDOWN, 1);
205 } else {
206 this->send_to_all_(MAX7219_REGISTER_SHUTDOWN, 0);
207 }
208}
209
210void MAX7219Component::scroll(bool on_off, ScrollMode mode, uint16_t speed, uint16_t delay, uint16_t dwell) {
211 this->set_scroll(on_off);
212 this->set_scroll_mode(mode);
213 this->set_scroll_speed(speed);
214 this->set_scroll_dwell(dwell);
215 this->set_scroll_delay(delay);
216}
217
219 this->set_scroll(on_off);
220 this->set_scroll_mode(mode);
221}
222
223void MAX7219Component::intensity(uint8_t intensity) {
224 this->intensity_ = intensity;
225 this->send_to_all_(MAX7219_REGISTER_INTENSITY, this->intensity_);
226}
227
228void MAX7219Component::scroll(bool on_off) { this->set_scroll(on_off); }
229
231 for (int chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
232 auto scroll = [&](std::vector<uint8_t> &line, uint16_t steps) {
233 std::rotate(line.begin(), std::next(line.begin(), steps), line.end());
234 };
235 if (this->update_) {
236 this->max_displaybuffer_[chip_line].push_back(this->bckgrnd_);
237 scroll(this->max_displaybuffer_[chip_line],
238 (this->stepsleft_ + 1) % (this->max_displaybuffer_[chip_line].size()));
239 } else {
240 scroll(this->max_displaybuffer_[chip_line], 1);
241 }
242 }
243 this->update_ = false;
244 this->stepsleft_++;
245 this->stepsleft_ %= this->max_displaybuffer_[0].size();
246}
247
248void MAX7219Component::send_char(uint8_t chip, uint8_t data) {
249 // get this character from PROGMEM
250 for (uint8_t i = 0; i < 8; i++)
251 this->max_displaybuffer_[0][chip * 8 + i] = progmem_read_byte(&MAX7219_DOT_MATRIX_FONT[data][i]);
252} // end of send_char
253
254// send one character (data) to position (chip)
255
256void MAX7219Component::send64pixels(uint8_t chip, const uint8_t pixels[8]) {
257 for (uint8_t col = 0; col < 8; col++) { // RUN THIS LOOP 8 times until column is 7
258 this->enable(); // start sending by enabling SPI
259 for (uint8_t i = 0; i < chip; i++) { // send extra NOPs to push the pixels out to extra displays
260 this->send_byte_(MAX7219_REGISTER_NOOP,
261 MAX7219_REGISTER_NOOP); // run this loop unit the matching chip is reached
262 }
263 uint8_t b = 0; // rotate pixels 90 degrees -- set byte to 0
264 if (this->orientation_ == 0) {
265 for (uint8_t i = 0; i < 8; i++) {
266 // run this loop 8 times for all the pixels[8] received
267 if (this->flip_x_) {
268 b |= ((pixels[i] >> col) & 1) << i; // change the column bits into row bits
269 } else {
270 b |= ((pixels[i] >> col) & 1) << (7 - i); // change the column bits into row bits
271 }
272 }
273 } else if (this->orientation_ == 1) {
274 b = pixels[col];
275 } else if (this->orientation_ == 2) {
276 for (uint8_t i = 0; i < 8; i++) {
277 if (this->flip_x_) {
278 b |= ((pixels[i] >> (7 - col)) & 1) << (7 - i);
279 } else {
280 b |= ((pixels[i] >> (7 - col)) & 1) << i;
281 }
282 }
283 } else {
284 for (uint8_t i = 0; i < 8; i++) {
285 b |= ((pixels[7 - col] >> i) & 1) << (7 - i);
286 }
287 }
288 // send this byte to display at selected chip
289 if (this->invert_) {
290 this->send_byte_(col + 1, ~b);
291 } else {
292 this->send_byte_(col + 1, b);
293 }
294 for (int i = 0; i < this->num_chips_ - chip - 1; i++) // end with enough NOPs so later chips don't update
295 this->send_byte_(MAX7219_REGISTER_NOOP, MAX7219_REGISTER_NOOP);
296 this->disable(); // all done disable SPI
297 } // end of for each column
298} // end of send64pixels
299
300uint8_t MAX7219Component::printdigit(const char *str) { return this->printdigit(0, str); }
301
302uint8_t MAX7219Component::printdigit(uint8_t start_pos, const char *s) {
303 uint8_t chip = start_pos;
304 for (; chip < this->num_chips_ && *s; chip++)
305 send_char(chip, *s++);
306 // space out rest
307 while (chip < (this->num_chips_))
308 send_char(chip++, ' ');
309 return 0;
310} // end of sendString
311
312uint8_t MAX7219Component::printdigitf(uint8_t pos, const char *format, ...) {
313 va_list arg;
314 va_start(arg, format);
315 char buffer[64];
316 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
317 va_end(arg);
318 if (ret > 0)
319 return this->printdigit(pos, buffer);
320 return 0;
321}
322uint8_t MAX7219Component::printdigitf(const char *format, ...) {
323 va_list arg;
324 va_start(arg, format);
325 char buffer[64];
326 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
327 va_end(arg);
328 if (ret > 0)
329 return this->printdigit(buffer);
330 return 0;
331}
332
333uint8_t MAX7219Component::strftimedigit(uint8_t pos, const char *format, ESPTime time) {
334 char buffer[64];
335 size_t ret = time.strftime(buffer, sizeof(buffer), format);
336 if (ret > 0)
337 return this->printdigit(pos, buffer);
338 return 0;
339}
340uint8_t MAX7219Component::strftimedigit(const char *format, ESPTime time) {
341 return this->strftimedigit(0, format, time);
342}
343
344} // namespace max7219digit
345} // namespace esphome
BedjetMode mode
BedJet operating mode.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void line(int x1, int y1, int x2, int y2, Color color=COLOR_ON)
Draw a straight line from the point [x1,y1] to [x2,y2] with the given color.
Definition display.cpp:19
void send_to_all_(uint8_t a_register, uint8_t data)
void draw_absolute_pixel_internal(int x, int y, Color color) override
uint8_t uint8_t uint8_t printdigit(uint8_t pos, const char *str)
Print str at the given position.
uint8_t printdigitf(uint8_t pos, const char *format,...) __attribute__((format(printf
Evaluate the printf-format and print the result at the given position.
void send64pixels(uint8_t chip, const uint8_t pixels[8])
void scroll(bool on_off, ScrollMode mode, uint16_t speed, uint16_t delay, uint16_t dwell)
void send_byte_(uint8_t a_register, uint8_t data)
float get_setup_priority() const override
void send_char(uint8_t chip, uint8_t data)
optional< max7219_writer_t > writer_local_
uint8_t num_chips_
Intensity of the display from 0 to 15 (most)
uint8_t strftimedigit(uint8_t pos, const char *format, ESPTime time) __attribute__((format(strftime
Evaluate the strftime-format and print the result at the given position.
std::vector< std::vector< uint8_t > > max_displaybuffer_
bool has_value() const
Definition optional.h:92
constexpr uint8_t MAX7219_NO_DISPLAY_TEST
constexpr uint8_t MAX7219_NO_SHUTDOWN
constexpr uint8_t MAX7219_DISPLAY_TEST
constexpr uint8_t MAX7219_SHUTDOWN
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.
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:58
bool is_on() ESPHOME_ALWAYS_INLINE
Definition color.h:61
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
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6