@@ -133,14 +133,15 @@ static uint32_t reverse(uint32_t x)
133
133
}
134
134
135
135
// 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
+
137
138
138
139
static uint8_t __attribute__ ((aligned)) framebuffer[NUM_LEDS / 8];
139
140
140
- class LED_Matrix {
141
+ class ArduinoLEDMatrix {
141
142
142
143
public:
143
- LED_Matrix () {}
144
+ ArduinoLEDMatrix () {}
144
145
// TODO: find a better name
145
146
// autoscroll will be slower than calling next() at precise times
146
147
void autoscroll (uint32_t interval_ms) {
@@ -156,50 +157,73 @@ class LED_Matrix {
156
157
uint8_t type;
157
158
uint8_t ch = FspTimer::get_available_timer (type);
158
159
// 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 ();
163
164
}
164
165
void next () {
165
166
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
+ }
172
180
memcpy (framebuffer, (uint32_t *)frame, sizeof (frame));
173
181
}
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 ();
181
188
}
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 ;
184
200
_frames = (uint32_t *)frames;
185
- _lines = (howMany / 4 ) / sizeof (uint32_t );
201
+ _framesCount = (howMany / 4 ) / sizeof (uint32_t );
186
202
}
203
+
204
+ void setCallback (voidFuncPtr callBack){
205
+ _callBack = callBack;
206
+ }
207
+
187
208
private:
209
+ int _currentFrame = 0 ;
188
210
uint32_t * _frames;
189
- uint32_t _lines ;
211
+ uint32_t _framesCount ;
190
212
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;
193
217
194
218
static void turnOnLedISR (timer_callback_args_t *arg) {
195
219
static volatile int i_isr = 0 ;
196
220
turnLed (i_isr, ((framebuffer[i_isr >> 3 ] & (1 << (i_isr % 8 ))) != 0 ));
197
221
i_isr = (i_isr + 1 ) % NUM_LEDS;
198
222
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 ) {
201
225
_m->next ();
202
- _m->_last_interval = millis ();
226
+ _m->_lastInterval = millis ();
203
227
}
204
228
}
205
229
}
0 commit comments