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
// Copyright 2018 First Rust Competition Developers.
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use super::sensor_util;
use wpilib_sys::usage::{instances, resource_types};
use wpilib_sys::*;

/// Corresponds to WPILibC's SolenoidBase, and is responsible for
/// Getting info about a solenoid module, (conceptually a PCM).
/// Even though each Solenoid will have a different instance, they all will
/// probably refer to the same piece of hardware.
#[derive(Debug)]
pub struct SolenoidModule {
    module: i32,
}

/// All of these methods are equivalent to their WPILib counterparts.
impl SolenoidModule {
    /// Gets the state of each solenoid on the module with the given number.
    /// Returns a bit mask.
    pub fn all_with_module(module: i32) -> HalResult<i32> {
        hal_call!(HAL_GetAllSolenoids(module))
    }

    /// Is the same as `all_with_module`, but on the module this instance
    /// refers to.
    pub fn all(&self) -> HalResult<i32> {
        Self::all_with_module(self.module)
    }

    pub fn pcm_solenoid_blacklist_with_module(module: i32) -> i32 {
        maybe_hal_call!(HAL_GetPCMSolenoidBlackList(module)).ok()
    }

    pub fn pcm_solenoid_blacklist(&self) -> i32 {
        Self::pcm_solenoid_blacklist_with_module(self.module)
    }

    pub fn pcm_solenoid_voltage_sticky_fault_with_module(module: i32) -> bool {
        maybe_hal_call!(HAL_GetPCMSolenoidVoltageStickyFault(module)).ok() != 0
    }

    pub fn pcm_solenoid_voltage_sticky_fault(&self) -> bool {
        Self::pcm_solenoid_voltage_sticky_fault_with_module(self.module)
    }

    pub fn pcm_solenoid_voltage_fault_with_module(module: i32) -> bool {
        maybe_hal_call!(HAL_GetPCMSolenoidVoltageFault(module)).ok() != 0
    }

    pub fn pcm_solenoid_voltage_fault(&self) -> bool {
        Self::pcm_solenoid_voltage_fault_with_module(self.module)
    }

    pub fn clear_all_pcm_sticky_faults_with_module(module: i32) {
        maybe_hal_call!(HAL_ClearAllPCMStickyFaults(module)).ok();
    }

    pub fn clear_all_pcm_sticky_faults(&self) {
        Self::clear_all_pcm_sticky_faults_with_module(self.module);
    }
}

#[derive(Debug)]
pub struct Solenoid {
    solenoid_handle: HAL_SolenoidHandle,
    channel: i32,
    module: SolenoidModule,
}

impl Solenoid {
    /// Make a new solenoid with the given channel.
    #[allow(clippy::new_ret_no_self)]
    pub fn new(channel: i32) -> HalResult<Solenoid> {
        Self::new_with_module(sensor_util::default_solenoid_module(), channel)
    }

    /// If for some reason the Pneumatic Control Module is not on CAN module 0,
    /// you can use this constructor. Most people will never need this.
    pub fn new_with_module(module_number: i32, channel: i32) -> HalResult<Solenoid> {
        if !sensor_util::check_solenoid_module(module_number) {
            return Err(HalError(0));
        };

        if !sensor_util::check_solenoid_channel(channel) {
            return Err(HalError(0));
        };

        let handle = hal_call!(HAL_InitializeSolenoidPort(HAL_GetPortWithModule(
            module_number,
            channel
        )))?;

        report_usage_context(
            resource_types::Solenoid,
            channel as instances::Type,
            module_number,
        );

        Ok(Solenoid {
            solenoid_handle: handle,
            channel,
            module: SolenoidModule {
                module: module_number,
            },
        })
    }

    /// Sets the solenoid to on or off
    pub fn set(&self, on: bool) -> HalResult<()> {
        hal_call!(HAL_SetSolenoid(self.solenoid_handle, on as i32))
    }

    /// Gets the state of the solenoid by calling out to the hardware through an FFI.
    /// If you need speed, consider caching the value you set yourself!
    pub fn get(&self) -> HalResult<bool> {
        Ok(hal_call!(HAL_GetSolenoid(self.solenoid_handle))? != 0)
    }

    pub fn is_blacklisted(&self) -> bool {
        (self.module.pcm_solenoid_blacklist() & (1 << self.channel)) != 0
    }

    pub fn set_pulse_duration(&self, seconds: f64) -> HalResult<()> {
        let duration_ms: i32 = (seconds * 1000.0) as i32;
        hal_call!(HAL_SetOneShotDuration(self.solenoid_handle, duration_ms))
    }

    pub fn start_pulse(&self) -> HalResult<()> {
        hal_call!(HAL_FireOneShot(self.solenoid_handle))
    }

    pub fn module(&self) -> &SolenoidModule {
        &self.module
    }
}

