arfur_rev/
controllers.rs

1pub mod sparkmax {
2    use std::ffi::c_int;
3
4    use arfur_wpilib::robot::Robot;
5
6    use crate::ffi::root::rev::{CANSparkMax, CANSparkMax_Set};
7
8    /// A handle to a REV CAN SparkMax motor controller.
9    pub struct SparkMax {
10        handle: CANSparkMax,
11    }
12
13    impl SparkMax {
14        /// Create a new CAN brushless SparkMax.
15        ///
16        /// # Safety
17        /// `id` is a valid CAN id, the motor type is internally ensured.
18        pub fn new(_: Robot, id: i32) -> Self {
19            let handle = unsafe { CANSparkMax::new(id as c_int, 1 as c_int) };
20
21            Self { handle }
22        }
23
24        /// Set the percentage output.
25        ///
26        /// Safety: the percentage is a number from -1 to 1.
27        pub fn set_percentage(&mut self, percentage: f64) {
28            unsafe {
29                CANSparkMax_Set(
30                    &mut self.handle as *mut _ as *mut std::ffi::c_void,
31                    percentage,
32                );
33            }
34        }
35    }
36}