Controller

Struct Controller 

Source
pub struct Controller { /* private fields */ }
Available on crate feature devices only.
Expand description

V5 Controller

Implementations§

Source§

impl Controller

Source

pub const UPDATE_INTERVAL: Duration

The update rate of the controller.

Source

pub const MAX_COLUMNS: usize = 19usize

Maximum number of characters that can be drawn to a text line.

Source

pub const MAX_LINES: usize = 3usize

Number of available text lines on the controller before clearing the screen.

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::{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"),
        }
    }
}
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::{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");
    }
}
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 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
§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;
}
Source

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

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

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

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 line is greater than or equal to Self::MAX_LINES.
  • Panics if col is greater than or equal to Self::MAX_COLUMNS.
  • Panics if a NUL (0x00) character was found anywhere in the specified text.
§Errors
§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;
}
Source

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 line is greater than or equal to Self::MAX_LINES.
  • Panics if col is greater than or equal to Self::MAX_COLUMNS.
  • Panics if a NUL (0x00) character was found anywhere in the specified text.
§Errors
§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);
}
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;
    _ = 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;
    _ = 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§

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§

§

impl !Freeze for Controller

§

impl !RefUnwindSafe for Controller

§

impl Send for Controller

§

impl !Sync for Controller

§

impl Unpin for Controller

§

impl UnwindSafe for Controller

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.