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

Commit

Permalink
refactor: InertialStatus bitflags
Browse files Browse the repository at this point in the history
  • Loading branch information
Tropix126 committed Mar 6, 2024
1 parent b6924b2 commit a88c9c4
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions packages/pros-devices/src/smart/imu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::{
time::Duration,
};

use bitflags::bitflags;
use pros_core::{
bail_on,
error::{take_errno, FromErrno, PortError},
Expand Down Expand Up @@ -56,8 +57,8 @@ impl InertialSensor {
}

/// Check if the Intertial Sensor is currently calibrating.
pub fn calibrating(&mut self) -> Result<bool, InertialError> {
Ok(self.status()?.calibrating())
pub fn is_calibrating(&mut self) -> Result<bool, InertialError> {
Ok(self.status()?.contains(InertialStatus::CALIBRATING))
}

/// Get the total number of degrees the Inertial Sensor has spun about the z-axis.
Expand Down Expand Up @@ -103,7 +104,11 @@ impl InertialSensor {

/// Read the inertial sensor's status code.
pub fn status(&self) -> Result<InertialStatus, InertialError> {
unsafe { pros_sys::imu_get_status(self.port.index()).try_into() }
let bits = bail_on!(pros_sys::E_IMU_STATUS_ERROR, unsafe {
pros_sys::motor_get_flags(self.port.index())
});

Ok(InertialStatus::from_bits_retain(bits))
}

/// Get a quaternion representing the Inertial Sensor’s orientation.
Expand Down Expand Up @@ -376,22 +381,12 @@ impl TryFrom<pros_sys::imu_raw_s> for InertialRaw {
}
}

/// Represents a status code returned by the Inertial Sensor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InertialStatus(pub u32);

impl InertialStatus {
/// Determine if the sensor is currently calibrating.
pub const fn calibrating(&self) -> bool {
self.0 & pros_sys::E_IMU_STATUS_CALIBRATING != 0
}
}

impl TryFrom<pros_sys::imu_status_e_t> for InertialStatus {
type Error = InertialError;

fn try_from(value: pros_sys::imu_status_e_t) -> Result<Self, Self::Error> {
Ok(Self(bail_on!(pros_sys::E_IMU_STATUS_ERROR, value)))
bitflags! {
/// The status bits returned by an [`InertialSensor`].
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct InertialStatus: u32 {
/// The sensor is currently calibrating.
const CALIBRATING = pros_sys::E_IMU_STATUS_CALIBRATING;
}
}

Expand Down

0 comments on commit a88c9c4

Please sign in to comment.