@@ -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 */
5260void 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
89100void 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
104118uint8_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
110125void 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
0 commit comments