-
Notifications
You must be signed in to change notification settings - Fork 5
/
fault_condition.rs
57 lines (47 loc) · 1.68 KB
/
fault_condition.rs
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
//! Fault condition
use dual_signal::{DualSignal, HighLowReader};
use embedded_hal::timer::CountDown;
use nucleo_f767zi::hal::timer::OnePulse;
pub struct FaultCondition<TIMER> {
monitoring_active: bool,
timer: TIMER,
}
impl<TIMER> FaultCondition<TIMER>
where
TIMER: CountDown + OnePulse,
{
pub fn new(mut timer: TIMER) -> Self {
timer.reconfigure_one_pulse_mode();
FaultCondition {
monitoring_active: false,
timer,
}
}
pub fn condition_exceeded_duration(&mut self, condition_active: bool) -> bool {
let mut faulted = false;
if !condition_active {
// If a fault condition is not active, update the state to clear
// the condition active flag and reset the last detection time.
self.monitoring_active = false;
} else {
if !self.monitoring_active {
// We just detected a condition that may lead to a fault. Update
// the state to track that the condition is active and store the
// first time of detection.
self.monitoring_active = true;
self.timer.reset();
}
if self.timer.wait().is_ok() {
// The fault condition has been active for longer than the maximum
// acceptable duration.
faulted = true;
self.timer.reset();
}
}
faulted
}
pub fn check_voltage_grounded<T: HighLowReader>(&mut self, signal: &DualSignal<T>) -> bool {
let condition_active = (signal.high() == 0) || (signal.low() == 0);
self.condition_exceeded_duration(condition_active)
}
}