From 5b76df903f84b7bda72415ef19fe186c2bc0b573 Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Sat, 18 Feb 2023 16:05:42 +0000 Subject: [PATCH] Inital code swapping DMX input over to use esp_dmx --- platformio.ini | 1 + wled00/dmx.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++----- wled00/wled.cpp | 11 ++++++++++ wled00/wled.h | 11 ++++++++-- 4 files changed, 73 insertions(+), 8 deletions(-) diff --git a/platformio.ini b/platformio.ini index 8a67f546..df6c8b11 100644 --- a/platformio.ini +++ b/platformio.ini @@ -257,6 +257,7 @@ lib_deps = https://github.com/softhack007/LITTLEFS-threadsafe.git#master makuna/NeoPixelBus @ 2.6.9 https://github.com/pbolduc/AsyncTCP.git @ 1.2.0 + https://github.com/someweisguy/esp_dmx.git ; @ v3.0.2-beta ;; ** For compiling with latest Frameworks (IDF4.4.x and arduino-esp32 v2.0.x) ** ;;; standard platform diff --git a/wled00/dmx.cpp b/wled00/dmx.cpp index 4ef4c589..889c9ba8 100644 --- a/wled00/dmx.cpp +++ b/wled00/dmx.cpp @@ -99,13 +99,59 @@ void initDMX() {} #endif #ifdef WLED_ENABLE_DMX_INPUT + +bool dmxIsConnected = false; +unsigned long dmxLastUpdate = 0; + void handleDMXInput() { - dmx.update(); - uint8_t data[512] = {}; - for(int i=0; i < 512; i++) { - data[i] = dmx.read(i + 1); + byte data[DMX_PACKET_SIZE]; + /* We need a place to store information about the DMX packets we receive. We + will use a dmx_packet_t to store that packet information. */ + dmx_packet_t packet; + + /* And now we wait! The DMX standard defines the amount of time until DMX + officially times out. That amount of time is converted into ESP32 clock + ticks using the constant `DMX_TIMEOUT_TICK`. If it takes longer than that + amount of time to receive data, this if statement will evaluate to false. */ + if (dmx_receive(dmxPort, &packet, DMX_TIMEOUT_TICK)) { + /* If this code gets called, it means we've received DMX data! */ + + /* Get the current time since boot in milliseconds so that we can find out + how long it has been since we last updated data and printed to the Serial + Monitor. */ + unsigned long now = millis(); + + /* We should check to make sure that there weren't any DMX errors. */ + if (!packet.err) { + /* If this is the first DMX data we've received, lets log it! */ + if (!dmxIsConnected) { + DEBUG_PRINTLN("DMX is connected!"); + dmxIsConnected = true; + } + + /* Don't forget we need to actually read the DMX data into our buffer so + that we can print it out. */ + dmx_read(dmxPort, data, packet.size); + + if (now - dmxLastUpdate > 1000) { + /* Print the received start code - it's usually 0. */ + DEBUG_PRINTF("Start code is 0x%02X and slot 1 is 0x%02X\n", data[0], + data[1]); + dmxLastUpdate = now; + } + } else { + /* Oops! A DMX error occurred! Don't worry, this can happen when you first + connect or disconnect your DMX devices. If you are consistently getting + DMX errors, then something may have gone wrong with your code or + something is seriously wrong with your DMX transmitter. */ + DEBUG_PRINTLN("A DMX error occurred."); + } + } else if (dmxIsConnected) { + DEBUG_PRINTLN("DMX was disconnected."); + } + if(dmxIsConnected) { + DEBUG_PRINTF("DMX channel 1 = %u\n", data(1)); // TODO: remove from final code + handleDMXData(1, 512, data, 1, REALTIME_MODE_DMX, 0); } - handleDMXData(1, 512, data, 1, REALTIME_MODE_DMX, 0); - DEBUG_PRINTF("DMX channel 1 = %u\n", dmx.read(1)); // TODO: remove from final code } #endif \ No newline at end of file diff --git a/wled00/wled.cpp b/wled00/wled.cpp index bc11c3d1..d1e8c3d1 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -618,6 +618,17 @@ void WLED::setup() } #endif + #ifdef WLED_ENABLE_DMX_INPUT + /* Set the DMX hardware pins to the pins that we want to use. */ + dmx_set_pin(dmxPort, dmxTransmitPin, dmxReceivePin, dmxEnablePin); + + /* Now we can install the DMX driver! We'll tell it which DMX port to use and + which interrupt priority it should have. If you aren't sure which interrupt + priority to use, you can use the macro `DMX_DEFAULT_INTR_FLAG` to set the + interrupt to its default settings.*/ + dmx_driver_install(dmxPort, DMX_DEFAULT_INTR_FLAGS); + #endif + //#endif // WLEDMM end } diff --git a/wled00/wled.h b/wled00/wled.h index f61f9015..d4d8892c 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -124,7 +124,7 @@ #include "src/dependencies/blynk/BlynkSimpleEsp.h" #endif -#if defined(WLED_ENABLE_DMX) || defined(WLED_ENABLE_DMX_INPUT) +#if defined(WLED_ENABLE_DMX) #ifdef ESP8266 #include "src/dependencies/dmx/ESPDMX.h" #else //ESP32 @@ -135,6 +135,10 @@ #endif #endif +#ifdef WLED_ENABLE_DMX_INPUT +#include +#endif + #include "src/dependencies/e131/ESPAsyncE131.h" #ifdef WLED_ENABLE_MQTT #include "src/dependencies/async-mqtt-client/AsyncMqttClient.h" @@ -394,7 +398,7 @@ WLED_GLOBAL bool receiveDirect _INIT(true); // receive UDP WLED_GLOBAL bool arlsDisableGammaCorrection _INIT(true); // activate if gamma correction is handled by the source WLED_GLOBAL bool arlsForceMaxBri _INIT(false); // enable to force max brightness if source has very dark colors that would be black -#if defined(WLED_ENABLE_DMX) || defined(WLED_ENABLE_DMX_INPUT) +#ifdef WLED_ENABLE_DMX #ifdef ESP8266 WLED_GLOBAL DMXESPSerial dmx; #else //ESP32 @@ -402,6 +406,9 @@ WLED_GLOBAL bool arlsForceMaxBri _INIT(false); // enable to f #endif WLED_GLOBAL uint16_t e131ProxyUniverse _INIT(0); // output this E1.31 (sACN) / ArtNet universe via MAX485 (0 = disabled) #endif +#ifdef WLED_ENABLE_DMX_INPUT +dmx_port_t dmxPort = 1; +#endif WLED_GLOBAL uint16_t e131Universe _INIT(1); // settings for E1.31 (sACN) protocol (only DMX_MODE_MULTIPLE_* can span over consequtive universes) WLED_GLOBAL uint16_t e131Port _INIT(5568); // DMX in port. E1.31 default is 5568, Art-Net is 6454 WLED_GLOBAL byte DMXMode _INIT(DMX_MODE_MULTIPLE_RGB); // DMX mode (s.a.)