Skip to content

Commit

Permalink
feat: automatically terminate ui task on controller interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
zabackary committed Jan 23, 2025
1 parent 6eecd9a commit 55a34e5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/controller.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use vexide_devices::controller::Controller;

pub fn has_interaction(controller: &Controller) -> bool {
let state = controller.state().unwrap_or_default();
state.button_a.is_pressed()
|| state.button_b.is_pressed()
|| state.button_x.is_pressed()
|| state.button_y.is_pressed()
|| state.button_up.is_pressed()
|| state.button_down.is_pressed()
|| state.button_left.is_pressed()
|| state.button_right.is_pressed()
|| state.button_l1.is_pressed()
|| state.button_l2.is_pressed()
|| state.button_r1.is_pressed()
|| state.button_r2.is_pressed()
|| state.left_stick.x() > 0.05
|| state.left_stick.x() < -0.05
|| state.left_stick.y() > 0.05
|| state.left_stick.y() < -0.05
|| state.right_stick.x() > 0.05
|| state.right_stick.x() < -0.05
|| state.right_stick.y() > 0.05
|| state.right_stick.y() < -0.05
}
23 changes: 19 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

extern crate alloc;

mod controller;
#[cfg(feature = "ui")]
mod platform;

Expand Down Expand Up @@ -69,17 +70,31 @@ pub trait CompeteWithSelector: Sized {
/// See [`Compete::driver`] for more information.
async fn driver(&mut self);

/// Calibrates the gyro. Called when the user requests a calibration.
fn calibrate_gyro(&mut self) {}

/// Returns whether the gyro is currently calibrating.
fn is_gyro_calibrating(&self) -> bool {
false
}

/// Returns a list of diagnostics to display in the UI.
fn diagnostics(&self) -> Vec<(String, String)> {
Vec::new()
}

/// Runs when an autonomous routine is started.
fn autonomous_route_started(&mut self, _route: &dyn AutonRoutine<Self, Return = Self::Return>) {
}

/// Runs when an autonomous routine finishes.
fn autonomous_route_finished(&mut self, _return_value: Self::Return) {}

/// A reference to the controller, if available. This allows the UI task
/// to exit early if a controller interaction is detected.
fn controller(&self) -> Option<&vexide_devices::controller::Controller> {
None
}
}

struct SharedData<'a, T, R> {
Expand Down Expand Up @@ -162,9 +177,6 @@ where
})
.finish();

// TODO: If we haven't connected to the competition system yet, we should run the UI in a Future until we are.
// This will allow us to show the UI while waiting for the competition system to connect.

runtime.await;
}
}
Expand Down Expand Up @@ -261,7 +273,10 @@ where
last_diagnostics_refresh = Instant::now();
}

if is_driver && vexide_core::competition::is_connected() {
if is_driver
&& (vexide_core::competition::is_connected()
|| s.user.controller().is_some_and(controller::has_interaction))
{
break ControlFlow::Continue(());
}

Expand Down

0 comments on commit 55a34e5

Please sign in to comment.