Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
feat(controller): allow checking individual buttons/axis
Browse files Browse the repository at this point in the history
  • Loading branch information
doinkythederp committed Dec 25, 2023
1 parent a5c5d5c commit b294a72
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion pros/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@ impl ControllerLine {
}
}

/// A digital channel (button) on the VEX controller.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum ControllerButton {
A = pros_sys::E_CONTROLLER_DIGITAL_A,
B = pros_sys::E_CONTROLLER_DIGITAL_B,
X = pros_sys::E_CONTROLLER_DIGITAL_X,
Y = pros_sys::E_CONTROLLER_DIGITAL_Y,
Up = pros_sys::E_CONTROLLER_DIGITAL_UP,
Down = pros_sys::E_CONTROLLER_DIGITAL_DOWN,
Left = pros_sys::E_CONTROLLER_DIGITAL_LEFT,
Right = pros_sys::E_CONTROLLER_DIGITAL_RIGHT,
LeftTrigger1 = pros_sys::E_CONTROLLER_DIGITAL_L1,
LeftTrigger2 = pros_sys::E_CONTROLLER_DIGITAL_L2,
RightTrigger1 = pros_sys::E_CONTROLLER_DIGITAL_R1,
RightTrigger2 = pros_sys::E_CONTROLLER_DIGITAL_R2,
}

/// An analog channel (joystick axis) on the VEX controller.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum ControllerAxis {
LeftX = pros_sys::E_CONTROLLER_ANALOG_LEFT_X,
LeftY = pros_sys::E_CONTROLLER_ANALOG_LEFT_Y,
RightX = pros_sys::E_CONTROLLER_ANALOG_RIGHT_X,
RightY = pros_sys::E_CONTROLLER_ANALOG_RIGHT_Y,
}

/// The basic type for a controller.
/// Used to get the state of its joysticks and controllers.
#[repr(u32)]
Expand Down Expand Up @@ -94,7 +122,7 @@ impl Controller {
}
}

/// Gets the state of the controller; the joysticks and buttons.
/// Gets the current state of the controller in its entirety, including its joysticks and buttons.
pub fn state(&self) -> ControllerState {
ControllerState {
joysticks: unsafe {
Expand Down Expand Up @@ -179,6 +207,16 @@ impl Controller {
},
}
}

/// Gets the state of a specific button on the controller.
pub fn get_button(&self, button: ControllerButton) -> bool {
unsafe { pros_sys::controller_get_digital(self.id(), button as u32) == 1 }
}

/// Gets the state of a specific joystick axis on the controller.
pub fn get_axis(&self, axis: ControllerAxis) -> f32 {
unsafe { pros_sys::controller_get_analog(self.id(), axis as u32) as f32 / 127.0 }
}
}

#[derive(Debug, Snafu)]
Expand Down

0 comments on commit b294a72

Please sign in to comment.