antaeus 0.3.8

A Versatile Framework for Vexide
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Controller input mapping for operator control.
//!
//! This module provides utilities for mapping controller button presses
//! to motor voltages and ADI digital outputs. It supports:
//!
//! * **Toggle controls**: Button press toggles a state (e.g., open/close piston).
//! * **Momentary controls**: Button held activates, release deactivates.
//! * **Dual-button controls**: Two buttons for forward/reverse or extend/retract.
//! * **Control button modifiers**: Require a "shift" button to be held.
//!
//! # Example
//!
//! ```ignore
//! use antaeus::peripherals::controller::{ControllerControl, ControllerButton};
//!
//! let control = ControllerControl::new(&controller, ControllerButton::ButtonA);
//!
//! // R1/R2 controls intake forward/reverse
//! control.dual_button_to_motors(
//!     ControllerButton::ButtonR1,
//!     ControllerButton::ButtonR2,
//!     vec![&mut intake],
//!     12.0, -12.0, 0.0, false,
//! );
//! ```

use std::vec::Vec;

use log::warn;
use vexide::{
    controller::{ButtonState, ControllerState},
    prelude::{AdiDigitalOut, Controller, Motor},
};

/// Controller input mapper for operator control.
///
/// This struct captures the current controller state and a designated
/// "control button" that acts as a modifier (like a shift key).
///
/// # Control Button
///
/// The control button enables extended controls. When `ctrl: true` is
/// passed to mapping methods, the action only triggers if the control
/// button is also held. This effectively doubles the available controls.
///
/// # Example
///
/// ```ignore
/// let control = ControllerControl::new(&controller, ControllerButton::ButtonA);
///
/// // ButtonB triggers normally
/// control.button_to_adi_toggle(ControllerButton::ButtonB, pistons, false);
///
/// // ButtonB + ButtonA (ctrl) triggers a different action
/// control.button_to_adi_toggle(ControllerButton::ButtonB, other_pistons, true);
/// ```
#[allow(dead_code)]
pub struct ControllerControl {
    /// The current state of all controller buttons and sticks.
    state:      ControllerState,
    /// The button designated as the control/modifier button.
    controlkey: ButtonState,
}

#[allow(dead_code)]
impl ControllerControl {
    /// Creates a new ControllerControl Instance that can be used to easily
    /// control motors and ADI ports.
    ///
    /// # Arguments
    ///
    /// * `controller` - The VEX controller to read input from.
    /// * `button` - The button to use as the control/modifier button.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use antaeus::peripherals::controller::{ControllerControl, ControllerButton};
    /// use vexide::prelude::*;
    ///
    /// let master = Controller::new(ControllerId::Primary);
    /// let master_control = ControllerControl::new(&master, ControllerButton::ButtonA);
    /// ```
    pub fn new(controller: &Controller, button: ControllerButton) -> Self {
        let state = get_state(controller);
        let control_button = get_button_state(state, button);

        ControllerControl {
            state,
            controlkey: control_button,
        }
    }

    /// Maps the output from a button to toggle one or more ADI Devices. A
    /// maximum of 8 ADI devices can be controlled at a time.
    ///
    /// # Arguments
    ///
    /// * `button` - The primary button that will control the device.
    /// * `adi_devices` - A `Vec` of ADI devices to control.
    /// * `ctrl` - Whether to use the control button. Usually set to `false`, unless you
    ///   wish to press the control button and the primary button together.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use antaeus::peripherals::controller::{ControllerControl, ControllerButton};
    /// use vexide::prelude::*;
    ///
    /// let master = Controller::new(ControllerId::Primary);
    /// let mut piston = AdiDigitalOut::new(peripherals.adi_a);
    ///
    /// let master_control = ControllerControl::new(&master, ControllerButton::ButtonA);
    /// master_control.button_to_adi_toggle(
    ///     ControllerButton::ButtonB,
    ///     vec![&mut piston],
    ///     false,
    /// );
    /// ```
    pub fn button_to_adi_toggle(
        &self,
        button: ControllerButton,
        adi_devices: Vec<&mut AdiDigitalOut>,
        ctrl: bool,
    ) {
        let button_state = get_button_state(self.state, button);
        if button_state.is_now_released() && self.controlkey.is_pressed() == ctrl {
            for device in adi_devices {
                device.toggle().unwrap_or_else(|e| {
                    warn!("ADI Toggle Error: {}", e);
                });
            }
        }
    }

