Skip to content

Commit

Permalink
Gain control working
Browse files Browse the repository at this point in the history
  • Loading branch information
keesverruijt committed Sep 9, 2024
1 parent ae96adb commit 221918b
Show file tree
Hide file tree
Showing 11 changed files with 812 additions and 523 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ libc = "0.2.156"
log = "0.4.22"
miette = { version = "7.2.0", features = ["fancy"] }
network-interface = "2.0.0"
num-derive = "0.4.2"
num-traits = "0.2.19"
protobuf = "3.5.1"
rust-embed = { version = "8.5.0", features = ["axum"] }
serde = { version = "1.0.206", features = ["derive", "serde_derive"] }
serde_json = "1.0.125"
serde_repr = "0.1.19"
serde_with = { version = "3.9.0", features = ["macros"] }
socket2 = "0.5.7"
terminal_size = "0.3.0"
Expand Down
24 changes: 19 additions & 5 deletions src/navico/command.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use log::{debug, trace};
use std::cmp::min;
use std::io;
use tokio::net::UdpSocket;

use crate::radar::RadarInfo;
use crate::radar::{RadarError, RadarInfo};
use crate::settings::{ControlType, ControlValue};
use crate::util::create_multicast_send;

pub const REQUEST_03_REPORT: [u8; 2] = [0x04, 0xc2]; // This causes the radar to report Report 3
Expand All @@ -25,7 +27,7 @@ impl Command {
}
}

async fn start_socket(&mut self) -> io::Result<()> {
async fn start_socket(&mut self) -> Result<(), RadarError> {
match create_multicast_send(&self.info.send_command_addr, &self.info.nic_addr) {
Ok(sock) => {
debug!(
Expand All @@ -41,20 +43,32 @@ impl Command {
"{} {} via {}: create multicast failed: {}",
self.key, &self.info.send_command_addr, &self.info.nic_addr, e
);
Err(e)
Err(RadarError::Io(e))
}
}
}

pub async fn send(&mut self, message: &[u8]) -> io::Result<()> {
pub async fn send(&mut self, message: &[u8]) -> Result<(), RadarError> {
if self.sock.is_none() {
self.start_socket().await?;
}
if let Some(sock) = &self.sock {
sock.send(message).await?;
sock.send(message).await.map_err(RadarError::Io)?;
trace!("{}: sent {:02X?}", self.key, message);
}

Ok(())
}

pub async fn set_control(&mut self, cv: &ControlValue) -> Result<(), RadarError> {
match cv.id {
ControlType::Gain => {
let v: i32 = min((cv.value.unwrap_or(0) + 1) * 255 / 100, 255);
let auto: u8 = if cv.auto.unwrap_or(false) { 1 } else { 0 };
let cmd: [u8;11] = [ 0x06, 0xc1, 0, 0, 0, 0, auto, 0, 0, 0, v as u8];
self.send(&cmd).await
},
_ => Err(RadarError::CannotSetControlType(cv.id))
}
}
}
Loading

0 comments on commit 221918b

Please sign in to comment.