Other ETH lib

This commit is contained in:
Patrick Schwarz 2024-11-09 21:59:25 +01:00
parent 3ce1df2136
commit c295017495
70 changed files with 21645 additions and 67 deletions

View file

@ -0,0 +1,87 @@
#pragma once
#ifndef ARTNET_ARTDMX_H
#define ARTNET_ARTDMX_H
#include "Common.h"
#include <stdint.h>
#include <stddef.h>
namespace art_net {
namespace art_dmx {
enum Index : uint16_t
{
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
};
struct Metadata
{
uint8_t sequence;
uint8_t physical;
uint8_t net;
uint8_t subnet;
uint8_t universe;
};
using CallbackType = std::function<void(const uint8_t *data, uint16_t size, const Metadata &metadata, const RemoteInfo &remote)>;
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
using CallbackMap = std::map<uint16_t, CallbackType>;
#else
using CallbackMap = arx::stdx::map<uint16_t, CallbackType>;
#endif
inline Metadata generateMetadataFrom(const uint8_t *packet)
{
Metadata metadata;
metadata.sequence = packet[SEQUENCE];
metadata.physical = packet[PHYSICAL];
metadata.net = packet[NET];
metadata.subnet = (packet[SUBUNI] >> 4) & 0x0F;
metadata.universe = (packet[SUBUNI] >> 0) & 0x0F;
return metadata;
}
inline void setMetadataTo(uint8_t *packet, uint8_t sequence, uint8_t physical, uint8_t net, uint8_t subnet, uint8_t universe)
{
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::Dmx) >> 0) & 0x00FF;
packet[OP_CODE_H] = (static_cast<uint16_t>(OpCode::Dmx) >> 8) & 0x00FF;
packet[PROTOCOL_VER_H] = (PROTOCOL_VER >> 8) & 0x00FF;
packet[PROTOCOL_VER_L] = (PROTOCOL_VER >> 0) & 0x00FF;
packet[SEQUENCE] = sequence;
packet[PHYSICAL] = physical & 0x03;
packet[NET] = net & 0x7F;
packet[SUBUNI] = ((subnet & 0x0F) << 4) | (universe & 0x0F);
packet[LENGTH_H] = (512 >> 8) & 0xFF;
packet[LENGTH_L] = (512 >> 0) & 0xFF;
}
inline void setDataTo(uint8_t *packet, const uint8_t* const data, uint16_t size)
{
memcpy(packet + art_dmx::DATA, data, size);
}
inline void setDataTo(uint8_t *packet, const uint16_t ch, const uint8_t data)
{
packet[art_dmx::DATA + ch] = data;
}
} // namespace art_dmx
} // namespace art_net
using ArtDmxMetadata = art_net::art_dmx::Metadata;
using ArtDmxCallback = art_net::art_dmx::CallbackType;
#endif // ARTNET_ARTDMX_H

View file

@ -0,0 +1,87 @@
#pragma once
#ifndef ARTNET_ARTNZS_H
#define ARTNET_ARTNZS_H
#include "Common.h"
#include <stdint.h>
#include <stddef.h>
namespace art_net {
namespace art_nzs {
enum Index : uint16_t
{
ID = 0,
OP_CODE_L = 8,
OP_CODE_H = 9,
PROTOCOL_VER_H = 10,
PROTOCOL_VER_L = 11,
SEQUENCE = 12,
START_CODE = 13,
SUBUNI = 14,
NET = 15,
LENGTH_H = 16,
LENGTH_L = 17,
DATA = 18
};
struct Metadata
{
uint8_t sequence;
uint8_t start_code;
uint8_t net;
uint8_t subnet;
uint8_t universe;
};
using CallbackType = std::function<void(const uint8_t *data, uint16_t size, const Metadata &metadata, const RemoteInfo &remote)>;
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
using CallbackMap = std::map<uint16_t, CallbackType>;
#else
using CallbackMap = arx::stdx::map<uint16_t, CallbackType>;
#endif
inline Metadata generateMetadataFrom(const uint8_t *packet)
{
Metadata metadata;
metadata.sequence = packet[SEQUENCE];
metadata.start_code = packet[START_CODE];
metadata.net = packet[NET];
metadata.subnet = (packet[SUBUNI] >> 4) & 0x0F;
metadata.universe = (packet[SUBUNI] >> 0) & 0x0F;
return metadata;
}
inline void setMetadataTo(uint8_t *packet, uint8_t sequence, uint8_t start_code, uint8_t net, uint8_t subnet, uint8_t universe)
{
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::Nzs) >> 0) & 0x00FF;
packet[OP_CODE_H] = (static_cast<uint16_t>(OpCode::Nzs) >> 8) & 0x00FF;
packet[PROTOCOL_VER_H] = (PROTOCOL_VER >> 8) & 0x00FF;
packet[PROTOCOL_VER_L] = (PROTOCOL_VER >> 0) & 0x00FF;
packet[SEQUENCE] = sequence;
packet[START_CODE] = start_code & 0x03;
packet[NET] = net & 0x7F;
packet[SUBUNI] = ((subnet & 0x0F) << 4) | (universe & 0x0F);
packet[LENGTH_H] = (512 >> 8) & 0xFF;
packet[LENGTH_L] = (512 >> 0) & 0xFF;
}
inline void setDataTo(uint8_t *packet, const uint8_t* const data, uint16_t size)
{
memcpy(packet + art_nzs::DATA, data, size);
}
inline void setDataTo(uint8_t *packet, const uint16_t ch, const uint8_t data)
{
packet[art_nzs::DATA + ch] = data;
}
} // namespace art_nzs
} // namespace art_net
using ArtNzsMetadata = art_net::art_nzs::Metadata;
using ArtNzsCallback = art_net::art_nzs::CallbackType;
#endif // ARTNET_ARTNZS_H

