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

Commit

Permalink
fast forward
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin-Niederman committed Jan 1, 2024
2 parents c56359d + a5c5d5c commit 9654440
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 14 deletions.
3 changes: 2 additions & 1 deletion pros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pros"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
description = "Rust bindings for PROS"
keywords = ["PROS", "Robotics", "bindings"]
Expand All @@ -17,6 +17,7 @@ spin = "0.9.8"
pros-sys = { version = "0.4", path = "../pros-sys", features = ["xapi"] }
snafu = { version = "0.7.5", default-features = false, features = [
"rust_1_61",
"unstable-core-error",
] }
no_std_io = { version = "0.6.0", features = ["alloc"] }
futures = { version = "0.3.28", default-features = false, features = ["alloc"] }
Expand Down
1 change: 0 additions & 1 deletion pros/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ pub enum PortError {
))]
PortCannotBeConfigured,
}
impl core::error::Error for PortError {}

map_errno!(PortError {
ENXIO => Self::PortOutOfRange,
Expand Down
1 change: 0 additions & 1 deletion pros/src/lcd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@ pub enum LcdError {
#[snafu(display("LCD not initialized"))]
NotInitialized,
}
impl core::error::Error for LcdError {}
4 changes: 2 additions & 2 deletions pros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//!```rust
//! // Sync
//! use pros::prelude::*;
//!
//!
//! #[derive(Default)]
//! struct Robot;
//! impl SyncRobot for Robot {
Expand All @@ -42,7 +42,7 @@
//! }
//! sync_robot!(Robot);
//! ```
//!
//!
//! You may have noticed the `#[derive(Default)]` attribute on these Robot structs.
//! If you want to learn why, look at the docs for [`async_robot`] or [`sync_robot`].
Expand Down
1 change: 0 additions & 1 deletion pros/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ pub enum LinkError {
#[snafu(display("{source}"), context(false))]
Port { source: PortError },
}
impl core::error::Error for LinkError {}

map_errno! {
LinkError {
Expand Down
1 change: 0 additions & 1 deletion pros/src/motor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ pub enum MotorError {
#[snafu(display("{source}"), context(false))]
Port { source: PortError },
}
impl core::error::Error for MotorError {}

map_errno! {
MotorError {}
Expand Down
1 change: 0 additions & 1 deletion pros/src/sensors/gps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ pub enum GpsError {
#[snafu(display("{source}"), context(false))]
Port { source: PortError },
}
impl core::error::Error for GpsError {}

map_errno! {
GpsError {
Expand Down
1 change: 0 additions & 1 deletion pros/src/sensors/vision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ pub enum VisionError {
#[snafu(display("{source}"), context(false))]
Port { source: PortError },
}
impl core::error::Error for VisionError {}

map_errno! {
VisionError {
Expand Down
44 changes: 39 additions & 5 deletions pros/src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
pub mod local;

use core::hash::Hash;
use core::time::Duration;
use core::{future::Future, task::Poll};

use crate::async_runtime::executor::EXECUTOR;
Expand Down Expand Up @@ -272,14 +273,47 @@ map_errno! {
}
}

/// Blocks the current task for the given amount of time, if you are in an async function.
/// ## you probably don't want to use this.
/// This function will block the entire task, including the async executor!
/// Instead, you should use [`sleep`].
pub fn delay(duration: core::time::Duration) {
/// Blocks the current FreeRTOS task for the given amount of time.
///
/// ## Caveats
///
/// This function will block the entire task, preventing concurrent
/// execution of async code. When in an async context, it is recommended
/// to use [`sleep`] instead.
pub fn delay(duration: Duration) {
unsafe { pros_sys::delay(duration.as_millis() as u32) }
}

/// An interval that can be used to repeatedly run code at a given rate.
pub struct Interval {
last_unblock_time: u32,
}

impl Interval {
/// Creates a new interval. As time passes, the interval's actual delay
/// will become smaller so that the average rate is maintained.
pub fn start() -> Self {
Self {
last_unblock_time: unsafe { pros_sys::millis() },
}
}

/// Blocks the current FreeRTOS task until the interval has elapsed.
///
/// ## Caveats
///
/// This function will block the entire task, preventing concurrent
/// execution of async code. When in an async context, it is recommended
/// to an async-friendly equivalent instead.
pub fn delay(&mut self, delta: Duration) {
let delta = delta.as_millis() as u32;
unsafe {
// PROS handles loop overruns so there's no need to check for them here
pros_sys::task_delay_until((&mut self.last_unblock_time) as *mut _, delta);
}
}
}

/// A future that will complete after the given duration.
/// Sleep futures that are closer to completion are prioritized to improve accuracy.
pub struct SleepFuture {
Expand Down

0 comments on commit 9654440

Please sign in to comment.