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

Refactor/commonize some structs #108

Merged
merged 2 commits into from
Jun 19, 2024
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
50 changes: 0 additions & 50 deletions Artnet/ArtDmx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,6 @@ enum Index : uint16_t
DATA = 18
};

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;
}

struct Metadata
{
uint8_t sequence;
Expand All @@ -78,16 +36,8 @@ struct Metadata

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
// sender
using LastSendTimeMsMap = std::map<Destination, uint32_t>;
using SequenceMap = std::map<Destination, uint8_t>;
// receiver
using CallbackMap = std::map<uint16_t, CallbackType>;
#else
// sender
using LastSendTimeMsMap = arx::stdx::map<Destination, uint32_t>;
using SequenceMap = arx::stdx::map<Destination, uint8_t>;
// receiver
using CallbackMap = arx::stdx::map<uint16_t, CallbackType>;
#endif

Expand Down
3 changes: 1 addition & 2 deletions Artnet/ArtNzs.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ struct Metadata

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
// receiver
using CallbackMap = std::map<uint16_t, CallbackType>;
#else
// receiver
using CallbackMap = arx::stdx::map<uint16_t, CallbackType>;
#endif

Expand Down Expand Up @@ -84,5 +82,6 @@ inline void setDataTo(uint8_t *packet, const uint16_t ch, const uint8_t data)
} // namespace art_net

using ArtNzsMetadata = art_net::art_nzs::Metadata;
using ArtNzsCallback = art_net::art_nzs::CallbackType;

#endif // ARTNET_ARTNZS_H
52 changes: 52 additions & 0 deletions Artnet/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,58 @@ struct RemoteInfo
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()
{
Expand Down
18 changes: 9 additions & 9 deletions Artnet/Sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Sender_
{
S* stream;
Array<PACKET_SIZE> packet;
art_dmx::LastSendTimeMsMap last_send_times;
art_dmx::SequenceMap dmx_sequences;
art_nzs::SequenceMap nzs_sequences;
LastSendTimeMsMap last_send_times;
SequenceMap dmx_sequences;
SequenceMap nzs_sequences;

public:
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
Expand Down Expand Up @@ -51,7 +51,7 @@ class Sender_
}
void streamArtDmxTo(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, uint8_t physical)
{
art_dmx::Destination dest {ip, net, subnet, universe};
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)));
Expand Down Expand Up @@ -85,7 +85,7 @@ class Sender_
}
void streamArtNzsTo(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, uint8_t start_code)
{
art_dmx::Destination dest {ip, net, subnet, universe};
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)));
Expand All @@ -110,7 +110,7 @@ class Sender_
}
void sendArtDmx(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, uint8_t physical, const uint8_t *data, uint16_t size)
{
art_dmx::Destination dest {ip, net, subnet, universe};
Destination dest {ip, net, subnet, universe};
this->setArtDmxData(data, size);
this->sendArxDmxInternal(dest, physical);
}
Expand All @@ -129,7 +129,7 @@ class Sender_
}
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)
{
art_dmx::Destination dest {ip, net, subnet, universe};
Destination dest {ip, net, subnet, universe};
this->setArtNzsData(data, size);
this->sendArxNzsInternal(dest, start_code);
}
Expand All @@ -152,7 +152,7 @@ class Sender_
this->stream = &s;
}

void sendArxDmxInternal(const art_dmx::Destination &dest, uint8_t physical)
void sendArxDmxInternal(const Destination &dest, uint8_t physical)
{
#ifdef ARTNET_ENABLE_WIFI
if (!isNetworkReady()) {
Expand All @@ -167,7 +167,7 @@ class Sender_
this->dmx_sequences[dest] = (this->dmx_sequences[dest] + 1) % 256;
}

void sendArxNzsInternal(const art_dmx::Destination &dest, uint8_t start_code)
void sendArxNzsInternal(const Destination &dest, uint8_t start_code)
{
#ifdef ARTNET_ENABLE_WIFI
if (!isNetworkReady()) {
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,20 @@ void setArtDmxData(uint16_t ch, uint8_t data);
void streamArtDmxTo(const String& ip, uint16_t universe15bit);
void streamArtDmxTo(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe);
void streamArtDmxTo(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, uint8_t physical);
// streaming artnzs packet
void setArtNzsData(const uint8_t* const data, uint16_t size);
void setArtNzsData(uint16_t ch, uint8_t data);
void streamArtNzsTo(const String& ip, uint16_t universe15bit);
void streamArtNzsTo(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe);
void streamArtNzsTo(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, uint8_t start_code);
// one-line artdmx sender
void sendArtDmx(const String& ip, uint16_t universe15bit, const uint8_t* const data, uint16_t size);
void sendArtDmx(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, const uint8_t* const data, uint16_t 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);
// one-line artnzs sender
void sendArtNzs(const String& ip, uint16_t universe15bit, const uint8_t* const data, uint16_t size);
void sendArtNzs(const String& ip, uint8_t net, uint8_t subnet, uint8_t universe, const uint8_t* const data, uint16_t 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);
// send other packets
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);
void sendArtSync(const String& ip);
Expand Down Expand Up @@ -324,6 +334,8 @@ OpCode parse()
void subscribeArtDmxUniverse(uint8_t net, uint8_t subnet, uint8_t universe, const ArtDmxCallback &func);
// subscribe artdmx packet for specified universe (15 bit)
void subscribeArtDmxUniverse(uint16_t universe, const ArtDmxCallback &func);
// subscribe artnzs packet for specified universe (15 bit)
auto subscribeArtNzsUniverse(uint16_t universe, const ArtNzsCallback &func);
// subscribe artdmx packet for all universes
void subscribeArtDmx(const ArtDmxCallback &func);
// subscribe other packets
Expand All @@ -334,6 +346,7 @@ void unsubscribeArtDmxUniverse(uint8_t net, uint8_t subnet, uint8_t universe);
void unsubscribeArtDmxUniverse(uint16_t universe);
void unsubscribeArtDmxUniverses();
void unsubscribeArtDmx();
void unsubscribeArtNzsUniverse(uint16_t universe);
void unsubscribeArtSync();
void unsubscribeArtTrigger();
// set artdmx data to CRGB (FastLED) directly
Expand Down
Loading