Skip to content

Commit 2a838fa

Browse files
committed
Change to use TeensyDMX library
This also fixes DMX reads to disallow ASC (Alternate Start Code) packets.
1 parent efc97ab commit 2a838fa

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

Deevstock/DSGrid/control_tdmx.h

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma message Teensy 3.2 + MSGEQ7
2-
#include <TeensyDmx.h>
2+
#include <TeensyDMX.h>
33
//#define MSGEQ7_10BIT
44
// MSGEQ7
55
#include "MSGEQ7.h"
@@ -14,25 +14,28 @@
1414

1515
CMSGEQ7<true, MSGEQ7_RESET_PIN, MSGEQ7_STROBE_PIN, AUDIO_LEFT_PIN, AUDIO_RIGHT_PIN> MSGEQ7;
1616

17-
TeensyDmx Dmx(Serial1);
17+
namespace teensydmx = qindesign::teensydmx;
1818

19-
void InitMSGEQ7() {
19+
teensydmx::Receiver dmxRx{Serial1};
20+
uint8_t dmxRxBuf[513]; // Buffer up to 513 channels, including the start code
21+
22+
void InitMSGEQ7() {
2023
MSGEQ7.begin();
2124
}
2225

2326
void controlSetup() {
2427
pinMode(LED_BUILTIN, OUTPUT);
25-
Dmx.setMode(TeensyDmx::DMX_IN);
28+
dmxRx.begin();
2629
pgm = 0;
2730

2831
InitMSGEQ7();
29-
32+
3033
delay(1000);
3134
Serial.println("Startup");
3235
}
3336

3437
int getValue(int chan, int minV, int maxV) {
35-
return map(Dmx.getBuffer()[(chan - 1)], 0, 255, minV, maxV);
38+
return map(dmxRxBuf[chan], 0, 255, minV, maxV);
3639
}
3740

3841

@@ -50,10 +53,21 @@ int MSGEQ7get(int band, int channel) {
5053

5154
int led = 0;
5255

56+
// Checks if there's a new DMX frame and returns the frame size.
57+
static int newFrame(teensydmx::Receiver &dmxRx) {
58+
return dmxRx.readPacket(dmxRxBuf, 0, 513);
59+
// Note: It's less efficient to read bytes you don't need;
60+
// this is only here because it was requested to make the
61+
// code look better. Ideally, you should call
62+
// `readPacket(buf, 0, size_needed)` instead.
63+
}
64+
5365
void controlLoop() {
5466
int gPatternCount = 32; // FIXME
55-
Dmx.loop();
56-
if (Dmx.newFrame()) {
67+
68+
// Read at least 5 bytes (4 channels) starting from channel 0 (start code)
69+
int read = newFrame(dmxRx, dmxRxBuf);
70+
if (read >= 5 && dmxRxBuf[0] == 0) { // Ensure start code is zero
5771
EVERY_N_SECONDS( 2 ) {
5872
Serial.printf("Brighness: %u\n", getValue(1, 0, 255)); // Dimmer data for Channel 1
5973
}

Deevstock/DeevstockDMX/DeevstockDMX.ino

+29-16
Original file line numberDiff line numberDiff line change
@@ -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

4043
CRGB leds[NUM_LEDS];
4144
CRGB ledsAudio[NUM_AUDIO_LEDS];
@@ -204,13 +207,13 @@ 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

211214
// FastLED.addLeds<CHIPSET, LED_PIN>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
212215
LEDS.addLeds<OCTOWS2811, RGB>(leds, NUM_LEDS_PER_STRIP);
213-
216+
214217
FastLED.setBrightness(BRIGHTNESS);
215218
Serial.println("Setup");
216219

@@ -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+
238251
int pattern = 0;
239252
void 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

Comments
 (0)