use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
fn main() {
let port = tiny_artnet::PORT;
let ip_address = Ipv4Addr::new(127, 0, 0, 1).octets();
let mac_address_bytes = mac_address::get_mac_address().unwrap().unwrap().bytes();
let socket = UdpSocket::bind(SocketAddr::from((ip_address, port))).unwrap();
println!(
"\n\nServer Started, listening on {}:{}",
IpAddr::from(ip_address),
port
);
let mut buf = [0; 65_507];
use tiny_artnet::Art;
loop {
let (len, from_addr) = socket.recv_from(&mut buf).unwrap();
match tiny_artnet::from_slice(&buf[..len]) {
Ok(Art::Dmx(dmx)) => {
println!(
"RX: ArtDMX - These packets contain data for one DMX512 universe - use them to control your node's lighting, etc. Seq: {:?} Data: {:?}...",
dmx.sequence,
&dmx.data[0..10],
);
}
Ok(Art::Sync) => {
println!("RX: ArtSync - Use these to buffer DMX packets and then synchronize the rendering of multiple DMX universes.");
}
Ok(Art::Poll(poll)) => {
println!("RX: ArtPoll - Someone is looking for ArtNet nodes. Let's respond to them to make this node discoverable! {:?}", poll);
let poll_reply = tiny_artnet::PollReply {
ip_address: &ip_address,
port,
firmware_version: 0x0001,
short_name: "Example Node",
long_name: "Tiny Artnet Example Node",
mac_address: &mac_address_bytes,
num_ports: 1,
port_types: &[0b10000000, 0, 0, 0],
good_output_a: &[0b10000000, 0, 0, 0],
..Default::default()
};
let msg_len = poll_reply.serialize(&mut buf);
socket.send_to(&buf[..msg_len], &from_addr).unwrap();
println!("TX: Sent ArtPollReply to {:?}: {:?}", from_addr, poll_reply);
}
Err(err) => {
println!("Error: {:?}", err);
}
msg => {
println!("Something else! {:?}", msg);
}
};
}
}