    /// Maps the output from a button to set one or more ADI Devices to high. A
    /// maximum of 8 ADI devices can be controlled at a time.
    ///
    /// # Arguments
    ///
    /// * `button` - The primary button that will control the device.
    /// * `adi_devices` - A `Vec` of ADI devices to control.
    /// * `ctrl` - Whether to use the control button. Usually set to `false`, unless you
    ///   wish to press the control button and the primary button together.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use antaeus::peripherals::controller::{ControllerControl, ControllerButton};
    /// use vexide::prelude::*;
    ///
    /// let master = Controller::new(ControllerId::Primary);
    /// let mut piston = AdiDigitalOut::new(peripherals.adi_a);
    ///
    /// let master_control = ControllerControl::new(&master, ControllerButton::ButtonA);
    /// master_control.button_to_adi_high(
    ///     ControllerButton::ButtonB,
    ///     vec![&mut piston],
    ///     false,
    /// );
    /// ```
    pub fn button_to_adi_high(
        &self,
        button: ControllerButton,
        adi_devices: Vec<&mut AdiDigitalOut>,
        ctrl: bool,
    ) {
        let button_state = get_button_state(self.state, button);
        if button_state.is_now_released() && self.controlkey.is_pressed() == ctrl {
            for device in adi_devices {
                device.set_high().unwrap_or_else(|e| {
                    warn!("ADI Set High Error: {}", e);
                });
            }
        }
    }

    /// Maps the output from a button to set one or more ADI Devices to low. A
    /// maximum of 8 ADI devices can be controlled at a time.
    ///
    /// # Arguments
    ///
    /// * `button` - The primary button that will control the device.
    /// * `adi_devices` - A `Vec` of ADI devices to control.
    /// * `ctrl` - Whether to use the control button. Usually set to `false`, unless you
    ///   wish to press the control button and the primary button together.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use antaeus::peripherals::controller::{ControllerControl, ControllerButton};
    /// use vexide::prelude::*;
    ///
    /// let master = Controller::new(ControllerId::Primary);
    /// let mut piston = AdiDigitalOut::new(peripherals.adi_a);
    ///
    /// let master_control = ControllerControl::new(&master, ControllerButton::ButtonA);
    /// master_control.button_to_adi_low(
    ///     ControllerButton::ButtonB,
    ///     vec![&mut piston],
    ///     false,
    /// );
    /// ```
    pub fn button_to_adi_low(
        &self,
        button: ControllerButton,
        adi_devices: Vec<&mut AdiDigitalOut>,
        ctrl: bool,
    ) {
        let button_state = get_button_state(self.state, button);
        if button_state.is_now_released() && self.controlkey.is_pressed() == ctrl {
            for device in adi_devices {
                device.set_low().unwrap_or_else(|e| {
                    warn!("ADI Set Low Error: {}", e);
                });
            }
        }
    }