View file

@ -0,0 +1,148 @@
#pragma once
#ifndef ARTNET_ARTPOLLREPLY_H
#define ARTNET_ARTPOLLREPLY_H
#include "Common.h"
#include <stdint.h>
#include <stddef.h>
#include <string.h>
namespace art_net {
namespace art_poll_reply {
static constexpr size_t NUM_POLLREPLY_PUBLIC_PORT_LIMIT {4};
union Packet
{
struct
{
uint8_t id[8];
uint8_t op_code_l;
uint8_t op_code_h;
uint8_t ip[4];
uint8_t port_l;
uint8_t port_h;
uint8_t ver_h;
uint8_t ver_l;
uint8_t net_sw;
uint8_t sub_sw;
uint8_t oem_h;
uint8_t oem_l;
uint8_t ubea_ver;
uint8_t status_1;
uint8_t esta_man_l;
uint8_t esta_man_h;
uint8_t short_name[18];
uint8_t long_name[64];
uint8_t node_report[64];
uint8_t num_ports_h;
uint8_t num_ports_l;
uint8_t port_types[NUM_POLLREPLY_PUBLIC_PORT_LIMIT];
uint8_t good_input[NUM_POLLREPLY_PUBLIC_PORT_LIMIT];
uint8_t good_output[NUM_POLLREPLY_PUBLIC_PORT_LIMIT];
uint8_t sw_in[NUM_POLLREPLY_PUBLIC_PORT_LIMIT];
uint8_t sw_out[NUM_POLLREPLY_PUBLIC_PORT_LIMIT];
uint8_t sw_video;
uint8_t sw_macro;
uint8_t sw_remote;
uint8_t spare[3];
uint8_t style;
uint8_t mac[6];
uint8_t bind_ip[4];
uint8_t bind_index;
uint8_t status_2;
uint8_t filler[26];
};
uint8_t b[239];
};
struct Config
{
uint16_t oem {0x00FF}; // OemUnknown https://github.com/tobiasebsen/ArtNode/blob/master/src/Art-NetOemCodes.h
uint16_t esta_man {0x0000}; // ESTA manufacturer code
uint8_t status1 {0x00}; // Unknown / Normal
uint8_t status2 {0x08}; // sACN capable
String short_name {"ChaosArtNet"};
String long_name {"Chaos ArtNet Interface"};
String node_report {""};
// Four universes from Device to Controller
// NOTE: Only low 4 bits of the universes
// NOTE: Upper 11 bits of the universes will be
// shared with the subscribed universe (net, subnet)
// e.g.) If you have subscribed universe 0x1234,
// you can set the device to controller universes
// from 0x1230 to 0x123F (sw_in will be from 0x0 to 0xF).
// So, I recommned subscribing only in the range of
// 0x1230 to 0x123F if you want to set the sw_in.
// REF: Please refer the Art-Net spec for the detail.
// https://art-net.org.uk/downloads/art-net.pdf
uint8_t sw_in[4] {0};
};
inline Packet generatePacketFrom(const IPAddress &my_ip, const uint8_t my_mac[6], uint16_t universe, const Config &metadata)
{
Packet r;
for (size_t i = 0; i < ID_LENGTH; i++) {
r.id[i] = static_cast<uint8_t>(ARTNET_ID[i]);
}
r.op_code_l = ((uint16_t)OpCode::PollReply >> 0) & 0x00FF;
r.op_code_h = ((uint16_t)OpCode::PollReply >> 8) & 0x00FF;
memcpy(r.mac, my_mac, 6);
for (size_t i = 0; i < 4; ++i) {
r.ip[i] = my_ip[i];
}
r.port_l = (DEFAULT_PORT >> 0) & 0xFF;
r.port_h = (DEFAULT_PORT >> 8) & 0xFF;
r.ver_h = (PROTOCOL_VER >> 8) & 0x00FF;
r.ver_l = (PROTOCOL_VER >> 0) & 0x00FF;
r.oem_h = (metadata.oem >> 8) & 0xFF; //
r.oem_l = (metadata.oem >> 0) & 0xFF;
r.ubea_ver = 0; // UBEA not programmed
r.status_1 = metadata.status1;
r.esta_man_l = (metadata.esta_man >> 8) & 0xFF;
r.esta_man_h = (metadata.esta_man >> 8) & 0xFF;
memset(r.short_name, 0, 18);
memset(r.long_name, 0, 64);
memset(r.node_report, 0, 64);
memcpy(r.short_name, metadata.short_name.c_str(), metadata.short_name.length());
memcpy(r.long_name, metadata.long_name.c_str(), metadata.long_name.length());
memcpy(r.node_report, metadata.node_report.c_str(), metadata.node_report.length());
r.num_ports_h = 0; // Reserved
r.num_ports_l = 1;
memset(r.sw_in, 0, 4);
memset(r.sw_out, 0, 4);
memset(r.port_types, 0, 4);
memset(r.good_input, 0, 4);
memset(r.good_output, 0, 4);
r.net_sw = (universe >> 8) & 0x7F;
r.sub_sw = (universe >> 4) & 0x0F;
// https://github.com/hideakitai/ArtNet/issues/81
// https://github.com/hideakitai/ArtNet/issues/121
r.sw_out[0] = (universe >> 0) & 0x0F;
for (size_t i = 0; i < 4; ++i) {
r.sw_in[i] = metadata.sw_in[i] & 0x0F;
}
r.port_types[0] = 0xC0; // I/O available by DMX512
r.good_input[0] = 0x80; // Data received without error
r.good_output[0] = 0x80; // Data transmitted without error
r.sw_video = 0; // Video display shows local data
r.sw_macro = 0; // No support for macro key inputs
r.sw_remote = 0; // No support for remote trigger inputs
memset(r.spare, 0x00, 3);
r.style = 0x00; // A DMX to / from Art-Net device
for (size_t i = 0; i < 4; ++i) {
r.bind_ip[i] = my_ip[i];
}
r.bind_index = 0;
r.status_2 = metadata.status2;
memset(r.filler, 0x00, 26);
return r;
}
} // namespace art_poll_reply
} // namespace art_net
using ArtPollReplyConfig = art_net::art_poll_reply::Config;
#endif // ARTNET_ARTPOLLREPLY_H

