use crate::messages::utils::{long, short};
use crate::traits::{ThorlabsDevice, UnitConversion, Units};
const MOVE_ABSOLUTE: [u8; 2] = [0x53, 0x04];
const MOVE_COMPLETED: [u8; 2] = [0x64, 0x04];
#[doc = include_str!("../documentation/move_absolute.md")]
pub(crate) async fn move_absolute<A, const CH: usize>(device: &A, channel: usize, position: f64)
where
A: ThorlabsDevice<CH> + UnitConversion,
{
log::info!("{device} CHANNEL {channel} MOVE_ABSOLUTE {position} (requested)");
loop {
let rx = device.inner().receiver(&MOVE_COMPLETED, channel).await;
if rx.is_new() {
log::info!("{device} CHANNEL {channel} MOVE_ABSOLUTE {position} (is new)");
let command = {
let mut data: Vec<u8> = Vec::with_capacity(6);
data.extend((channel as u16).to_le_bytes());
data.extend(A::distance_from_f64(position));
long(MOVE_ABSOLUTE, &data)
};
device.inner().send(command).await;
}
let response = rx.receive().await;
log::info!("{device} CHANNEL {channel} MOVE_ABSOLUTE {position} (responded)");
let p = device.decode(Units::distance_from_slice(&response[8..12]));
if Units::approx(p, position) {
log::info!("{device} CHANNEL {channel} MOVE_ABSOLUTE {position} (success)");
return;
}
}
}
#[doc = include_str!("../documentation/move_absolute_from_params.md")]
pub(crate) async fn move_absolute_from_params<A, const CH: usize>(device: &A, channel: usize) -> f64
where
A: ThorlabsDevice<CH> + UnitConversion,
{
log::info!("{device} CHANNEL {channel} MOVE_ABSOLUTE_FROM_PARAMS (requested)");
let rx = device.inner().new_receiver(&MOVE_COMPLETED, channel).await;
{
log::info!("{device} CHANNEL {channel} MOVE_ABSOLUTE_FROM_PARAMS (is new)");
let command = short(MOVE_ABSOLUTE, channel as u8, 0);
device.inner().send(command).await;
}
let response = rx.receive().await;
log::info!("{device} CHANNEL {channel} MOVE_ABSOLUTE_FROM_PARAMS (responded)");
device.decode(Units::distance_from_slice(&response[8..12]))
}