54 lines
2.0 KiB
Rust
54 lines
2.0 KiB
Rust
use crate::{handle_cmd, Command, MonitorCommand, StdError};
|
|
use rosc;
|
|
use std::net::UdpSocket;
|
|
use std::sync::mpsc::Sender;
|
|
|
|
pub fn run_osc(txes: &Vec<Sender<MonitorCommand>>) -> StdError<()> {
|
|
let sock = UdpSocket::bind("0.0.0.0:1234")?;
|
|
let mut buf = [0u8; rosc::decoder::MTU];
|
|
|
|
loop {
|
|
let (size, addr) = sock.recv_from(&mut buf).unwrap();
|
|
println!("Got {} bytes from {}", size, addr);
|
|
let (_, pack) = rosc::decoder::decode_udp(&buf[..size]).unwrap();
|
|
match pack {
|
|
rosc::OscPacket::Message(msg) => {
|
|
match osc_message_to_command(msg) {
|
|
Ok(cmd) => {
|
|
if let Err(e) = handle_cmd(cmd, &txes) {
|
|
println!("Error handling command: {:?}", e);
|
|
}
|
|
}
|
|
Err(e) => println!("Unrecognised OSC command: {:?}", e),
|
|
};
|
|
}
|
|
rosc::OscPacket::Bundle(bundle) => {
|
|
println!("OSC Bundle: {:?}", bundle);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn osc_message_to_command(msg: rosc::OscMessage) -> StdError<Command> {
|
|
println!("OSC: {}, args: {:?}", msg.addr, msg.args);
|
|
let splitaddr: Vec<_> = msg.addr.split("/").collect();
|
|
match &splitaddr[1..] {
|
|
["monitor", idx, control] => {
|
|
println!("Monitor {}, control {}, args {:?}", idx, control, msg.args);
|
|
let command = match *control {
|
|
"brightness" => Some(MonitorCommand::Brightness(
|
|
(msg.args[0].clone().float().unwrap() * 100.0) as u8,
|
|
)),
|
|
"input" => Some(MonitorCommand::Input(
|
|
(msg.args[0].clone().int().unwrap()) as u8,
|
|
)),
|
|
_ => None,
|
|
}
|
|
.ok_or(format!("Unrecognised monitor control: {}", *control))?;
|
|
let idx = idx.parse::<usize>().or(Err("Bad monitor index"))?;
|
|
Ok(Command::Monitor((idx, command)))
|
|
}
|
|
_ => Err("Unsupported osc address, ignoring".into()),
|
|
}
|
|
}
|