Created
November 13, 2023 10:33
-
-
Save namachan10777/c1d557607fac48c7bd6da59797982302 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-FileCopyrightText: (c) 2021-2023 Shawn Silverman <shawn@pobox.com> | |
// SPDX-License-Identifier: AGPL-3.0-or-later | |
// OSCPrinter prints received OSC messages. It uses the well-known | |
// OSC port 8000 and advertises an OSC mDNS service on that port. | |
// To see messages, discover this example using a program such as | |
// TouchOSC and then send some messages. | |
// | |
// This example relies on the LiteOSCParser library. | |
// | |
// This file is part of the QNEthernet library. | |
#include <QNEthernet.h> | |
using namespace qindesign::network; | |
constexpr uint8_t offset = 3; | |
constexpr uint8_t my_sub_uni = 0; | |
constexpr uint8_t my_net = 0; | |
constexpr uint8_t my_ch = offset; | |
constexpr size_t timeout = 2000; | |
constexpr int pwm_pin = 13; | |
EthernetUDP udp; | |
IPAddress ip(192, 168, 10, 100 + offset); | |
IPAddress netmask(255, 255, 255, 0); | |
IPAddress gw(192, 168, 10, 1); | |
constexpr uint16_t artnet_port = 6454; | |
uint8_t buf[Ethernet.mtu() - 20 - 8]; // Maximum UDP payload size | |
// 20-byte IP, 8-byte UDP header | |
constexpr char artnet_header[] = "Art-Net"; | |
// Main program setup. | |
void setup() { | |
Serial.begin(9600); | |
/*while (!Serial && millis() < 4000) { | |
// Wait for Serial | |
}*/ | |
Serial.println("Starting..."); | |
pinMode(pwm_pin, OUTPUT); | |
analogWrite(pwm_pin, 0); | |
// Print the MAC address | |
uint8_t mac[6]; | |
Ethernet.macAddress(mac); // This is informative; it retrieves, not sets | |
Serial.printf("MAC = %02x:%02x:%02x:%02x:%02x:%02x\r\n", | |
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); | |
// Initialize Ethernet, in this case with DHCP | |
Serial.println("Starting Ethernet with Static IP..."); | |
if (!Ethernet.begin(ip, netmask, gw)) { | |
Serial.println("Failed to start Ethernet"); | |
return; | |
} | |
IPAddress ip = Ethernet.localIP(); | |
Serial.printf(" Local IP = %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3]); | |
ip = Ethernet.subnetMask(); | |
Serial.printf(" Subnet mask = %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3]); | |
ip = Ethernet.broadcastIP(); | |
Serial.printf(" Broadcast IP = %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3]); | |
ip = Ethernet.gatewayIP(); | |
Serial.printf(" Gateway = %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3]); | |
ip = Ethernet.dnsServerIP(); | |
Serial.printf(" DNS = %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3]); | |
// Listen on port and start an mDNS service | |
udp.begin(artnet_port); | |
Serial.println("Waiting for artnet messages..."); | |
} | |
uint8_t *parse_artnet(uint8_t *buf) { | |
if (memcmp(buf, artnet_header, sizeof(artnet_header)) != 0) { | |
return NULL; | |
} | |
size_t cursor = sizeof(artnet_header); | |
uint16_t opcode_lo = buf[cursor++]; | |
uint16_t opcode_hi = buf[cursor++]; | |
uint16_t opcode = opcode_hi << 8 | opcode_lo; | |
uint16_t proto_ver_hi = buf[cursor++]; | |
uint16_t proto_ver_lo = buf[cursor++]; | |
uint16_t proto_ver = proto_ver_hi << 8 | proto_ver_lo; | |
uint8_t seq = buf[cursor++]; | |
uint8_t phy = buf[cursor++]; | |
uint8_t sub_uni = buf[cursor++]; | |
if (sub_uni != my_sub_uni) { | |
return NULL; | |
} | |
uint8_t net = buf[cursor++]; | |
if (net != my_net) { | |
return NULL; | |
} | |
uint16_t len_hi = buf[cursor++]; | |
uint16_t len_lo = buf[cursor++]; | |
uint8_t *data = &buf[cursor]; | |
return data; | |
} | |
static size_t timeout_count = 0; | |
// Main program loop. | |
void loop() { | |
int size = udp.parsePacket(); | |
if (timeout_count >= timeout) { | |
analogWrite(pwm_pin, 0); | |
} | |
else { | |
++timeout_count; | |
} | |
if (0 < size && static_cast<unsigned int>(size) <= sizeof(buf)) { | |
udp.read(buf, size); | |
uint8_t *data; | |
if ((data = parse_artnet(buf)) == NULL) { | |
return; | |
} | |
timeout_count = 0; | |
analogWrite(pwm_pin, data[my_ch]); | |
} | |
delay(5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment