mirror of
https://github.com/hubaldv/bioz-host-rs.git
synced 2026-03-09 23:50:31 +00:00
109 lines
2.6 KiB
Rust
109 lines
2.6 KiB
Rust
use postcard_rpc::{
|
|
header::VarSeqKind,
|
|
host_client::{HostClient, HostErr},
|
|
standard_icd::{WireError, ERROR_PATH},
|
|
};
|
|
use std::convert::Infallible;
|
|
use bioz_icd_rs::{
|
|
PingEndpoint, GetUniqueIdEndpoint, SetGreenLedEndpoint, StartImpedance, StartImpedanceEndpoint,
|
|
StopImpedanceEndpoint,
|
|
};
|
|
|
|
#[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)
|
|
}
|
|
}
|
|
|
|
trait FlattenErr {
|
|
type Good;
|
|
type Bad;
|
|
fn flatten(self) -> Result<Self::Good, WorkbookError<Self::Bad>>;
|
|
}
|
|
|
|
impl<T, E> FlattenErr for Result<T, E> {
|
|
type Good = T;
|
|
type Bad = E;
|
|
fn flatten(self) -> Result<Self::Good, WorkbookError<Self::Bad>> {
|
|
self.map_err(WorkbookError::Endpoint)
|
|
}
|
|
}
|
|
|
|
// ---
|
|
|
|
impl WorkbookClient {
|
|
pub fn new() -> Self {
|
|
let client = HostClient::new_raw_nusb(
|
|
|d| d.product_string() == Some("Bioz Amplifier"),
|
|
ERROR_PATH,
|
|
8,
|
|
VarSeqKind::Seq2,
|
|
);
|
|
Self { client }
|
|
}
|
|
|
|
pub fn new_serial(port: &str) -> Self {
|
|
let client = HostClient::new_serial_cobs(port, ERROR_PATH, 8, 9600, VarSeqKind::Seq2);
|
|
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 set_green_led(
|
|
&self,
|
|
frequency: f32,
|
|
) -> Result<(), WorkbookError<Infallible>> {
|
|
self.client.send_resp::<SetGreenLedEndpoint>(&frequency).await?;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn start_impedancemeter(
|
|
&self,
|
|
frequency: f32,
|
|
) -> Result<(), WorkbookError<Infallible>> {
|
|
self.client
|
|
.send_resp::<StartImpedanceEndpoint>(&StartImpedance { update_frequency: 60, sinus_frequency: frequency })
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn stop_impedancemeter(&self) -> Result<bool, WorkbookError<Infallible>> {
|
|
let res = self
|
|
.client
|
|
.send_resp::<StopImpedanceEndpoint>(&())
|
|
.await?;
|
|
|
|
Ok(res)
|
|
}
|
|
}
|
|
|
|
impl Default for WorkbookClient {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|