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 #12 from pros-rs/state
Browse files Browse the repository at this point in the history
feat: add functions for checking robot state
  • Loading branch information
Gavin-Niederman authored Dec 23, 2023
2 parents 1079336 + 02c9d36 commit 68642ed
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pros-sys/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use core::ffi::*;

pub const NUM_V5_PORTS: usize = 21;
// v5 comp
pub const COMPETITION_DISABLED: i32 = 0b001;
pub const COMPETITION_AUTONOMOUS: i32 = 0b010;
pub const COMPETITION_CONNECTED: i32 = 0b100;
pub const COMPETITION_AUTONOMOUS: u8 = 1 << 0;
pub const COMPETITION_DISABLED: u8 = 1 << 1;
pub const COMPETITION_CONNECTED: u8 = 1 << 2;
extern "C" {
pub fn competition_get_status() -> u8;
pub fn competition_is_autonomous() -> bool;
pub fn competition_is_connected() -> bool;
pub fn competition_is_disabled() -> bool;
}
// controller
pub const E_CONTROLLER_MASTER: c_uint = 0;
Expand Down
19 changes: 19 additions & 0 deletions pros/src/battery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// Get the robot's battery capacity.
pub fn get_capacity() -> f64 {
unsafe { pros_sys::misc::battery_get_capacity() }
}

/// Get the electric current of the robot's battery.
pub fn get_current() -> i32 {
unsafe { pros_sys::misc::battery_get_current() }
}

/// Get the current temperature of the robot's battery.
pub fn get_temperature() -> f64 {
unsafe { pros_sys::misc::battery_get_temperature() }
}

/// Get the robot's battery voltage.
pub fn get_voltage() -> i32 {
unsafe { pros_sys::misc::battery_get_voltage() }
}
36 changes: 36 additions & 0 deletions pros/src/competition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// The current status of the robot, allowing checks to be made
/// for autonomous, disabled, and connected states.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CompetitionStatus(pub u8);

impl CompetitionStatus {
pub const fn autonomous(&self) -> bool {
self.0 & pros_sys::misc::COMPETITION_AUTONOMOUS != 0
}
pub const fn disabled(&self) -> bool {
self.0 & pros_sys::misc::COMPETITION_DISABLED != 0
}
pub const fn connected(&self) -> bool {
self.0 & pros_sys::misc::COMPETITION_CONNECTED != 0
}
}

/// Get the current status of the robot.
pub fn get_status() -> CompetitionStatus {
CompetitionStatus(unsafe { pros_sys::misc::competition_get_status() })
}

/// Check if the robot is in autonomous mode.
pub fn is_autonomous() -> bool {
unsafe { pros_sys::misc::competition_is_autonomous() }
}

/// Check if the robot is disabled.
pub fn is_disabled() -> bool {
unsafe { pros_sys::misc::competition_is_disabled() }
}

/// Check if the robot is connected to a VEX field or competition switch.
pub fn is_connected() -> bool {
unsafe { pros_sys::misc::competition_is_connected() }
}
3 changes: 3 additions & 0 deletions pros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ mod wasm_env;
#[macro_use]
pub mod lcd;
pub mod adi;
pub mod battery;
pub mod competition;
pub mod link;
pub mod lvgl;
pub mod usd;

pub use async_trait::async_trait;

Expand Down
4 changes: 4 additions & 0 deletions pros/src/usd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// Checks if an SD card is installed.
pub fn usd_installed() -> bool {
unsafe { pros_sys::misc::usd_is_installed() == 1 }
}

0 comments on commit 68642ed

Please sign in to comment.