pub mod distance;
pub mod expander;
pub mod gps;
pub mod imu;
pub mod link;
pub mod motor;
pub mod optical;
pub mod rotation;
pub mod vision;
use core::fmt;
pub use distance::DistanceSensor;
pub use expander::AdiExpander;
pub use gps::GpsSensor;
pub use imu::InertialSensor;
pub use link::{Link, RxLink, TxLink};
pub use motor::Motor;
pub use optical::OpticalSensor;
use pros_core::{bail_on, error::PortError};
pub use rotation::RotationSensor;
pub use vision::VisionSensor;
pub trait SmartDevice {
fn port_index(&self) -> u8;
fn device_type(&self) -> SmartDeviceType;
fn port_connected(&self) -> bool {
let plugged_type_result: Result<SmartDeviceType, _> =
unsafe { pros_sys::apix::registry_get_plugged_type(self.port_index() - 1).try_into() };
if let Ok(plugged_type) = plugged_type_result {
plugged_type == self.device_type()
} else {
false
}
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct SmartPort {
index: u8,
}
impl SmartPort {
pub const unsafe fn new(index: u8) -> Self {
Self { index }
}
pub const fn index(&self) -> u8 {
self.index
}
pub fn connected_type(&self) -> Result<SmartDeviceType, PortError> {
unsafe { pros_sys::apix::registry_get_plugged_type(self.index() - 1).try_into() }
}
pub fn configured_type(&self) -> Result<SmartDeviceType, PortError> {
unsafe { pros_sys::apix::registry_get_bound_type(self.index() - 1).try_into() }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum SmartDeviceType {
None = pros_sys::apix::E_DEVICE_NONE,
Motor = pros_sys::apix::E_DEVICE_MOTOR,
Rotation = pros_sys::apix::E_DEVICE_ROTATION,
Imu = pros_sys::apix::E_DEVICE_IMU,
Distance = pros_sys::apix::E_DEVICE_DISTANCE,
Vision = pros_sys::apix::E_DEVICE_VISION,
Optical = pros_sys::apix::E_DEVICE_OPTICAL,
Gps = pros_sys::apix::E_DEVICE_GPS,
Radio = pros_sys::apix::E_DEVICE_RADIO,
Adi = pros_sys::apix::E_DEVICE_ADI,
Serial = pros_sys::apix::E_DEVICE_SERIAL,
}
impl TryFrom<pros_sys::apix::v5_device_e_t> for SmartDeviceType {
type Error = PortError;
fn try_from(value: pros_sys::apix::v5_device_e_t) -> Result<Self, Self::Error> {
bail_on!(pros_sys::apix::E_DEVICE_UNDEFINED, value);
Ok(match value {
pros_sys::apix::E_DEVICE_NONE => Self::None,
pros_sys::apix::E_DEVICE_MOTOR => Self::Motor,
pros_sys::apix::E_DEVICE_ROTATION => Self::Rotation,
pros_sys::apix::E_DEVICE_IMU => Self::Imu,
pros_sys::apix::E_DEVICE_DISTANCE => Self::Distance,
pros_sys::apix::E_DEVICE_VISION => Self::Vision,
pros_sys::apix::E_DEVICE_OPTICAL => Self::Optical,
pros_sys::apix::E_DEVICE_RADIO => Self::Radio,
pros_sys::apix::E_DEVICE_ADI => Self::Adi,
pros_sys::apix::E_DEVICE_SERIAL => Self::Serial,
_ => unreachable!(),
})
}
}
impl From<SmartDeviceType> for pros_sys::apix::v5_device_e_t {
fn from(value: SmartDeviceType) -> Self {
value as _
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SmartDeviceTimestamp(pub u32);
impl fmt::Debug for SmartDeviceTimestamp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}