View file

@ -0,0 +1,45 @@
#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,
PACKET_SIZE = 14,
};
using CallbackType = std::function<void(const ArtNetRemoteInfo &remote)>;
inline void setMetadataTo(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
using ArtSyncCallback = art_net::art_sync::CallbackType;
#endif // ARTNET_ART_SYNC_H

View file

@ -0,0 +1,67 @@
#pragma once
#ifndef ARTNET_ART_TRIGGER_H
#define ARTNET_ART_TRIGGER_H
#include "Common.h"
#include <stdint.h>
#include <stddef.h>
namespace art_net {
namespace art_trigger {
enum Index : uint16_t
{
ID = 0,
OP_CODE_L = 8,
OP_CODE_H = 9,
PROTOCOL_VER_H = 10,
PROTOCOL_VER_L = 11,
FILTER_1 = 12,
FILTER_2 = 13,
OEM_H = 14,
OEM_L = 15,
KEY = 16,
SUB_KEY = 17,
PAYLOAD = 18
};
struct Metadata
{
uint16_t oem;
uint8_t key;
uint8_t sub_key;
const uint8_t *payload;
uint16_t size;
};
using CallbackType = std::function<void(const Metadata &metadata, const RemoteInfo &remote)>;
inline void setDataTo(uint8_t *packet, uint16_t oem, uint8_t key, uint8_t subkey, const uint8_t* const payload, uint16_t size)
{
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::Trigger) >> 0) & 0x00FF;
packet[OP_CODE_H] = (static_cast<uint16_t>(OpCode::Trigger) >> 8) & 0x00FF;
packet[PROTOCOL_VER_H] = (PROTOCOL_VER >> 8) & 0x00FF;
packet[PROTOCOL_VER_L] = (PROTOCOL_VER >> 0) & 0x00FF;
packet[FILTER_1] = 0;
packet[FILTER_2] = 0;
packet[OEM_H] = (oem >> 8) & 0x00FF;
packet[OEM_L] = (oem >> 0) & 0x00FF;
packet[KEY] = key;
packet[SUB_KEY] = subkey;
if (payload) {
memcpy(packet + PAYLOAD, payload, size);
} else {
memset(packet + PAYLOAD, 0, 512);
}
}
} // namespace art_trigger
} // namespace art_net
using ArtTriggerCallback = art_net::art_trigger::CallbackType;
using ArtTriggerMetadata = art_net::art_trigger::Metadata;
#endif // ARTNET_ART_TRIGGER_H

144
lib/ArtNet/Artnet/Common.h Normal file
View file

@ -0,0 +1,144 @@
#pragma once
#ifndef ARTNET_COMMON_H
#define ARTNET_COMMON_H
#include <ArxTypeTraits.h>
#include <ArxContainer.h>
#include <stdint.h>
#include <stddef.h>
namespace art_net {
// Packet Summary : https://art-net.org.uk/structure/packet-summary-2/
// Packet Definition : https://art-net.org.uk/structure/streaming-packets/artdmx-packet-definition/
enum class OpCode : uint16_t {
// 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,
// Others
NoPacket = 0x0000,
Unsupported = 0xFFFE,
ParseFailed = 0xFFFF,
};
constexpr uint16_t DEFAULT_PORT {6454}; // 0x1936
constexpr uint16_t PROTOCOL_VER {14}; // 0x000E
constexpr uint8_t ID_LENGTH {8};
constexpr char ARTNET_ID[ID_LENGTH] {"Art-Net"};
constexpr float DEFAULT_FPS {40.};
constexpr uint32_t DEFAULT_INTERVAL_MS {(uint32_t)(1000. / DEFAULT_FPS)};
// ArtDmx, ArtTrigger has same structure
constexpr uint16_t HEADER_SIZE {18};
constexpr uint16_t PACKET_SIZE {530};
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
template <uint16_t SIZE>
using Array = std::array<uint8_t, SIZE>;
#else
template <uint16_t SIZE>
using Array = arx::stdx::vector<uint8_t, SIZE>;
#endif
struct RemoteInfo
{
IPAddress ip;
uint16_t port;
};
struct Destination
{
String ip;
uint8_t net;
uint8_t subnet;
uint8_t universe;
};
inline bool operator<(const Destination &rhs, const Destination &lhs)
{
if (rhs.ip < lhs.ip) {
return true;
}
if (rhs.ip > lhs.ip) {
return false;
}
if (rhs.net < lhs.net) {
return true;
}
if (rhs.net > lhs.net) {
return false;
}
if (rhs.subnet < lhs.subnet) {
return true;
}
if (rhs.subnet > lhs.subnet) {
return false;
}
if (rhs.universe < lhs.universe) {
return true;
}
if (rhs.universe > lhs.universe) {
return false;
}
return false;
}
inline bool operator==(const Destination &rhs, const Destination &lhs)
{
return rhs.ip == lhs.ip && rhs.net == lhs.net && rhs.subnet == lhs.subnet && rhs.universe == lhs.universe;
}
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
// sender
using LastSendTimeMsMap = std::map<Destination, uint32_t>;
using SequenceMap = std::map<Destination, uint8_t>;
#else
// sender
using LastSendTimeMsMap = arx::stdx::map<Destination, uint32_t>;
using SequenceMap = arx::stdx::map<Destination, uint8_t>;
#endif
#ifdef ARTNET_ENABLE_WIFI
inline bool isNetworkReady()
{
return true;
}
#endif
} // namespace art_net
using ArtNetRemoteInfo = art_net::RemoteInfo;
#endif // ARTNET_COMMON_H

