Struct Controller

Source
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

Source

pub const UPDATE_INTERVAL: Duration

The update rate of the controller.

Source

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.

Source

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"),
        }
    }
}
Source

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
§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;
    }
}
Source

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");
    }
}
Source

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
§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));
}
Source

pub fn battery_level(&self) -> Result<i32, ControllerError>

Returns the controller’s battery level.

§Errors
§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;
    }
}
Source

pub fn flags(&self) -> Result<i32, ControllerError>

Returns the controller’s flags.

§Errors
Source

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
§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;
}
Source

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
§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(". -. -.");
}

Trait Implementations§

Source§

impl Debug for Controller

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Controller

Source§

fn eq(&self, other: &Controller) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Controller

Source§

impl StructuralPartialEq for Controller

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.