Skip to content

Commit 0bcfa5e

Browse files
committed
Change to use TeensyDMX library
This also fixes DMX reads to disallow ASC (Alternate Start Code) packets.
1 parent 6e03cf0 commit 0bcfa5e

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

Deevstock/DSGrid/control_tdmx.h

Lines changed: 16 additions & 6 deletions
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,15 +14,18 @@
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;
18+
19+
teensydmx::Receiver dmxRx{Serial1};
20+
uint8_t dmxRxBuf[513]; // Buffer up to 513 channels, including the start code
1821

1922
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();
@@ -32,7 +35,7 @@ void controlSetup() {
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

@@ -54,10 +57,17 @@ int led = 0;
5457
int left[7];
5558
int right[7];
5659

60+
// Checks if there's a new DMX frame and returns the frame size.
61+
static int newFrame(teensydmx::Receiver dmxRx) {
62+
return dmxRx.readPacket(dmxRxBuf, 0, 513);
63+
}
64+
5765
void controlLoop() {
5866
int gPatternCount = 32; // FIXME
59-
Dmx.loop();
60-
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
6171
EVERY_N_SECONDS( 2 ) {
6272
Serial.printf("Brighness: %u\n", getValue(1, 0, 255)); // Dimmer data for Channel 1
6373
}

Deevstock/DeevstockDMX/DeevstockDMX.ino

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ const bool kMatrixSerpentineLayout = true;
3030
#define USE_OCTOWS2811
3131
#include<OctoWS2811.h>
3232

33-
#include <TeensyDmx.h>
33+
#include <TeensyDMX.h>
3434
#include <FastLED.h>
3535
#include <Audio.h>
3636

3737
// **********************************************************************************************************
3838

39-
TeensyDmx Dmx(Serial1);
39+
namespace teensydmx = qindesign::teensydmx;
40+
41+
teensydmx::Receiver dmxRx{Serial1};
42+
uint8_t dmxRxBuf[513]; // Buffer up to 513 channels, including the start code
4043

4144
CRGB leds[NUM_LEDS];
4245
CRGB 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);
210213

@@ -235,32 +238,38 @@ 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+
}
246+
238247
int pattern = 0;
239248
void loop()
240249
{
241-
Dmx.loop();
242-
if (Dmx.newFrame()) {
243-
250+
// Read at least to 7 bytes (6 channels) starting from channel 0 (start code)
251+
int read = newFrame(dmxRx);
252+
if (read >= 7 && dmxRxBuf[0] == 0) { // Ensure start code is zero
244253
led = !led;
245254
digitalWrite(LED_BUILTIN, led);
246-
int b = Dmx.getBuffer()[0]; // brightness = 1
255+
int b = dmxRxBuf[1]; // brightness = 1
247256
if (b != BRIGHTNESS) {
248257
BRIGHTNESS = b;
249258
FastLED.setBrightness(BRIGHTNESS);
250259
Serial.printf("Brightness: %u\n", BRIGHTNESS);
251260
}
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
261+
STEPS = dmxRxBuf[2]; // steps = 2
262+
SPEEDO = dmxRxBuf[3]; //speed = 3
263+
FADE = dmxRxBuf[4]; // fade = 4
264+
int p = dmxRxBuf[5]; // pattern = 5
256265
pattern = map(p, 0, 255, 0, (gPatternCount - 1));
257266
if(p > (gPatternCount - 1)) {
258267
p = 0;
259268
}
260269
else {
261270
pattern = p;
262271
}
263-
currentPalette = palettes[map(Dmx.getBuffer()[5], 0, 255, 0, (paletteCount - 1))]; // channel 6
272+
currentPalette = palettes[map(dmxRxBuf[6], 0, 255, 0, (paletteCount - 1))]; // channel 6
264273
// EVERY_N_SECONDS( 2 ) {
265274
// Serial.println(p);
266275
// Serial.print("b=");

0 commit comments

Comments
 (0)