vex_sdk/
device.rs

1//! V5 Smart Devices
2
3use core::ffi::{c_double, c_int};
4
5use crate::map_jump_table;
6
7/// The max number of internal port indicies that could theoretically exist in VEXos.
8///
9/// This serves as the upper limit for the number of internal ports that will be added
10/// to VEXos and is thus a somewhatsafe value to set as a buffer length for functions
11/// such as [`vexDeviceGetStatus`].
12pub const V5_MAX_DEVICE_PORTS: usize = 32;
13
14/// Handle to an opaque [`V5_Device`].
15#[allow(non_camel_case_types)]
16pub type V5_DeviceT = *mut V5_Device;
17
18/// A device plugged into a smart port.
19pub type V5_Device = *mut core::ffi::c_void;
20
21#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
22#[repr(transparent)]
23pub struct V5_DeviceType(pub core::ffi::c_uchar);
24
25impl V5_DeviceType {
26    /// No device connected
27    pub const kDeviceTypeNoSensor: Self = Self(0);
28
29    /// V5 Smart Motor
30    pub const kDeviceTypeMotorSensor: Self = Self(2);
31
32    /// Unknown use (possibly unreleased hardware)
33    pub const kDeviceTypeLedSensor: Self = Self(3);
34
35    /// Rotation Sensor
36    pub const kDeviceTypeAbsEncSensor: Self = Self(4);
37
38    /// V5 Motor CR (unknown use)
39    pub const kDeviceTypeCrMotorSensor: Self = Self(5);
40
41    /// Inertial Sensor
42    pub const kDeviceTypeImuSensor: Self = Self(6);
43
44    /// Distance Sensor
45    pub const kDeviceTypeDistanceSensor: Self = Self(7);
46
47    /// Radio
48    pub const kDeviceTypeRadioSensor: Self = Self(8);
49
50    /// Master Controller
51    pub const kDeviceTypeTetherSensor: Self = Self(9);
52
53    /// Brain
54    pub const kDeviceTypeBrainSensor: Self = Self(10);
55
56    /// Vision Sensor
57    pub const kDeviceTypeVisionSensor: Self = Self(11);
58
59    /// ADI
60    pub const kDeviceTypeAdiSensor: Self = Self(12);
61
62    /// Partner Controller
63    pub const kDeviceTypeRes1Sensor: Self = Self(13);
64
65    /// Battery
66    pub const kDeviceTypeRes2Sensor: Self = Self(14);
67
68    /// Solenoid (unknown use)
69    pub const kDeviceTypeRes3Sensor: Self = Self(15);
70
71    /// Optical Sensor
72    pub const kDeviceTypeOpticalSensor: Self = Self(16);
73
74    /// Electromagnet
75    pub const kDeviceTypeMagnetSensor: Self = Self(17);
76
77    /// GPS
78    pub const kDeviceTypeGpsSensor: Self = Self(20);
79
80    /// AI Stereo Camera
81    pub const kDeviceTypeAicameraSensor: Self = Self(26);
82
83    /// CTE Workcell Light Tower
84    pub const kDeviceTypeLightTowerSensor: Self = Self(27);
85
86    /// CTE Workcell Arm
87    pub const kDeviceTypeArmDevice: Self = Self(28);
88
89    /// AI Vision Sensor
90    pub const kDeviceTypeAiVisionSensor: Self = Self(29);
91
92    /// CTE Workcell Pneumatics
93    pub const kDeviceTypePneumaticSensor: Self = Self(30);
94
95    // All of these are probably just unreleased or beta hardware...
96    pub const kDeviceTypeBumperSensor: Self = Self(0x40);
97    pub const kDeviceTypeGyroSensor: Self = Self(0x46);
98    pub const kDeviceTypeSonarSensor: Self = Self(0x47);
99    pub const kDeviceTypeGenericSensor: Self = Self(128);
100
101    /// Generic Serial
102    pub const kDeviceTypeGenericSerial: Self = Self(129);
103
104    /// Unknown use
105    pub const kDeviceTypeUndefinedSensor: Self = Self(255);
106}
107
108map_jump_table! {
109    0x190 =>
110        /// Get the number of device ports currently present on this system.
111        ///
112        /// As of VEXos 1.1.4, this number is the constant `23`.
113        pub fn vexDevicesGetNumber() -> u32,
114    0x194 =>
115        /// Get the number of devices of a specific type plugged into the brain.
116        pub fn vexDevicesGetNumberByType(device_type: V5_DeviceType) -> u32,
117    0x198 =>
118        /// Get a buffer of all devices on the brain.
119        pub fn vexDevicesGet() -> V5_DeviceT,
120    0x19c =>
121        /// Get a handle to a device plugged into the specified port index.
122        pub fn vexDeviceGetByIndex(index: u32) -> V5_DeviceT,
123    0x1a0 =>
124        /// Get a list of device types plugged into the brain.
125        ///
126        /// Returns -1 if a null pointer is passed, otherwise the number of devices
127        /// that were written to the buffer.
128        ///
129        /// The length of the buffer should be at most [`V5_MAX_DEVICE_PORTS`].
130        pub fn vexDeviceGetStatus(devices: *mut V5_DeviceType) -> i32,
131    0x1b0 =>
132        /// Get the timestamp recorded by a device's internal clock.
133        pub fn vexDeviceGetTimestamp(device: V5_DeviceT) -> u32,
134    0x2a8 =>
135        pub fn vexDeviceGenericValueGet(device: V5_DeviceT) -> c_double,
136    0x1b4 => pub fn vexDeviceButtonStateGet() -> c_int,
137}