@@ -29,13 +29,16 @@ const bool kMatrixSerpentineLayout = true;
2929#define USE_OCTOWS2811
3030#include < OctoWS2811.h>
3131
32- #include < TeensyDmx .h>
32+ #include < TeensyDMX .h>
3333#include < FastLED.h>
3434#include < Audio.h>
3535
3636// **********************************************************************************************************
3737
38- TeensyDmx Dmx (Serial1);
38+ namespace teensydmx = qindesign::teensydmx;
39+
40+ teensydmx::Receiver dmxRx{Serial1};
41+ uint8_t dmxRxBuf[513 ]; // Buffer up to 513 channels, including the start code
3942
4043CRGB leds[NUM_LEDS];
4144CRGB ledsAudio[NUM_AUDIO_LEDS];
@@ -204,7 +207,7 @@ void setup() {
204207 /* USB serial */
205208 Serial.begin (115200 );
206209
207- Dmx. setMode (TeensyDmx::DMX_IN );
210+ dmxRx. begin ( );
208211
209212// pinMode(LED_BUILTIN, OUTPUT); --- BREAKS AUDIO ?!
210213
@@ -235,36 +238,46 @@ elapsedMillis elapsed;
235238// **********************************************************************************************************
236239// Main
237240// **********************************************************************************************************
241+
242+ // Checks if there's a new DMX frame and returns the frame size.
243+ static int newFrame (teensydmx::Receiver dmxRx) {
244+ return dmxRx.readPacket (dmxRxBuf, 0 , 513 );
245+ // Note: It's less efficient to read bytes you don't need;
246+ // this is only here because it was requested to make the
247+ // code look better. Ideally, you should call
248+ // `readPacket(buf, 0, size_needed)` instead.
249+ }
250+
238251int pattern = 0 ;
239252void loop ()
240253{
241- Dmx. loop ();
242- if (Dmx. newFrame ()) {
243-
254+ // Read at least to 7 bytes (6 channels) starting from channel 0 (start code)
255+ int read = newFrame (dmxRx);
256+ if (read >= 7 && dmxRxBuf[ 0 ] == 0 ) { // Ensure start code is zero
244257 led = !led;
245258 digitalWrite (LED_BUILTIN, led);
246- int b = Dmx. getBuffer ()[ 0 ]; // brightness = 1
259+ int b = dmxRxBuf[ 1 ]; // brightness = 1
247260 if (b != BRIGHTNESS) {
248261 BRIGHTNESS = b;
249262 FastLED.setBrightness (BRIGHTNESS);
250263 Serial.printf (" Brightness: %u\n " , BRIGHTNESS);
251264 }
252- STEPS = Dmx. getBuffer ()[ 1 ]; // steps = 2
253- SPEEDO = Dmx. getBuffer ()[ 2 ]; // speed = 3
254- FADE = Dmx. getBuffer ()[ 3 ]; // fade = 4
255- int p = Dmx. getBuffer ()[ 4 ]; // pattern = 5
265+ STEPS = dmxRxBuf[ 2 ]; // steps = 2
266+ SPEEDO = dmxRxBuf[ 3 ]; // speed = 3
267+ FADE = dmxRxBuf[ 4 ]; // fade = 4
268+ int p = dmxRxBuf[ 5 ]; // pattern = 5
256269 pattern = map (p, 0 , 255 , 0 , (gPatternCount - 1 ));
257270 if (p > (gPatternCount - 1 )) {
258271 p = 0 ;
259272 }
260273 else {
261274 pattern = p;
262275 }
263- currentPalette = palettes[map (Dmx. getBuffer ()[ 5 ], 0 , 255 , 0 , (paletteCount - 1 ))]; // channel 6
276+ currentPalette = palettes[map (dmxRxBuf[ 6 ], 0 , 255 , 0 , (paletteCount - 1 ))]; // channel 6
264277
265- RED = Dmx. getBuffer ()[ 6 ];
266- GREEN = Dmx. getBuffer ()[ 7 ];
267- BLUE = Dmx. getBuffer ()[ 8 ];
278+ RED = dmxRxBuf[ 7 ];
279+ GREEN = dmxRxBuf[ 8 ];
280+ BLUE = dmxRxBuf[ 9 ];
268281
269282// EVERY_N_SECONDS( 2 ) {
270283// Serial.println(p);
0 commit comments