Skip to content

Commit

Permalink
refactor: split files
Browse files Browse the repository at this point in the history
  • Loading branch information
hideakitai committed Jan 4, 2024
1 parent c0e5b80 commit bb0ae83
Show file tree
Hide file tree
Showing 12 changed files with 840 additions and 721 deletions.
93 changes: 93 additions & 0 deletions Artnet/ArtDmx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#pragma once
#ifndef ARTNET_ARTDMX_H
#define ARTNET_ARTDMX_H

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

namespace art_net {

namespace artdmx {

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

class ArtDmx {
uint8_t target_net {0};
uint8_t target_subnet {0};
uint8_t target_universe {0};
uint8_t seq {0};
uint8_t phy {0};

public:
void set_header(uint8_t *packet)
{
for (size_t i = 0; i < ID_LENGTH; i++) packet[artdmx::ID + i] = static_cast<uint8_t>(ARTNET_ID[i]);
packet[artdmx::OP_CODE_H] = (static_cast<uint16_t>(OpCode::Dmx) >> 8) & 0x00FF;
packet[artdmx::OP_CODE_L] = (static_cast<uint16_t>(OpCode::Dmx) >> 0) & 0x00FF;
packet[artdmx::PROTOCOL_VER_H] = (PROTOCOL_VER >> 8) & 0x00FF;
packet[artdmx::PROTOCOL_VER_L] = (PROTOCOL_VER >> 0) & 0x00FF;
packet[artdmx::SEQUENCE] = seq++;
packet[artdmx::PHYSICAL] = phy;
packet[artdmx::NET] = target_net;
packet[artdmx::SUBUNI] = (target_subnet << 4) | target_universe;
packet[artdmx::LENGTH_H] = (512 >> 8) & 0xFF;
packet[artdmx::LENGTH_L] = (512 >> 0) & 0xFF;
#ifdef ARTNET_ENABLE_WIFI
auto status = WiFi.status();
auto is_connected = status == WL_CONNECTED;
#if defined(ARDUINO_ARCH_ESP32) || defined(ESP8266) || defined(ARDUINO_ARCH_RP2040)
bool is_ap_active = WiFi.getMode() == WIFI_AP;
#else
bool is_ap_active = status == WL_AP_CONNECTED;
#endif
if (!is_connected && !is_ap_active) {
return;
}
#endif
}

void set_universe(const uint32_t universe_) {
target_net = (universe_ >> 8) & 0x7F;
target_subnet = (universe_ >> 4) & 0x0F;
target_universe = universe_ & 0x0F;
}
void set_universe(const uint8_t net_, const uint8_t subnet_, const uint8_t universe_) {
target_net = net_ & 0x7F;
target_subnet = subnet_ & 0x0F;
target_universe = universe_ & 0x0F;
}
void set_physical(const uint8_t i) {
phy = constrain(i, 0, 3);
}

uint8_t sequence() const {
return seq;
}

void set_data(uint8_t *packet, const uint8_t* const data, const uint16_t size) {
memcpy((&packet[artdmx::DATA]), data, size);
}
void set_data(uint8_t *packet, const uint16_t ch, const uint8_t data) {
packet[artdmx::DATA + ch] = data;
}
};

} // namespace artdmx

} // namespace art_net

#endif // ARTNET_ARTDMX_H
131 changes: 131 additions & 0 deletions Artnet/ArtPollReply.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#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 artpollreply {

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

class ArtPollReply
{
String short_name {"Arduino ArtNet"};
String long_name {"Ardino ArtNet Protocol by hideakitai/ArtNet"};
String node_report {""};

public:
Packet generate_reply(const IPAddress &my_ip, const uint8_t my_mac[6], const CallbackMap &callbacks, uint8_t net_switch, uint8_t sub_switch)
{
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 = 0; // https://github.com/tobiasebsen/ArtNode/blob/master/src/Art-NetOemCodes.h
r.oem_l = 0xFF; // OemUnknown
r.ubea_ver = 0; // UBEA not programmed
r.status_1 = 0x00; // Unknown / Normal
r.esta_man_l = 0; // No EATA manufacture code
r.esta_man_h = 0; // No ESTA manufacture code
memset(r.short_name, 0, 18);
memset(r.long_name, 0, 64);
memset(r.node_report, 0, 64);
memcpy(r.short_name, short_name.c_str(), short_name.length());
memcpy(r.long_name, long_name.c_str(), long_name.length());
memcpy(r.node_report, node_report.c_str(), node_report.length());
r.num_ports_h = 0; // Reserved
r.num_ports_l = (callbacks.size() > NUM_POLLREPLY_PUBLIC_PORT_LIMIT) ? NUM_POLLREPLY_PUBLIC_PORT_LIMIT : callbacks.size();
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 = net_switch & 0x7F;
r.sub_sw = sub_switch & 0x0F;
size_t i = 0;
for (const auto& pair : callbacks) {
r.sw_in[i] = pair.first & 0x0F;
r.sw_out[i] = i; // dummy: output port is flexible
r.port_types[i] = 0xC0; // I/O available by DMX512
r.good_input[i] = 0x80; // Data received without error
r.good_output[i] = 0x80; // Data transmitted without error
if (++i >= NUM_POLLREPLY_PUBLIC_PORT_LIMIT) break;
}
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 = 0x08; // sACN capable
memset(r.filler, 0x00, 26);

return r;
}

void shortname(const String& sn) {
short_name = sn;
}
void longname(const String& ln) {
long_name = ln;
}
void nodereport(const String& nr) {
node_report = nr;
}
};

} // namespace artpollreply
} // namespace art_net

#endif // ARTNET_ARTPOLLREPLY_H
9 changes: 9 additions & 0 deletions Artnet/ArtTrigger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#ifndef ARTNET_ARTPOLLREPLY_H
#define ARTNET_ARTPOLLREPLY_H

namespace art_net {

} // namespace art_net

#endif // ARTNET_ARTPOLLREPLY_H
Loading

0 comments on commit bb0ae83

Please sign in to comment.