From 4f329b25afdb3ecbc9964f34031450b0fccfcf81 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Tue, 4 Feb 2020 22:04:19 +0100 Subject: [PATCH 1/4] implement Multicast Ethernet Initializers --- .gitignore | 4 +++- E131.cpp | 39 ++++++++++++++++++++++++++++++++++++++- utility/util.h | 24 ++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 utility/util.h diff --git a/.gitignore b/.gitignore index 53c0ab0..cf28b5e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ Visual Micro/ *.sdf *.vcxproj* *.sln -*.suo \ No newline at end of file +*.suo +.development +E131.ino diff --git a/E131.cpp b/E131.cpp index af49a37..cc065d0 100644 --- a/E131.cpp +++ b/E131.cpp @@ -235,7 +235,44 @@ void E131::begin(uint8_t *mac, IPAddress ip, IPAddress netmask, /* Multicast Ethernet Initializers */ int E131::beginMulticast(uint8_t *mac, uint16_t universe, uint8_t n) { - //TODO: Add ethernet multicast support + int retval = 0; + + if (Serial) { + Serial.println(""); + Serial.println(F("Requesting Address via DHCP")); + Serial.print(F("- MAC: ")); + for (int i = 0; i < 6; i++) + Serial.print(mac[i], HEX); + Serial.println(""); + } + + retval = Ethernet.begin(mac); + + if (Serial) { + if (retval) { + Serial.print(F("- IP Address: ")); + Serial.println(Ethernet.localIP()); + } else { + Serial.println(F("** DHCP FAILED")); + } + } + + if (retval) { + delay(100); + IPAddress address = IPAddress(239, 255, ((universe >> 8) & 0xff), + ((universe >> 0) & 0xff)); + + retval = udp.beginMulticast(address, E131_DEFAULT_PORT); + + if (Serial) { + if (retval) + Serial.println(F("- Multicast Enabled")); + else + Serial.println(F("- Failed to enable Multicast")); + } + } + + return retval; } void E131::beginMulticast(uint8_t *mac, uint16_t universe, diff --git a/utility/util.h b/utility/util.h new file mode 100644 index 0000000..b5d9f70 --- /dev/null +++ b/utility/util.h @@ -0,0 +1,24 @@ +#ifndef UTIL_H +#define UTIL_H + +#ifndef htons +#define htons(x) ( ((x)<< 8 & 0xFF00) | \ + ((x)>> 8 & 0x00FF) ) +#endif + +#ifndef ntohs +#define ntohs(x) htons(x) +#endif + +#ifndef htonl +#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \ + ((x)<< 8 & 0x00FF0000UL) | \ + ((x)>> 8 & 0x0000FF00UL) | \ + ((x)>>24 & 0x000000FFUL) ) +#endif + +#ifndef ntohl +#define ntohl(x) htonl(x) +#endif + +#endif From 5bed303d725ef11d90103db05ed674b056d167ec Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Tue, 4 Feb 2020 22:25:30 +0100 Subject: [PATCH 2/4] refactor implementation and add support for static ip --- E131.cpp | 76 ++++++++++++++++++++++++++++++++------------------------ E131.h | 12 ++++++--- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/E131.cpp b/E131.cpp index cc065d0..fe1beda 100644 --- a/E131.cpp +++ b/E131.cpp @@ -52,7 +52,7 @@ void E131::initUnicast() { Serial.println(E131_DEFAULT_PORT); } } - +#ifndef INT_ETHERNET void E131::initMulticast(uint16_t universe, uint8_t n) { delay(100); IPAddress address = IPAddress(239, 255, ((universe >> 8) & 0xff), @@ -70,11 +70,13 @@ void E131::initMulticast(uint16_t universe, uint8_t n) { } udp.beginMulticast(WiFi.localIP(), address, E131_DEFAULT_PORT); + #endif if (Serial) { Serial.println(F("- Multicast Enabled")); } } +#endif /****** START - Wireless ifdef block ******/ #if defined (INT_ESP8266) || defined (INT_WIFI) @@ -189,6 +191,15 @@ int E131::beginMulticast(const char *ssid, const char *passphrase, /* Unicast Ethernet Initializers */ int E131::begin(uint8_t *mac) { + int retval = initDHCP(mac); + + if (retval) + initUnicast(); + + return retval; +} + +int E131::initDHCP(uint8_t *mac) { int retval = 0; if (Serial) { @@ -210,15 +221,18 @@ int E131::begin(uint8_t *mac) { Serial.println(F("** DHCP FAILED")); } } - - if (retval) - initUnicast(); - return retval; } void E131::begin(uint8_t *mac, IPAddress ip, IPAddress netmask, IPAddress gateway, IPAddress dns) { + initStaticIP(mac, ip, dns, gateway, netmask); + + initUnicast(); +} + +void E131::initStaticIP(uint8_t *mac, IPAddress ip, IPAddress netmask, + IPAddress gateway, IPAddress dns) { Ethernet.begin(mac, ip, dns, gateway, netmask); if (Serial) { Serial.println(""); @@ -229,36 +243,14 @@ void E131::begin(uint8_t *mac, IPAddress ip, IPAddress netmask, Serial.print(F("- IP Address: ")); Serial.println(Ethernet.localIP()); } - - initUnicast(); } -/* Multicast Ethernet Initializers */ -int E131::beginMulticast(uint8_t *mac, uint16_t universe, uint8_t n) { - int retval = 0; - - if (Serial) { - Serial.println(""); - Serial.println(F("Requesting Address via DHCP")); - Serial.print(F("- MAC: ")); - for (int i = 0; i < 6; i++) - Serial.print(mac[i], HEX); - Serial.println(""); - } - - retval = Ethernet.begin(mac); - if (Serial) { - if (retval) { - Serial.print(F("- IP Address: ")); - Serial.println(Ethernet.localIP()); - } else { - Serial.println(F("** DHCP FAILED")); - } - } +/* DHCP Multicast Ethernet Initializers */ +int E131::beginMulticast(uint8_t *mac, uint16_t universe, uint8_t n) { + int retval = initDHCP(mac); if (retval) { - delay(100); IPAddress address = IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff)); @@ -274,12 +266,30 @@ int E131::beginMulticast(uint8_t *mac, uint16_t universe, uint8_t n) { return retval; } - -void E131::beginMulticast(uint8_t *mac, uint16_t universe, +/* Static IP Multicast Ethernet Initializers */ +int E131::beginMulticast(uint8_t *mac, uint16_t universe, IPAddress ip, IPAddress netmask, IPAddress gateway, IPAddress dns, uint8_t n) { - //TODO: Add ethernet multicast support + Ethernet.begin(mac, ip, dns, gateway, netmask); + + return initMulticast(universe); +} + +int E131::initMulticast(uint16_t universe, uint8_t n) { + IPAddress address = IPAddress(239, 255, ((universe >> 8) & 0xff), + ((universe >> 0) & 0xff)); + + int retval = udp.beginMulticast(address, E131_DEFAULT_PORT); + + if (Serial) { + if (retval) + Serial.println(F("- Multicast Enabled")); + else + Serial.println(F("- Failed to enable Multicast")); + } + return retval; } + #endif /****** END - Ethernet ifdef block ******/ diff --git a/E131.h b/E131.h index db295d5..1030287 100644 --- a/E131.h +++ b/E131.h @@ -147,10 +147,16 @@ class E131 { /* Internal Initializers */ int initWiFi(const char *ssid, const char *passphrase); - int initEthernet(uint8_t *mac, IPAddress ip, IPAddress netmask, - IPAddress gateway, IPAddress dns); void initUnicast(); + +#if defined (INT_ETHERNET) + int initDHCP(uint8_t *mac); + void initStaticIP(uint8_t *mac, IPAddress ip, IPAddress netmask, + IPAddress gateway, IPAddress dns); + int initMulticast(uint16_t universe, uint8_t n = 1); +#elif void initMulticast(uint16_t universe, uint8_t n = 1); +#endif public: uint8_t *data; /* Pointer to DMX channel data */ @@ -192,7 +198,7 @@ class E131 { /* Multicast Ethernet Initializers */ int beginMulticast(uint8_t *mac, uint16_t universe, uint8_t n = 1); - void beginMulticast(uint8_t *mac, uint16_t universe, + int beginMulticast(uint8_t *mac, uint16_t universe, IPAddress ip, IPAddress netmask, IPAddress gateway, IPAddress dns, uint8_t n = 1); #endif From cbe89f287cc2b41d5c830941703b408975742a58 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Thu, 5 Mar 2020 17:41:37 +0100 Subject: [PATCH 3/4] add support for multiple multicast sockets --- E131.cpp | 86 ++------------------------------------------------------ E131.h | 17 ++++------- 2 files changed, 9 insertions(+), 94 deletions(-) diff --git a/E131.cpp b/E131.cpp index fe1beda..10f6a31 100644 --- a/E131.cpp +++ b/E131.cpp @@ -33,6 +33,7 @@ E131::E131() { memset(pbuff1.raw, 0, sizeof(pbuff1.raw)); packet = &pbuff1; pwbuff = packet; + data = packet->property_values + 1; #else memset(pbuff1.raw, 0, sizeof(pbuff1.raw)); memset(pbuff2.raw, 0, sizeof(pbuff2.raw)); @@ -190,92 +191,11 @@ int E131::beginMulticast(const char *ssid, const char *passphrase, #if defined (INT_ETHERNET) /* Unicast Ethernet Initializers */ -int E131::begin(uint8_t *mac) { - int retval = initDHCP(mac); - - if (retval) - initUnicast(); - - return retval; -} - -int E131::initDHCP(uint8_t *mac) { - int retval = 0; - - if (Serial) { - Serial.println(""); - Serial.println(F("Requesting Address via DHCP")); - Serial.print(F("- MAC: ")); - for (int i = 0; i < 6; i++) - Serial.print(mac[i], HEX); - Serial.println(""); - } - - retval = Ethernet.begin(mac); - - if (Serial) { - if (retval) { - Serial.print(F("- IP Address: ")); - Serial.println(Ethernet.localIP()); - } else { - Serial.println(F("** DHCP FAILED")); - } - } - return retval; -} - -void E131::begin(uint8_t *mac, IPAddress ip, IPAddress netmask, - IPAddress gateway, IPAddress dns) { - initStaticIP(mac, ip, dns, gateway, netmask); - +int E131::beginUnicast() { initUnicast(); } -void E131::initStaticIP(uint8_t *mac, IPAddress ip, IPAddress netmask, - IPAddress gateway, IPAddress dns) { - Ethernet.begin(mac, ip, dns, gateway, netmask); - if (Serial) { - Serial.println(""); - Serial.println(F("Static Configuration")); - Serial.println(F("- MAC: ")); - for (int i = 0; i < 6; i++) - Serial.print(mac[i], HEX); - Serial.print(F("- IP Address: ")); - Serial.println(Ethernet.localIP()); - } -} - - -/* DHCP Multicast Ethernet Initializers */ -int E131::beginMulticast(uint8_t *mac, uint16_t universe, uint8_t n) { - int retval = initDHCP(mac); - - if (retval) { - IPAddress address = IPAddress(239, 255, ((universe >> 8) & 0xff), - ((universe >> 0) & 0xff)); - - retval = udp.beginMulticast(address, E131_DEFAULT_PORT); - - if (Serial) { - if (retval) - Serial.println(F("- Multicast Enabled")); - else - Serial.println(F("- Failed to enable Multicast")); - } - } - - return retval; -} -/* Static IP Multicast Ethernet Initializers */ -int E131::beginMulticast(uint8_t *mac, uint16_t universe, - IPAddress ip, IPAddress netmask, IPAddress gateway, - IPAddress dns, uint8_t n) { - Ethernet.begin(mac, ip, dns, gateway, netmask); - - return initMulticast(universe); -} - -int E131::initMulticast(uint16_t universe, uint8_t n) { +int E131::beginMulticast(uint16_t universe, uint8_t n) { IPAddress address = IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff)); diff --git a/E131.h b/E131.h index 1030287..5530a18 100644 --- a/E131.h +++ b/E131.h @@ -32,7 +32,7 @@ # define _UDP WiFiUDP # define INT_ESP8266 # define INT_WIFI -#elif defined (ARDUINO_ARCH_AVR) +#else # include # include # include @@ -99,10 +99,10 @@ typedef union { uint16_t first_address; uint16_t address_increment; uint16_t property_value_count; - uint8_t property_values[513]; + uint8_t property_values[1 + 512 * 4]; } __attribute__((packed)); - uint8_t raw[638]; + uint8_t raw[125 + 1 + 512 * 4]; } e131_packet_t; /* Error Types */ @@ -154,7 +154,7 @@ class E131 { void initStaticIP(uint8_t *mac, IPAddress ip, IPAddress netmask, IPAddress gateway, IPAddress dns); int initMulticast(uint16_t universe, uint8_t n = 1); -#elif +#else void initMulticast(uint16_t universe, uint8_t n = 1); #endif @@ -192,15 +192,10 @@ class E131 { /****** START - Ethernet ifdef block ******/ #if defined (INT_ETHERNET) /* Unicast Ethernet Initializers */ - int begin(uint8_t *mac); - void begin(uint8_t *mac, - IPAddress ip, IPAddress netmask, IPAddress gateway, IPAddress dns); + int beginUnicast(); /* Multicast Ethernet Initializers */ - int beginMulticast(uint8_t *mac, uint16_t universe, uint8_t n = 1); - int beginMulticast(uint8_t *mac, uint16_t universe, - IPAddress ip, IPAddress netmask, IPAddress gateway, - IPAddress dns, uint8_t n = 1); + int beginMulticast(uint16_t universe, uint8_t n = 1); #endif /****** END - Ethernet ifdef block ******/ From e7f9787e1850738aad5898f4dfe4dfcc62d25e49 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Sat, 2 Jan 2021 00:37:11 +0100 Subject: [PATCH 4/4] add support for CORE_TEENSY --- E131.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/E131.h b/E131.h index 5530a18..9c2f1ad 100644 --- a/E131.h +++ b/E131.h @@ -20,6 +20,8 @@ #ifndef E131_H_ #define E131_H_ +#define MAX_DMX_VALUES 1024 + #include "Arduino.h" /* Network interface detection. WiFi for ESP8266 and Ethernet for AVR */ @@ -32,6 +34,13 @@ # define _UDP WiFiUDP # define INT_ESP8266 # define INT_WIFI +#elif defined(CORE_TEENSY) +# include +# include +# include +# define _UDP EthernetUDP +# define INT_ETHERNET +# define NO_DOUBLE_BUFFER #else # include # include @@ -99,10 +108,10 @@ typedef union { uint16_t first_address; uint16_t address_increment; uint16_t property_value_count; - uint8_t property_values[1 + 512 * 4]; + uint8_t property_values[1 + MAX_DMX_VALUES]; } __attribute__((packed)); - uint8_t raw[125 + 1 + 512 * 4]; + uint8_t raw[125 + 1 + MAX_DMX_VALUES]; } e131_packet_t; /* Error Types */