Skip to content

Commit

Permalink
feat: support send/receive art-sync
Browse files Browse the repository at this point in the history
  • Loading branch information
hideakitai committed Jan 31, 2024
1 parent 41ee725 commit 1a7d6a8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Artnet/ArtSync.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#ifndef ARTNET_ART_SYNC_H
#define ARTNET_ART_SYNC_H

#include "Common.h"
#include <stdint.h>
#include <stddef.h>

namespace art_net {
namespace art_sync {

enum Index : uint16_t {
ID = 0,
OP_CODE_L = 8,
OP_CODE_H = 9,
PROTOCOL_VER_H = 10,
PROTOCOL_VER_L = 11,
AUX1 = 12,
AUX2 = 13,
};

inline void set_header(uint8_t *packet)
{
for (size_t i = 0; i < ID_LENGTH; i++) packet[i] = static_cast<uint8_t>(ARTNET_ID[i]);
packet[OP_CODE_L] = (static_cast<uint16_t>(OpCode::Sync) >> 0) & 0x00FF;
packet[OP_CODE_H] = (static_cast<uint16_t>(OpCode::Sync) >> 8) & 0x00FF;
packet[PROTOCOL_VER_H] = (PROTOCOL_VER >> 8) & 0x00FF;
packet[PROTOCOL_VER_L] = (PROTOCOL_VER >> 0) & 0x00FF;
packet[AUX1] = 0;
packet[AUX2] = 0;
}

} // namespace art_sync
} // namespace art_net

#endif // ARTNET_ART_SYNC_H
1 change: 1 addition & 0 deletions Artnet/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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)>;

#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
template <uint16_t SIZE>
Expand Down
17 changes: 17 additions & 0 deletions Artnet/Receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Receiver_ {
uint8_t sub_switch; // subnet of universe
CallbackMap callbacks;
CallbackAllType callback_all;
CallbackArtSync callback_artsync;
S* stream;
bool b_verbose {false};
artpollreply::ArtPollReply artpollreply_ctx;
Expand Down Expand Up @@ -71,6 +72,11 @@ class Receiver_ {
op_code = OpCode::Poll;
break;
}
case OpCode::Sync: {
if (callback_artsync) callback_artsync();
op_code = OpCode::Sync;
break;
}
default: {
if (b_verbose) {
Serial.print(F("Unsupported OpCode: "));
Expand Down Expand Up @@ -178,6 +184,14 @@ class Receiver_ {
inline auto subscribe(F* func) -> std::enable_if_t<arx::is_callable<F>::value> {
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> {
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> {
callback_artsync = arx::function_traits<F>::cast(func);
}

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

inline void clear_subscribers() {
unsubscribe();
Expand Down
7 changes: 7 additions & 0 deletions Artnet/Sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Common.h"
#include "ArtDmx.h"
#include "ArtTrigger.h"
#include "ArtSync.h"

namespace art_net {

Expand Down Expand Up @@ -100,6 +101,12 @@ class Sender_ {
send_raw(ip, DEFAULT_PORT, packet.data(), packet.size());
}

// ArtSync
void sync(const String& ip) {
art_sync::set_header(packet.data());
send_raw(ip, DEFAULT_PORT, packet.data(), packet.size());
}

protected:
void attach(S& s) {
stream = &s;
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ If you have already installed this library, please follow:
- One-line send to desired destination
- Flexible net/subnet/universe setting
- Easy data forwarding to [FastLED](https://github.com/FastLED/FastLED)
- Supports following protocols:
- ArtDmx
- ArtPoll/ArtPollReply
- ArtTrigger
- ArtSync

## Supported Platforms

Expand Down Expand Up @@ -284,6 +289,8 @@ void set_subkey(uint8_t subkey);
void set_payload(const uint8_t* const payload, uint16_t size);
// send ArtTrigger based on the config above
void trigger(const String& ip);
// send ArtSync
void sync(const String& ip);
```
### ArtnetReceiver
Expand All @@ -295,12 +302,19 @@ 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);
// 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)>;
// 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();
// ArtPollReply information
void shortname(const String& sn);
void longname(const String& ln);
Expand Down

0 comments on commit 1a7d6a8

Please sign in to comment.