    /// Maps 2 Buttons to one or more ADI Devices. The High Button outputs a
    /// high value to the ADI Devices. The Low Button outputs a low value to
    /// the ADI Devices. A maximum of 8 ADI devices can be controlled at a time.
    ///
    /// # Arguments
    ///
    /// * `button_high` - The button that will set the device to high.
    /// * `button_low` - The button that will set the device to low.
    /// * `adi_devices` - A `Vec` of ADI devices to control.
    /// * `ctrl` - Whether to use the control button. Usually set to `false`, unless you
    ///   wish to press the control button and another button.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use antaeus::peripherals::controller::{ControllerControl, ControllerButton};
    /// use vexide::prelude::*;
    ///
    /// let master = Controller::new(ControllerId::Primary);
    /// let mut piston = AdiDigitalOut::new(peripherals.adi_a);
    ///
    /// let master_control = ControllerControl::new(&master, ControllerButton::ButtonA);
    /// master_control.dual_button_to_adi(
    ///     ControllerButton::ButtonL1,
    ///     ControllerButton::ButtonL2,
    ///     vec![&mut piston],
    ///     false,
    /// );
    /// // L1 extends, L2 retracts
    /// ```
    pub fn dual_button_to_adi(
        &self,
        button_high: ControllerButton,
        button_low: ControllerButton,
        adi_devices: Vec<&mut AdiDigitalOut>,
        ctrl: bool,
    ) {
        let button_high_state = get_button_state(self.state, button_high);
        let button_low_state = get_button_state(self.state, button_low);
        if self.controlkey.is_pressed() == ctrl {
            if button_high_state.is_now_released() {
                for device in adi_devices {
                    device.set_high().unwrap_or_else(|e| {
                        warn!("ADI Toggle Error: {}", e);
                    });
                }
            } else if button_low_state.is_now_released() {
                for device in adi_devices {
                    device.set_low().unwrap_or_else(|e| {
                        warn!("ADI Toggle Error: {}", e);
                    });
                }
            }
        }
    }

    /// Maps a button to one or more motors. Pressing the button will run the
    /// motor at active power. Otherwise, the motor will run at passive power.
    /// A maximum of 8 motors can be controlled at a time.
    ///
    /// # Arguments
    ///
    /// * `button` - The primary button that will control the motors.
    /// * `motors` - A `:Vec` of motors to control.
    /// * `active_pwr` - The power (in Volts) given to the motors when button is pressed.
    /// * `passive_pwr` - The power (in Volts) given to the motors when button is not pressed.
    /// * `ctrl` - Whether to use the control button. Usually set to `false`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use antaeus::peripherals::controller::{ControllerControl, ControllerButton};
    /// use vexide::prelude::*;
    ///
    /// let master = Controller::new(ControllerId::Primary);
    /// let mut intake = Motor::new(peripherals.port_1, Gearset::Green, Direction::Forward);
    ///
    /// let master_control = ControllerControl::new(&master, ControllerButton::ButtonA);
    /// master_control.button_to_motors(
    ///     ControllerButton::ButtonL1,
    ///     vec![&mut intake],
    ///     12.0,  // active power
    ///     0.0,   // passive power
    ///     false,
    /// );
    /// ```
    pub fn button_to_motors(
        &self,
        button: ControllerButton,
        motors: Vec<&mut Motor>,
        active_pwr: f64,
        passive_pwr: f64,
        ctrl: bool,
    ) {
        let button_state = get_button_state(self.state, button);
        if self.controlkey.is_pressed() == ctrl {
            if button_state.is_pressed() {
                for motor in motors {
                    motor.set_voltage(active_pwr).unwrap_or_else(|e| {
                        warn!("Motor Set Voltage Error: {}", e);
                    });
                }
            } else {
                for motor in motors {
                    motor.set_voltage(passive_pwr).unwrap_or_else(|e| {
                        warn!("Motor Set Voltage Error: {}", e);
                    });
                }
            }
        }
    }

