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..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)); @@ -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), @@ -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) @@ -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 ******/ diff --git a/E131.h b/E131.h index db295d5..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,7 +34,14 @@ # define _UDP WiFiUDP # define INT_ESP8266 # define INT_WIFI -#elif defined (ARDUINO_ARCH_AVR) +#elif defined(CORE_TEENSY) +# include +# include +# include +# define _UDP EthernetUDP +# define INT_ETHERNET +# define NO_DOUBLE_BUFFER +#else # include # 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[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 */ @@ -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 */ @@ -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 ******/ 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