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 #98 from Tropix126/refactor/associated-constants
Browse files Browse the repository at this point in the history
refactor: move device constants into associated struct `impl`s
  • Loading branch information
Gavin-Niederman authored Mar 1, 2024
2 parents edcb299 + 342b6ba commit c610aa7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 40 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Before releasing:
- Renamed `AdiUltrasonic::value` to `AdiUltrasonic::distance` (**Breaking Change**) (#61).
- Renamed `AdiEncoder::value` to `AdiEncoder::position` (**Breaking Change**) (#61).
- Repurposed `AdiAnalogOut` as `AdiPwmOut` to correct match port output. (**Breaking Change**) (#90).
- Moved most device-related constants into their associated struct `impl` (**Breaking Change**) (#98).
- Renamed IMU_RESET_TIMEOUT to `InertialSensor::CALIBRATION_TIMEOUT` (**Breaking Change**) (#98).

### Removed

Expand Down
12 changes: 6 additions & 6 deletions packages/pros/src/devices/adi/gyro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ use pros_sys::{ext_adi_gyro_t, PROS_ERR, PROS_ERR_F};
use super::{AdiDevice, AdiDeviceType, AdiError, AdiPort};
use crate::error::bail_on;

/// The time it takes to calibrate an [`AdiGyro`].
///
/// The theoretical calibration time is 1024ms, but in practice this seemed to be the
/// actual time that it takes.
pub const GYRO_CALIBRATION_TIME: Duration = Duration::from_millis(1300);

/// ADI gyro device.
#[derive(Debug, Eq, PartialEq)]
pub struct AdiGyro {
Expand All @@ -21,6 +15,12 @@ pub struct AdiGyro {
}

impl AdiGyro {
/// The time it takes to calibrate an [`AdiGyro`].
///
/// The theoretical calibration time is 1024ms, but in practice this seemed to be the
/// actual time that it takes.
pub const CALIBRATION_TIME: Duration = Duration::from_millis(1300);

/// Create a new gyro from an [`AdiPort`].
///
/// If the given port has not previously been configured as a gyro, then this
Expand Down
34 changes: 18 additions & 16 deletions packages/pros/src/devices/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,12 @@ pub struct Screen {
current_line: i16,
}

/// The maximum number of lines that can be visible on the screen at once.
pub const SCREEN_MAX_VISIBLE_LINES: usize = 12;
/// The height of a single line of text on the screen.
pub const SCREEN_LINE_HEIGHT: i16 = 20;

/// The horizontal resolution of the display.
pub const SCREEN_HORIZONTAL_RESOLUTION: i16 = 480;
/// The vertical resolution of the writable part of the display.
pub const SCREEN_VERTICAL_RESOLUTION: i16 = 240;

impl core::fmt::Write for Screen {
fn write_str(&mut self, text: &str) -> core::fmt::Result {
for character in text.chars() {
if character == '\n' {
if self.current_line > (SCREEN_MAX_VISIBLE_LINES as i16 - 2) {
self.scroll(0, SCREEN_LINE_HEIGHT)
if self.current_line > (Self::MAX_VISIBLE_LINES as i16 - 2) {
self.scroll(0, Self::LINE_HEIGHT)
.map_err(|_| core::fmt::Error)?;
} else {
self.current_line += 1;
Expand Down Expand Up @@ -344,6 +334,18 @@ impl From<TouchState> for pros_sys::last_touch_e_t {
}

impl Screen {
/// The maximum number of lines that can be visible on the screen at once.
pub const MAX_VISIBLE_LINES: usize = 12;

/// The height of a single line of text on the screen.
pub const LINE_HEIGHT: i16 = 20;

/// The horizontal resolution of the display.
pub const HORIZONTAL_RESOLUTION: i16 = 480;

/// The vertical resolution of the writable part of the display.
pub const VERTICAL_RESOLUTION: i16 = 240;

/// Create a new screen.
///
/// # Safety
Expand Down Expand Up @@ -488,8 +490,8 @@ impl Screen {
let error_box_rect = Rect::new(
ERROR_BOX_MARGIN,
ERROR_BOX_MARGIN,
SCREEN_HORIZONTAL_RESOLUTION - ERROR_BOX_MARGIN,
SCREEN_VERTICAL_RESOLUTION - ERROR_BOX_MARGIN,
Self::HORIZONTAL_RESOLUTION - ERROR_BOX_MARGIN,
Self::VERTICAL_RESOLUTION - ERROR_BOX_MARGIN,
);

self.fill(&error_box_rect, Rgb::RED)?;
Expand All @@ -509,7 +511,7 @@ impl Screen {
buffer.as_str(),
TextPosition::Point(
ERROR_BOX_MARGIN + ERROR_BOX_PADDING,
ERROR_BOX_MARGIN + ERROR_BOX_PADDING + (line * SCREEN_LINE_HEIGHT),
ERROR_BOX_MARGIN + ERROR_BOX_PADDING + (line * Self::LINE_HEIGHT),
),
TextFormat::Small,
),
Expand All @@ -526,7 +528,7 @@ impl Screen {
buffer.as_str(),
TextPosition::Point(
ERROR_BOX_MARGIN + ERROR_BOX_PADDING,
ERROR_BOX_MARGIN + ERROR_BOX_PADDING + (line * SCREEN_LINE_HEIGHT),
ERROR_BOX_MARGIN + ERROR_BOX_PADDING + (line * Self::LINE_HEIGHT),
),
TextFormat::Small,
),
Expand Down
17 changes: 9 additions & 8 deletions packages/pros/src/devices/smart/imu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ use crate::{
time::Instant,
};

/// The timeout for the IMU to calibrate.
pub const IMU_RESET_TIMEOUT: Duration = Duration::from_secs(3);
/// The minimum data rate that you can set an IMU to.
pub const IMU_MIN_DATA_RATE: Duration = Duration::from_millis(5);

/// Represents a smart port configured as a V5 inertial sensor (IMU)
#[derive(Debug, Eq, PartialEq)]
pub struct InertialSensor {
port: SmartPort,
}

impl InertialSensor {
/// The timeout for the IMU to calibrate.
pub const CALIBRATION_TIMEOUT: Duration = Duration::from_secs(3);

/// The minimum data rate that you can set an IMU to.
pub const MIN_DATA_RATE: Duration = Duration::from_millis(5);

/// Create a new inertial sensor from a smart port index.
pub const fn new(port: SmartPort) -> Self {
Self { port }
Expand Down Expand Up @@ -237,10 +238,10 @@ impl InertialSensor {

/// Sets the update rate of the IMU.
///
/// This duration must be above [`IMU_MIN_DATA_RATE`] (5 milliseconds).
/// This duration must be above [`Self::MIN_DATA_RATE`] (5 milliseconds).
pub fn set_data_rate(&mut self, data_rate: Duration) -> Result<(), InertialError> {
unsafe {
let rate_ms = if data_rate > IMU_MIN_DATA_RATE {
let rate_ms = if data_rate > Self::MIN_DATA_RATE {
if let Ok(rate) = u32::try_from(data_rate.as_millis()) {
rate
} else {
Expand Down Expand Up @@ -431,7 +432,7 @@ impl core::future::Future for InertialCalibrateFuture {

if !is_calibrating {
return Poll::Ready(Ok(()));
} else if timestamp.elapsed() > IMU_RESET_TIMEOUT {
} else if timestamp.elapsed() > InertialSensor::CALIBRATION_TIMEOUT {
return Poll::Ready(Err(InertialError::CalibrationTimedOut));
}

Expand Down
21 changes: 11 additions & 10 deletions packages/pros/src/devices/smart/optical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ use snafu::Snafu;
use super::{SmartDevice, SmartDeviceType, SmartPort};
use crate::error::{bail_on, map_errno, PortError};

/// The smallest integration time you can set on an optical sensor.
pub const MIN_INTEGRATION_TIME: Duration = Duration::from_millis(3);
/// The largest integration time you can set on an optical sensor.
pub const MAX_INTEGRATION_TIME: Duration = Duration::from_millis(712);

/// The maximum value for the LED PWM.
pub const MAX_LED_PWM: u8 = 100;

/// Represents a smart port configured as a V5 optical sensor
#[derive(Debug, Eq, PartialEq)]
pub struct OpticalSensor {
Expand All @@ -24,6 +16,15 @@ pub struct OpticalSensor {
}

impl OpticalSensor {
/// The smallest integration time you can set on an optical sensor.
pub const MIN_INTEGRATION_TIME: Duration = Duration::from_millis(3);

/// The largest integration time you can set on an optical sensor.
pub const MAX_INTEGRATION_TIME: Duration = Duration::from_millis(712);

/// The maximum value for the LED PWM.
pub const MAX_LED_PWM: u8 = 100;

/// Creates a new inertial sensor from a smart port index.
///
/// Gesture detection features can be optionally enabled, allowing the use of [`Self::last_gesture_direction()`] and [`Self::last_gesture_direction()`].
Expand Down Expand Up @@ -54,7 +55,7 @@ impl OpticalSensor {

/// Sets the pwm value of the White LED. Valid values are in the range `0` `100`.
pub fn set_led_pwm(&mut self, value: u8) -> Result<(), OpticalError> {
if value > MAX_LED_PWM {
if value > Self::MAX_LED_PWM {
return Err(OpticalError::InvalidLedPwm);
}
unsafe {
Expand Down Expand Up @@ -86,7 +87,7 @@ impl OpticalSensor {
/// https://www.vexforum.com/t/v5-optical-sensor-refresh-rate/109632/9 for
/// more information.
pub fn set_integration_time(&mut self, time: Duration) -> Result<(), OpticalError> {
if time < MIN_INTEGRATION_TIME || time > MAX_INTEGRATION_TIME {
if time < Self::MIN_INTEGRATION_TIME || time > Self::MAX_INTEGRATION_TIME {
return Err(OpticalError::InvalidIntegrationTime);
}

Expand Down

0 comments on commit c610aa7

Please sign in to comment.