pub struct Controller { /* private fields */ }devices only.Expand description
V5 Controller
Implementations§
Source§impl Controller
impl Controller
Sourcepub const UPDATE_INTERVAL: Duration
pub const UPDATE_INTERVAL: Duration
The update rate of the controller.
Sourcepub const MAX_COLUMNS: usize = 19usize
pub const MAX_COLUMNS: usize = 19usize
Maximum number of characters that can be drawn to a text line.
Sourcepub const MAX_LINES: usize = 3usize
pub const MAX_LINES: usize = 3usize
Number of available text lines on the controller before clearing the screen.
Sourcepub const unsafe fn new(id: ControllerId) -> Controller
pub const unsafe fn new(id: ControllerId) -> Controller
Create a new controller.
§Safety
Creating new Controllers is inherently unsafe due to the possibility of constructing
more than one screen at once allowing multiple mutable references to the same
hardware device. Prefer using Peripherals to register
devices if possible.
Sourcepub const fn id(&self) -> ControllerId
pub const fn id(&self) -> ControllerId
Returns the identifier of this controller.
§Examples
Perform a different action based on the controller ID.
use vexide::{controller::ControllerId, prelude::*};
fn print_a_pressed(controller: &Controller) {
let state = controller.state().unwrap_or_default();
if state.button_a.is_pressed() {
match controller.id() {
ControllerId::Primary => println!("Primary Controller A Pressed"),
ControllerId::Partner => println!("Partner Controller A Pressed"),
}
}
}Sourcepub fn state(&self) -> Result<ControllerState, ControllerError>
pub fn state(&self) -> Result<ControllerState, ControllerError>
Returns the current state of all buttons and joysticks on the controller.
§Note
If the current competition mode is not driver control, this function will error.
§Errors
- A
ControllerError::CompetitionControlerror is returned if access to the controller data is being restricted by competition control. - A
ControllerError::Offlineerror is returned if the controller is not connected.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let controller = peripherals.primary_controller;
loop {
let state = controller.state().unwrap_or_default();
println!("Left Stick X: {}", state.left_stick.x());
if state.button_a.is_now_pressed() {
println!("Button A was just pressed!");
}
if state.button_x.is_pressed() {
println!("Button X is pressed!");
}
if state.button_b.is_released() {
println!("Button B is released!");
}
sleep(Controller::UPDATE_INTERVAL).await;
}
}Sourcepub fn connection(&self) -> ControllerConnection
pub fn connection(&self) -> ControllerConnection
Returns the controller’s connection type.
§Examples
Print less information over a slow and unreliable VEXnet connection:
use vexide::{controller::ControllerConnection, prelude::*};
#[vexide::main]
async fn main(peripherals: Peripherals) {
let controller = peripherals.primary_controller;
if controller.connection() != ControllerConnection::VexNet {
println!("A big info dump");
}
}Sourcepub fn battery_capacity(&self) -> Result<f64, ControllerError>
pub fn battery_capacity(&self) -> Result<f64, ControllerError>
Returns the controller’s battery capacity as an f64 in the interval [0.0, 1.0].
§Errors
- A
ControllerError::Offlineerror is returned if the controller is not connected.
§Examples
Print the controller’s battery capacity:
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let controller = peripherals.primary_controller;
println!(
"Controller battery capacity: {}",
controller.battery_capacity().unwrap_or(0.0)
);
}Sourcepub fn battery_level(&self) -> Result<i32, ControllerError>
pub fn battery_level(&self) -> Result<i32, ControllerError>
Returns the controller’s battery level.
§Errors
- A
ControllerError::Offlineerror is returned if the controller is not connected.
§Examples
Print a warning if the controller battery is low:
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let controller = peripherals.primary_controller;
loop {
// If the controller isn't connected, it may as well be dead.
let battery_level = controller.battery_level().unwrap_or(0);
if battery_level < 10 {
println!("WARNING: Controller battery is low!");
}
sleep(Controller::UPDATE_INTERVAL).await;
}
}Sourcepub fn flags(&self) -> Result<i32, ControllerError>
pub fn flags(&self) -> Result<i32, ControllerError>
Returns the controller’s flags.
§Errors
- A
ControllerError::Offlineerror is returned if the controller is not connected.
Sourcepub fn clear_line(&mut self, line: u8) -> ControllerScreenWriteFuture<'_> ⓘ
pub fn clear_line(&mut self, line: u8) -> ControllerScreenWriteFuture<'_> ⓘ
Clears the contents of a specific text line, waiting until the controller successfully clears the line.
Lines are 1-indexed.
Controller text setting is a slow process, so calls to this function at intervals faster than 10ms on wired connection or 50ms over VEXnet will take longer to complete.
§Errors
- A
ControllerError::Offlineerror is returned if the controller is not connected.
§Examples
use std::time::Duration;
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut controller = peripherals.primary_controller;
// Write to line 1
_ = controller.set_text("Hello, world!", 1, 1).await;
sleep(Duration::from_millis(500)).await;
// Clear line 1
_ = controller.clear_line(1).await;
}Sourcepub fn try_clear_line(&mut self, line: u8) -> Result<(), ControllerError>
pub fn try_clear_line(&mut self, line: u8) -> Result<(), ControllerError>
Attempts to clear the contents of a specific text line.
Lines are 1-indexed.
Unlike clear_line this function will fail if the
controller screen is busy.
Controller text setting is a slow process, so updates faster than 10ms when on a wired connection or 50ms over VEXnet will not be applied to the controller.
§Errors
- A
ControllerError::Offlineerror is returned if the controller is not connected. - A
ControllerError::WriteBusyerror is returned if a screen write occurred too quickly after the previous write attempt.
§Examples
use std::time::Duration;
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut controller = peripherals.primary_controller;
// Write to line 1
_ = controller.set_text("Hello, world!", 1, 1).await;
sleep(Duration::from_millis(500)).await;
// Clear line 1
_ = controller.try_clear_line(1);
}Sourcepub fn clear_screen(&mut self) -> ControllerScreenWriteFuture<'_> ⓘ
pub fn clear_screen(&mut self) -> ControllerScreenWriteFuture<'_> ⓘ
Clears the whole screen, waiting until the controller successfully clears the screen.
This includes the default widget displayed by the controller if it hasn’t already been cleared.
Controller text setting is a slow process, so calls to this function at intervals faster than 10ms on wired connection or 50ms over VEXnet will take longer to complete.
§Errors
- A
ControllerError::Offlineerror is returned if the controller is not connected.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut controller = peripherals.primary_controller;
// Remove the default widget on the controller screen that displays match time.
_ = controller.clear_screen().await;
}Sourcepub fn try_clear_screen(&mut self) -> Result<(), ControllerError>
pub fn try_clear_screen(&mut self) -> Result<(), ControllerError>
Clears the whole screen, including the default widget displayed by the controller if
it hasn’t already been cleared.
Unlike clear_screen this function will fail if the
controller screen is busy.
Controller text setting is a slow process, so updates faster than 10ms when on a wired connection or 50ms over VEXnet will not be applied to the controller.
§Errors
- A
ControllerError::Offlineerror is returned if the controller is not connected. - A
ControllerError::WriteBusyerror is returned if a screen write occurred too quickly after the previous write attempt.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut controller = peripherals.primary_controller;
// Remove the default widget on the controller screen that displays match time.
_ = controller.try_clear_screen();
}Sourcepub fn set_text(
&mut self,
text: impl AsRef<str>,
line: u8,
col: u8,
) -> ControllerScreenWriteFuture<'_> ⓘ
pub fn set_text( &mut self, text: impl AsRef<str>, line: u8, col: u8, ) -> ControllerScreenWriteFuture<'_> ⓘ
Set the text contents at a specific row/column offset, waiting until the controller successfully writes the text.
Both lines and columns are 1-indexed.
Controller text setting is a slow process, so calls to this function at intervals faster than 10ms on wired connection or 50ms over VEXnet will take longer to complete.
§Panics
- Panics if
lineis greater than or equal toSelf::MAX_LINES. - Panics if
colis greater than or equal toSelf::MAX_COLUMNS. - Panics if a NUL (0x00) character was found anywhere in the specified text.
§Errors
- A
ControllerError::Offlineerror is returned if the controller is not connected.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut controller = peripherals.primary_controller;
_ = controller.set_text("Hello, world!", 1, 1).await;
_ = controller.set_text("Hello, world!", 2, 1).await;
}Sourcepub fn try_set_text(
&mut self,
text: impl AsRef<str>,
line: u8,
column: u8,
) -> Result<(), ControllerError>
pub fn try_set_text( &mut self, text: impl AsRef<str>, line: u8, column: u8, ) -> Result<(), ControllerError>
Set the text contents at a specific row/column offset.
Both lines and columns are 1-indexed.
Unlike set_text this function will fail if the controller
screen is busy.
Controller text setting is a slow process, so updates faster than 10ms when on a wired connection or 50ms over VEXnet will not be applied to the controller.
§Panics
- Panics if
lineis greater than or equal toSelf::MAX_LINES. - Panics if
colis greater than or equal toSelf::MAX_COLUMNS. - Panics if a NUL (0x00) character was found anywhere in the specified text.
§Errors
- A
ControllerError::Offlineerror is returned if the controller is not connected. - A
ControllerError::WriteBusyerror is returned if a screen write occurred too quickly after the previous write attempt.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut controller = peripherals.primary_controller;
_ = controller.try_set_text("Hello, world!", 1, 1);
}Sourcepub fn rumble(
&mut self,
pattern: impl AsRef<str>,
) -> ControllerScreenWriteFuture<'_> ⓘ
pub fn rumble( &mut self, pattern: impl AsRef<str>, ) -> ControllerScreenWriteFuture<'_> ⓘ
Send a rumble pattern to the controller’s vibration motor.
This function takes a string consisting of the characters ‘.’, ‘-’, and ’ ’, where dots are short rumbles, dashes are long rumbles, and spaces are pauses. Maximum supported length is 8 characters.
§Errors
- A
ControllerError::Offlineerror is returned if the controller is not connected.
§Panics
- Panics if a NUL (0x00) character was found anywhere in the specified text.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut controller = peripherals.primary_controller;
_ = controller.rumble(". -. -.").await;
}Sourcepub fn try_rumble(
&mut self,
pattern: impl AsRef<str>,
) -> Result<(), ControllerError>
pub fn try_rumble( &mut self, pattern: impl AsRef<str>, ) -> Result<(), ControllerError>
Send a rumble pattern to the controller’s vibration motor
Unlike rumble this function will fail if the controller screen is
busy.
This function takes a string consisting of the characters ‘.’, ‘-’, and ’ ’, where dots are short rumbles, dashes are long rumbles, and spaces are pauses. Maximum supported length is 8 characters.
§Errors
- A
ControllerError::Offlineerror is returned if the controller is not connected.
§Panics
- Panics if a NUL (0x00) character was found anywhere in the specified text.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut controller = peripherals.primary_controller;
_ = controller.try_rumble(". -. -.");
}