You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

453 lines
17 KiB
C

6 years ago
#pragma once
#ifndef ARDUINO_ARTNET_H
#define ARDUINO_ARTNET_H
// Spec (Art-Net 4) : http://artisticlicence.com/WebSiteMaster/User%20Guides/art-net.pdf
// Packet Summary : https://art-net.org.uk/structure/packet-summary-2/
// Packet Definition : https://art-net.org.uk/structure/streaming-packets/artdmx-packet-definition/
#include <Arduino.h>
#include "util/ArxTypeTraits/ArxTypeTraits.h"
#include "util/ArxContainer/ArxContainer.h"
4 years ago
#if defined(ESP_PLATFORM) || defined(ESP8266) || defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_NANO_33_IOT)
#define ARTNET_ENABLE_WIFI
#endif
4 years ago
#if defined(ESP8266) || !defined(ARTNET_ENABLE_WIFI)
#define ARTNET_ENABLE_ETHER
6 years ago
#endif
4 years ago
#if !defined(ARTNET_ENABLE_WIFI) && !defined(ARTNET_ENABLE_ETHER)
#error THIS PLATFORM HAS NO WIFI OR ETHERNET OR NOT SUPPORTED ARCHITECTURE. PLEASE LET ME KNOW!
#endif
4 years ago
#ifdef ARTNET_ENABLE_WIFI
4 years ago
#ifdef ESP_PLATFORM
#include <WiFi.h>
#include <WiFiUdp.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#elif defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(ARDUINO_SAMD_NANO_33_IOT)
#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#elif defined(ARDUINO_SAMD_MKR1000)
#include <SPI.h>
#include <WiFi101.h>
#include <WiFiUdp.h>
#endif
#endif // ARTNET_ENABLE_WIFI
4 years ago
#ifdef ARTNET_ENABLE_ETHER
4 years ago
#include <Ethernet.h>
#include <EthernetUdp.h>
#include "util/TeensyDirtySTLErrorSolution/TeensyDirtySTLErrorSolution.h"
#endif // ARTNET_ENABLE_ETHER
6 years ago
4 years ago
namespace arx {
namespace artnet {
5 years ago
// Packet Summary : https://art-net.org.uk/structure/packet-summary-2/
// Packet Definition : https://art-net.org.uk/structure/streaming-packets/artdmx-packet-definition/
4 years ago
enum class OpCode : uint16_t {
6 years ago
// Device Discovery
Poll = 0x2000,
PollReply = 0x2100,
// Device Configuration
Address = 0x6000,
Input = 0x7000,
IpProg = 0xF800,
IpProgReply = 0xF900,
Command = 0x2400,
// Streaming Control
Dmx = 0x5000,
Nzs = 0x5100,
Sync = 0x5200,
// RDM
TodRequest = 0x8000,
TodData = 0x8100,
TodControl = 0x8200,
Rdm = 0x8300,
RdmSub = 0x8400,
// Time-Keeping
TimeCode = 0x9700,
TimeSync = 0x9800,
// Triggering
Trigger = 0x9900,
// Diagnostics
DiagData = 0x2300,
// File Transfer
FirmwareMaster = 0xF200,
FirmwareReply = 0xF300,
Directory = 0x9A00,
DirectoryReply = 0x9B00,
FileTnMaster = 0xF400,
FileFnMaster = 0xF500,
FileFnReply = 0xF600
};
constexpr uint16_t OPC(OpCode op) { return static_cast<uint16_t>(op); }
4 years ago
enum class Index : uint16_t {
6 years ago
ID = 0,
OP_CODE_L = 8,
OP_CODE_H = 9,
PROTOCOL_VER_H = 10,
PROTOCOL_VER_L = 11,
SEQUENCE = 12,
PHYSICAL = 13,
SUBUNI = 14,
NET = 15,
LENGTH_H = 16,
LENGTH_L = 17,
DATA = 18
};
constexpr uint16_t IDX(Index i) { return static_cast<uint16_t>(i); }
4 years ago
constexpr uint16_t DEFAULT_PORT{6454};
constexpr uint16_t HEADER_SIZE{18};
constexpr uint16_t PACKET_SIZE{530};
constexpr uint16_t PROTOCOL_VER{0x0014};
constexpr uint8_t ID_LENGTH{8};
constexpr char ID[ID_LENGTH]{"Art-Net"};
constexpr float DEFAULT_FPS{40.};
constexpr uint32_t DEFAULT_INTERVAL_MS{(uint32_t)(1000. / DEFAULT_FPS)};
6 years ago
4 years ago
static constexpr uint8_t NUM_PIXELS_PER_UNIV{170};
6 years ago
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)>;
4 years ago
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
template <uint16_t SIZE>
using Array = std::array<uint8_t, SIZE>;
using CallbackMap = std::map<uint32_t, CallbackType>;
using namespace std;
#else
6 years ago
template <uint16_t SIZE>
using Array = arx::vector<uint8_t, SIZE>;
using CallbackMap = arx::map<uint32_t, CallbackType, 8>;
using namespace arx;
#endif
6 years ago
template <typename S>
4 years ago
class Sender_ {
6 years ago
Array<PACKET_SIZE> packet;
String ip;
4 years ago
uint16_t port{DEFAULT_PORT};
uint8_t target_net{0};
uint8_t target_subnet{0};
uint8_t target_universe{0};
uint8_t seq{0};
uint8_t phy{0};
uint32_t prev_send_ms{0};
6 years ago
S* stream;
public:
4 years ago
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
#else
Sender_() { packet.resize(PACKET_SIZE); }
#endif
6 years ago
virtual ~Sender_() {}
4 years ago
void net(const uint8_t n) { target_net = n & 0x7F; }
void subnet(const uint8_t s) { target_subnet = s & 0x0F; }
void universe(const uint8_t u) { target_universe = u & 0x0F; }
4 years ago
void universe15bit(const uint8_t u) {
6 years ago
net((u >> 8) & 0xFF);
subnet((u >> 4) & 0x0F);
universe((u >> 0) & 0x0F);
}
4 years ago
void set(const uint8_t* const data, const uint16_t size = 512) {
6 years ago
packet[IDX(Index::PHYSICAL)] = phy;
packet[IDX(Index::NET)] = target_net;
packet[IDX(Index::SUBUNI)] = (target_subnet << 4) | target_universe;
packet[IDX(Index::LENGTH_H)] = (size >> 8) & 0xFF;
packet[IDX(Index::LENGTH_L)] = (size >> 0) & 0xFF;
memcpy((&packet[IDX(Index::DATA)]), data, size);
}
4 years ago
void set(const uint32_t universe_, const uint8_t* const data, const uint16_t size = 512) {
6 years ago
universe15bit(universe_);
set(data, size);
}
4 years ago
void set(const uint8_t net_, const uint8_t subnet_, const uint8_t universe_, const uint8_t* const data, const uint16_t size = 512) {
6 years ago
net(net_);
subnet(subnet_);
universe(universe_);
set(data, size);
}
4 years ago
void set(const uint16_t ch, const uint8_t data) {
packet[IDX(Index::DATA) + ch] = data;
}
4 years ago
void send() {
6 years ago
packet[IDX(Index::SEQUENCE)] = seq++;
stream->beginPacket(ip.c_str(), port);
6 years ago
stream->write(packet.data(), packet.size());
stream->endPacket();
}
4 years ago
void send(const uint8_t* const data, const uint16_t size = 512) {
6 years ago
set(data, size);
send();
}
4 years ago
void send(const uint32_t universe_, const uint8_t* const data, const uint16_t size = 512) {
6 years ago
set(universe_, data, size);
send();
}
4 years ago
void send(const uint8_t net_, const uint8_t subnet_, const uint8_t universe_, const uint8_t* const data, const uint16_t size = 512) {
6 years ago
set(net_, subnet_, universe_, data, size);
send();
}
4 years ago
void streaming() {
if ((millis() - prev_send_ms) > DEFAULT_INTERVAL_MS) {
6 years ago
send();
prev_send_ms = millis();
}
}
4 years ago
void physical(const uint8_t i) const { phy = constrain(i, 0, 3); }
6 years ago
uint8_t sequence() const { return seq; }
protected:
4 years ago
void attach(S& s, const String& user_ip, const uint16_t user_port = DEFAULT_PORT) {
6 years ago
stream = &s;
ip = user_ip;
port = user_port;
for (size_t i = 0; i < ID_LENGTH; i++) packet[IDX(Index::ID) + i] = static_cast<uint8_t>(ID[i]);
packet[IDX(Index::OP_CODE_H)] = (OPC(OpCode::Dmx) >> 8) & 0x00FF;
packet[IDX(Index::OP_CODE_L)] = (OPC(OpCode::Dmx) >> 0) & 0x00FF;
packet[IDX(Index::PROTOCOL_VER_H)] = (PROTOCOL_VER >> 8) & 0x00FF;
packet[IDX(Index::PROTOCOL_VER_L)] = (PROTOCOL_VER >> 0) & 0x00FF;
}
};
template <typename S>
4 years ago
class Receiver_ {
6 years ago
Array<PACKET_SIZE> packet;
IPAddress remote_ip;
uint16_t remote_port;
CallbackMap callbacks;
CallbackAllType callback_all;
6 years ago
S* stream;
public:
4 years ago
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
#else
Receiver_() { packet.resize(PACKET_SIZE); }
#endif
6 years ago
virtual ~Receiver_() {}
4 years ago
bool parse() {
6 years ago
const size_t size = stream->parsePacket();
if (size == 0) return false;
6 years ago
uint8_t d[size];
stream->read(d, size);
if (size <= HEADER_SIZE) return false; // discard packet if incomplete
6 years ago
4 years ago
if (checkID(d)) {
if (opcode(d) == OPC(OpCode::Dmx)) {
6 years ago
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);
6 years ago
for (auto& c : callbacks)
if (universe15bit() == c.first) c.second(data(), size - HEADER_SIZE);
6 years ago
return true;
}
}
return false;
}
inline const IPAddress& ip() const { return remote_ip; }
inline uint16_t port() const { return remote_port; }
4 years ago
inline String id() const {
6 years ago
String str;
for (uint8_t i = 0; i < ID_LENGTH; ++i) str += packet[IDX(Index::ID) + i];
return str;
}
4 years ago
inline uint16_t opcode() const {
6 years ago
return (packet[IDX(Index::OP_CODE_H)] << 8) | packet[IDX(Index::OP_CODE_L)];
}
4 years ago
inline uint16_t opcode(const uint8_t* p) const {
6 years ago
return (p[IDX(Index::OP_CODE_H)] << 8) | p[IDX(Index::OP_CODE_L)];
}
4 years ago
inline uint16_t version() const {
6 years ago
return (packet[IDX(Index::PROTOCOL_VER_H)] << 8) | packet[IDX(Index::PROTOCOL_VER_L)];
}
4 years ago
inline uint8_t sequence() const {
6 years ago
return packet[IDX(Index::SEQUENCE)];
}
4 years ago
inline uint8_t physical() const {
6 years ago
return packet[IDX(Index::PHYSICAL)];
}
4 years ago
inline uint8_t net() const {
6 years ago
return packet[IDX(Index::NET)] & 0x7F;
}
4 years ago
inline uint8_t subnet() const {
6 years ago
return (packet[IDX(Index::SUBUNI)] >> 4) & 0x0F;
}
4 years ago
inline uint8_t universe() const {
6 years ago
return packet[IDX(Index::SUBUNI)] & 0x0F;
}
4 years ago
inline uint16_t universe15bit() const {
6 years ago
return (packet[IDX(Index::NET)] << 8) | packet[IDX(Index::SUBUNI)];
}
4 years ago
inline uint16_t length() const {
6 years ago
return (packet[IDX(Index::LENGTH_H)] << 8) | packet[IDX(Index::LENGTH_L)];
}
4 years ago
inline uint16_t size() const {
6 years ago
return length();
}
4 years ago
inline uint8_t* data() {
6 years ago
return &(packet[HEADER_SIZE]);
}
4 years ago
inline uint8_t data(const uint16_t i) const {
6 years ago
return packet[HEADER_SIZE + i];
}
template <typename F>
inline auto subscribe(const uint32_t universe, F&& func)
4 years ago
-> std::enable_if_t<arx::is_callable<F>::value> {
callbacks.insert(make_pair(universe, arx::function_traits<F>::cast(func)));
6 years ago
}
template <typename F>
inline auto subscribe(const uint32_t universe, F* func)
4 years ago
-> std::enable_if_t<arx::is_callable<F>::value> {
callbacks.insert(make_pair(universe, arx::function_traits<F>::cast(func)));
6 years ago
}
template <typename F>
inline auto subscribe(F&& func)
4 years ago
-> std::enable_if_t<arx::is_callable<F>::value> {
callback_all = arx::function_traits<F>::cast(func);
}
template <typename F>
inline auto subscribe(F* func)
4 years ago
-> std::enable_if_t<arx::is_callable<F>::value> {
callback_all = arx::function_traits<F>::cast(func);
}
template <typename F>
inline auto subscribe(const uint8_t net, const uint8_t subnet, const uint8_t universe, F&& func)
4 years ago
-> std::enable_if_t<arx::is_callable<F>::value> {
uint32_t u = ((uint32_t)net << 8) | ((uint32_t)subnet << 4) | (uint32_t)universe;
subscribe(u, func);
}
template <typename F>
inline auto subscribe(const uint8_t net, const uint8_t subnet, const uint8_t universe, F* func)
4 years ago
-> std::enable_if_t<arx::is_callable<F>::value> {
6 years ago
uint32_t u = ((uint32_t)net << 8) | ((uint32_t)subnet << 4) | (uint32_t)universe;
subscribe(u, func);
}
4 years ago
inline void unsubscribe(const uint32_t universe) {
auto it = callbacks.find(universe);
if (it != callbacks.end())
callbacks.erase(it);
}
4 years ago
inline void unsubscribe() {
callback_all = nullptr;
}
4 years ago
inline void unsubscribe(const uint8_t net, const uint8_t subnet, const uint8_t universe) {
uint32_t u = ((uint32_t)net << 8) | ((uint32_t)subnet << 4) | (uint32_t)universe;
unsubscribe(u);
}
6 years ago
4 years ago
inline void clear_subscribers() {
unsubscribe();
callbacks.clear();
}
#ifdef FASTLED_VERSION
inline void forward(const uint32_t universe, CRGB* leds, const uint16_t num) {
subscribe(universe, [&](const uint8_t* data, const uint16_t size) {
if (size < num * 3) Serial.println(F("ERROR: Too many LEDs to forward"));
for (size_t pixel = 0; pixel < num; ++pixel) {
size_t idx = pixel * 3;
leds[pixel].r = data[idx + 0];
leds[pixel].g = data[idx + 1];
leds[pixel].b = data[idx + 2];
}
});
}
inline void forward(const uint8_t net, const uint8_t subnet, const uint8_t universe, CRGB* leds, const uint16_t num) {
uint32_t u = ((uint32_t)net << 8) | ((uint32_t)subnet << 4) | (uint32_t)universe;
forward(u, leds, num);
}
#endif
6 years ago
protected:
4 years ago
void attach(S& s) {
stream = &s;
}
6 years ago
private:
4 years ago
inline bool checkID() const {
6 years ago
const char* idptr = reinterpret_cast<const char*>(packet.data());
return !strcmp(ID, idptr);
}
4 years ago
inline bool checkID(const uint8_t* p) const {
6 years ago
const char* idptr = reinterpret_cast<const char*>(p);
return !strcmp(ID, idptr);
}
};
template <typename S>
4 years ago
class Manager : public Sender_<S>, public Receiver_<S> {
6 years ago
S stream;
4 years ago
6 years ago
public:
4 years ago
void begin(const String& send_ip, const uint16_t send_port = DEFAULT_PORT, const uint16_t recv_port = DEFAULT_PORT) {
6 years ago
stream.begin(recv_port);
this->Sender_<S>::attach(stream, send_ip, send_port);
this->Receiver_<S>::attach(stream);
}
};
template <typename S>
4 years ago
class Sender : public Sender_<S> {
6 years ago
S stream;
4 years ago
6 years ago
public:
4 years ago
void begin(const String& ip, const uint16_t port = DEFAULT_PORT) {
stream.begin(DEFAULT_PORT);
6 years ago
this->Sender_<S>::attach(stream, ip, port);
}
};
template <typename S>
4 years ago
class Receiver : public Receiver_<S> {
6 years ago
S stream;
4 years ago
6 years ago
public:
4 years ago
void begin(const uint16_t port = DEFAULT_PORT) {
6 years ago
stream.begin(port);
this->Receiver_<S>::attach(stream);
}
};
4 years ago
} // namespace artnet
} // namespace arx
6 years ago
4 years ago
#ifdef ARTNET_ENABLE_WIFI
4 years ago
using ArtnetWiFi = arx::artnet::Manager<WiFiUDP>;
using ArtnetWiFiSender = arx::artnet::Sender<WiFiUDP>;
using ArtnetWiFiReceiver = arx::artnet::Receiver<WiFiUDP>;
#endif
4 years ago
#ifdef ARTNET_ENABLE_ETHER
4 years ago
using Artnet = arx::artnet::Manager<EthernetUDP>;
using ArtnetSender = arx::artnet::Sender<EthernetUDP>;
using ArtnetReceiver = arx::artnet::Receiver<EthernetUDP>;
6 years ago
#endif
4 years ago
#endif // ARDUINO_ARTNET_H