diff --git a/E131.cpp b/E131.cpp index af49a37..3ddb9d8 100644 --- a/E131.cpp +++ b/E131.cpp @@ -53,19 +53,30 @@ void E131::initUnicast() { } } +/* + * Converts a DMX 'universe' to the corresponding e131 multicast address. + * + * e131 mandates a fixed multicase base-IP of 239.255.0.0, to which + * the (two-byte, 1-63.999) universe value is added. + * (effectively defining the last two positions of the IP) + */ +IPAddress E131::multicastIPFor(uint16_t universe) { + return IPAddress(239, 255, + ((universe >> 8) & 0xff), + ((universe >> 0) & 0xff) + ); +} + void E131::initMulticast(uint16_t universe, uint8_t n) { delay(100); - IPAddress address = IPAddress(239, 255, ((universe >> 8) & 0xff), - ((universe >> 0) & 0xff)); + IPAddress address = multicastIPFor(universe); #ifdef INT_ESP8266 ip_addr_t ifaddr; ip_addr_t multicast_addr; ifaddr.addr = static_cast(WiFi.localIP()); for (uint8_t i = 1; i < n; i++) { - multicast_addr.addr = static_cast(IPAddress(239, 255, - (((universe + i) >> 8) & 0xff), (((universe + i) >> 0) - & 0xff))); + multicast_addr.addr = static_cast(MulticastIPFor(universe + i)); igmp_joingroup(&ifaddr, &multicast_addr); } diff --git a/E131.h b/E131.h index db295d5..eff46ba 100644 --- a/E131.h +++ b/E131.h @@ -160,6 +160,9 @@ class E131 { E131(); + /* Maps a DMX universe to the correct multicast IP */ + static IPAddress multicastIPFor(uint16_t universe); + /* Generic UDP listener, no physical or IP configuration */ void begin(e131_listen_t type, uint16_t universe = 1, uint8_t n = 1);