use pros_core::bail_on;
use pros_sys::{adi_potentiometer_type_e_t, ext_adi_potentiometer_t, PROS_ERR, PROS_ERR_F};
use super::{AdiDevice, AdiDeviceType, AdiError, AdiPort};
#[derive(Debug, Eq, PartialEq)]
pub struct AdiPotentiometer {
potentiometer_type: AdiPotentiometerType,
raw: ext_adi_potentiometer_t,
port: AdiPort,
}
impl AdiPotentiometer {
pub fn new(port: AdiPort, potentiometer_type: AdiPotentiometerType) -> Result<Self, AdiError> {
let raw = bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_potentiometer_init(
port.internal_expander_index(),
port.index(),
potentiometer_type.into(),
)
});
Ok(Self {
potentiometer_type,
raw,
port,
})
}
pub const fn potentiometer_type(&self) -> AdiPotentiometerType {
self.potentiometer_type
}
pub fn angle(&self) -> Result<f64, AdiError> {
Ok(bail_on!(PROS_ERR_F, unsafe {
pros_sys::ext_adi_potentiometer_get_angle(self.raw)
}) / 10.0)
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[repr(i32)]
pub enum AdiPotentiometerType {
PotentiometerEdr = pros_sys::E_ADI_POT_EDR,
PotentiometerV2 = pros_sys::E_ADI_POT_V2,
}
impl From<AdiPotentiometerType> for adi_potentiometer_type_e_t {
fn from(value: AdiPotentiometerType) -> Self {
value as _
}
}
impl AdiDevice for AdiPotentiometer {
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::AnalogIn
}
}