    /// Maps 2 buttons to one or more motors. The High button outputs a high
    /// power to the motors. The Low button outputs a low power to the motors.
    /// A maximum of 8 motors can be controlled at a time.
    ///
    /// # Arguments
    ///
    /// * `button_high` - The button that will give high power to the motors.
    /// * `button_low` - The button that will give low power to the motors.
    /// * `motors` - A `Vec` of motors to control.
    /// * `high_pwr` - The power (in Volts) given to the motors when High button is pressed.
    /// * `low_pwr` - The power (in Volts) given to the motors when Low button is pressed.
    /// * `passive_pwr` - The power (in Volts) given to the motors when no button is pressed.
    /// * `ctrl` - Whether to use the control button. Usually set to `false`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use antaeus::peripherals::controller::{ControllerControl, ControllerButton};
    /// use vexide::prelude::*;
    ///
    /// let master = Controller::new(ControllerId::Primary);
    /// let mut intake = Motor::new(peripherals.port_1, Gearset::Green, Direction::Forward);
    ///
    /// let master_control = ControllerControl::new(&master, ControllerButton::ButtonA);
    /// master_control.dual_button_to_motors(
    ///     ControllerButton::ButtonL1,  // forward
    ///     ControllerButton::ButtonL2,  // reverse
    ///     vec![&mut intake],
    ///     12.0,  // high power
    ///     -12.0, // low power
    ///     0.0,   // passive power
    ///     false,
    /// );
    /// ```
    pub fn dual_button_to_motors(
        &self,
        button_high: ControllerButton,
        button_low: ControllerButton,
        motors: Vec<&mut Motor>,
        high_pwr: f64,
        low_pwr: f64,
        passive_pwr: f64,
        ctrl: bool,
    ) {
        let button_high_state = get_button_state(self.state, button_high);
        let button_low_state = get_button_state(self.state, button_low);

        if self.controlkey.is_pressed() == ctrl {
            if button_high_state.is_pressed() {
                for motor in motors {
                    motor.set_voltage(high_pwr).unwrap_or_else(|e| {
                        warn!("Motor Set Voltage Error: {}", e);
                    });
                }
            } else if button_low_state.is_pressed() {
                for motor in motors {
                    motor.set_voltage(low_pwr).unwrap_or_else(|e| {
                        warn!("Motor Set Voltage Error: {}", e);
                    });
                }
            } else {
                for motor in motors {
                    motor.set_voltage(passive_pwr).unwrap_or_else(|e| {
                        warn!("Motor Set Voltage Error: {}", e);
                    });
                }
            }
        }
    }
}

#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
/// An enumeration of controller buttons.
///
/// These represent the physical buttons on a VEX controller that can be
/// mapped to robot actions.
///
/// # Example
///
/// ```ignore
/// use antaeus::peripherals::controller::{ControllerControl, ControllerButton};
/// use vexide::prelude::*;
///
/// let master = Controller::new(ControllerId::Primary);
/// let master_control = ControllerControl::new(&master, ControllerButton::ButtonA);
/// ```
pub enum ControllerButton {
    ButtonA,
    ButtonB,
    ButtonX,
    ButtonY,
    ButtonUp,
    ButtonDown,
    ButtonLeft,
    ButtonRight,
    ButtonL1,
    ButtonL2,
    ButtonR1,
    ButtonR2,
}

fn get_button_state(state: ControllerState, button: ControllerButton) -> ButtonState {
    match button {
        ControllerButton::ButtonA => state.button_a,
        ControllerButton::ButtonB => state.button_b,
        ControllerButton::ButtonX => state.button_x,
        ControllerButton::ButtonY => state.button_y,
        ControllerButton::ButtonUp => state.button_up,
        ControllerButton::ButtonDown => state.button_down,
        ControllerButton::ButtonLeft => state.button_left,
        ControllerButton::ButtonRight => state.button_right,
        ControllerButton::ButtonL1 => state.button_l1,
        ControllerButton::ButtonL2 => state.button_l2,
        ControllerButton::ButtonR1 => state.button_r1,
        ControllerButton::ButtonR2 => state.button_r2,
    }
}

fn get_state(controller: &Controller) -> ControllerState {
    controller.state().unwrap_or_else(|e| {
        warn!("Controller State Error: {}", e);
        ControllerState::default()
    })
}