From 85b692e931212c2c06781eb002a882cd17d865f9 Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Tue, 15 Apr 2025 22:02:16 +0200 Subject: [PATCH 1/7] remove unnecessary tracking of time of the lastDMX packet and remove unnecessary comments --- src/main.cpp | 34 +--------------------------------- 1 file changed, 1 insertion(+), 33 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6844aa4..6926b04 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -469,21 +469,11 @@ void loop() dmx_packet_t dmx1_packet; dmx_packet_t dmx2_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. */ + // check if there's a new DMX packet if (direction1 == Input && dmx_receive(dmx1, &dmx1_packet, 0)) { - // Serial.println("Recv DMX1"); - /* If this code gets called, it means we've received DMX data! */ - dmx_read_offset(dmx1, 1, dmx1_data, 512); artnet.sendArtDmx(broadcastIp, universe1, dmx1_data, 512); - /* 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 (!dmx1_packet.err) @@ -493,13 +483,6 @@ void loop() /*dmx_read_offset(dmx1, 1, dmx1_data, dmx1_packet.size); artnet.sendArtDmx(broadcastIp, universe1, dmx1_data, 512);*/ - - if (now - dmx1_lastUpdate > 1000) - { - /* Print the received start code - it's usually 0. */ - // Serial.printf("Start code is 0x%02X and slot 1 is 0x%02X\n", dmx1_data[0], dmx1_data[1]); - dmx1_lastUpdate = now; - } } else { @@ -513,17 +496,9 @@ void loop() if (direction2 == Input && dmx_receive(dmx2, &dmx2_packet, 0)) { - // Serial.println("Recv DMX2"); - /* If this code gets called, it means we've received DMX data! */ - dmx_read_offset(dmx2, 1, dmx2_data, 512); artnet.sendArtDmx(broadcastIp, universe2, dmx2_data, 512); - /* 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 (!dmx2_packet.err) { @@ -532,13 +507,6 @@ void loop() /*dmx_read_offset(dmx2, 1, dmx2_data, dmx2_packet.size); artnet.sendArtDmx(broadcastIp, universe2, dmx2_data, 512);*/ - - if (now - dmx2_lastUpdate > 1000) - { - /* Print the received start code - it's usually 0. */ - // Serial.printf("Start code is 0x%02X and slot 1 is 0x%02X\n", dmx2_data[0], dmx2_data[1]); - dmx2_lastUpdate = now; - } } else { From f2f17a6f4cb01d4a9ec28fa298a606aa8e071d6b Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Tue, 15 Apr 2025 22:05:44 +0200 Subject: [PATCH 2/7] only send DMX packets if there is no packet error --- src/main.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6926b04..5573923 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -472,17 +472,13 @@ void loop() // check if there's a new DMX packet if (direction1 == Input && dmx_receive(dmx1, &dmx1_packet, 0)) { - dmx_read_offset(dmx1, 1, dmx1_data, 512); - artnet.sendArtDmx(broadcastIp, universe1, dmx1_data, 512); - /* We should check to make sure that there weren't any DMX errors. */ if (!dmx1_packet.err) { /* Don't forget we need to actually read the DMX data into our buffer so that we can print it out. */ - - /*dmx_read_offset(dmx1, 1, dmx1_data, dmx1_packet.size); - artnet.sendArtDmx(broadcastIp, universe1, dmx1_data, 512);*/ + dmx_read_offset(dmx1, 1, dmx1_data, dmx1_packet.size); + artnet.sendArtDmx(broadcastIp, universe1, dmx1_data, dmx1_packet.size); } else { @@ -496,17 +492,13 @@ void loop() if (direction2 == Input && dmx_receive(dmx2, &dmx2_packet, 0)) { - dmx_read_offset(dmx2, 1, dmx2_data, 512); - artnet.sendArtDmx(broadcastIp, universe2, dmx2_data, 512); - /* We should check to make sure that there weren't any DMX errors. */ if (!dmx2_packet.err) { /* Don't forget we need to actually read the DMX data into our buffer so that we can print it out. */ - - /*dmx_read_offset(dmx2, 1, dmx2_data, dmx2_packet.size); - artnet.sendArtDmx(broadcastIp, universe2, dmx2_data, 512);*/ + dmx_read_offset(dmx2, 1, dmx2_data, dmx2_packet.size); + artnet.sendArtDmx(broadcastIp, universe2, dmx2_data, dmx2_packet.size); } else { From 2191406031d65c8ab0fd9cb53362fafe2dc5e56e Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Tue, 15 Apr 2025 22:40:14 +0200 Subject: [PATCH 3/7] remove unnecessary variables --- src/main.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5573923..cddc9b4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,10 +31,6 @@ dmx_port_t dmx1 = DMX_NUM_0; // for esp32s2 dmx_port_t dmx2 = DMX_NUM_1; byte dmx1_data[DMX_PACKET_SIZE]; byte dmx2_data[DMX_PACKET_SIZE]; -unsigned long dmx1_lastUpdate = millis(); -unsigned long dmx2_lastUpdate = millis(); -bool dmx1_IsConnected = false; -bool dmx2_IsConnected = false; // Button #define PIN_LED 7 From a62400434040a9897c8f74b1a9d0d34c00386e06 Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Tue, 15 Apr 2025 23:08:43 +0200 Subject: [PATCH 4/7] extract DMX transmission to separate function --- src/main.cpp | 72 +++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index cddc9b4..c04dc8e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -455,54 +455,46 @@ void setup() Serial.printf("Flash Size %d, Flash Speed %d\n", ESP.getFlashChipSize(), ESP.getFlashChipSpeed()); } +void transmitDmxToArtnet(dmx_port_t dmxPort, byte *dmx_data, uint8_t artnetUniverse) +{ + /* 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 dmx_packet; + + // check if there's a new DMX packet + if (dmx_receive(dmxPort, &dmx_packet, 0)) + { + /* We should check to make sure that there weren't any DMX errors. */ + if (!dmx_packet.err) + { + /* Don't forget we need to actually read the DMX data into our buffer so + that we can print it out. */ + dmx_read_offset(dmxPort, 1, dmx_data, dmx_packet.size); + artnet.sendArtDmx(broadcastIp, artnetUniverse, dmx_data, dmx_packet.size); + } + 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. */ + Serial.printf("A DMX error occurred on port %d.\n", dmxPort); + } + } +} + void loop() { // check if artnet packet has come and execute callback artnet.parse(); - /* 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 dmx1_packet; - dmx_packet_t dmx2_packet; - - // check if there's a new DMX packet - if (direction1 == Input && dmx_receive(dmx1, &dmx1_packet, 0)) + if (direction1 == Input) { - /* We should check to make sure that there weren't any DMX errors. */ - if (!dmx1_packet.err) - { - /* Don't forget we need to actually read the DMX data into our buffer so - that we can print it out. */ - dmx_read_offset(dmx1, 1, dmx1_data, dmx1_packet.size); - artnet.sendArtDmx(broadcastIp, universe1, dmx1_data, dmx1_packet.size); - } - 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. */ - Serial.println("A DMX 1 error occurred."); - } + transmitDmxToArtnet(dmx1, dmx1_data, universe1); } - if (direction2 == Input && dmx_receive(dmx2, &dmx2_packet, 0)) + if (direction2 == Input) { - /* We should check to make sure that there weren't any DMX errors. */ - if (!dmx2_packet.err) - { - /* Don't forget we need to actually read the DMX data into our buffer so - that we can print it out. */ - dmx_read_offset(dmx2, 1, dmx2_data, dmx2_packet.size); - artnet.sendArtDmx(broadcastIp, universe2, dmx2_data, dmx2_packet.size); - } - 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. */ - Serial.println("A DMX 2 error occurred."); - } + transmitDmxToArtnet(dmx2, dmx2_data, universe2); } } From c0da3a6af18811f6974f295351aa9eb301c2756c Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Tue, 15 Apr 2025 23:20:27 +0200 Subject: [PATCH 5/7] only parse artnet packets when necessary --- src/main.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c04dc8e..5ec2d45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -485,8 +485,12 @@ void transmitDmxToArtnet(dmx_port_t dmxPort, byte *dmx_data, uint8_t artnetUnive void loop() { - // check if artnet packet has come and execute callback - artnet.parse(); + // only check for artnet packets if we expect to receive data + if (direction1 == Output || direction2 == Output) + { + // check if artnet packet has come and execute callback + artnet.parse(); + } if (direction1 == Input) { From 37adc08a1e2ebdb78b48d727abde0219ccfb59a9 Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Fri, 18 Apr 2025 19:54:15 +0200 Subject: [PATCH 6/7] remove unnecessary comments and improve logging --- src/main.cpp | 48 +++++++----------------------------------------- 1 file changed, 7 insertions(+), 41 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5ec2d45..26254c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,8 +25,6 @@ #include "routes/networks.h" #include "routes/status.h" -// DMXESPSerial dmx1; -// DMXESPSerial dmx2; dmx_port_t dmx1 = DMX_NUM_0; // for esp32s2 dmx_port_t dmx2 = DMX_NUM_1; byte dmx1_data[DMX_PACKET_SIZE]; @@ -196,15 +194,8 @@ void setup() direction1 = static_cast(config.getUInt("direction-1", DEFAULT_DIRECTION1)); direction2 = static_cast(config.getUInt("direction-2", DEFAULT_DIRECTION2)); - Serial.print("Port A: Universe "); - Serial.print(universe1); - Serial.print(" "); - Serial.println((direction1 == Input) ? "DMX -> Art-Net" : "Art-Net -> DMX"); - - Serial.print("Port B: Universe "); - Serial.print(universe2); - Serial.print(" "); - Serial.println((direction2 == Input) ? "DMX -> Art-Net" : "Art-Net -> DMX"); + Serial.printf("Port A: Universe %d %s\n", universe1, (direction1 == Input) ? "DMX -> Art-Net" : "Art-Net -> DMX"); + Serial.printf("Port B: Universe %d %s\n", universe2, (direction2 == Input) ? "DMX -> Art-Net" : "Art-Net -> DMX"); Connection connection = static_cast(config.getUInt("connection", DEFAULT_CONNECTION)); IpMethod ipMethod = static_cast(config.getUInt("ip-method"), DEFAULT_IP_METHOD); @@ -322,44 +313,21 @@ void setup() Serial.println("Initialize DMX..."); #ifdef CONFIG_IDF_TARGET_ESP32S2 - // dmx1.init(21, 33, Serial0); - // dmx2.init(17, 18, Serial1); - - Serial.print("DMX driver 1 installed: "); - Serial.println(dmx_driver_is_installed(dmx1)); - - Serial.print("DMX driver 2 installed: "); - Serial.println(dmx_driver_is_installed(dmx2)); dmx_config_t dmx_config = DMX_CONFIG_DEFAULT; - dmx_personality_t personalities[] = {}; - /*dmx_personality_t personalities[] = { - {1, "Default Personality"} - };*/ - /*int personality_count = 1;*/ int personality_count = 0; + dmx_driver_install(dmx1, &dmx_config, personalities, personality_count); dmx_set_pin(dmx1, 21, 33, -1); dmx_driver_install(dmx2, &dmx_config, personalities, personality_count); dmx_set_pin(dmx2, 17, 18, -1); - Serial.print("DMX driver 1 installed: "); - Serial.println(dmx_driver_is_installed(dmx1)); + Serial.printf("DMX driver 1 installed: %d\n", dmx_driver_is_installed(dmx1)); + Serial.printf("DMX driver 2 installed: %d\n", dmx_driver_is_installed(dmx2)); - Serial.print("DMX driver 2 installed: "); - Serial.println(dmx_driver_is_installed(dmx2)); - - Serial.print("DMX driver 1 enabled: "); - Serial.println(dmx_driver_is_enabled(dmx1)); - - Serial.print("DMX driver 2 enabled: "); - Serial.println(dmx_driver_is_enabled(dmx2)); - - // TX/RX Pins und Serial0/Serial1 ausgeben - - /* Now set the DMX hardware pins to the pins that we want to use and setup - will be complete! */ + Serial.printf("DMX driver 1 enabled: %d\n", dmx_driver_is_enabled(dmx1)); + Serial.printf("DMX driver 2 enabled: %d\n", dmx_driver_is_enabled(dmx2)); #else dmx1.init(21, 33, Serial1); @@ -373,7 +341,6 @@ void setup() // if Artnet packet comes to this universe, this function is called if (direction1 == Output) { - Serial.println("DMX1 as out"); artnet.subscribeArtDmxUniverse(universe1, [&](const uint8_t *data, uint16_t size, const ArtDmxMetadata &metadata, const ArtNetRemoteInfo &remote) { dmx_write_offset(dmx1, 1, data, size); @@ -383,7 +350,6 @@ void setup() if (direction2 == Output) { - Serial.println("DMX2 as out"); artnet.subscribeArtDmxUniverse(universe2, [&](const uint8_t *data, uint16_t size, const ArtDmxMetadata &metadata, const ArtNetRemoteInfo &remote) { dmx_write_offset(dmx2, 1, data, size); From 19cbe60854fc508b119265734a5507ba4f81b3e3 Mon Sep 17 00:00:00 2001 From: RaffaelW Date: Fri, 18 Apr 2025 19:55:24 +0200 Subject: [PATCH 7/7] remove old commented-out code --- src/main.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 26254c2..172c999 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -357,9 +357,6 @@ void setup() dmx_wait_sent(dmx2, DMX_TIMEOUT_TICK); }); } - // if Artnet packet comes, this function is called to every universe - // artnet.subscribeArtDmx([&](const uint8_t *data, uint16_t size, const ArtDmxMetadata &metadata, const ArtNetRemoteInfo &remote) {}); - if (!LittleFS.begin(true)) { Serial.println("An Error has occurred while mounting LittleFS");