From a9a7720a1158dc5b7a2c95c430bcebbc14c4ae3f Mon Sep 17 00:00:00 2001 From: Travis J Dean Date: Mon, 30 Mar 2020 07:45:33 -0400 Subject: [PATCH] Import dmx output library into project. --- wled00/dmx.h | 39 ++++----- wled00/src/dependencies/dmx/ESPDMX.cpp | 106 +++++++++++++++++++++++++ wled00/src/dependencies/dmx/ESPDMX.h | 31 ++++++++ wled00/wled.cpp | 1 + 4 files changed, 158 insertions(+), 19 deletions(-) create mode 100644 wled00/src/dependencies/dmx/ESPDMX.cpp create mode 100644 wled00/src/dependencies/dmx/ESPDMX.h diff --git a/wled00/dmx.h b/wled00/dmx.h index 76aab4849..0ab2a4a20 100644 --- a/wled00/dmx.h +++ b/wled00/dmx.h @@ -1,64 +1,65 @@ #ifndef WLED_DMX_H #define WLED_DMX_H -#include "wled.h" +#include "wled.h" /* * Support for DMX via MAX485. - * Needs the espdmx library. You might have to change the output pin within the library. Sketchy, i know. + * Change the output pin in src/dependencies/ESPDMX.cpp if needed. + * Library from: * https://github.com/Rickgg/ESP-Dmx */ #ifdef WLED_ENABLE_DMX -#include +#include "src/dependencies/dmx/ESPDMX.h" DMXESPSerial dmx; -#ifdef WLED_ENABLE_DMX -void handleDMX() { +void handleDMX() +{ // TODO: calculate brightness manually if no shutter channel is set - + uint8_t brightness = strip.getBrightness(); - for (int i = 0; i < ledCount; i++) { // uses the amount of LEDs as fixture count + for (int i = 0; i < ledCount; i++) { // uses the amount of LEDs as fixture count - uint32_t in = strip.getPixelColor(i); // time to get the colors for the individual fixtures as suggested by AirCookie at issue #462 + uint32_t in = strip.getPixelColor(i); // time to get the colors for the individual fixtures as suggested by AirCookie at issue #462 byte w = in >> 24 & 0xFF; byte r = in >> 16 & 0xFF; - byte g = in >> 8 & 0xFF; - byte b = in & 0xFF; + byte g = in >> 8 & 0xFF; + byte b = in & 0xFF; int DMXFixtureStart = DMXStart + (DMXGap * i); for (int j = 0; j < DMXChannels; j++) { int DMXAddr = DMXFixtureStart + j; switch (DMXFixtureMap[j]) { - case 0: // Set this channel to 0. Good way to tell strobe- and fade-functions to fuck right off. + case 0: // Set this channel to 0. Good way to tell strobe- and fade-functions to fuck right off. dmx.write(DMXAddr, 0); break; - case 1: // Red + case 1: // Red dmx.write(DMXAddr, r); break; - case 2: // Green + case 2: // Green dmx.write(DMXAddr, g); break; - case 3: // Blue + case 3: // Blue dmx.write(DMXAddr, b); break; - case 4: // White + case 4: // White dmx.write(DMXAddr, w); break; - case 5: // Shutter channel. Controls the brightness. + case 5: // Shutter channel. Controls the brightness. dmx.write(DMXAddr, brightness); break; - case 6:// Sets this channel to 255. Like 0, but more wholesome. + case 6: // Sets this channel to 255. Like 0, but more wholesome. dmx.write(DMXAddr, 255); break; } } } - dmx.update(); // update the DMX bus + dmx.update(); // update the DMX bus } #else void handleDMX() {} #endif -#endif //WLED_DMX_H \ No newline at end of file +#endif //WLED_DMX_H \ No newline at end of file diff --git a/wled00/src/dependencies/dmx/ESPDMX.cpp b/wled00/src/dependencies/dmx/ESPDMX.cpp new file mode 100644 index 000000000..f3ece1c8e --- /dev/null +++ b/wled00/src/dependencies/dmx/ESPDMX.cpp @@ -0,0 +1,106 @@ +// - - - - - +// ESPDMX - A Arduino library for sending and receiving DMX using the builtin serial hardware port. +// ESPDMX.cpp: Library implementation file +// +// Copyright (C) 2015 Rick +// This work is licensed under a GNU style license. +// +// Last change: Marcel Seerig +// +// Documentation and samples are available at https://github.com/Rickgg/ESP-Dmx +// - - - - - + +/* ----- LIBRARIES ----- */ +#include + +#include "ESPDMX.h" + + + +#define dmxMaxChannel 512 +#define defaultMax 32 + +#define DMXSPEED 250000 +#define DMXFORMAT SERIAL_8N2 +#define BREAKSPEED 83333 +#define BREAKFORMAT SERIAL_8N1 + +bool dmxStarted = false; +int sendPin = 2; //dafault on ESP8266 + +//DMX value array and size. Entry 0 will hold startbyte +uint8_t dmxData[dmxMaxChannel] = {}; +int chanSize; + + +void DMXESPSerial::init() { + chanSize = defaultMax; + + Serial1.begin(DMXSPEED); + pinMode(sendPin, OUTPUT); + dmxStarted = true; +} + +// Set up the DMX-Protocol +void DMXESPSerial::init(int chanQuant) { + + if (chanQuant > dmxMaxChannel || chanQuant <= 0) { + chanQuant = defaultMax; + } + + chanSize = chanQuant; + + Serial1.begin(DMXSPEED); + pinMode(sendPin, OUTPUT); + dmxStarted = true; +} + +// Function to read DMX data +uint8_t DMXESPSerial::read(int Channel) { + if (dmxStarted == false) init(); + + if (Channel < 1) Channel = 1; + if (Channel > dmxMaxChannel) Channel = dmxMaxChannel; + return(dmxData[Channel]); +} + +// Function to send DMX data +void DMXESPSerial::write(int Channel, uint8_t value) { + if (dmxStarted == false) init(); + + if (Channel < 1) Channel = 1; + if (Channel > chanSize) Channel = chanSize; + if (value < 0) value = 0; + if (value > 255) value = 255; + + dmxData[Channel] = value; +} + +void DMXESPSerial::end() { + delete dmxData; + chanSize = 0; + Serial1.end(); + dmxStarted == false; +} + +void DMXESPSerial::update() { + if (dmxStarted == false) init(); + + //Send break + digitalWrite(sendPin, HIGH); + Serial1.begin(BREAKSPEED, BREAKFORMAT); + Serial1.write(0); + Serial1.flush(); + delay(1); + Serial1.end(); + + //send data + Serial1.begin(DMXSPEED, DMXFORMAT); + digitalWrite(sendPin, LOW); + Serial1.write(dmxData, chanSize); + Serial1.flush(); + delay(1); + Serial1.end(); +} + +// Function to update the DMX bus diff --git a/wled00/src/dependencies/dmx/ESPDMX.h b/wled00/src/dependencies/dmx/ESPDMX.h new file mode 100644 index 000000000..4585bdd26 --- /dev/null +++ b/wled00/src/dependencies/dmx/ESPDMX.h @@ -0,0 +1,31 @@ +// - - - - - +// ESPDMX - A Arduino library for sending and receiving DMX using the builtin serial hardware port. +// ESPDMX.cpp: Library implementation file +// +// Copyright (C) 2015 Rick +// This work is licensed under a GNU style license. +// +// Last change: Marcel Seerig +// +// Documentation and samples are available at https://github.com/Rickgg/ESP-Dmx +// - - - - - + +#include + + +#ifndef ESPDMX_h +#define ESPDMX_h + +// ---- Methods ---- + +class DMXESPSerial { +public: + void init(); + void init(int MaxChan); + uint8_t read(int Channel); + void write(int channel, uint8_t value); + void update(); + void end(); +}; + +#endif diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 48231599d..fdb607765 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -2,6 +2,7 @@ #include "alexa.h" #include "blynk.h" #include "button.h" +#include "dmx.h" #include "file.h" #include "hue.h" #include "ir.h"