fmt, update deps, random tweaks
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
use ::ddcmqtt::{main as main_, StdError};
|
||||
|
||||
|
||||
fn main() -> StdError<()> {
|
||||
main_()
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -1,7 +1,7 @@
|
||||
use ddc_i2c::{I2cDeviceEnumerator, I2cDeviceDdc};
|
||||
use ddc::Ddc;
|
||||
use ddc_i2c::{I2cDeviceDdc, I2cDeviceEnumerator};
|
||||
|
||||
use ::ddcmqtt::{StdError};
|
||||
use ::ddcmqtt::StdError;
|
||||
|
||||
fn main() -> StdError<()> {
|
||||
let args_: Vec<_> = std::env::args().skip(1).collect();
|
||||
@@ -10,14 +10,14 @@ fn main() -> StdError<()> {
|
||||
["brightness", arg] => ddc_all(0x10, arg.parse::<u16>()?),
|
||||
["input", arg] => ddc_all(0x60, arg.parse::<u16>()?),
|
||||
_ => Err("Bad usage".into()),
|
||||
|
||||
}?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ddc_all(feat: u8, val: u16) -> StdError<()> {
|
||||
for mut display in I2cDeviceEnumerator::new()? {
|
||||
display.set_vcp_feature(feat, val)?;
|
||||
}
|
||||
let mut m1 = ddc_i2c::from_i2c_device("/dev/i2c-20")?;
|
||||
let mut m2 = ddc_i2c::from_i2c_device("/dev/i2c-21")?;
|
||||
m1.set_vcp_feature(feat, val)?;
|
||||
m2.set_vcp_feature(feat, val)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+32
-25
@@ -1,14 +1,12 @@
|
||||
#![allow(unreachable_code)]
|
||||
use ddc::Ddc;
|
||||
use ddc_i2c::{I2cDeviceEnumerator, I2cDeviceDdc};
|
||||
use rosc;
|
||||
use std::net::UdpSocket;
|
||||
use std::thread;
|
||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||
use std::time::{Duration, Instant};
|
||||
use ddc_i2c::{I2cDeviceDdc, I2cDeviceEnumerator};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use std::error::Error;
|
||||
use std::net::UdpSocket;
|
||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
use rosc;
|
||||
|
||||
pub type StdError<T> = Result<T, Box<dyn Error>>;
|
||||
|
||||
@@ -18,7 +16,7 @@ enum Command {
|
||||
|
||||
enum MonitorCommand {
|
||||
Brightness(u8),
|
||||
Input(u8)
|
||||
Input(u8),
|
||||
}
|
||||
|
||||
impl MonitorCommand {
|
||||
@@ -37,10 +35,9 @@ impl MonitorCommand {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Run an i2c command handler for a specific i2c device, rate limiting each type of command
|
||||
fn run_i2c(mut dev: I2cDeviceDdc, command_channel: Receiver<MonitorCommand>) {
|
||||
let mut last_sent_command: HashMap<&str, Option<Instant>> = HashMap::new();
|
||||
let mut last_sent_command: HashMap<&str, Option<Instant>> = HashMap::new();
|
||||
loop {
|
||||
let cmd = command_channel.recv().unwrap();
|
||||
if let Some(last) = last_sent_command.entry(cmd.cmd_str()).or_insert(None) {
|
||||
@@ -52,7 +49,7 @@ fn run_i2c(mut dev: I2cDeviceDdc, command_channel: Receiver<MonitorCommand>) {
|
||||
match cmd {
|
||||
MonitorCommand::Brightness(b) => dev.set_vcp_feature(0x10, b.into()).unwrap(),
|
||||
// Hack - add 15 to align with DELL monitors
|
||||
MonitorCommand::Input(i) => dev.set_vcp_feature(0x60, (i+15).into()).unwrap(),
|
||||
MonitorCommand::Input(i) => dev.set_vcp_feature(0x60, (i + 15).into()).unwrap(),
|
||||
}
|
||||
last_sent_command.insert(cmd.cmd_str(), Some(Instant::now()));
|
||||
}
|
||||
@@ -61,24 +58,29 @@ fn run_i2c(mut dev: I2cDeviceDdc, command_channel: Receiver<MonitorCommand>) {
|
||||
pub fn main() -> StdError<()> {
|
||||
let displays = I2cDeviceEnumerator::new().unwrap().collect::<Vec<_>>();
|
||||
println!("Enumerated {} displays", displays.len());
|
||||
let txes: Vec<_> = displays.into_iter().map(|d| {
|
||||
let (tx, rx) = channel();
|
||||
thread::spawn(move|| run_i2c(d, rx));
|
||||
tx
|
||||
}).collect();
|
||||
let txes: Vec<_> = displays
|
||||
.into_iter()
|
||||
.map(|d| {
|
||||
let (tx, rx) = channel();
|
||||
thread::spawn(move || run_i2c(d, rx));
|
||||
tx
|
||||
})
|
||||
.collect();
|
||||
let sock = UdpSocket::bind("0.0.0.0:1234").unwrap();
|
||||
let mut buf = [0u8; rosc::decoder::MTU];
|
||||
|
||||
loop {
|
||||
let (size, addr) = sock.recv_from(&mut buf).unwrap();
|
||||
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);
|
||||
},
|
||||
Ok(cmd) => {
|
||||
if let Err(e) = handle_cmd(cmd, &txes) {
|
||||
println!("Error handling command: {:?}", e);
|
||||
}
|
||||
}
|
||||
Err(e) => println!("Unrecognised OSC command: {:?}", e),
|
||||
};
|
||||
}
|
||||
@@ -103,13 +105,18 @@ fn osc_message_to_command(msg: rosc::OscMessage) -> StdError<Command> {
|
||||
["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)),
|
||||
"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))?;
|
||||
}
|
||||
.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()),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user