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
Project: ESPAsyncE131 - Asynchronous E.131 (sACN) library for Arduino ESP8266 and ESP32
Copyright (c) 2019 Shelby Merrick
http://www.forkineye.com
@ -8,7 +8,7 @@
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
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
to this program or the documentation contained in this document. The
Author shall not be liable in any event for incidental or consequential
@ -28,39 +28,24 @@ ESPAsyncE131::ESPAsyncE131(uint8_t buffers) {
_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;
if (type == E131_UNICAST)
success = initUnicast();
if (type == E131_MULTICAST)
success = initMulticast(universe);
return success;
}
bool ESPAsyncE131::initUnicast() {
bool success = false;
if (udp.listen(E131_DEFAULT_PORT)) {
udp.onPacket(std::bind(&ESPAsyncE131::parsePacket, this,
std::placeholders::_1));
success = true;
switch (type) {
case E131_UNICAST:
Serial.println("Unicasting");
success = udp.listen(E131_DEFAULT_PORT);
break;
case E131_MULTICAST:
success = udp.listenMulticast(E131MulticastAddress(universe),
E131_DEFAULT_PORT);
break;
default:
return false;
}
return success;
}
bool ESPAsyncE131::initMulticast(uint16_t universe) {
bool success = false;
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;
}
udp.onPacket(std::bind(&ESPAsyncE131::parsePacket, this,
std::placeholders::_1));
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)
{
_handler = callback;

View File

@ -118,11 +118,12 @@ class ESPAsyncE131 {
ESPAsyncE131(uint8_t buffers = 1);
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 IPAddress E131MulticastAddress(uint16_t universe);
e131_stats_t stats; // Statistics tracker
protected:
E131PacketHandlerFunction _handler;
@ -136,10 +137,6 @@ class ESPAsyncE131 {
e131_packet_t *sbuff; // Pointer to scratch packet buffer
AsyncUDP udp; // AsyncUDP
// Internal Initializers
bool initUnicast();
bool initMulticast(uint16_t universe);
// UDP packet parser callback
void parsePacket(AsyncUDPPacket _packet);
};

View File

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