safe_vex/
controller.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! # Controller API

use crate::{bindings, error::PROSErr};

/// A controller that you can read from
#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum Controller {
    /// The master controller
    Master = 0,
    /// The partner controller
    Partner = 1,
}

/// An Analog Joystick on the Controller
#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum ControllerAnalog {
    /// The x axis of the left joystick
    LeftX = 0,
    /// The y axis of the left joystick
    LeftY = 1,
    /// The x axis of the right joystick
    RightX = 2,
    /// The y axis of the right joystick
    RightY = 3,
}

/// A digital (button) on the controller
#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum ControllerDigital {
    /// The first trigger on the left side of the controller
    L1 = 6,
    /// The second trigger on the left side of the controller
    L2,
    /// The first trigger on the right side of the controller
    R1,
    /// The second trigger on the right side of the controller
    R2,
    /// The up arrow on the left arrow pad of the controller
    Up,
    /// The down arrow on the left arrow pad of the controller
    Down,
    /// The left arrow on the left arrow pad of the controller
    Left,
    /// The right arrow on the left arrow pad of the controller
    Right,
    /// The ‘X’ button on the right button pad of the controller
    X,
    /// The ‘B’ button on the right button pad of the controller
    B,
    /// The ‘Y’ button on the right button pad of the controller
    Y,
    /// The ‘A’ button on the right button pad of the controller
    A,
}

/// Returns the analog value of a controller
///
/// # Errors
///
/// Returns `PROSErr::Access` if another resource is currently trying to access the controller
/// Returns `0` if the controller is not connected
pub fn get_analog(controller: Controller, analog: ControllerAnalog) -> Result<i8, PROSErr> {
    // get the analog value of the controller joystick
    let code = unsafe {
        bindings::controller_get_analog(controller as u32, analog as u32)
    };

    // check for errors
    if let Err(err) = PROSErr::parse(code) {
        return Err(err);
    }

    // return valid analog controller input
    Ok(code as i8)
}

/// Returns the digital value of a controller button
///
/// # Errors
///
/// Returns `PROSErr::Access` if another resource is currently trying to access the controller
/// Returns `false` if the controller is not connected
pub fn get_digital(controller: Controller, digital: ControllerDigital) -> Result<bool, PROSErr> {
    // get the analog value of the controller joystick
    let code = unsafe {
        bindings::controller_get_digital(controller as u32, digital as u32)
    };

    // check for errors
    if let Err(err) = PROSErr::parse(code) {
        return Err(err);
    }

    // return valid analog controller input
    Ok(code != 0)
}