@@ -13,7 +13,7 @@ use e310x_hal::gpio::gpio0::Pin5;
13
13
#[ cfg( any( feature = "board-hifive1" , feature = "board-hifive1-revb" ) ) ]
14
14
use e310x_hal:: gpio:: gpio0:: { Pin19 , Pin21 , Pin22 } ;
15
15
use e310x_hal:: gpio:: { Invert , Output , Regular } ;
16
- use embedded_hal:: digital:: v2:: OutputPin ;
16
+ use embedded_hal:: digital:: v2:: { OutputPin , ToggleableOutputPin } ;
17
17
18
18
#[ cfg( any( feature = "board-hifive1" , feature = "board-hifive1-revb" ) ) ]
19
19
/// Red LED
@@ -47,36 +47,34 @@ pub trait Led {
47
47
48
48
/// Turns the LED on
49
49
fn on ( & mut self ) ;
50
- }
51
-
52
- #[ cfg( any( feature = "board-hifive1" , feature = "board-hifive1-revb" ) ) ]
53
- impl Led for RED {
54
- fn off ( & mut self ) {
55
- self . set_low ( ) . unwrap ( ) ;
56
- }
57
50
58
- fn on ( & mut self ) {
59
- self . set_high ( ) . unwrap ( ) ;
60
- }
51
+ /// Toggles the LED state
52
+ fn toggle ( & mut self ) ;
61
53
}
62
54
63
- #[ cfg( any( feature = "board-hifive1" , feature = "board-hifive1-revb" ) ) ]
64
- impl Led for GREEN {
65
- fn off ( & mut self ) {
66
- self . set_low ( ) . unwrap ( ) ;
67
- }
55
+ /// Macro to implement the Led trait for each of the board LEDs
56
+ macro_rules! led_impl {
57
+ ( $( $LEDTYPE: ident) ,+) => {
58
+ $(
59
+ impl Led for $LEDTYPE {
60
+ fn off( & mut self ) {
61
+ self . set_low( ) . unwrap( ) ;
62
+ }
63
+
64
+ fn on( & mut self ) {
65
+ self . set_high( ) . unwrap( ) ;
66
+ }
68
67
69
- fn on ( & mut self ) {
70
- self . set_high ( ) . unwrap ( ) ;
68
+ fn toggle( & mut self ) {
69
+ ToggleableOutputPin :: toggle( self ) . unwrap( ) ;
70
+ }
71
+ }
72
+ ) +
71
73
}
72
74
}
73
75
74
- impl Led for BLUE {
75
- fn off ( & mut self ) {
76
- self . set_low ( ) . unwrap ( ) ;
77
- }
76
+ /// Call the macro for each LED
77
+ #[ cfg( any( feature = "board-hifive1" , feature = "board-hifive1-revb" ) ) ]
78
+ led_impl ! ( RED , GREEN ) ;
78
79
79
- fn on ( & mut self ) {
80
- self . set_high ( ) . unwrap ( ) ;
81
- }
82
- }
80
+ led_impl ! ( BLUE ) ;
0 commit comments