From 5cefd9b788e8602aeb58eab45a238bf89aa4d925 Mon Sep 17 00:00:00 2001 From: Hideaki Tai Date: Wed, 30 Sep 2020 16:26:12 +0900 Subject: [PATCH] fix and modify the subscriber for every universe --- Artnet.h | 11 ++++++----- README.md | 6 ++++++ examples/Ethernet/send_receive/send_receive.ino | 8 ++++++++ examples/WiFi/send_receive/send_receive.ino | 7 +++++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Artnet.h b/Artnet.h index 550e405..34f1225 100644 --- a/Artnet.h +++ b/Artnet.h @@ -134,6 +134,7 @@ namespace arx static constexpr uint8_t NUM_PIXELS_PER_UNIV { 170 }; + using CallbackAllType = std::function; using CallbackType = std::function; struct Map { uint32_t universe; CallbackType func; }; @@ -263,6 +264,7 @@ namespace arx IPAddress remote_ip; uint16_t remote_port; CallbackMap callbacks; + CallbackAllType callback_all {nullptr}; S* stream; public: @@ -285,10 +287,9 @@ namespace arx memcpy(packet.data(), d, size); remote_ip = stream->S::remoteIP(); remote_port = (uint16_t)stream->S::remotePort(); + if (callback_all) callback_all(universe15bit(), data(), size); for (auto& c : callbacks) - { if (universe15bit() == c.universe) c.func(data(), size); - } return true; } } @@ -361,9 +362,9 @@ namespace arx { callbacks.push_back(Map{universe, func}); } - inline void subscribe(const CallbackType& func) + inline void subscribe(const CallbackAllType& func) { - subscribe(0, func); + callback_all = func; } inline void subscribe(const uint8_t net, const uint8_t subnet, const uint8_t universe, const CallbackType& func) { @@ -385,7 +386,7 @@ namespace arx } inline void unsubscribe() { - unsubscribe(0); + callback_all = nullptr; } inline void unsubscribe(const uint8_t net, const uint8_t subnet, const uint8_t universe) { diff --git a/README.md b/README.md index 4d4a6b8..3249976 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,12 @@ void setup() { // do something with data coming to universe }); + + // if Artnet packet comes, this function is called to every universe + artnet.subscribe([&](uint32_t univ, uint8_t* data, uint16_t size) + { + // do something with data coming to all universe + }); } void loop() diff --git a/examples/Ethernet/send_receive/send_receive.ino b/examples/Ethernet/send_receive/send_receive.ino index d37176e..48e68b2 100644 --- a/examples/Ethernet/send_receive/send_receive.ino +++ b/examples/Ethernet/send_receive/send_receive.ino @@ -33,6 +33,14 @@ void setup() Serial.println(); }); + + // if Artnet packet comes, this function is called to every universe + artnet.subscribe([&](uint32_t univ, uint8_t* data, uint16_t size) + { + Serial.print("ArtNet data has come to universe: "); + Serial.println(univ); + }); + Serial.println("start"); } diff --git a/examples/WiFi/send_receive/send_receive.ino b/examples/WiFi/send_receive/send_receive.ino index 2bc6ae1..2fee298 100644 --- a/examples/WiFi/send_receive/send_receive.ino +++ b/examples/WiFi/send_receive/send_receive.ino @@ -38,6 +38,13 @@ void setup() } Serial.println(); }); + + // if Artnet packet comes, this function is called to every universe + artnet.subscribe([&](uint32_t univ, uint8_t* data, uint16_t size) + { + Serial.print("ArtNet data has come to universe: "); + Serial.println(univ); + }); } void loop()