Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: support art trigger callback #87

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Artnet/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ constexpr uint16_t PACKET_SIZE {530};
using CallbackAllType = std::function<void(const uint32_t universe, const uint8_t* data, const uint16_t size)>;
using CallbackType = std::function<void(const uint8_t* data, const uint16_t size)>;
using CallbackArtSync = std::function<void(void)>;
using CallbackArtTrigger = std::function<void(uint16_t oem, uint8_t key, uint8_t sub_key, const uint8_t *payload, uint16_t size)>;

#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
template <uint16_t SIZE>
Expand Down
33 changes: 27 additions & 6 deletions Artnet/Receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Common.h"
#include "ArtDmx.h"
#include "ArtPollReply.h"
#include "ArtTrigger.h"

namespace art_net {

Expand All @@ -18,6 +19,7 @@ class Receiver_ {
CallbackMap callbacks;
CallbackAllType callback_all;
CallbackArtSync callback_artsync;
CallbackArtTrigger callback_arttrigger;
S* stream;
bool b_verbose {false};
artpollreply::ArtPollReply artpollreply_ctx;
Expand Down Expand Up @@ -53,25 +55,33 @@ class Receiver_ {

OpCode op_code = OpCode::NA;
if (checkID(d)) {
remote_ip = stream->S::remoteIP();
remote_port = (uint16_t)stream->S::remotePort();
OpCode received_op_code = static_cast<OpCode>(opcode(d));
switch (received_op_code) {
case OpCode::Dmx: {
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 - HEADER_SIZE);
for (auto& c : callbacks)
if (universe15bit() == c.first) c.second(data(), size - HEADER_SIZE);
op_code = OpCode::Dmx;
break;
}
case OpCode::Poll: {
remote_ip = stream->S::remoteIP();
remote_port = (uint16_t)stream->S::remotePort();
poll_reply();
op_code = OpCode::Poll;
break;
}
case OpCode::Trigger: {
if (callback_arttrigger) {
uint16_t oem = (d[art_trigger::OEM_H] << 8) | d[art_trigger::OEM_L];
uint8_t key = d[art_trigger::KEY];
uint8_t sub_key = d[art_trigger::SUB_KEY];
callback_arttrigger(oem, key, sub_key, d + art_trigger::PAYLOAD, size - art_trigger::PAYLOAD);
}
op_code = OpCode::Trigger;
break;
}
case OpCode::Sync: {
if (callback_artsync) callback_artsync();
op_code = OpCode::Sync;
Expand Down Expand Up @@ -185,13 +195,21 @@ class Receiver_ {
callback_all = arx::function_traits<F>::cast(func);
}
template <typename F>
inline auto subscribeArtsync(F&& func) -> std::enable_if_t<arx::is_callable<F>::value> {
inline auto subscribeArtSync(F&& func) -> std::enable_if_t<arx::is_callable<F>::value> {
callback_artsync = arx::function_traits<F>::cast(func);
}
template <typename F>
inline auto subscribeArtsync(F* func) -> std::enable_if_t<arx::is_callable<F>::value> {
inline auto subscribeArtSync(F* func) -> std::enable_if_t<arx::is_callable<F>::value> {
callback_artsync = arx::function_traits<F>::cast(func);
}
template <typename F>
inline auto subscribeArtTrigger(F&& func) -> std::enable_if_t<arx::is_callable<F>::value> {
callback_arttrigger = arx::function_traits<F>::cast(func);
}
template <typename F>
inline auto subscribeArtTrigger(F* func) -> std::enable_if_t<arx::is_callable<F>::value> {
callback_arttrigger = arx::function_traits<F>::cast(func);
}

inline void unsubscribe(const uint8_t universe) {
auto it = callbacks.find(universe);
Expand All @@ -203,6 +221,9 @@ class Receiver_ {
inline void unsubscribeArtSync() {
callback_artsync = nullptr;
}
inline void unsubscribeArtTrigger() {
callback_arttrigger = nullptr;
}

inline void clear_subscribers() {
unsubscribe();
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,23 +298,23 @@ void sync(const String& ip);
```C++
OpCode parse();
// subscribers
template <typename F> inline auto subscribe(const uint8_t universe, F&& func);
template <typename F> inline auto subscribe(const uint8_t universe, F* func);
template <typename F> inline auto subscribe(F&& func);
template <typename F> inline auto subscribe(F* func);
template <typename F> inline auto subscribeArtsync(F&& func);
template <typename F> inline auto subscribeArtsync(F* func);
template <typename F> inline auto subscribe(const uint8_t universe, CallbackType);
template <typename F> inline auto subscribe(CallbackAllTyp);
template <typename F> inline auto subscribeArtSync(CallbackArtSync);
template <typename F> inline auto subscribeArtTrigger(CallbackArtTrigger);
// callback definitions for subscribers
using CallbackAllType = std::function<void(const uint32_t universe, const uint8_t* data, const uint16_t size)>;
using CallbackType = std::function<void(const uint8_t* data, const uint16_t size)>;
using CallbackArtSync = std::function<void(void)>;
using CallbackArTrigger = std::function<void(uint16_t oem, uint8_t key, uint8_t sub_key, const uint8_t *payload, uint16_t size)>;
// for FastLED
inline void forward(const uint8_t universe, CRGB* leds, const uint16_t num);
// unsubscribe
inline void unsubscribe(const uint8_t universe);
inline void unsubscribe();
inline void clear_subscribers();
inline void unsubscribeArtSync();
inline void unsubscribeArtTrigger();
// ArtPollReply information
void shortname(const String& sn);
void longname(const String& ln);
Expand Down
Loading