Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from pros-rs/refactor/adi
Browse files Browse the repository at this point in the history
fix/refactor: make `vex_devices` build
  • Loading branch information
Gavin-Niederman authored Apr 1, 2024
2 parents 3be5296 + 75dd8e9 commit 81c5495
Show file tree
Hide file tree
Showing 20 changed files with 226 additions and 924 deletions.
2 changes: 1 addition & 1 deletion packages/vex-devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ authors = [
[dependencies]
pros-core = { version = "0.1.0", path = "../pros-core" }
pros-sys = { path = "../pros-sys", version = "0.7.0", features = ["xapi"] }
vex-sdk = { version = "0.3.0" }
vex-sdk = { version = "0.5.0" }
snafu = { version = "0.8.0", default-features = false, features = [
"rust_1_61",
"unstable-core-error",
Expand Down
83 changes: 8 additions & 75 deletions packages/vex-devices/src/adi/analog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
//! Analog-to-Digital Converter (ADC) in the V5 brain. The brain measures analog input
//! using 12-bit values ranging from 0 (0V) to 4095 (5V).
use pros_core::bail_on;
use pros_sys::PROS_ERR;

use super::{AdiDevice, AdiDeviceType, AdiError, AdiPort};
use vex_sdk::vexDeviceAdiValueGet;

/// Generic analog input ADI device.
#[derive(Debug, Eq, PartialEq)]
Expand All @@ -21,50 +19,24 @@ pub struct AdiAnalogIn {

impl AdiAnalogIn {
/// Create a analog input from an ADI port.
pub fn new(port: AdiPort) -> Result<Self, AdiError> {
bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_port_set_config(
port.internal_expander_index(),
port.index(),
pros_sys::E_ADI_ANALOG_IN,
)
});
pub fn new(mut port: AdiPort) -> Result<Self, AdiError> {
port.configure(AdiDeviceType::AnalogIn)?;

Ok(Self { port })
}

/// Calibrates the analog sensor on the specified channel.
///
/// This method assumes that the true sensor value is
/// not actively changing at this time and computes an average
/// from approximately 500 samples, 1 ms apart, for a 0.5 s period of calibration.
///
/// The average value thus calculated is returned and stored for later calls
/// to the value_calibrated and value_calibrated_hr functions.
///
/// These functions will return the difference between this value and the current
/// sensor value when called.
pub fn calibrate(&mut self) -> Result<(), AdiError> {
bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_analog_calibrate(
self.port.internal_expander_index(),
self.port.index(),
)
});

Ok(())
}

/// Reads an analog input channel and returns the 12-bit value.
///
/// # Sensor Compatibility
///
/// The value returned is undefined if the analog pin has been switched to a different mode.
/// The meaning of the returned value varies depending on the sensor attached.
pub fn value(&self) -> Result<u16, AdiError> {
Ok(bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_analog_read(self.port.internal_expander_index(), self.port.index())
}) as u16)
self.port.validate_expander()?;

Ok(unsafe {
vexDeviceAdiValueGet(self.port.device_handle(), self.port.internal_index())
} as u16)
}

/// Reads an analog input channel and returns the calculated voltage input (0-5V).
Expand All @@ -81,45 +53,6 @@ impl AdiAnalogIn {
pub fn voltage(&self) -> Result<f64, AdiError> {
Ok(self.value()? as f64 / 4095.0 * 5.0)
}

/// Reads the calibrated value of an analog input channel.
///
/// The [`Self::calibrate`] function must be run first on that channel.
///
/// This function is inappropriate for sensor values intended for integration,
/// as round-off error can accumulate causing drift over time.
/// Use [`Self::high_precision_calibrated_value`] instead.
pub fn calibrated_value(&self) -> Result<i16, AdiError> {
Ok(bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_analog_read_calibrated(
self.port.internal_expander_index(),
self.port.index(),
)
}) as i16)
}

/// Reads the calibrated value of an analog input channel with enhanced precision.
///
/// The calibrate function must be run first.
///
/// This is intended for integrated sensor values such as gyros and accelerometers
/// to reduce drift due to round-off, and should not be used on a sensor such as a
/// line tracker or potentiometer.
///
/// The value returned actually has 16 bits of "precision",
/// even though the ADC only reads 12 bits,
/// so that errors induced by the average value being
/// between two values come out in the wash when integrated over time.
///
/// Think of the value as the true value times 16.
pub fn high_precision_calibrated_value(&self) -> Result<i16, AdiError> {
Ok(bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_analog_read_calibrated_HR(
self.port.internal_expander_index(),
self.port.index(),
)
}) as i16)
}
}

impl AdiDevice for AdiAnalogIn {
Expand Down
45 changes: 17 additions & 28 deletions packages/vex-devices/src/adi/digital.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Digital input and output ADI devices
use pros_core::bail_on;
use pros_sys::PROS_ERR;
use vex_sdk::{vexDeviceAdiValueGet, vexDeviceAdiValueSet};

use super::{AdiDevice, AdiDeviceType, AdiError, AdiPort};

Expand Down Expand Up @@ -61,23 +60,19 @@ pub struct AdiDigitalIn {

impl AdiDigitalIn {
/// Create a digital input from an ADI port.
pub fn new(port: AdiPort) -> Result<Self, AdiError> {
bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_port_set_config(
port.internal_expander_index(),
port.index(),
pros_sys::E_ADI_DIGITAL_IN,
)
});
pub fn new(mut port: AdiPort) -> Result<Self, AdiError> {
port.configure(AdiDeviceType::DigitalIn)?;

Ok(Self { port })
}

/// Gets the current logic level of a digital input pin.
pub fn level(&self) -> Result<LogicLevel, AdiError> {
let value = bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_digital_read(self.port.internal_expander_index(), self.port.index())
}) != 0;
self.port.validate_expander()?;

let value =
unsafe { vexDeviceAdiValueGet(self.port.device_handle(), self.port.internal_index()) }
!= 0;

Ok(match value {
true => LogicLevel::High,
Expand Down Expand Up @@ -120,27 +115,21 @@ pub struct AdiDigitalOut {

impl AdiDigitalOut {
/// Create a digital output from an [`AdiPort`].
pub fn new(port: AdiPort) -> Result<Self, AdiError> {
bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_port_set_config(
port.internal_expander_index(),
port.index(),
pros_sys::E_ADI_DIGITAL_OUT,
)
});
pub fn new(mut port: AdiPort) -> Result<Self, AdiError> {
port.configure(AdiDeviceType::DigitalOut)?;

Ok(Self { port })
}

/// Sets the digital logic level (high or low) of a pin.
pub fn set_level(&mut self, level: LogicLevel) -> Result<(), AdiError> {
bail_on!(PROS_ERR, unsafe {
pros_sys::ext_adi_digital_write(
self.port.internal_expander_index(),
self.port.index(),
level.is_high(),
)
});
unsafe {
vexDeviceAdiValueSet(
self.port.device_handle(),
self.port.internal_index(),
level.is_high() as i32,
);
}

Ok(())
}
Expand Down
74 changes: 0 additions & 74 deletions packages/vex-devices/src/adi/encoder.rs

This file was deleted.

65 changes: 0 additions & 65 deletions packages/vex-devices/src/adi/gyro.rs

This file was deleted.

Loading

0 comments on commit 81c5495

Please sign in to comment.