Skip to content

Commit

Permalink
NoTransmit zones
Browse files Browse the repository at this point in the history
  • Loading branch information
keesverruijt committed Oct 13, 2024
1 parent 0942e49 commit c3074c3
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 21 deletions.
114 changes: 95 additions & 19 deletions src/navico/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::cmp::{max, min};
use tokio::net::UdpSocket;

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

use super::Model;
Expand Down Expand Up @@ -129,13 +129,47 @@ impl Command {
}
}

pub async fn set_control(&mut self, cv: &ControlValue) -> Result<(), RadarError> {
fn get_angle_value(ct: &ControlType, controls: &Controls) -> i16 {
if let Some(control) = controls.get(ct) {
if let Some(value) = control.value {
let value = (value * 10.0) as i32;
return Self::mod_deci_degrees(value) as i16;
}
}
return 0;
}

async fn send_no_transmit_cmd(
&mut self,
value_start: i16,
value_end: i16,
enabled: u8,
sector: u8,
) -> Result<Vec<u8>, RadarError> {
let mut cmd = Vec::with_capacity(12);

cmd.extend_from_slice(&[0x0d, 0xc1, sector, 0, 0, 0, enabled]);
self.send(&cmd).await?;
cmd.clear();
cmd.extend_from_slice(&[0xc0, 0xc1, sector, 0, 0, 0, enabled]);
cmd.extend_from_slice(&value_start.to_le_bytes());
cmd.extend_from_slice(&value_end.to_le_bytes());

Ok(cmd)
}

pub async fn set_control(
&mut self,
cv: &ControlValue,
controls: &Controls,
) -> Result<(), RadarError> {
let value = cv
.value
.parse::<f32>()
.map_err(|_| RadarError::MissingValue(cv.id))?;
let deci_value = (value * 10.0) as i32;
let auto: u8 = if cv.auto.unwrap_or(false) { 1 } else { 0 };
let enabled: u8 = if cv.enabled.unwrap_or(false) { 1 } else { 0 };

let mut cmd = Vec::with_capacity(6);

Expand Down Expand Up @@ -226,23 +260,65 @@ impl Command {
ControlType::SeaState => {
cmd.extend_from_slice(&[0x0b, 0xc1, value as u8]);
}
ControlType::NoTransmitStart1
| ControlType::NoTransmitStart2
| ControlType::NoTransmitStart3
| ControlType::NoTransmitStart4 => {
let sector: u8 =
cv.id.to_u8().unwrap() - ControlType::NoTransmitStart1.to_u8().unwrap();
cmd.extend_from_slice(&[0x0d, 0xc1, sector, 0, 0, 0, auto]);
todo!();
}
ControlType::NoTransmitEnd1
| ControlType::NoTransmitEnd2
| ControlType::NoTransmitEnd3
| ControlType::NoTransmitEnd4 => {
let sector: u8 =
cv.id.to_u8().unwrap() - ControlType::NoTransmitEnd1.to_u8().unwrap();
cmd.extend_from_slice(&[0x0d, 0xc1, sector, 0, 0, 0, auto]);
todo!();
ControlType::NoTransmitStart1 => {
let value_start: i16 = Self::mod_deci_degrees(deci_value) as i16;
let value_end: i16 = Self::get_angle_value(&ControlType::NoTransmitEnd1, controls);
cmd = self
.send_no_transmit_cmd(value_start, value_end, enabled, 0)
.await?;
}
ControlType::NoTransmitStart2 => {
let value_start: i16 = Self::mod_deci_degrees(deci_value) as i16;
let value_end: i16 = Self::get_angle_value(&ControlType::NoTransmitEnd2, controls);
cmd = self
.send_no_transmit_cmd(value_start, value_end, enabled, 1)
.await?;
}
ControlType::NoTransmitStart3 => {
let value_start: i16 = Self::mod_deci_degrees(deci_value) as i16;
let value_end: i16 = Self::get_angle_value(&ControlType::NoTransmitEnd3, controls);
cmd = self
.send_no_transmit_cmd(value_start, value_end, enabled, 2)
.await?;
}
ControlType::NoTransmitStart4 => {
let value_start: i16 = Self::mod_deci_degrees(deci_value) as i16;
let value_end: i16 = Self::get_angle_value(&ControlType::NoTransmitEnd4, controls);
cmd = self
.send_no_transmit_cmd(value_start, value_end, enabled, 3)
.await?;
}
ControlType::NoTransmitEnd1 => {
let value_start: i16 =
Self::get_angle_value(&ControlType::NoTransmitStart1, controls);
let value_end: i16 = Self::mod_deci_degrees(deci_value) as i16;
cmd = self
.send_no_transmit_cmd(value_start, value_end, enabled, 0)
.await?;
}
ControlType::NoTransmitEnd2 => {
let value_start: i16 =
Self::get_angle_value(&ControlType::NoTransmitStart2, controls);
let value_end: i16 = Self::mod_deci_degrees(deci_value) as i16;
cmd = self
.send_no_transmit_cmd(value_start, value_end, enabled, 1)
.await?;
}
ControlType::NoTransmitEnd3 => {
let value_start: i16 =
Self::get_angle_value(&ControlType::NoTransmitStart3, controls);
let value_end: i16 = Self::mod_deci_degrees(deci_value) as i16;
cmd = self
.send_no_transmit_cmd(value_start, value_end, enabled, 2)
.await?;
}
ControlType::NoTransmitEnd4 => {
let value_start: i16 =
Self::get_angle_value(&ControlType::NoTransmitStart4, controls);
let value_end: i16 = Self::mod_deci_degrees(deci_value) as i16;
cmd = self
.send_no_transmit_cmd(value_start, value_end, enabled, 3)
.await?;
}
ControlType::LocalInterferenceRejection => {
cmd.extend_from_slice(&[0x0e, 0xc1, value as u8]);
Expand Down
10 changes: 8 additions & 2 deletions src/navico/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,11 @@ impl NavicoReportReceiver {
_ => {} // rest is numeric
}

if let Err(e) = self.command_sender.set_control(cv).await {
if let Err(e) = self
.command_sender
.set_control(cv, &self.info.controls)
.await
{
// Find our current control value for this ControlType
if let Some(control) = self.info.controls.get(&cv.id) {
self.info
Expand Down Expand Up @@ -620,7 +624,9 @@ impl NavicoReportReceiver {
);
range_detection.commanded_range = next_range;
let cv = ControlValue::new(ControlType::Range, next_range.to_string());
self.command_sender.set_control(&cv).await?;
self.command_sender
.set_control(&cv, &self.info.controls)
.await?;
}

if complete {
Expand Down

0 comments on commit c3074c3

Please sign in to comment.