2
2
3
3
use super :: Channel ;
4
4
use bit_field:: BitField ;
5
- use hal:: hal:: digital:: { ErrorType , InputPin } ;
6
5
use stm32f4xx_hal as hal;
7
6
8
- use debounced_pin :: { Debounce , DebounceState , DebouncedInputPin } ;
7
+ use debouncr :: { Debouncer , Repeat10 } ;
9
8
10
9
/// Represents an event indicated through the GPIO buttons.
11
10
pub enum ButtonEvent {
@@ -19,8 +18,10 @@ type Button2 = hal::gpio::gpiof::PF15<hal::gpio::Input>;
19
18
20
19
/// Represents the two user input buttons on the front panel.
21
20
pub struct UserButtons {
22
- button1 : InputButton < Button1 , <Button1 as ErrorType >:: Error > ,
23
- button2 : InputButton < Button2 , <Button2 as ErrorType >:: Error > ,
21
+ button1 : Button1 ,
22
+ button2 : Button2 ,
23
+ button1_state : Debouncer < u16 , Repeat10 > ,
24
+ button2_state : Debouncer < u16 , Repeat10 > ,
24
25
}
25
26
26
27
impl UserButtons {
@@ -34,8 +35,10 @@ impl UserButtons {
34
35
/// The user interface button manager.
35
36
pub fn new ( button1 : Button1 , button2 : Button2 ) -> Self {
36
37
UserButtons {
37
- button1 : InputButton :: new ( button1) ,
38
- button2 : InputButton :: new ( button2) ,
38
+ button1,
39
+ button2,
40
+ button1_state : debouncr:: debounce_10 ( false ) ,
41
+ button2_state : debouncr:: debounce_10 ( false ) ,
39
42
}
40
43
}
41
44
@@ -45,61 +48,19 @@ impl UserButtons {
45
48
/// An option containing any event that is indicated by the button update.
46
49
pub fn update ( & mut self ) -> Option < ButtonEvent > {
47
50
// Prioritize entering standby.
48
- if self . button2 . update ( ) {
51
+ let button2_pressed = self . button2 . is_low ( ) ;
52
+ if let Some ( debouncr:: Edge :: Rising ) = self . button2_state . update ( button2_pressed) {
49
53
return Some ( ButtonEvent :: Standby ) ;
50
54
}
51
55
52
- if self . button1 . update ( ) {
56
+ let button1_pressed = self . button1 . is_low ( ) ;
57
+ if let Some ( debouncr:: Edge :: Rising ) = self . button1_state . update ( button1_pressed) {
53
58
return Some ( ButtonEvent :: InterlockReset ) ;
54
59
}
55
-
56
60
None
57
61
}
58
62
}
59
63
60
- /// A structure representing one of the input buttons.
61
- struct InputButton < INPUT , E >
62
- where
63
- INPUT : InputPin < Error = E > ,
64
- E : core:: fmt:: Debug ,
65
- {
66
- button : DebouncedInputPin < INPUT , debounced_pin:: ActiveLow > ,
67
- was_active : bool ,
68
- }
69
-
70
- impl < INPUT , E > InputButton < INPUT , E >
71
- where
72
- INPUT : InputPin < Error = E > ,
73
- E : core:: fmt:: Debug ,
74
- {
75
- /// Construct a new input button.
76
- pub fn new ( button : INPUT ) -> Self {
77
- InputButton {
78
- was_active : false ,
79
- button : DebouncedInputPin :: new ( button, debounced_pin:: ActiveLow ) ,
80
- }
81
- }
82
-
83
- /// Periodically check the state of the input button.
84
- ///
85
- /// # Returns
86
- /// True if the debounced button state has encountered an activation.
87
- pub fn update ( & mut self ) -> bool {
88
- match self . button . update ( ) . unwrap ( ) {
89
- DebounceState :: Active => {
90
- let result = !self . was_active ;
91
- self . was_active = true ;
92
- result
93
- }
94
- DebounceState :: NotActive => {
95
- self . was_active = false ;
96
- false
97
- }
98
- _ => false ,
99
- }
100
- }
101
- }
102
-
103
64
/// Represents LED colors on the front panel.
104
65
pub enum Color {
105
66
Red ,
0 commit comments