unify sACN subscribers

This commit is contained in:
Kevin Matz 2020-12-04 14:37:47 -05:00
parent efad085a2a
commit 7f1bd834cc
3 changed files with 26 additions and 40 deletions

View File

@ -1,6 +1,6 @@
/* /*
ESPAsyncE131.cpp ESPAsyncE131.cpp
Project: ESPAsyncE131 - Asynchronous E.131 (sACN) library for Arduino ESP8266 and ESP32 Project: ESPAsyncE131 - Asynchronous E.131 (sACN) library for Arduino ESP8266 and ESP32
Copyright (c) 2019 Shelby Merrick Copyright (c) 2019 Shelby Merrick
http://www.forkineye.com http://www.forkineye.com
@ -8,7 +8,7 @@
This program is provided free for you to use in any way that you wish, This program is provided free for you to use in any way that you wish,
subject to the laws and regulations where you are using it. Due diligence subject to the laws and regulations where you are using it. Due diligence
is strongly suggested before using this code. Please give credit where due. is strongly suggested before using this code. Please give credit where due.
The Author makes no warranty of any kind, express or implied, with regard The Author makes no warranty of any kind, express or implied, with regard
to this program or the documentation contained in this document. The to this program or the documentation contained in this document. The
Author shall not be liable in any event for incidental or consequential Author shall not be liable in any event for incidental or consequential
@ -28,39 +28,24 @@ ESPAsyncE131::ESPAsyncE131(uint8_t buffers) {
_handler = NULL; _handler = NULL;
} }
bool ESPAsyncE131::begin(e131_listen_t type, uint16_t universe) { bool ESPAsyncE131::subscribe(uint16_t universe, e131_listen_t type) {
bool success = false; bool success = false;
if (type == E131_UNICAST) switch (type) {
success = initUnicast(); case E131_UNICAST:
if (type == E131_MULTICAST) Serial.println("Unicasting");
success = initMulticast(universe); success = udp.listen(E131_DEFAULT_PORT);
break;
return success; case E131_MULTICAST:
} success = udp.listenMulticast(E131MulticastAddress(universe),
E131_DEFAULT_PORT);
bool ESPAsyncE131::initUnicast() { break;
bool success = false; default:
return false;
if (udp.listen(E131_DEFAULT_PORT)) {
udp.onPacket(std::bind(&ESPAsyncE131::parsePacket, this,
std::placeholders::_1));
success = true;
} }
return success;
}
bool ESPAsyncE131::initMulticast(uint16_t universe) { udp.onPacket(std::bind(&ESPAsyncE131::parsePacket, this,
bool success = false; std::placeholders::_1));
IPAddress address = IPAddress(239, 255, ((universe >> 8) & 0xff),
((universe >> 0) & 0xff));
if (udp.listenMulticast(address, E131_DEFAULT_PORT)) {
udp.onPacket(std::bind(&ESPAsyncE131::parsePacket, this,
std::placeholders::_1));
success = true;
}
return success; return success;
} }
@ -125,6 +110,10 @@ void ESPAsyncE131::dumpError(e131_packet_t *packet, e131_error_t error) {
} }
} }
IPAddress ESPAsyncE131::E131MulticastAddress(uint16_t universe) {
return IPAddress(239, 255, ((universe >> 8) & 0xff), ((universe >> 0) & 0xff));
}
void ESPAsyncE131::onPacket(E131PacketHandlerFunction callback) void ESPAsyncE131::onPacket(E131PacketHandlerFunction callback)
{ {
_handler = callback; _handler = callback;

View File

@ -118,11 +118,12 @@ class ESPAsyncE131 {
ESPAsyncE131(uint8_t buffers = 1); ESPAsyncE131(uint8_t buffers = 1);
void onPacket(E131PacketHandlerFunction callback); void onPacket(E131PacketHandlerFunction callback);
bool begin(e131_listen_t type, uint16_t universe = 1); bool subscribe(uint16_t universe = 1, e131_listen_t type = E131_MULTICAST);
static void dumpError(e131_packet_t *packet, e131_error_t error); static void dumpError(e131_packet_t *packet, e131_error_t error);
static IPAddress E131MulticastAddress(uint16_t universe);
e131_stats_t stats; // Statistics tracker e131_stats_t stats; // Statistics tracker
protected: protected:
E131PacketHandlerFunction _handler; E131PacketHandlerFunction _handler;
@ -136,10 +137,6 @@ class ESPAsyncE131 {
e131_packet_t *sbuff; // Pointer to scratch packet buffer e131_packet_t *sbuff; // Pointer to scratch packet buffer
AsyncUDP udp; // AsyncUDP AsyncUDP udp; // AsyncUDP
// Internal Initializers
bool initUnicast();
bool initMulticast(uint16_t universe);
// UDP packet parser callback // UDP packet parser callback
void parsePacket(AsyncUDPPacket _packet); void parsePacket(AsyncUDPPacket _packet);
}; };

View File

@ -120,12 +120,12 @@ void setup() {
} }
//// start sACN //// start sACN
if (!e131->begin(sACN_mode, strobe_universe)) { if (!e131->subscribe(strobe_universe, sACN_mode)) {
Serial.println("Failed to start E1.31"); Serial.println("Failed to start E1.31");
} else { } else {
e131->onPacket(std::bind(&Strobe::recvData, strobe, e131->onPacket(std::bind(&Strobe::recvData, strobe,
std::placeholders::_1)); std::placeholders::_1));
Serial.println("Listening for sACN"); Serial.println("Listening for sACN");
} }