View file

@ -0,0 +1,32 @@
#pragma once
#ifndef ARTNET_MANAGER_H
#include "Common.h"
#include "Receiver.h"
#include "Sender.h"
namespace art_net {
template <typename S>
class Manager : public Sender_<S>, public Receiver_<S>
{
S stream;
public:
void begin(uint16_t recv_port = DEFAULT_PORT)
{
this->stream.begin(recv_port);
this->Sender_<S>::attach(this->stream);
this->Receiver_<S>::attach(this->stream);
}
void parse()
{
this->Receiver_<S>::parse();
}
};
} // namespace art_net
#endif // ARTNET_MANAGER_H

View file

@ -0,0 +1,490 @@
#pragma once
#ifndef ARTNET_RECEIVER_H
#define ARTNET_RECEIVER_H
#include "Common.h"
#include "ArtDmx.h"
#include "ArtNzs.h"
#include "ArtPollReply.h"
#include "ArtTrigger.h"
#include "ArtSync.h"
namespace art_net {
namespace {
struct NoPrint : public Print
{
size_t write(uint8_t) override { return 0; }
};
static NoPrint no_log;
} // namespace
template <typename S>
class Receiver_
{
S *stream;
Array<PACKET_SIZE> packet;
art_dmx::CallbackMap callback_art_dmx_universes;
art_dmx::CallbackType callback_art_dmx;
art_nzs::CallbackMap callback_art_nzs_universes;
art_sync::CallbackType callback_art_sync;
art_trigger::CallbackType callback_art_trigger;
ArtPollReplyConfig art_poll_reply_config;
Print *logger {&no_log};
public:
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
#else
Receiver_()
{
this->packet.resize(PACKET_SIZE);
}
#endif
OpCode parse()
{
#ifdef ARTNET_ENABLE_WIFI
if (!isNetworkReady()) {
return OpCode::NoPacket;
}
#endif
size_t size = this->stream->parsePacket();
if (size == 0) {
return OpCode::NoPacket;
}
this->logger->print(F("Packet received: size = "));
this->logger->println(size);
if (size > PACKET_SIZE) {
this->logger->print(F("Packet size is unexpectedly too large: "));
this->logger->println(size);
size = PACKET_SIZE;
}
this->stream->read(this->packet.data(), size);
if (!checkID()) {
this->logger->println(F("Packet ID is not Art-Net"));
return OpCode::ParseFailed;
}
RemoteInfo remote_info;
remote_info.ip = this->stream->S::remoteIP();
remote_info.port = (uint16_t)this->stream->S::remotePort();
OpCode op_code = OpCode::Unsupported;
OpCode received_op_code = static_cast<OpCode>(this->getOpCode());
switch (received_op_code) {
case OpCode::Dmx: {
art_dmx::Metadata metadata = art_dmx::generateMetadataFrom(this->packet.data());
if (this->callback_art_dmx) {
this->callback_art_dmx(this->getArtDmxData(), size - HEADER_SIZE, metadata, remote_info);
}
for (auto& cb : this->callback_art_dmx_universes) {
if (this->getArtDmxUniverse15bit() == cb.first) {
cb.second(this->getArtDmxData(), size - HEADER_SIZE, metadata, remote_info);
}
}
op_code = OpCode::Dmx;
break;
}
case OpCode::Nzs: {
art_nzs::Metadata metadata = art_nzs::generateMetadataFrom(this->packet.data());
for (auto& cb : this->callback_art_nzs_universes) {
if (this->getArtDmxUniverse15bit() == cb.first) {
cb.second(this->getArtDmxData(), size - HEADER_SIZE, metadata, remote_info);
}
}
op_code = OpCode::Nzs;
break;
}
case OpCode::Poll: {
this->sendArtPollReply(remote_info);
op_code = OpCode::Poll;
break;
}
case OpCode::Trigger: {
if (this->callback_art_trigger) {
ArtTriggerMetadata metadata = {
.oem = this->getArtTriggerOEM(),
.key = this->getArtTriggerKey(),
.sub_key = this->getArtTriggerSubKey(),
.payload = this->getArtTriggerPayload(),
.size = static_cast<uint16_t>(size - art_trigger::PAYLOAD),
};
this->callback_art_trigger(metadata, remote_info);
}
op_code = OpCode::Trigger;
break;
}
case OpCode::Sync: {
if (this->callback_art_sync) {
this->callback_art_sync(remote_info);
}
op_code = OpCode::Sync;
break;
}
default: {
this->logger->print(F("Unsupported OpCode: "));
this->logger->println(this->getOpCode(), HEX);
op_code = OpCode::Unsupported;
break;
}
}
this->stream->flush();
return op_code;
}
// subscribe artdmx packet for specified net, subnet, and universe
template <typename Fn>
auto subscribeArtDmxUniverse(uint8_t net, uint8_t subnet, uint8_t universe, const Fn &func)
-> std::enable_if_t<arx::is_callable<Fn>::value>
{
if (net > 0x7F) {
this->logger->println(F("net should be less than 0x7F"));
return;
}
if (subnet > 0xF) {
this->logger->println(F("subnet should be less than 0xF"));
return;
}
if (universe > 0xF) {
this->logger->println(F("universe should be less than 0xF"));
return;
}
uint16_t u = ((uint16_t)net << 8) | ((uint16_t)subnet << 4) | (uint16_t)universe;
this->subscribeArtDmxUniverse(u, func);
}
// subscribe artdmx packet for specified universe (15 bit)
template <typename Fn>
auto subscribeArtDmxUniverse(uint16_t universe, const Fn &func)
-> std::enable_if_t<arx::is_callable<Fn>::value>
{
this->callback_art_dmx_universes.insert(std::make_pair(universe, arx::function_traits<Fn>::cast(func)));
}
// subscribe artnzs packet for specified universe (15 bit)
template <typename Fn>
auto subscribeArtNzsUniverse(uint16_t universe, const Fn &func)
-> std::enable_if_t<arx::is_callable<Fn>::value>
{
this->callback_art_nzs_universes.insert(std::make_pair(universe, arx::function_traits<Fn>::cast(func)));
}
// subscribe artdmx packet for all universes
template <typename Fn>
auto subscribeArtDmx(const Fn &func)
-> std::enable_if_t<arx::is_callable<Fn>::value>
{
this->callback_art_dmx = arx::function_traits<Fn>::cast(func);
}
// subscribe other packets
template <typename Fn>
auto subscribeArtSync(const Fn &func)
-> std::enable_if_t<arx::is_callable<Fn>::value>
{
this->callback_art_sync = arx::function_traits<Fn>::cast(func);
}
template <typename Fn>
auto subscribeArtTrigger(const Fn &func)
-> std::enable_if_t<arx::is_callable<Fn>::value>
{
this->callback_art_trigger = arx::function_traits<Fn>::cast(func);
}
void unsubscribeArtDmxUniverse(uint8_t net, uint8_t subnet, uint8_t universe)
{
uint16_t u = ((uint16_t)net << 8) | ((uint16_t)subnet << 4) | (uint16_t)universe;
this->unsubscribe(u);
}
void unsubscribeArtDmxUniverse(uint16_t universe)
{
auto it = this->callback_art_dmx_universes.find(universe);
if (it != this->callback_art_dmx_universes.end()) {
this->callback_art_dmx_universes.erase(it);
}
}
void unsubscribeArtDmxUniverses()
{
this->callback_art_dmx_universes.clear();
}
void unsubscribeArtDmx()
{
this->callback_art_dmx = nullptr;
}
void unsubscribeArtNzsUniverse(uint16_t universe)
{
auto it = this->callback_art_nzs_universes.find(universe);
if (it != this->callback_art_nzs_universes.end()) {
this->callback_art_nzs_universes.erase(it);
}
}
void unsubscribeArtSync()
{
this->callback_art_sync = nullptr;
}
void unsubscribeArtTrigger()
{
this->callback_art_trigger = nullptr;
}
#ifdef FASTLED_VERSION
void forwardArtDmxDataToFastLED(uint8_t net, uint8_t subnet, uint8_t universe, CRGB* leds, uint16_t num)
{
uint16_t u = ((uint16_t)net << 8) | ((uint16_t)subnet << 4) | (uint16_t)universe;
this->forwardArtDmxDataToFastLED(u, leds, num);
}
void forwardArtDmxDataToFastLED(uint16_t universe, CRGB* leds, uint16_t num)
{
this->subscribeArtDmxUniverse(universe, [this, leds, num](const uint8_t* data, const uint16_t size, const ArtDmxMetadata &, const RemoteInfo &) {
size_t n;
if (num <= size / 3) {
// OK: requested number of LEDs is less than received data size
n = num;
} else {
n = size / 3;
this->logger->println(F("WARN: ArtNet packet size is less than requested LED numbers to forward"));
this->logger->print(F(" requested: "));
this->logger->print(num * 3);
this->logger->print(F(" received : "));
this->logger->println(size);
}
for (size_t pixel = 0; pixel < n; ++pixel) {
size_t idx = pixel * 3;
leds[pixel].r = data[idx + 0];
leds[pixel].g = data[idx + 1];
leds[pixel].b = data[idx + 2];
}
});
}
#endif
// https://art-net.org.uk/how-it-works/discovery-packets/artpollreply/
void setArtPollReplyConfigOem(uint16_t oem)
{
this->art_poll_reply_config.oem = oem;
}
void setArtPollReplyConfigEstaMan(uint16_t esta_man)
{
this->art_poll_reply_config.esta_man = esta_man;
}
void setArtPollReplyConfigStatus1(uint8_t status1)
{
this->art_poll_reply_config.status1 = status1;
}
void setArtPollReplyConfigStatus2(uint8_t status2)
{
this->art_poll_reply_config.status2 = status2;
}
void setArtPollReplyConfigShortName(const String &short_name)
{
this->art_poll_reply_config.short_name = short_name;
}
void setArtPollReplyConfigLongName(const String &long_name)
{
this->art_poll_reply_config.long_name = long_name;
}
void setArtPollReplyConfigNodeReport(const String &node_report)
{
this->art_poll_reply_config.node_report = node_report;
}
void setArtPollReplyConfigSwIn(size_t index, uint8_t sw_in)
{
if (index < 4) {
this->art_poll_reply_config.sw_in[index] = sw_in;
}
}
void setArtPollReplyConfigSwIn(uint8_t sw_in[4])
{
for (size_t i = 0; i < 4; ++i) {
this->art_poll_reply_config.sw_in[i] = sw_in[i];
}
}
void setArtPollReplyConfigSwIn(uint8_t sw_in_0, uint8_t sw_in_1, uint8_t sw_in_2, uint8_t sw_in_3)
{
this->setArtPollReplyConfigSwIn(0, sw_in_0);
this->setArtPollReplyConfigSwIn(1, sw_in_1);
this->setArtPollReplyConfigSwIn(2, sw_in_2);
this->setArtPollReplyConfigSwIn(3, sw_in_3);
}
void setArtPollReplyConfig(
uint16_t oem,
uint16_t esta_man,
uint8_t status1,
uint8_t status2,
const String &short_name,
const String &long_name,
const String &node_report,
uint8_t sw_in[4]
) {
this->setArtPollReplyConfigOem(oem);
this->setArtPollReplyConfigEstaMan(esta_man);
this->setArtPollReplyConfigStatus1(status1);
this->setArtPollReplyConfigStatus2(status2);
this->setArtPollReplyConfigShortName(short_name);
this->setArtPollReplyConfigLongName(long_name);
this->setArtPollReplyConfigNodeReport(node_report);
this->setArtPollReplyConfigSwIn(sw_in);
}
void setArtPollReplyConfig(const ArtPollReplyConfig &cfg)
{
this->art_poll_reply_config = cfg;
}
void setLogger(Print* logger)
{
this->logger = logger;
}
protected:
void attach(S& s)
{
this->stream = &s;
}
private:
bool checkID() const
{
const char* idptr = reinterpret_cast<const char*>(this->packet.data());
return !strcmp(ARTNET_ID, idptr);
}
uint16_t getOpCode() const
{
return (this->packet[art_dmx::OP_CODE_H] << 8) | this->packet[art_dmx::OP_CODE_L];
}
uint16_t getArtDmxUniverse15bit() const
{
return (this->packet[art_dmx::NET] << 8) | this->packet[art_dmx::SUBUNI];
}
const uint8_t *getArtDmxData() const
{
return &(this->packet[art_dmx::DATA]);
}
void sendArtPollReply(const RemoteInfo &remote)
{
const IPAddress my_ip = this->localIP();
uint8_t my_mac[6];
this->macAddress(my_mac);
arx::stdx::map<uint16_t, bool> universes;
for (const auto &cb_pair : this->callback_art_dmx_universes) {
universes[cb_pair.first] = true;
}
for (const auto &cb_pair : this->callback_art_nzs_universes) {
universes[cb_pair.first] = true;
}
// if no universe is subscribed, send reply for universe 0
if (universes.empty()) {
universes[0] = true;
}
for (const auto &u_pair : universes) {
art_poll_reply::Packet reply = art_poll_reply::generatePacketFrom(my_ip, my_mac, u_pair.first, this->art_poll_reply_config);
this->stream->beginPacket(remote.ip, DEFAULT_PORT);
this->stream->write(reply.b, sizeof(art_poll_reply::Packet));
this->stream->endPacket();
}
}
uint16_t getArtTriggerOEM() const
{
return (this->packet[art_trigger::OEM_H] << 8) | this->packet[art_trigger::OEM_L];
}
uint8_t getArtTriggerKey() const
{
return this->packet[art_trigger::KEY];
}
uint8_t getArtTriggerSubKey() const
{
return this->packet[art_trigger::SUB_KEY];
}
const uint8_t *getArtTriggerPayload() const
{
return this->packet.data() + art_trigger::PAYLOAD;
}
IPAddress localIP()
{
switch (WiFi.getMode())
{
case WIFI_AP:
return WiFi.softAPIP();
break;
case WIFI_STA:
return WiFi.localIP();
break;
default:
return ETH.localIP();
break;
}
}
IPAddress subnetMask()
{
switch (WiFi.getMode())
{
case WIFI_AP:
return WiFi.softAPSubnetMask();
break;
case WIFI_STA:
return WiFi.subnetMask();
break;
default:
return ETH.subnetMask();
break;
}
}
void macAddress(uint8_t* mac)
{
switch (WiFi.getMode())
{
case WIFI_AP:
WiFi.softAPmacAddress(mac);
break;
case WIFI_STA:
WiFi.macAddress(mac);
break;
default:
ETH.macAddress(mac);
//Ethernet.MACAddress(mac);
break;
}
}
};
template <typename S>
class Receiver : public Receiver_<S>
{
S stream;
public:
void begin(uint16_t recv_port = DEFAULT_PORT)
{
this->stream.begin(recv_port);
this->Receiver_<S>::attach(this->stream);
}
};
} // namespace art_net
#endif // ARTNET_RECEIVER_H

