Skip to content

Commit 9380b2b

Browse files
committed
SparkFunDMX: fix for issue wled#2928
* make SparlFunDMX driver more robust: - made variables static (so they don't overlap with other global variables) - made sure all valriables are properly initialized - do not apply pinMode and digitalRead to invalid pins (as this creates problems on -S3, -C3 and -S2) * disable DMX sending code (unneeded code that may case troubles)
1 parent 8caeddd commit 9380b2b

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

wled00/src/dependencies/dmx/SparkFunDMX.cpp

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Distributed as-is; no warranty is given.
1414
******************************************************************************/
1515

1616
/* ----- LIBRARIES ----- */
17-
#ifdef ESP32
17+
#ifdef ARDUINO_ARCH_ESP32
1818

1919
#include <Arduino.h>
2020

@@ -29,28 +29,36 @@ Distributed as-is; no warranty is given.
2929
#define BREAKSPEED 83333
3030
#define BREAKFORMAT SERIAL_8N1
3131

32-
int enablePin = -1; // disable the enable pin because it is not needed
33-
int rxPin = -1; // disable the receiving pin because it is not needed
34-
int txPin = 2; // transmit DMX data over this pin (default is pin 2)
32+
static const int enablePin = -1; // disable the enable pin because it is not needed
33+
static const int rxPin = -1; // disable the receiving pin because it is not needed - softhack007: Pin=-1 means "use default" not "disable"
34+
static const int txPin = 2; // transmit DMX data over this pin (default is pin 2)
3535

3636
//DMX value array and size. Entry 0 will hold startbyte
37-
uint8_t dmxData[dmxMaxChannel] = {};
38-
int chanSize;
39-
int currentChannel = 0;
37+
static uint8_t dmxData[dmxMaxChannel] = { 0 };
38+
static int chanSize = 0;
39+
static int currentChannel = 0;
4040

41-
HardwareSerial DMXSerial(2);
41+
// Some new MCUs (-S2, -C3) don't have HardwareSerial(2)
42+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0)
43+
#if SOC_UART_NUM < 3
44+
#error DMX output is not possible on your MCU, as it not have HardwareSerial(2)
45+
#endif
46+
#endif
47+
48+
static HardwareSerial DMXSerial(2);
4249

4350
/* Interrupt Timer for DMX Receive */
44-
hw_timer_t * timer = NULL;
45-
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
51+
static hw_timer_t * timer = NULL;
52+
static portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
4653

47-
volatile int _interruptCounter;
48-
volatile bool _startCodeDetected = false;
54+
static volatile int _interruptCounter = 0;
55+
static volatile bool _startCodeDetected = false;
4956

5057

58+
#if !defined(DMX_SEND_ONLY)
5159
/* Start Code is detected by 21 low interrupts */
5260
void IRAM_ATTR onTimer() {
53-
if (digitalRead(rxPin) == 1)
61+
if ((rxPin >= 0) && (digitalRead(rxPin) == 1))
5462
{
5563
_interruptCounter = 0; //If the RX Pin is high, we are not in an interrupt
5664
}
@@ -80,10 +88,13 @@ void SparkFunDMX::initRead(int chanQuant) {
8088
chanQuant = defaultMax;
8189
}
8290
chanSize = chanQuant;
83-
pinMode(enablePin, OUTPUT);
84-
digitalWrite(enablePin, LOW);
85-
pinMode(rxPin, INPUT);
91+
if (enablePin >= 0) {
92+
pinMode(enablePin, OUTPUT);
93+
digitalWrite(enablePin, LOW);
94+
}
95+
if (rxPin >= 0) pinMode(rxPin, INPUT);
8696
}
97+
#endif
8798

8899
// Set up the DMX-Protocol
89100
void SparkFunDMX::initWrite (int chanQuant) {
@@ -96,15 +107,19 @@ void SparkFunDMX::initWrite (int chanQuant) {
96107
chanSize = chanQuant + 1; //Add 1 for start code
97108

98109
DMXSerial.begin(DMXSPEED, DMXFORMAT, rxPin, txPin);
99-
pinMode(enablePin, OUTPUT);
100-
digitalWrite(enablePin, HIGH);
110+
if (enablePin >= 0) {
111+
pinMode(enablePin, OUTPUT);
112+
digitalWrite(enablePin, HIGH);
113+
}
101114
}
102115

116+
#if !defined(DMX_SEND_ONLY)
103117
// Function to read DMX data
104118
uint8_t SparkFunDMX::read(int Channel) {
105119
if (Channel > chanSize) Channel = chanSize;
106120
return(dmxData[Channel - 1]); //subtract one to account for start byte
107121
}
122+
#endif
108123

109124
// Function to send DMX data
110125
void SparkFunDMX::write(int Channel, uint8_t value) {
@@ -133,6 +148,7 @@ void SparkFunDMX::update() {
133148
DMXSerial.flush();
134149
DMXSerial.end();//clear our DMX array, end the Hardware Serial port
135150
}
151+
#if !defined(DMX_SEND_ONLY)
136152
else if (_READWRITE == _READ)//In a perfect world, this function ends serial communication upon packet completion and attaches RX to a CHANGE interrupt so the start code can be read again
137153
{
138154
if (_startCodeDetected == true)
@@ -153,6 +169,7 @@ void SparkFunDMX::update() {
153169
}
154170
}
155171
}
172+
#endif
156173
}
157174

158175
// Function to update the DMX bus

wled00/src/dependencies/dmx/SparkFunDMX.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,23 @@ Distributed as-is; no warranty is given.
1919
#ifndef SparkFunDMX_h
2020
#define SparkFunDMX_h
2121

22+
#define DMX_SEND_ONLY // this disables DMX sending features, and saves us two GPIO pins
23+
2224
// ---- Methods ----
2325

2426
class SparkFunDMX {
2527
public:
26-
void initRead(int maxChan);
2728
void initWrite(int maxChan);
29+
#if !defined(DMX_SEND_ONLY)
30+
void initRead(int maxChan);
2831
uint8_t read(int Channel);
32+
#endif
2933
void write(int channel, uint8_t value);
3034
void update();
3135
private:
32-
uint8_t _startCodeValue = 0xFF;
33-
bool _READ = true;
34-
bool _WRITE = false;
36+
const uint8_t _startCodeValue = 0xFF;
37+
const bool _READ = true;
38+
const bool _WRITE = false;
3539
bool _READWRITE;
3640
};
3741

0 commit comments

Comments
 (0)