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
use super::sensor_util;
use wpilib_sys::usage::{instances, resource_types};
use wpilib_sys::*;
#[allow(dead_code)]
#[derive(Debug)]
pub struct DigitalOutput {
channel: i32,
handle: HAL_DigitalHandle,
pwm: Option<HAL_DigitalPWMHandle>,
}
impl DigitalOutput {
#[allow(clippy::new_ret_no_self)]
pub fn new(channel: i32) -> HalResult<Self> {
if !sensor_util::check_digital_channel(channel) {
return Err(HalError(0));
}
let handle = hal_call!(HAL_InitializeDIOPort(
HAL_GetPort(channel as i32),
false as i32
))?;
report_usage(resource_types::DigitalOutput, channel as instances::Type);
Ok(DigitalOutput {
channel,
handle,
pwm: None,
})
}
pub fn set_pwm_rate(rate: f64) -> HalResult<()> {
hal_call!(HAL_SetDigitalPWMRate(rate))
}
pub fn set(&mut self, value: bool) -> HalResult<()> {
hal_call!(HAL_SetDIO(self.handle, value as i32))
}
pub fn get(&self) -> HalResult<bool> {
Ok(hal_call!(HAL_GetDIO(self.handle))? != 0)
}
pub fn channel(&self) -> i32 {
self.channel
}
pub fn handle(&self) -> HAL_DigitalHandle {
self.handle
}
pub fn pulse(&mut self, length: f64) -> HalResult<()> {
hal_call!(HAL_Pulse(self.handle, length))
}
pub fn is_pulsing(&self) -> HalResult<bool> {
Ok(hal_call!(HAL_IsPulsing(self.handle))? != 0)
}
pub fn enable_pwm(&mut self, initial_duty_cycle: f64) -> HalResult<()> {
let pwm = hal_call!(HAL_AllocateDigitalPWM())?;
hal_call!(HAL_SetDigitalPWMDutyCycle(pwm, initial_duty_cycle))?;
hal_call!(HAL_SetDigitalPWMOutputChannel(pwm, self.channel))?;
self.pwm = Some(pwm);
Ok(())
}
pub fn disable_pwm(&mut self) -> HalResult<()> {
if let Some(pwm) = self.pwm {
hal_call!(HAL_SetDigitalPWMOutputChannel(
pwm,
*sensor_util::NUM_DIGITAL_CHANNELS
))?;
hal_call!(HAL_FreeDigitalPWM(pwm))?;
self.pwm = None;
}
Ok(())
}
pub fn update_duty_cycle(&mut self, duty_cycle: f64) -> HalResult<()> {
if let Some(pwm) = self.pwm {
hal_call!(HAL_SetDigitalPWMDutyCycle(pwm, duty_cycle))
} else {
Ok(())
}
}
}
impl Drop for DigitalOutput {
fn drop(&mut self) {
let _ = self.disable_pwm();
unsafe {
HAL_FreeDIOPort(self.handle);
}
}
}
#[allow(dead_code)]
#[derive(Debug)]
pub struct DigitalInput {
channel: i32,
handle: HAL_DigitalHandle,
}
impl DigitalInput {
#[allow(clippy::new_ret_no_self)]
pub fn new(channel: i32) -> HalResult<Self> {
if !sensor_util::check_digital_channel(channel) {
return Err(HalError(0));
}
let handle = hal_call!(HAL_InitializeDIOPort(
HAL_GetPort(channel as i32),
true as i32
))?;
report_usage(resource_types::DigitalInput, channel as instances::Type);
Ok(DigitalInput { channel, handle })
}
pub fn get(&self) -> HalResult<bool> {
Ok(hal_call!(HAL_GetDIO(self.handle))? != 0)
}
pub fn handle(&self) -> HAL_DigitalHandle {
self.handle
}
pub fn channel(&self) -> i32 {
self.channel
}
}
impl Drop for DigitalInput {
fn drop(&mut self) {
unsafe {
HAL_FreeDIOPort(self.handle);
}
}
}