From 85e8dfeebb49a3478a783f02b910d050aebdf397 Mon Sep 17 00:00:00 2001 From: Shawn Silverman Date: Fri, 28 Jan 2022 11:41:06 -0800 Subject: [PATCH] Change to use TeensyDMX library This also fixes DMX reads to disallow ASC (Alternate Start Code) packets. --- Deevstock/DSGrid/control_tdmx.h | 30 ++++++++++++----- Deevstock/DeevstockDMX/DeevstockDMX.ino | 43 ++++++++++++++++--------- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/Deevstock/DSGrid/control_tdmx.h b/Deevstock/DSGrid/control_tdmx.h index e639e9e..ab17afb 100644 --- a/Deevstock/DSGrid/control_tdmx.h +++ b/Deevstock/DSGrid/control_tdmx.h @@ -1,5 +1,5 @@ #pragma message Teensy 3.2 + MSGEQ7 -#include +#include //#define MSGEQ7_10BIT // MSGEQ7 #include "MSGEQ7.h" @@ -14,25 +14,28 @@ CMSGEQ7 MSGEQ7; -TeensyDmx Dmx(Serial1); +namespace teensydmx = qindesign::teensydmx; -void InitMSGEQ7() { +teensydmx::Receiver dmxRx{Serial1}; +uint8_t dmxRxBuf[513]; // Buffer up to 513 channels, including the start code + +void InitMSGEQ7() { MSGEQ7.begin(); } void controlSetup() { pinMode(LED_BUILTIN, OUTPUT); - Dmx.setMode(TeensyDmx::DMX_IN); + dmxRx.begin(); pgm = 0; InitMSGEQ7(); - + delay(1000); Serial.println("Startup"); } int getValue(int chan, int minV, int maxV) { - return map(Dmx.getBuffer()[(chan - 1)], 0, 255, minV, maxV); + return map(dmxRxBuf[chan], 0, 255, minV, maxV); } @@ -50,10 +53,21 @@ int MSGEQ7get(int band, int channel) { int led = 0; +// Checks if there's a new DMX frame and returns the frame size. +static int newFrame(teensydmx::Receiver &dmxRx) { + return dmxRx.readPacket(dmxRxBuf, 0, 513); + // Note: It's less efficient to read bytes you don't need; + // this is only here because it was requested to make the + // code look better. Ideally, you should call + // `readPacket(buf, 0, size_needed)` instead. +} + void controlLoop() { int gPatternCount = 32; // FIXME - Dmx.loop(); - if (Dmx.newFrame()) { + + // Read at least 5 bytes (4 channels) starting from channel 0 (start code) + int read = newFrame(dmxRx, dmxRxBuf); + if (read >= 5 && dmxRxBuf[0] == 0) { // Ensure start code is zero EVERY_N_SECONDS( 2 ) { Serial.printf("Brighness: %u\n", getValue(1, 0, 255)); // Dimmer data for Channel 1 } diff --git a/Deevstock/DeevstockDMX/DeevstockDMX.ino b/Deevstock/DeevstockDMX/DeevstockDMX.ino index 84994d9..00f1ff9 100644 --- a/Deevstock/DeevstockDMX/DeevstockDMX.ino +++ b/Deevstock/DeevstockDMX/DeevstockDMX.ino @@ -29,13 +29,16 @@ const bool kMatrixSerpentineLayout = true; #define USE_OCTOWS2811 #include -#include +#include #include #include // ********************************************************************************************************** -TeensyDmx Dmx(Serial1); +namespace teensydmx = qindesign::teensydmx; + +teensydmx::Receiver dmxRx{Serial1}; +uint8_t dmxRxBuf[513]; // Buffer up to 513 channels, including the start code CRGB leds[NUM_LEDS]; CRGB ledsAudio[NUM_AUDIO_LEDS]; @@ -204,7 +207,7 @@ void setup() { /* USB serial */ Serial.begin(115200); - Dmx.setMode(TeensyDmx::DMX_IN); + dmxRx.begin(); // pinMode(LED_BUILTIN, OUTPUT); --- BREAKS AUDIO ?! @@ -235,24 +238,34 @@ elapsedMillis elapsed; // ********************************************************************************************************** // Main // ********************************************************************************************************** + +// Checks if there's a new DMX frame and returns the frame size. +static int newFrame(teensydmx::Receiver dmxRx) { + return dmxRx.readPacket(dmxRxBuf, 0, 513); + // Note: It's less efficient to read bytes you don't need; + // this is only here because it was requested to make the + // code look better. Ideally, you should call + // `readPacket(buf, 0, size_needed)` instead. +} + int pattern = 0; void loop() { - Dmx.loop(); - if (Dmx.newFrame()) { - + // Read at least to 7 bytes (6 channels) starting from channel 0 (start code) + int read = newFrame(dmxRx); + if (read >= 7 && dmxRxBuf[0] == 0) { // Ensure start code is zero led = !led; digitalWrite(LED_BUILTIN, led); - int b = Dmx.getBuffer()[0]; // brightness = 1 + int b = dmxRxBuf[1]; // brightness = 1 if (b != BRIGHTNESS) { BRIGHTNESS = b; FastLED.setBrightness(BRIGHTNESS); Serial.printf("Brightness: %u\n", BRIGHTNESS); } - STEPS = Dmx.getBuffer()[1]; // steps = 2 - SPEEDO = Dmx.getBuffer()[2]; //speed = 3 - FADE = Dmx.getBuffer()[3]; // fade = 4 - int p = Dmx.getBuffer()[4]; // pattern = 5 + STEPS = dmxRxBuf[2]; // steps = 2 + SPEEDO = dmxRxBuf[3]; //speed = 3 + FADE = dmxRxBuf[4]; // fade = 4 + int p = dmxRxBuf[5]; // pattern = 5 pattern = map(p, 0, 255, 0, (gPatternCount - 1)); if(p > (gPatternCount - 1)) { p = 0; @@ -260,11 +273,11 @@ void loop() else { pattern = p; } - currentPalette = palettes[map(Dmx.getBuffer()[5], 0, 255, 0, (paletteCount - 1))]; // channel 6 + currentPalette = palettes[map(dmxRxBuf[6], 0, 255, 0, (paletteCount - 1))]; // channel 6 - RED = Dmx.getBuffer()[6]; - GREEN = Dmx.getBuffer()[7]; - BLUE = Dmx.getBuffer()[8]; + RED = dmxRxBuf[7]; + GREEN = dmxRxBuf[8]; + BLUE = dmxRxBuf[9]; // EVERY_N_SECONDS( 2 ) { // Serial.println(p);