208
lib/ArtNet/Artnet/Sender.h Normal file
View file

@ -0,0 +1,208 @@
#pragma once
#ifndef ARTNET_SENDER_H
#define ARTNET_SENDER_H
#include "Common.h"
#include "ArtDmx.h"
#include "ArtNzs.h"
#include "ArtTrigger.h"
#include "ArtSync.h"
namespace art_net {
template <typename S>
class Sender_
{
S* stream;
Array<PACKET_SIZE> packet;
LastSendTimeMsMap last_send_times;
SequenceMap dmx_sequences;
SequenceMap nzs_sequences;
public:
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
#else
Sender_()
{
this->packet.resize(PACKET_SIZE);
}
#endif
// streaming artdmx packet
void setArtDmxData(const uint8_t* const data, uint16_t size)
{
art_dmx::setDataTo(this->packet.data(), data, size);
}
void setArtDmxData(uint16_t ch, uint8_t data)
{
art_dmx::setDataTo(this->packet.data(), ch, data);
}
void streamArtDmxTo(const String& ip, uint16_t universe15bit)
{
uint8_t net = (universe15bit >> 8) & 0x7F;
uint8_t subnet = (universe15bit >> 4) & 0x0F;
uint8_t universe = (universe15bit >> 0) & 0x0F;
this->streamArtDmxTo(ip, net, subnet, universe, 0);
}
void streamArtDmxTo(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe)
{
this->streamArtDmxTo(ip, net, subnet, universe, 0);
}
void streamArtDmxTo(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, uint8_t physical)
{
Destination dest {ip, net, subnet, universe};
uint32_t now = millis();
if (this->last_send_times.find(dest) == this->last_send_times.end()) {
this->last_send_times.insert(std::make_pair(dest, uint32_t(0)));
}
if (now >= this->last_send_times[dest] + DEFAULT_INTERVAL_MS) {
this->sendArxDmxInternal(dest, physical);
this->last_send_times[dest] = now;
}
}
// streaming artnzs packet
void setArtNzsData(const uint8_t* const data, uint16_t size)
{
art_nzs::setDataTo(this->packet.data(), data, size);
}
void setArtNzsData(uint16_t ch, uint8_t data)
{
art_nzs::setDataTo(this->packet.data(), ch, data);
}
void streamArtNzsTo(const String& ip, uint16_t universe15bit)
{
uint8_t net = (universe15bit >> 8) & 0x7F;
uint8_t subnet = (universe15bit >> 4) & 0x0F;
uint8_t universe = (universe15bit >> 0) & 0x0F;
this->streamArtNzsTo(ip, net, subnet, universe, 0);
}
void streamArtNzsTo(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe)
{
this->streamArtNzsTo(ip, net, subnet, universe, 0, 0);
}
void streamArtNzsTo(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, uint8_t start_code)
{
Destination dest {ip, net, subnet, universe};
uint32_t now = millis();
if (this->last_send_times.find(dest) == this->last_send_times.end()) {
this->last_send_times.insert(std::make_pair(dest, uint32_t(0)));
}
if (now >= this->last_send_times[dest] + DEFAULT_INTERVAL_MS) {
this->sendArxNzsInternal(dest, start_code);
this->last_send_times[dest] = now;
}
}
// one-line artdmx sender
void sendArtDmx(const String& ip, uint16_t universe15bit, const uint8_t* const data, uint16_t size)
{
uint8_t net = (universe15bit >> 8) & 0x7F;
uint8_t subnet = (universe15bit >> 4) & 0x0F;
uint8_t universe = (universe15bit >> 0) & 0x0F;
this->sendArtDmx(ip, net, subnet, universe, 0, data, size);
}
void sendArtDmx(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, const uint8_t* const data, uint16_t size)
{
this->sendArtDmx(ip, net, subnet, universe, 0, data, size);
}
void sendArtDmx(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, uint8_t physical, const uint8_t *data, uint16_t size)
{
Destination dest {ip, net, subnet, universe};
this->setArtDmxData(data, size);
this->sendArxDmxInternal(dest, physical);
}
// one-line artnzs sender
void sendArtNzs(const String& ip, uint16_t universe15bit, const uint8_t* const data, uint16_t size)
{
uint8_t net = (universe15bit >> 8) & 0x7F;
uint8_t subnet = (universe15bit >> 4) & 0x0F;
uint8_t universe = (universe15bit >> 0) & 0x0F;
this->sendArtNzs(ip, net, subnet, universe, data, size);
}
void sendArtNzs(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, const uint8_t* const data, uint16_t size)
{
this->sendArtNzs(ip, net, subnet, universe, 0, data, size);
}
void sendArtNzs(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, uint8_t start_code, const uint8_t *data, uint16_t size)
{
Destination dest {ip, net, subnet, universe};
this->setArtNzsData(data, size);
this->sendArxNzsInternal(dest, start_code);
}
void sendArtTrigger(const String& ip, uint16_t oem = 0, uint8_t key = 0, uint8_t subkey = 0, const uint8_t *payload = nullptr, uint16_t size = 512)
{
art_trigger::setDataTo(packet.data(), oem, key, subkey, payload, size);
this->sendRawData(ip, DEFAULT_PORT, packet.data(), packet.size());
}
void sendArtSync(const String& ip)
{
art_sync::setMetadataTo(packet.data());
this->sendRawData(ip, DEFAULT_PORT, packet.data(), art_sync::PACKET_SIZE);
}
protected:
void attach(S& s)
{
this->stream = &s;
}
void sendArxDmxInternal(const Destination &dest, uint8_t physical)
{
#ifdef ARTNET_ENABLE_WIFI
if (!isNetworkReady()) {
return;
}
#endif
if (this->dmx_sequences.find(dest) == this->dmx_sequences.end()) {
this->dmx_sequences.insert(std::make_pair(dest, uint8_t(0)));
}
art_dmx::setMetadataTo(this->packet.data(), this->dmx_sequences[dest], physical, dest.net, dest.subnet, dest.universe);
this->sendRawData(dest.ip, DEFAULT_PORT, this->packet.data(), this->packet.size());
this->dmx_sequences[dest] = (this->dmx_sequences[dest] + 1) % 256;
}
void sendArxNzsInternal(const Destination &dest, uint8_t start_code)
{
#ifdef ARTNET_ENABLE_WIFI
if (!isNetworkReady()) {
return;
}
#endif
if (this->nzs_sequences.find(dest) == this->nzs_sequences.end()) {
this->nzs_sequences.insert(std::make_pair(dest, uint8_t(0)));
}
art_nzs::setMetadataTo(this->packet.data(), this->nzs_sequences[dest], start_code, dest.net, dest.subnet, dest.universe);
this->sendRawData(dest.ip, DEFAULT_PORT, this->packet.data(), this->packet.size());
this->nzs_sequences[dest] = (this->nzs_sequences[dest] + 1) % 256;
}
void sendRawData(const String& ip, uint16_t port, const uint8_t* const data, size_t size)
{
this->stream->beginPacket(ip.c_str(), port);
this->stream->write(data, size);
this->stream->endPacket();
}
};
template <typename S>
class Sender : public Sender_<S>
{
S stream;
public:
void begin()
{
this->stream.begin(DEFAULT_PORT);
this->Sender_<S>::attach(this->stream);
}
};
} // namespace art_net
#endif // ARTNET_SENDER_H