impl Drop for Solenoid {
    fn drop(&mut self) {
        unsafe { HAL_FreeSolenoidPort(self.solenoid_handle) }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Action {
    Forward,
    Reverse,
    Off,
}

#[derive(Debug)]
pub struct DoubleSolenoid {
    forward: Solenoid,
    reverse: Solenoid,
}

impl DoubleSolenoid {
    #[allow(clippy::new_ret_no_self)]
    pub fn new(forward_channel: i32, reverse_channel: i32) -> HalResult<DoubleSolenoid> {
        Self::new_with_module(
            sensor_util::default_solenoid_module(),
            forward_channel,
            reverse_channel,
        )
    }

    pub fn new_with_module(
        module_number: i32,
        forward_channel: i32,
        reverse_channel: i32,
    ) -> HalResult<DoubleSolenoid> {
        Ok(DoubleSolenoid {
            forward: Solenoid::new_with_module(module_number, forward_channel)?,
            reverse: Solenoid::new_with_module(module_number, reverse_channel)?,
        })
    }

    pub fn set(&self, action: Action) -> HalResult<()> {
        let forward;
        let reverse;
        match action {
            Action::Forward => {
                forward = true;
                reverse = false;
            }
            Action::Reverse => {
                forward = false;
                reverse = true;
            }
            Action::Off => {
                forward = false;
                reverse = false;
            }
        };
        self.forward.set(forward)?;
        self.reverse.set(reverse)?;
        Ok(())
    }

    pub fn get(&self) -> HalResult<Action> {
        if self.forward.get()? {
            return Ok(Action::Forward);
        };
        if self.reverse.get()? {
            return Ok(Action::Reverse);
        };
        Ok(Action::Off)
    }

    pub fn is_fwd_blacklisted(&self) -> bool {
        self.forward.is_blacklisted()
    }

    pub fn is_rev_blacklisted(&self) -> bool {
        self.reverse.is_blacklisted()
    }

    pub fn module(&self) -> &SolenoidModule {
        self.forward.module()
    }
}

#[derive(Debug)]
pub struct Compressor {
    compressor_handle: HAL_CompressorHandle,
    module: i32,
}

impl Compressor {
    #[allow(clippy::new_ret_no_self)]
    pub fn new() -> HalResult<Self> {
        Self::new_with_module(sensor_util::default_solenoid_module())
    }

    pub fn new_with_module(module: i32) -> HalResult<Self> {
        let compressor_handle = hal_call!(HAL_InitializeCompressor(module))?;
        Ok(Self {
            compressor_handle,
            module,
        })
    }

    pub fn set_closed_loop_control(&self, on: bool) {
        hal_call!(HAL_SetCompressorClosedLoopControl(
            self.compressor_handle,
            on as i32
        ))
        .ok();
    }

    pub fn start(&self) {
        self.set_closed_loop_control(true);
    }

    pub fn stop(&self) {
        self.set_closed_loop_control(false);
    }

    pub fn enabled(&self) -> bool {
        maybe_hal_call!(HAL_GetCompressor(self.compressor_handle)).ok() != 0
    }

    pub fn pressure_switch_value(&self) -> bool {
        maybe_hal_call!(HAL_GetCompressorPressureSwitch(self.compressor_handle)).ok() != 0
    }

    pub fn compressor_current(&self) -> f64 {
        maybe_hal_call!(HAL_GetCompressorCurrent(self.compressor_handle)).ok()
    }

    pub fn closed_loop_control(&self) -> bool {
        maybe_hal_call!(HAL_GetCompressorClosedLoopControl(self.compressor_handle)).ok() != 0
    }

    pub fn compressor_current_too_high_fault(&self) -> bool {
        maybe_hal_call!(HAL_GetCompressorCurrentTooHighStickyFault(
            self.compressor_handle
        ))
        .ok()
            != 0
    }

    pub fn compressor_current_too_high_sticky_fault(&self) -> bool {
        maybe_hal_call!(HAL_GetCompressorCurrentTooHighStickyFault(
            self.compressor_handle
        ))
        .ok()
            != 0
    }

    pub fn compressor_shorted_sticky_fault(&self) -> bool {
        maybe_hal_call!(HAL_GetCompressorShortedStickyFault(self.compressor_handle)).ok() != 0
    }

    pub fn compressor_shorted_fault(&self) -> bool {
        maybe_hal_call!(HAL_GetCompressorShortedFault(self.compressor_handle)).ok() != 0
    }

    pub fn compressor_not_connected_sticky_fault(&self) -> bool {
        maybe_hal_call!(HAL_GetCompressorNotConnectedStickyFault(
            self.compressor_handle
        ))
        .ok()
            != 0
    }

    pub fn compressor_not_connected_fault(&self) -> bool {
        maybe_hal_call!(HAL_GetCompressorNotConnectedFault(self.compressor_handle)).ok() != 0
    }

    pub fn clear_all_pcm_sticky_faults(&self) {
        hal_call!(HAL_ClearAllPCMStickyFaults(self.module)).ok();
    }
}