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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use core::convert::TryInto;
use crate::{
bindings,
error::{get_errno, Error},
};
pub struct Controller {
pub left_stick: AnalogStick,
pub right_stick: AnalogStick,
pub l1: Button,
pub l2: Button,
pub r1: Button,
pub r2: Button,
pub up: Button,
pub down: Button,
pub left: Button,
pub right: Button,
pub x: Button,
pub y: Button,
pub a: Button,
pub b: Button,
}
impl Controller {
pub unsafe fn new(id: ControllerId) -> Self {
let id: bindings::controller_id_e_t = id.into();
Controller {
left_stick: AnalogStick {
id,
x_channel: bindings::controller_analog_e_t_E_CONTROLLER_ANALOG_LEFT_X,
y_channel: bindings::controller_analog_e_t_E_CONTROLLER_ANALOG_LEFT_Y,
},
right_stick: AnalogStick {
id,
x_channel: bindings::controller_analog_e_t_E_CONTROLLER_ANALOG_RIGHT_X,
y_channel: bindings::controller_analog_e_t_E_CONTROLLER_ANALOG_RIGHT_Y,
},
l1: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_L1,
},
l2: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_L2,
},
r1: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_R1,
},
r2: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_R2,
},
up: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_UP,
},
down: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_DOWN,
},
right: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_RIGHT,
},
left: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_LEFT,
},
x: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_X,
},
y: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_Y,
},
b: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_B,
},
a: Button {
id,
button: bindings::controller_digital_e_t_E_CONTROLLER_DIGITAL_A,
},
}
}
}
pub struct AnalogStick {
id: bindings::controller_id_e_t,
x_channel: bindings::controller_analog_e_t,
y_channel: bindings::controller_analog_e_t,
}
impl AnalogStick {
pub fn get_x(&self) -> Result<i8, ControllerError> {
self.get_channel(self.x_channel)
}
pub fn get_y(&self) -> Result<i8, ControllerError> {
self.get_channel(self.y_channel)
}
fn get_channel(&self, channel: bindings::controller_analog_e_t) -> Result<i8, ControllerError> {
match unsafe { bindings::controller_get_analog(self.id, channel) } {
bindings::PROS_ERR_ => Err(ControllerError::from_errno()),
x => match x.try_into() {
Ok(converted_x) => Ok(converted_x),
Err(_) => {
panic!(
"bindings::motor_get_direction returned unexpected value: {}",
x
)
}
},
}
}
}
pub struct Button {
id: bindings::controller_id_e_t,
button: bindings::controller_digital_e_t,
}
impl Button {
pub fn is_pressed(&self) -> Result<bool, ControllerError> {
match unsafe { bindings::controller_get_digital(self.id, self.button) } {
0 => Ok(false),
1 => Ok(true),
_ => Err(ControllerError::from_errno()),
}
}
}
pub enum ControllerId {
Master,
Partner,
}
impl From<ControllerId> for bindings::controller_id_e_t {
fn from(id: ControllerId) -> Self {
match id {
ControllerId::Master => bindings::controller_id_e_t_E_CONTROLLER_MASTER,
ControllerId::Partner => bindings::controller_id_e_t_E_CONTROLLER_PARTNER,
}
}
}
#[derive(Debug)]
pub enum ControllerError {
InvalidController,
ControllerBusy,
Unknown(i32),
}
impl ControllerError {
fn from_errno() -> Self {
match { get_errno() } {
libc::EINVAL => Self::InvalidController,
libc::EACCES => Self::ControllerBusy,
x => Self::Unknown(x),
}
}
}
impl From<ControllerError> for Error {
fn from(err: ControllerError) -> Self {
match err {
ControllerError::InvalidController => Error::Custom("invalid controller id".into()),
ControllerError::ControllerBusy => Error::Custom("controller is busy".into()),
ControllerError::Unknown(n) => Error::System(n),
}
}
}