View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Hideaki Tai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,57 @@
# TeensyDirtySTLErrorSolution
dirty solution to use STL in teensy
## Introduction
Sometimes we want to use STL on Teensy, but Teensy 3.x in Arduino IDE does not support STL by default. Though STL is enabled on Teensy 4.x, but you have to enable it manually on Teensy 3.x. If you use PlatformIO, you can also use STL on Teensy 3.x.
| | Teensy 4.x | Teensy 3.x |
| ----------- | ----------------- | ----------------- |
| PlatformIO | Almost Compilable | Almost Compilable |
| Arduino IDE | Almost Compilable | NOT Compilable |
## Enable STL in Arduino IDE
To enable STL on Teensy 3.x using Arduino IDE, add `-lstdc++` to `build flags`, which is defined in `boards.txt`. Many STL can be used by adding this flag. You have to do this procedure every time you upgrade the version of Teensyduino.
| OS | default location |
| ------- | --------------------------------------------------------------- |
| macOS | `Arduino.app/Contents/Java/hardware/teensy/avr/boards.txt` |
| Windows | `C:\Program Files (x86)\Arduino\hardware\teensy\avr\boards.txt` |
#### Before
``` boards.txt
teensy36.build.flags.libs=-larm_cortexM4lf_math -lm
teensy35.build.flags.libs=-larm_cortexM4lf_math -lm
teensy31.build.flags.libs=-larm_cortexM4l_math -lm
```
#### After
``` boards.txt
teensy36.build.flags.libs=-larm_cortexM4lf_math -lm -lstdc++
teensy35.build.flags.libs=-larm_cortexM4lf_math -lm -lstdc++
teensy31.build.flags.libs=-larm_cortexM4l_math -lm -lstdc++
```
## Do you still have compile error?
Yes, maybe you still have compilation error in both 4.x and 3.x for some STLs... Then you should add this library (header file) to your project. Some dirty solution I found is included in this header file.
``` C++
#include <TeensyDirtySTLErrorSolution.h>
```
## Warning
This method is not recommended because STL is not enabled by default... (if you know the reason, please let me know). I am not responsible for any trouble that may occur as a result of the use of this library. Please use at your own risk.
## License
MIT

