Files
bioz-host-rs/src/client.rs

103 lines
3.2 KiB
Rust

use postcard_rpc::{
header::VarSeqKind,
host_client::{HostClient, HostErr},
standard_icd::{WireError, ERROR_PATH},
};
use std::convert::Infallible;
use bioz_icd_rs::{
BioImpedanceLeadMode, ElectrodeConfiguration, GetMultiplexerCapabilityEndpoint, GetUniqueIdEndpoint, ImpedanceInitResult, PingEndpoint, SetGreenLedEndpoint, SingleImpedanceStartRequest, StartSingleImpedanceEndpoint, StartSweepImpedanceEndpoint, StopImpedanceEndpoint, SweepImpedanceInitResult, SweepImpedanceStartRequest
};
use crate::icd::{IcdDftNum, SweepPoints, MultiplexerCapability};
#[derive(Debug)]
pub struct WorkbookClient {
pub client: HostClient<WireError>,
}
#[derive(Debug)]
pub enum WorkbookError<E> {
Comms(HostErr<WireError>),
Endpoint(E),
}
impl<E> From<HostErr<WireError>> for WorkbookError<E> {
fn from(value: HostErr<WireError>) -> Self {
Self::Comms(value)
}
}
// ---
impl WorkbookClient {
pub fn new() -> Result<Self, String> {
let client = HostClient::try_new_raw_nusb(
|d| d.product_string() == Some("Bioz Amplifier"),
ERROR_PATH,
8,
VarSeqKind::Seq2,
)?;
Ok(Self { client })
}
pub async fn wait_closed(&self) {
self.client.wait_closed().await;
}
pub async fn ping(&self, id: u32) -> Result<u32, WorkbookError<Infallible>> {
let val = self.client.send_resp::<PingEndpoint>(&id).await?;
Ok(val)
}
pub async fn get_id(&self) -> Result<[u8; 12], WorkbookError<Infallible>> {
let id = self.client.send_resp::<GetUniqueIdEndpoint>(&()).await?;
Ok(id)
}
pub async fn get_device_info(&self) -> Result<MultiplexerCapability, WorkbookError<Infallible>> {
let info = self.client.send_resp::<GetMultiplexerCapabilityEndpoint>(&()).await?;
Ok(info)
}
pub async fn set_green_led(
&self,
frequency: f32,
) -> Result<(), WorkbookError<Infallible>> {
self.client.send_resp::<SetGreenLedEndpoint>(&frequency).await?;
Ok(())
}
pub async fn start_impedancemeter_single(
&self,
frequency: u32,
lead_mode: BioImpedanceLeadMode,
electrode_config: Option<ElectrodeConfiguration>,
dft_number: IcdDftNum,
) -> Result<ImpedanceInitResult, WorkbookError<Infallible>> {
let response = self.client
.send_resp::<StartSingleImpedanceEndpoint>(&SingleImpedanceStartRequest { update_frequency: 60, sinus_frequency: frequency, lead_mode, electrode_config, dft_number})
.await?;
Ok(response)
}
pub async fn start_impedancemeter_sweep(
&self,
lead_mode: BioImpedanceLeadMode,
electrode_config: Option<ElectrodeConfiguration>,
points: SweepPoints,
) -> Result<SweepImpedanceInitResult, WorkbookError<Infallible>> {
let response = self.client
.send_resp::<StartSweepImpedanceEndpoint>(&SweepImpedanceStartRequest { lead_mode, electrode_config, points })
.await?;
Ok(response)
}
pub async fn stop_impedancemeter(&self) -> Result<bool, WorkbookError<Infallible>> {
let res = self
.client
.send_resp::<StopImpedanceEndpoint>(&())
.await?;
Ok(res)
}
}