@@ -14,14 +14,16 @@ Distributed as-is; no warranty is given.
1414******************************************************************************/
1515
1616/* ----- LIBRARIES ----- */
17- #if defined(ARDUINO_ARCH_ESP32)
17+ #if defined(ARDUINO_ARCH_ESP32) && defined(WLED_ENABLE_DMX)
1818
1919#include < Arduino.h>
2020#if !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S2)
2121
2222#include " SparkFunDMX.h"
2323#include < HardwareSerial.h>
2424
25+ #pragma message "using SparkFunDMX"
26+
2527#define dmxMaxChannel 512
2628#define defaultMax 32
2729
@@ -34,8 +36,8 @@ static const int enablePin = -1; // disable the enable pin because it is not ne
3436static const int rxPin = -1 ; // disable the receiving pin because it is not needed - softhack007: Pin=-1 means "use default" not "disable"
3537static const int txPin = 2 ; // transmit DMX data over this pin (default is pin 2)
3638
37- // DMX value array and size. Entry 0 will hold startbyte
38- static uint8_t dmxData[dmxMaxChannel] = { 0 };
39+ // DMX value array and size. Entry 0 will hold startbyte, so we need 512+1 elements
40+ static uint8_t dmxData[dmxMaxChannel+ 1 ] = { 0 };
3941static int chanSize = 0 ;
4042#if !defined(DMX_SEND_ONLY)
4143static int currentChannel = 0 ;
@@ -121,15 +123,17 @@ void SparkFunDMX::initWrite (int chanQuant) {
121123#if !defined(DMX_SEND_ONLY)
122124// Function to read DMX data
123125uint8_t SparkFunDMX::read (int Channel) {
126+ if ((Channel > dmxMaxChannel) || (Channel < 1 )) return 0 ; // WLEDMM prevent array out-of-bounds access
124127 if (Channel > chanSize) Channel = chanSize;
125128 return (dmxData[Channel - 1 ]); // subtract one to account for start byte
126129}
127130#endif
128131
129132// Function to send DMX data
130133void SparkFunDMX::write (int Channel, uint8_t value) {
131- if (Channel < 0 ) Channel = 0 ;
132- if (Channel > chanSize) chanSize = Channel;
134+ if (Channel < 1 ) Channel = 1 ;
135+ if (Channel+1 > chanSize) chanSize = min (dmxMaxChannel +1 , Channel+1 ); // WLEDMM "+1" as we need to account for start byte
136+ if (Channel > dmxMaxChannel) Channel = dmxMaxChannel; // WLEDMM prevent array out-of-bounds access
133137 dmxData[0 ] = 0 ;
134138 dmxData[Channel] = value; // add one to account for start byte
135139}
@@ -149,7 +153,7 @@ void SparkFunDMX::update() {
149153
150154 // Send DMX data
151155 DMXSerial.begin (DMXSPEED, DMXFORMAT, rxPin, txPin);// Begin the Serial port
152- DMXSerial.write (dmxData, chanSize);
156+ DMXSerial.write (dmxData, min (dmxMaxChannel+ 1 , chanSize)); // send max 513 bytes = start byte + 512 channels
153157 DMXSerial.flush ();
154158 DMXSerial.end ();// clear our DMX array, end the Hardware Serial port
155159 }
@@ -160,9 +164,11 @@ void SparkFunDMX::update() {
160164 {
161165 while (DMXSerial.available ())
162166 {
163- dmxData[currentChannel++] = DMXSerial.read ();
167+ uint8_t newdata = DMXSerial.read ();
168+ if (currentChannel <= dmxMaxChannel)
169+ dmxData[currentChannel++] = newdata;
164170 }
165- if (currentChannel > chanSize) // Set the channel counter back to 0 if we reach the known end size of our packet
171+ if (( currentChannel > chanSize) || (currentChannel > dmxMaxChannel) ) // Set the channel counter back to 0 if we reach the known end size of our packet
166172 {
167173
168174 portENTER_CRITICAL (&timerMux);
0 commit comments