View file

@ -0,0 +1,65 @@
#pragma once
#include <Arduino.h>
#ifdef TEENSYDUINO
#ifndef TEENSYDUINO_DIRTY_STL_ERROR_SOLUTION
#define TEENSYDUINO_DIRTY_STL_ERROR_SOLUTION
#if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) // TEENSY 3.1/3.2, 3.5, 3.6
extern "C"
{
int _write() { return -1; }
}
#elif defined(__IMXRT1062__) // TEENSY4.0
extern "C"
{
void *__exidx_start __attribute__((__visibility__ ("hidden")));
void *__exidx_end __attribute__((__visibility__ ("hidden")));
}
#else
#error NOT SUPPORTED TEENSY VERSION
#endif
// ----- template -----
//
// #if defined(__MK20DX256__) // TEENSY3.1/3.2
// #elif defined(__MK64FX512__) // TEENSY3.5
// #elif defined(__MK66FX1M0__) // TEENSY3.6
// #elif defined(__IMXRT1062__) // TEENSY4.0
//
// extern "C"
// {
// int _getpid() { return -1; }
// int _kill(int pid, int sig) { return -1; }
// int _write() { return -1; }
// void *__exidx_start __attribute__((__visibility__ ("hidden")));
// void *__exidx_end __attribute__((__visibility__ ("hidden")));
// int _getpid();// { return -1; }
// int _kill(int pid, int sig);// { return -1; }
// int _write() { return -1; }
// void *__exidx_start __attribute__((__visibility__ ("hidden")));
// void *__exidx_end __attribute__((__visibility__ ("hidden")));
// }
//
// // copied from https://github.com/gcc-mirror/
// namespace std
// {
// void __throw_bad_alloc() { _GLIBCXX_THROW_OR_ABORT(bad_alloc()); }
// void __throw_length_error(const char* __s __attribute__((unused))) { _GLIBCXX_THROW_OR_ABORT(length_error(_(__s))); }
// void __throw_bad_function_call() { _GLIBCXX_THROW_OR_ABORT(bad_function_call()); }
// void _Rb_tree_decrement(std::_Rb_tree_node_base* a) {}
// void _Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::_Rb_tree_node_base&) {}
// }
#endif // TEENSYDUINO_DIRTY_STL_ERROR_SOLUTION
#endif // TEENSYDUINO