Skip to content

Commit

Permalink
update example so it builds successfully (fixes Trangar/artnet_protoc…
Browse files Browse the repository at this point in the history
…ol#43) (#35)

Co-authored-by: Timoteus Ruotsalainen <[email protected]>
  • Loading branch information
TimoteusRuotsalainen and Timoteus Ruotsalainen authored Nov 5, 2024
1 parent 264a571 commit f8b6cad
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 29 deletions.
68 changes: 39 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,47 @@

Contains the [ArtCommand](struct.ArtCommand.html) enum which holds the entire ArtNet protocol v4, as per [https://artisticlicence.com/WebSiteMaster/User%20Guides/art-net.pdf](https://artisticlicence.com/WebSiteMaster/User%20Guides/art-net.pdf)

You can run the example program using

cargo build --example simple_sender
cargo run --example simple_sender

```rust
use artnet_protocol::*;
use std::net::{UdpSocket, ToSocketAddrs};

let socket = UdpSocket::bind(("0.0.0.0", 6454)).unwrap();
let broadcast_addr = ("255.255.255.255", 6454).to_socket_addrs().unwrap().next().unwrap();
socket.set_broadcast(true).unwrap();
let buff = ArtCommand::Poll(Poll::default()).write_to_buffer().unwrap();
socket.send_to(&buff, &broadcast_addr).unwrap();

loop {
let mut buffer = [0u8; 1024];
let (length, addr) = socket.recv_from(&mut buffer).unwrap();
let command = ArtCommand::from_buffer(&buffer[..length]).unwrap();

println!("Received {:?}", command);
match command {
ArtCommand::Poll(poll) => {
// This will most likely be our own poll request, as this is broadcast to all devices on the network
},
ArtCommand::PollReply(reply) => {
// This is an ArtNet node on the network. We can send commands to it like this:
let command = ArtCommand::Output(Output {
length: 5, // must match your data.len()
data: vec![1, 2, 3, 4, 5], // The data we're sending to the node
..Output::default()
});
let bytes = command.write_to_buffer().unwrap();
socket.send_to(&bytes, &addr).unwrap();
},
_ => {}
use std::net::{ToSocketAddrs, UdpSocket};

fn main() {
let socket = UdpSocket::bind(("0.0.0.0", 6454)).unwrap();
let broadcast_addr = ("255.255.255.255", 6454)
.to_socket_addrs()
.unwrap()
.next()
.unwrap();
socket.set_broadcast(true).unwrap();
let buff = ArtCommand::Poll(Poll::default()).write_to_buffer().unwrap();
socket.send_to(&buff, &broadcast_addr).unwrap();

loop {
let mut buffer = [0u8; 1024];
let (length, addr) = socket.recv_from(&mut buffer).unwrap();
let command = ArtCommand::from_buffer(&buffer[..length]).unwrap();

println!("Received {:?}", command);
match command {
ArtCommand::Poll(poll) => {
// This will most likely be our own poll request, as this is broadcast to all devices on the network
}
ArtCommand::PollReply(reply) => {
// This is an ArtNet node on the network. We can send commands to it like this:
let command = ArtCommand::Output(Output {
data: vec![1, 2, 3, 4, 5].into(), // The data we're sending to the node
..Output::default()
});
let bytes = command.write_to_buffer().unwrap();
socket.send_to(&bytes, &addr).unwrap();
}
_ => {}
}
}
}
```
Expand Down
37 changes: 37 additions & 0 deletions examples/simple_sender.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use artnet_protocol::*;
use std::net::{ToSocketAddrs, UdpSocket};

fn main() {
let socket = UdpSocket::bind(("0.0.0.0", 6454)).unwrap();
let broadcast_addr = ("255.255.255.255", 6454)
.to_socket_addrs()
.unwrap()
.next()
.unwrap();
socket.set_broadcast(true).unwrap();
let buff = ArtCommand::Poll(Poll::default()).write_to_buffer().unwrap();
socket.send_to(&buff, &broadcast_addr).unwrap();

loop {
let mut buffer = [0u8; 1024];
let (length, addr) = socket.recv_from(&mut buffer).unwrap();
let command = ArtCommand::from_buffer(&buffer[..length]).unwrap();

println!("Received {:?}", command);
match command {
ArtCommand::Poll(poll) => {

Check warning on line 22 in examples/simple_sender.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `poll`
// This will most likely be our own poll request, as this is broadcast to all devices on the network
}
ArtCommand::PollReply(reply) => {

Check warning on line 25 in examples/simple_sender.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `reply`
// This is an ArtNet node on the network. We can send commands to it like this:
let command = ArtCommand::Output(Output {
data: vec![1, 2, 3, 4, 5].into(), // The data we're sending to the node
..Output::default()
});
let bytes = command.write_to_buffer().unwrap();
socket.send_to(&bytes, &addr).unwrap();
}
_ => {}
}
}
}

0 comments on commit f8b6cad

Please sign in to comment.