use core::time::Duration;
use pros_core::bail_on;
use pros_sys::{ext_adi_gyro_t, PROS_ERR, PROS_ERR_F};
use super::{AdiDevice, AdiDeviceType, AdiError, AdiPort};
#[derive(Debug, Eq, PartialEq)]
pub struct AdiGyro {
raw: ext_adi_gyro_t,
port: AdiPort,
}
impl AdiGyro {
pub const CALIBRATION_TIME: Duration = Duration::from_millis(1300);
pub fn new(port: AdiPort, multiplier: f64) -> Result<Self, AdiError> {
let raw = bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_gyro_init(port.internal_expander_index(), port.index(), multiplier)
});
Ok(Self { raw, port })
}
pub fn angle(&self) -> Result<f64, AdiError> {
Ok(bail_on!(PROS_ERR_F, unsafe { pros_sys::ext_adi_gyro_get(self.raw) }) / 10.0)
}
pub fn zero(&mut self) -> Result<(), AdiError> {
bail_on!(PROS_ERR, unsafe { pros_sys::ext_adi_gyro_reset(self.raw) });
Ok(())
}
}
impl AdiDevice for AdiGyro {
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::LegacyGyro
}
}