-
Notifications
You must be signed in to change notification settings - Fork 9
/
max7219grid.hpp
225 lines (191 loc) · 6.91 KB
/
max7219grid.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "esphome/defines.h"
#include "esphome/helpers.h"
#include "esphome/spi_component.h"
#include "esphome/time/rtc_component.h"
ESPHOME_NAMESPACE_BEGIN
namespace display {
extern const uint8_t MAX7219_ASCII_TO_RAW[94] PROGMEM;
class MAX7219GridComponent;
using max7219_writer_t = std::function<void(MAX7219GridComponent &)>;
class MAX7219GridComponent : public PollingComponent, public SPIDevice {
public:
MAX7219GridComponent(SPIComponent *parent, GPIOPin *cs, uint32_t update_interval = 1000);
void set_writer(max7219_writer_t &&writer);
void setup() override;
void dump_config() override;
void update() override;
float get_setup_priority() const override;
void display();
void set_intensity(uint8_t intensity);
void set_num_chips(uint8_t num_chips);
uint8_t printf(uint8_t pos, const char *format, ...) __attribute__((format(printf, 3, 4)));
uint8_t printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
uint8_t print(uint8_t pos, const char *str);
uint8_t print(const char *str);
uint16_t width(){ return this->num_chips_ * 8;}
uint16_t height(){ return 8;}
void fill_vertical_line(uint16_t line, uint8_t val){
if(line < width()){
this->buffer_[line] = val;
}
}
#ifdef USE_TIME
uint8_t strftime(uint8_t pos, const char *format, time::ESPTime time) __attribute__((format(strftime, 3, 0)));
uint8_t strftime(const char *format, time::ESPTime time) __attribute__((format(strftime, 2, 0)));
#endif
protected:
void send_byte_(uint8_t a_register, uint8_t data);
void send_to_all_(uint8_t a_register, uint8_t data);
bool is_device_msb_first() override;
uint8_t intensity_{15};
uint8_t num_chips_{1};
uint8_t *buffer_;
optional<max7219_writer_t> writer_{};
};
} // namespace display
ESPHOME_NAMESPACE_END
#include "esphome/log.h"
ESPHOME_NAMESPACE_BEGIN
namespace display {
static const char *TAG = "display.max7219";
static const uint8_t MAX7219_REGISTER_NOOP = 0x00;
static const uint8_t MAX7219_REGISTER_DECODE_MODE = 0x09;
static const uint8_t MAX7219_REGISTER_INTENSITY = 0x0A;
static const uint8_t MAX7219_REGISTER_SCAN_LIMIT = 0x0B;
static const uint8_t MAX7219_REGISTER_SHUTDOWN = 0x0C;
static const uint8_t MAX7219_REGISTER_DISPLAY_TEST = 0x0F;
constexpr uint8_t MAX7219_NO_SHUTDOWN = 0x00;
constexpr uint8_t MAX7219_SHUTDOWN = 0x01;
constexpr uint8_t MAX7219_NO_DISPLAY_TEST = 0x00;
constexpr uint8_t MAX7219_DISPLAY_TEST = 0x01;
MAX7219GridComponent::MAX7219GridComponent(SPIComponent *parent, GPIOPin *cs, uint32_t update_interval)
: PollingComponent(update_interval), SPIDevice(parent, cs) {}
float MAX7219GridComponent::get_setup_priority() const { return setup_priority::POST_HARDWARE; }
void MAX7219GridComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up MAX7219...");
this->spi_setup();
this->buffer_ = new uint8_t[this->num_chips_ * 8];
for (uint8_t i = 0; i < this->num_chips_ * 8; i++)
this->buffer_[i] = 0;
// let's assume the user has all 8 digits connected, only important in daisy chained setups anyway
this->send_to_all_(MAX7219_REGISTER_SHUTDOWN, MAX7219_NO_SHUTDOWN);
this->send_to_all_(MAX7219_REGISTER_DISPLAY_TEST, MAX7219_NO_DISPLAY_TEST);
//this->send_to_all_(MAX7219_REGISTER_SHUTDOWN, MAX7219_NO_SHUTDOWN);
// let's assume the user has all 8 digits connected, only important in daisy chained setups anyway
this->send_to_all_(MAX7219_REGISTER_SCAN_LIMIT, 7);
// let's use our own ASCII -> led pattern encoding
this->send_to_all_(MAX7219_REGISTER_DECODE_MODE, 0);
this->send_to_all_(MAX7219_REGISTER_INTENSITY, this->intensity_);
delay(250);
this->display();
// power up
this->send_to_all_(MAX7219_REGISTER_SHUTDOWN, 1);
}
void MAX7219GridComponent::dump_config() {
ESP_LOGCONFIG(TAG, "MAX7219:");
ESP_LOGCONFIG(TAG, " Number of Chips: %u", this->num_chips_);
ESP_LOGCONFIG(TAG, " Intensity: %u", this->intensity_);
LOG_PIN(" CS Pin: ", this->cs_);
LOG_UPDATE_INTERVAL(this);
}
void MAX7219GridComponent::display() {
for (uint8_t i = 0; i < 8; i++) {
this->enable();
for (uint8_t j = 0; j < this->num_chips_; j++) {
this->send_byte_(8 - i, this->buffer_[j * 8 + i]);
}
this->disable();
}
}
void MAX7219GridComponent::send_byte_(uint8_t a_register, uint8_t data) {
// ESP_LOGW(TAG, "R(%x)<- %x", a_register, data);
this->write_byte(a_register);
this->write_byte(data);
}
void MAX7219GridComponent::send_to_all_(uint8_t a_register, uint8_t data) {
this->enable();
for (uint8_t i = 0; i < this->num_chips_; i++)
this->send_byte_(a_register, data);
this->disable();
}
bool MAX7219GridComponent::is_device_msb_first() { return true; }
void MAX7219GridComponent::update() {
for (uint8_t i = 0; i < this->num_chips_ * 8; i++){
this->buffer_[i] = 0;
}
if (this->writer_.has_value())
(*this->writer_)(*this);
this->display();
}
uint8_t MAX7219GridComponent::print(uint8_t start_pos, const char *str) {
// uint8_t pos = start_pos;
// for (; *str != '\0'; str++) {
// uint8_t data = MAX7219_UNKNOWN_CHAR;
// if (*str >= ' ' && *str <= '}')
// data = pgm_read_byte(&MAX7219_ASCII_TO_RAW[*str - ' ']);
//
// if (data == MAX7219_UNKNOWN_CHAR) {
// ESP_LOGW(TAG, "Encountered character '%c' with no MAX7219 representation while translating string!", *str);
// }
// if (*str == '.') {
// if (pos != start_pos)
// pos--;
// this->buffer_[pos] |= 0b10000000;
// } else {
// if (pos >= this->num_chips_ * 8) {
// ESP_LOGE(TAG, "MAX7219 String is too long for the display!");
// break;
// }
// this->buffer_[pos] = data;
// }
// pos++;
// }
// return pos - start_pos;
return 0;
}
uint8_t MAX7219GridComponent::print(const char *str) {
return this->print(0, str);
}
uint8_t MAX7219GridComponent::printf(uint8_t pos, const char *format, ...) {
va_list arg;
va_start(arg, format);
char buffer[64];
int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
va_end(arg);
if (ret > 0)
return this->print(pos, buffer);
return 0;
}
uint8_t MAX7219GridComponent::printf(const char *format, ...) {
va_list arg;
va_start(arg, format);
char buffer[64];
int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
va_end(arg);
if (ret > 0)
return this->print(buffer);
return 0;
}
void MAX7219GridComponent::set_writer(max7219_writer_t &&writer) {
this->writer_ = writer;
}
void MAX7219GridComponent::set_intensity(uint8_t intensity) {
this->intensity_ = intensity;
}
void MAX7219GridComponent::set_num_chips(uint8_t num_chips) {
this->num_chips_ = num_chips;
}
#ifdef USE_TIME
uint8_t MAX7219GridComponent::strftime(uint8_t pos, const char *format, time::ESPTime time) {
char buffer[64];
size_t ret = time.strftime(buffer, sizeof(buffer), format);
if (ret > 0)
return this->print(pos, buffer);
return 0;
}
uint8_t MAX7219GridComponent::strftime(const char *format, time::ESPTime time) {
return this->strftime(0, format, time);
}
#endif
} // namespace display
ESPHOME_NAMESPACE_END