Skip to content

Commit d8e213f

Browse files
committed
LED Matrix library refactoring
1 parent ec091f7 commit d8e213f

File tree

7 files changed

+53
-29
lines changed

7 files changed

+53
-29
lines changed

libraries/LED_Matrix/src/LED_Matrix.h renamed to libraries/Arduino_LED_Matrix/src/Arduino_LED_Matrix.h

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ static uint32_t reverse(uint32_t x)
133133
}
134134

135135
// TODO: this is dangerous, use with care
136-
#define load(x) load_wrapper(x, sizeof(x))
136+
#define loadSequence(x) loadWrapper(x, sizeof(x))
137+
137138

138139
static uint8_t __attribute__((aligned)) framebuffer[NUM_LEDS / 8];
139140

140-
class LED_Matrix {
141+
class ArduinoLEDMatrix {
141142

142143
public:
143-
LED_Matrix() {}
144+
ArduinoLEDMatrix() {}
144145
// TODO: find a better name
145146
// autoscroll will be slower than calling next() at precise times
146147
void autoscroll(uint32_t interval_ms) {
@@ -156,50 +157,73 @@ class LED_Matrix {
156157
uint8_t type;
157158
uint8_t ch = FspTimer::get_available_timer(type);
158159
// TODO: avoid passing "this" argument to remove autoscroll
159-
led_timer.begin(TIMER_MODE_PERIODIC, type, ch, 10000.0, 50.0, turnOnLedISR, this);
160-
led_timer.setup_overflow_irq();
161-
led_timer.open();
162-
led_timer.start();
160+
_ledTimer.begin(TIMER_MODE_PERIODIC, type, ch, 10000.0, 50.0, turnOnLedISR, this);
161+
_ledTimer.setup_overflow_irq();
162+
_ledTimer.open();
163+
_ledTimer.start();
163164
}
164165
void next() {
165166
uint32_t frame[3];
166-
static int j = 0;
167-
frame[0] = reverse(*(_frames+(j*4)+0));
168-
frame[1] = reverse(*(_frames+(j*4)+1));
169-
frame[2] = reverse(*(_frames+(j*4)+2));
170-
_interval = *(_frames+(j*4)+3);
171-
j = (j + 1) % _lines;
167+
frame[0] = reverse(*(_frames+(_currentFrame*4)+0));
168+
frame[1] = reverse(*(_frames+(_currentFrame*4)+1));
169+
frame[2] = reverse(*(_frames+(_currentFrame*4)+2));
170+
_interval = *(_frames+(_currentFrame*4)+3);
171+
_currentFrame = (_currentFrame + 1) % _framesCount;
172+
if(_currentFrame == 0){
173+
if(!_loop){
174+
_interval = 0;
175+
}
176+
if(_callBack != nullptr){
177+
_callBack();
178+
}
179+
}
172180
memcpy(framebuffer, (uint32_t*)frame, sizeof(frame));
173181
}
174-
void render_buffer(const uint32_t buffer[3]){
175-
uint32_t frame[3];
176-
static int j = 0;
177-
frame[0] = reverse(*(buffer+(j*4)+0));
178-
frame[1] = reverse(*(buffer+(j*4)+1));
179-
frame[2] = reverse(*(buffer+(j*4)+2));
180-
memcpy(framebuffer, (uint32_t*)frame, sizeof(frame));
182+
void loadFrame(const uint32_t buffer[3]){
183+
uint32_t tempBuffer[][4] = {{
184+
buffer[0], buffer[1], buffer[2], 0
185+
}};
186+
loadSequence(tempBuffer);
187+
next();
181188
}
182-
183-
void load_wrapper(const uint32_t frames[][4], uint32_t howMany) {
189+
void renderFrame(uint8_t frameNumber){
190+
_currentFrame = frameNumber % _framesCount;
191+
next();
192+
_interval = 0;
193+
}
194+
void play(bool loop = false){
195+
_loop = loop;
196+
next();
197+
}
198+
void loadWrapper(const uint32_t frames[][4], uint32_t howMany) {
199+
_currentFrame = 0;
184200
_frames = (uint32_t*)frames;
185-
_lines = (howMany / 4) / sizeof(uint32_t);
201+
_framesCount = (howMany / 4) / sizeof(uint32_t);
186202
}
203+
204+
void setCallback(voidFuncPtr callBack){
205+
_callBack = callBack;
206+
}
207+
187208
private:
209+
int _currentFrame = 0;
188210
uint32_t* _frames;
189-
uint32_t _lines;
211+
uint32_t _framesCount;
190212
uint32_t _interval = 0;
191-
uint32_t _last_interval = 0;
192-
FspTimer led_timer;
213+
uint32_t _lastInterval = 0;
214+
bool _loop = false;
215+
FspTimer _ledTimer;
216+
voidFuncPtr _callBack;
193217

194218
static void turnOnLedISR(timer_callback_args_t *arg) {
195219
static volatile int i_isr = 0;
196220
turnLed(i_isr, ((framebuffer[i_isr >> 3] & (1 << (i_isr % 8))) != 0));
197221
i_isr = (i_isr + 1) % NUM_LEDS;
198222
if (arg != nullptr && arg->p_context != nullptr) {
199-
LED_Matrix* _m = (LED_Matrix*)arg->p_context;
200-
if (_m->_interval != 0 && millis() - _m->_last_interval > _m->_interval) {
223+
ArduinoLEDMatrix* _m = (ArduinoLEDMatrix*)arg->p_context;
224+
if (_m->_interval != 0 && millis() - _m->_lastInterval > _m->_interval) {
201225
_m->next();
202-
_m->_last_interval = millis();
226+
_m->_lastInterval = millis();
203227
}
204228
}
205229
}

0 commit comments

Comments
 (0)