use pros_core::{bail_on, error::PortError};
use pros_sys::PROS_ERR;
use super::{SmartDevice, SmartDeviceType, SmartPort};
use crate::position::Position;
#[derive(Debug, Eq, PartialEq)]
pub struct RotationSensor {
port: SmartPort,
pub reversed: bool,
}
impl RotationSensor {
pub fn new(port: SmartPort, reversed: bool) -> Result<Self, PortError> {
unsafe {
bail_on!(PROS_ERR, pros_sys::rotation_reset_position(port.index()));
if reversed {
bail_on!(
PROS_ERR,
pros_sys::rotation_set_reversed(port.index(), true)
);
}
}
Ok(Self { port, reversed })
}
pub fn zero(&mut self) -> Result<(), PortError> {
unsafe {
bail_on!(
PROS_ERR,
pros_sys::rotation_reset_position(self.port.index())
);
}
Ok(())
}
pub fn set_position(&mut self, position: Position) -> Result<(), PortError> {
unsafe {
bail_on!(
PROS_ERR,
pros_sys::rotation_set_position(
self.port.index(),
(position.into_counts() * 100) as _
)
);
}
Ok(())
}
pub fn set_reversed(&mut self, reversed: bool) -> Result<(), PortError> {
self.reversed = reversed;
unsafe {
bail_on!(
PROS_ERR,
pros_sys::rotation_set_reversed(self.port.index(), reversed)
);
}
Ok(())
}
pub fn reverse(&mut self) -> Result<(), PortError> {
self.set_reversed(!self.reversed)
}
pub fn position(&self) -> Result<Position, PortError> {
Ok(unsafe {
Position::from_degrees(
bail_on!(PROS_ERR, pros_sys::rotation_get_angle(self.port.index())) as f64 / 100.0,
)
})
}
}
impl SmartDevice for RotationSensor {
fn port_index(&self) -> u8 {
self.port.index()
}
fn device_type(&self) -> SmartDeviceType {
SmartDeviceType::Rotation
}
}