Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation for sACN Multicast for Ethernet Shield #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ Visual Micro/
*.sdf
*.vcxproj*
*.sln
*.suo
*.suo
.development
E131.ino
67 changes: 17 additions & 50 deletions E131.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -52,7 +53,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),
Expand All @@ -70,11 +71,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)
Expand Down Expand Up @@ -188,61 +191,25 @@ int E131::beginMulticast(const char *ssid, const char *passphrase,
#if defined (INT_ETHERNET)

/* Unicast Ethernet Initializers */
int E131::begin(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"));
}
}

if (retval)
initUnicast();

return retval;
int E131::beginUnicast() {
initUnicast();
}

void E131::begin(uint8_t *mac, IPAddress ip, IPAddress netmask,
IPAddress gateway, IPAddress dns) {
Ethernet.begin(mac, ip, dns, gateway, netmask);
int E131::beginMulticast(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) {
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());
if (retval)
Serial.println(F("- Multicast Enabled"));
else
Serial.println(F("- Failed to enable Multicast"));
}

initUnicast();
}

/* Multicast Ethernet Initializers */
int E131::beginMulticast(uint8_t *mac, uint16_t universe, uint8_t n) {
//TODO: Add ethernet multicast support
return retval;
}

void E131::beginMulticast(uint8_t *mac, uint16_t universe,
IPAddress ip, IPAddress netmask, IPAddress gateway,
IPAddress dns, uint8_t n) {
//TODO: Add ethernet multicast support
}
#endif
/****** END - Ethernet ifdef block ******/

Expand Down
34 changes: 22 additions & 12 deletions E131.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -32,7 +34,14 @@
# define _UDP WiFiUDP
# define INT_ESP8266
# define INT_WIFI
#elif defined (ARDUINO_ARCH_AVR)
#elif defined(CORE_TEENSY)
# include <NativeEthernet.h>
# include <avr/pgmspace.h>
# include <utility/util.h>
# define _UDP EthernetUDP
# define INT_ETHERNET
# define NO_DOUBLE_BUFFER
#else
# include <Ethernet.h>
# include <EthernetUdp.h>
# include <avr/pgmspace.h>
Expand Down Expand Up @@ -99,10 +108,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 + MAX_DMX_VALUES];
} __attribute__((packed));

uint8_t raw[638];
uint8_t raw[125 + 1 + MAX_DMX_VALUES];
} e131_packet_t;

/* Error Types */
Expand Down Expand Up @@ -147,10 +156,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);
#else
void initMulticast(uint16_t universe, uint8_t n = 1);
#endif

public:
uint8_t *data; /* Pointer to DMX channel data */
Expand Down Expand Up @@ -186,15 +201,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);
void 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 ******/

Expand Down
24 changes: 24 additions & 0 deletions utility/util.h
Original file line number Diff line number Diff line change
@@ -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