From 7f1bd834ccd4f656b207b5b87258605fe971b2fa Mon Sep 17 00:00:00 2001 From: Kevin Matz Date: Fri, 4 Dec 2020 14:37:47 -0500 Subject: [PATCH] unify sACN subscribers --- wiflash_esp32/sacn.cpp | 51 +++++++++++++-------------------- wiflash_esp32/sacn.h | 9 ++---- wiflash_esp32/wiflash_esp32.ino | 6 ++-- 3 files changed, 26 insertions(+), 40 deletions(-) diff --git a/wiflash_esp32/sacn.cpp b/wiflash_esp32/sacn.cpp index 4467a10..8b880eb 100644 --- a/wiflash_esp32/sacn.cpp +++ b/wiflash_esp32/sacn.cpp @@ -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; diff --git a/wiflash_esp32/sacn.h b/wiflash_esp32/sacn.h index 6963f1c..57adcb1 100644 --- a/wiflash_esp32/sacn.h +++ b/wiflash_esp32/sacn.h @@ -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); }; diff --git a/wiflash_esp32/wiflash_esp32.ino b/wiflash_esp32/wiflash_esp32.ino index 5478edc..8531ff0 100644 --- a/wiflash_esp32/wiflash_esp32.ino +++ b/wiflash_esp32/wiflash_esp32.ino @@ -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"); }