Skip to main content

pros_devices/adi/
potentiometer.rs

1//! ADI Potentiometer device.
2
3use pros_core::bail_on;
4use pros_sys::{adi_potentiometer_type_e_t, ext_adi_potentiometer_t, PROS_ERR, PROS_ERR_F};
5
6use super::{AdiDevice, AdiDeviceType, AdiError, AdiPort};
7
8#[derive(Debug, Eq, PartialEq)]
9/// Analog potentiometer ADI device.
10pub struct AdiPotentiometer {
11    potentiometer_type: AdiPotentiometerType,
12    raw: ext_adi_potentiometer_t,
13    port: AdiPort,
14}
15
16impl AdiPotentiometer {
17    /// Create a new potentiometer from an [`AdiPort`].
18    pub fn new(port: AdiPort, potentiometer_type: AdiPotentiometerType) -> Result<Self, AdiError> {
19        let raw = bail_on!(PROS_ERR, unsafe {
20            pros_sys::ext_adi_potentiometer_init(
21                port.internal_expander_index(),
22                port.index(),
23                potentiometer_type.into(),
24            )
25        });
26
27        Ok(Self {
28            potentiometer_type,
29            raw,
30            port,
31        })
32    }
33
34    /// Get the type of ADI potentiometer device.
35    pub const fn potentiometer_type(&self) -> AdiPotentiometerType {
36        self.potentiometer_type
37    }
38
39    /// Gets the current potentiometer angle in degrees.
40    ///
41    /// The original potentiometer rotates 250 degrees
42    /// thus returning an angle between 0-250 degrees.
43    /// Potentiometer V2 rotates 330 degrees
44    /// thus returning an angle between 0-330 degrees.
45    pub fn angle(&self) -> Result<f64, AdiError> {
46        Ok(bail_on!(PROS_ERR_F, unsafe {
47            pros_sys::ext_adi_potentiometer_get_angle(self.raw)
48        }) / 10.0)
49    }
50}
51
52#[derive(Debug, Clone, Copy, Eq, PartialEq)]
53#[repr(i32)]
54/// The type of potentiometer device.
55pub enum AdiPotentiometerType {
56    /// EDR potentiometer.
57    PotentiometerEdr = pros_sys::E_ADI_POT_EDR,
58    /// V2 potentiometer.
59    PotentiometerV2 = pros_sys::E_ADI_POT_V2,
60}
61
62impl From<AdiPotentiometerType> for adi_potentiometer_type_e_t {
63    fn from(value: AdiPotentiometerType) -> Self {
64        value as _
65    }
66}
67
68impl AdiDevice for AdiPotentiometer {
69    type PortIndexOutput = u8;
70
71    fn port_index(&self) -> Self::PortIndexOutput {
72        self.port.index()
73    }
74
75    fn expander_port_index(&self) -> Option<u8> {
76        self.port.expander_index()
77    }
78
79    fn device_type(&self) -> AdiDeviceType {
80        AdiDeviceType::AnalogIn
81    }
82}