ESPHome 2026.6.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#include <cinttypes>
10
11namespace esphome::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
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 (static_cast<size_t>(this->stepsleft_ + get_width_internal()) == first_line_size + 1) {
94 if (millis_since_last_scroll < this->scroll_dwell_) {
95 ESP_LOGVV(TAG,
96 "Dwell time at end of string in case of stop at end. Step %d, since last scroll %" PRIu32
97 ", dwell %d.",
98 this->stepsleft_, millis_since_last_scroll, this->scroll_dwell_);
99 return;
100 }
101 ESP_LOGV(TAG, "Dwell time passed. Continue scrolling.");
102 }
103 }
104
105 if (millis_since_last_scroll >= this->scroll_speed_) {
106 ESP_LOGVV(TAG, "Call to scroll left action");
107 this->last_scroll_ = now;
108 this->scroll_left();
109 this->display();
110 }
111}
112
114 uint8_t pixels[8];
115 // Run this loop for every MAX CHIP (GRID OF 64 leds)
116 // Run this routine for the rows of every chip 8x row 0 top to 7 bottom
117 // Fill the pixel parameter with display data
118 // Send the data to the chip
119 for (uint8_t chip = 0; chip < this->num_chips_ / this->num_chip_lines_; chip++) {
120 for (uint8_t chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
121 for (uint8_t j = 0; j < 8; j++) {
122 bool reverse =
123 chip_line % 2 != 0 && this->chip_lines_style_ == ChipLinesStyle::SNAKE ? !this->reverse_ : this->reverse_;
124 if (reverse) {
125 pixels[j] =
126 this->max_displaybuffer_[chip_line][(this->num_chips_ / this->num_chip_lines_ - chip - 1) * 8 + j];
127 } else {
128 pixels[j] = this->max_displaybuffer_[chip_line][chip * 8 + j];
129 }
130 }
131 if (chip_line % 2 != 0 && this->chip_lines_style_ == ChipLinesStyle::SNAKE)
133 this->send64pixels(chip_line * this->num_chips_ / this->num_chip_lines_ + chip, pixels);
134 if (chip_line % 2 != 0 && this->chip_lines_style_ == ChipLinesStyle::SNAKE)
136 }
137 }
138}
139
141 switch (this->orientation_) {
142 case 0:
143 return 2;
144 case 1:
145 return 3;
146 case 2:
147 return 0;
148 case 3:
149 return 1;
150 default:
151 return 0;
152 }
153}
154
156 return 8 * this->num_chip_lines_; // TO BE DONE -> CREATE Virtual size of screen and scroll
157}
158
160
162 if (x + 1 > (int) this->max_displaybuffer_[0].size()) { // Extend the display buffer in case required
163 for (int chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
164 this->max_displaybuffer_[chip_line].resize(x + 1, this->bckgrnd_);
165 }
166 }
167
168 if ((y >= this->get_height_internal()) || (y < 0) || (x < 0)) // If pixel is outside display then dont draw
169 return;
170
171 uint16_t pos = x; // X is starting at 0 top left
172 uint8_t subpos = y; // Y is starting at 0 top left
173
174 if (color.is_on()) {
175 this->max_displaybuffer_[subpos / 8][pos] |= (1 << subpos % 8);
176 } else {
177 this->max_displaybuffer_[subpos / 8][pos] &= ~(1 << subpos % 8);
178 }
179}
180
181void MAX7219Component::send_byte_(uint8_t a_register, uint8_t data) {
182 this->write_byte(a_register); // Write register value to MAX
183 this->write_byte(data); // Followed by actual data
184}
185void MAX7219Component::send_to_all_(uint8_t a_register, uint8_t data) {
186 this->enable(); // Enable SPI
187 for (uint8_t i = 0; i < this->num_chips_; i++) // Run the loop for every MAX chip in the stack
188 this->send_byte_(a_register, data); // Send the data to the chips
189 this->disable(); // Disable SPI
190}
192 this->update_ = true;
193 for (int chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
194 this->max_displaybuffer_[chip_line].clear();
195 this->max_displaybuffer_[chip_line].resize(get_width_internal(), this->bckgrnd_);
196 }
197 if (this->writer_local_.has_value()) // insert Labda function if available
198 (*this->writer_local_)(*this);
199}
200
201void MAX7219Component::invert_on_off(bool on_off) { this->invert_ = on_off; };
203
205 if (on_off) {
206 this->send_to_all_(MAX7219_REGISTER_SHUTDOWN, 1);
207 } else {
208 this->send_to_all_(MAX7219_REGISTER_SHUTDOWN, 0);
209 }
210}
211
212void MAX7219Component::scroll(bool on_off, ScrollMode mode, uint16_t speed, uint16_t delay, uint16_t dwell) {
213 this->set_scroll(on_off);
214 this->set_scroll_mode(mode);
215 this->set_scroll_speed(speed);
216 this->set_scroll_dwell(dwell);
217 this->set_scroll_delay(delay);
218}
219
221 this->set_scroll(on_off);
222 this->set_scroll_mode(mode);
223}
224
225void MAX7219Component::intensity(uint8_t intensity) {
226 this->intensity_ = intensity;
227 this->send_to_all_(MAX7219_REGISTER_INTENSITY, this->intensity_);
228}
229
230void MAX7219Component::scroll(bool on_off) { this->set_scroll(on_off); }
231
233 for (int chip_line = 0; chip_line < this->num_chip_lines_; chip_line++) {
234 auto scroll = [&](std::vector<uint8_t> &line, uint16_t steps) {
235 std::rotate(line.begin(), std::next(line.begin(), steps), line.end());
236 };
237 if (this->update_) {
238 this->max_displaybuffer_[chip_line].push_back(this->bckgrnd_);
239 scroll(this->max_displaybuffer_[chip_line],
240 (this->stepsleft_ + 1) % (this->max_displaybuffer_[chip_line].size()));
241 } else {
242 scroll(this->max_displaybuffer_[chip_line], 1);
243 }
244 }
245 this->update_ = false;
246 this->stepsleft_++;
247 this->stepsleft_ %= this->max_displaybuffer_[0].size();
248}
249
250void MAX7219Component::send_char(uint8_t chip, uint8_t data) {
251 // get this character from PROGMEM
252 for (uint8_t i = 0; i < 8; i++)
253 this->max_displaybuffer_[0][chip * 8 + i] = progmem_read_byte(&MAX7219_DOT_MATRIX_FONT[data][i]);
254} // end of send_char
255
256// send one character (data) to position (chip)
257
258void MAX7219Component::send64pixels(uint8_t chip, const uint8_t pixels[8]) {
259 for (uint8_t col = 0; col < 8; col++) { // RUN THIS LOOP 8 times until column is 7
260 this->enable(); // start sending by enabling SPI
261 for (uint8_t i = 0; i < chip; i++) { // send extra NOPs to push the pixels out to extra displays
262 this->send_byte_(MAX7219_REGISTER_NOOP,
263 MAX7219_REGISTER_NOOP); // run this loop unit the matching chip is reached
264 }
265 uint8_t b = 0; // rotate pixels 90 degrees -- set byte to 0
266 if (this->orientation_ == 0) {
267 for (uint8_t i = 0; i < 8; i++) {
268 // run this loop 8 times for all the pixels[8] received
269 if (this->flip_x_) {
270 b |= ((pixels[i] >> col) & 1) << i; // change the column bits into row bits
271 } else {
272 b |= ((pixels[i] >> col) & 1) << (7 - i); // change the column bits into row bits
273 }
274 }
275 } else if (this->orientation_ == 1) {
276 if (this->flip_x_) {
277 b = pixels[7 - col];
278 } else {
279 b = pixels[col];
280 }
281 } else if (this->orientation_ == 2) {
282 for (uint8_t i = 0; i < 8; i++) {
283 if (this->flip_x_) {
284 b |= ((pixels[i] >> (7 - col)) & 1) << (7 - i);
285 } else {
286 b |= ((pixels[i] >> (7 - col)) & 1) << i;
287 }
288 }
289 } else {
290 for (uint8_t i = 0; i < 8; i++) {
291 if (this->flip_x_) {
292 b |= ((pixels[col] >> i) & 1) << (7 - i);
293 } else {
294 b |= ((pixels[7 - col] >> i) & 1) << (7 - i);
295 }
296 }
297 }
298 // send this byte to display at selected chip
299 if (this->invert_) {
300 this->send_byte_(col + 1, ~b);
301 } else {
302 this->send_byte_(col + 1, b);
303 }
304 for (int i = 0; i < this->num_chips_ - chip - 1; i++) // end with enough NOPs so later chips don't update
305 this->send_byte_(MAX7219_REGISTER_NOOP, MAX7219_REGISTER_NOOP);
306 this->disable(); // all done disable SPI
307 } // end of for each column
308} // end of send64pixels
309
310uint8_t MAX7219Component::printdigit(const char *str) { return this->printdigit(0, str); }
311
312uint8_t MAX7219Component::printdigit(uint8_t start_pos, const char *s) {
313 uint8_t chip = start_pos;
314 for (; chip < this->num_chips_ && *s; chip++)
315 send_char(chip, *s++);
316 // space out rest
317 while (chip < (this->num_chips_))
318 send_char(chip++, ' ');
319 return 0;
320} // end of sendString
321
322uint8_t MAX7219Component::printdigitf(uint8_t pos, 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(pos, buffer);
330 return 0;
331}
332uint8_t MAX7219Component::printdigitf(const char *format, ...) {
333 va_list arg;
334 va_start(arg, format);
335 char buffer[64];
336 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
337 va_end(arg);
338 if (ret > 0)
339 return this->printdigit(buffer);
340 return 0;
341}
342
343uint8_t MAX7219Component::strftimedigit(uint8_t pos, const char *format, ESPTime time) {
344 char buffer[64];
345 size_t ret = time.strftime(buffer, sizeof(buffer), format);
346 if (ret > 0)
347 return this->printdigit(pos, buffer);
348 return 0;
349}
351 return this->strftimedigit(0, format, time);
352}
353
354} // namespace esphome::max7219digit
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 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)
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_
constexpr uint8_t MAX7219_NO_DISPLAY_TEST
constexpr uint8_t MAX7219_NO_SHUTDOWN
constexpr uint8_t MAX7219_DISPLAY_TEST
constexpr uint8_t MAX7219_SHUTDOWN
constexpr float PROCESSOR
For components that use data from sensors like displays.
Definition component.h:45
const char int line
Definition log.h:74
const char int const __FlashStringHelper * format
Definition log.h:74
va_end(args)
uint16_t size
Definition helpers.cpp:25
size_t size_t pos
Definition helpers.h:1038
size_t size_t const char va_start(args, fmt)
void HOT delay(uint32_t ms)
Definition hal.cpp:85
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:43
static void uint32_t
bool is_on() ESPHOME_ALWAYS_INLINE
Definition color.h:70
A more user-friendly version of struct tm from time.h.
Definition time.h:23
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:18
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6