pub struct Controller {
pub screen: ControllerScreen,
/* private fields */
}
Expand description
The basic type for a controller. Used to get the state of its joysticks and controllers.
Fields§
§screen: ControllerScreen
Controller Screen
Implementations§
Source§impl Controller
impl Controller
Sourcepub const UPDATE_INTERVAL: Duration
pub const UPDATE_INTERVAL: Duration
The update rate of the controller.
Sourcepub const unsafe fn new(id: ControllerId) -> Controller
pub const unsafe fn new(id: ControllerId) -> Controller
Create a new controller.
§Safety
Creating new Controller
s 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::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::CompetitionControl
error is returned if access to the controller data is being restricted by competition control. - A
ControllerError::Offline
error 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::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::Offline
error 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::Offline
error 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::Offline
error is returned if the controller is not connected.
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::Offline
error 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;
let _ = 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::Offline
error 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;
let _ = controller.try_rumble(". -. -.");
}