pros_devices/adi/
potentiometer.rs1use 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)]
9pub struct AdiPotentiometer {
11 potentiometer_type: AdiPotentiometerType,
12 raw: ext_adi_potentiometer_t,
13 port: AdiPort,
14}
15
16impl AdiPotentiometer {
17 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 pub const fn potentiometer_type(&self) -> AdiPotentiometerType {
36 self.potentiometer_type
37 }
38
39 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)]
54pub enum AdiPotentiometerType {
56 PotentiometerEdr = pros_sys::E_ADI_POT_EDR,
58 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}