use pros_core::bail_on;
use pros_sys::PROS_ERR;
use super::{AdiDevice, AdiDeviceType, AdiError, AdiPort};
#[derive(Debug, Eq, PartialEq)]
pub struct AdiMotor {
port: AdiPort,
}
impl AdiMotor {
pub const fn new(port: AdiPort) -> Self {
Self { port }
}
pub fn set_output(&mut self, value: f32) -> Result<(), AdiError> {
self.set_raw_output((value * 127.0) as i8)
}
pub fn set_raw_output(&mut self, value: i8) -> Result<(), AdiError> {
bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_motor_set(
self.port.internal_expander_index(),
self.port.index(),
value,
)
});
Ok(())
}
pub fn output(&self) -> Result<f32, AdiError> {
Ok(self.raw_output()? as f32 / 127.0)
}
pub fn raw_output(&self) -> Result<i8, AdiError> {
Ok(bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_motor_get(self.port.internal_expander_index(), self.port.index())
}) as i8)
}
pub fn stop(&mut self) -> Result<(), AdiError> {
bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_motor_stop(self.port.internal_expander_index(), self.port.index())
});
Ok(())
}
}
impl AdiDevice for AdiMotor {
type PortIndexOutput = u8;
fn port_index(&self) -> Self::PortIndexOutput {
self.port.index()
}
fn expander_port_index(&self) -> Option<u8> {
self.port.expander_index()
}
fn device_type(&self) -> AdiDeviceType {
AdiDeviceType::LegacyPwm
}
}