File tree 7 files changed +28
-10
lines changed
7 files changed +28
-10
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,12 @@ async fn main() -> TockResult<()> {
29
29
ble_parser:: find ( & my_buffer, simple_ble:: gap_data:: SERVICE_DATA as u8 )
30
30
. and_then ( |service_data| ble_parser:: extract_for_service ( [ 91 , 79 ] , service_data) )
31
31
. and_then ( |payload| corepack:: from_bytes :: < LedCommand > ( & payload) . ok ( ) )
32
- . and_then ( |msg| leds_driver. get ( msg. nr as usize ) . map ( |led| led. set ( msg. st ) ) ) ;
32
+ . and_then ( |msg| {
33
+ leds_driver
34
+ . get ( msg. nr as usize )
35
+ . map ( |led| led. set ( msg. st ) )
36
+ . into ( )
37
+ } ) ;
33
38
} ) ;
34
39
35
40
let _subscription = drivers. ble_scanning . start ( & mut callback) ?;
Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ async fn main() -> TockResult<()> {
31
31
// Takes the 4 least-significant bits of x, and turn the 4 leds on/off accordingly.
32
32
fn blink_nibble ( leds_driver : & LedsDriver , x : u8 ) -> TockResult < ( ) > {
33
33
for i in 0 ..4 {
34
- let led = leds_driver. get ( i) . unwrap ( ) ;
34
+ let led = leds_driver. get ( i) ? ;
35
35
if ( x >> i) & 1 != 0 {
36
36
led. on ( ) ?;
37
37
} else {
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ async fn main() -> TockResult<()> {
12
12
let leds_driver = drivers. leds . init_driver ( ) ?;
13
13
14
14
let mut callback = |button_num, state| {
15
- if let ( ButtonState :: Pressed , Some ( led) ) = ( state, leds_driver. get ( button_num) ) {
15
+ if let ( ButtonState :: Pressed , Ok ( led) ) = ( state, leds_driver. get ( button_num) ) {
16
16
led. toggle ( ) . ok ( ) . unwrap ( ) ;
17
17
}
18
18
} ;
Original file line number Diff line number Diff line change 1
1
use crate :: callback:: CallbackSubscription ;
2
2
use crate :: callback:: Consumer ;
3
3
use crate :: result:: OtherError ;
4
+ use crate :: result:: OutOfRangeError ;
4
5
use crate :: result:: TockResult ;
5
6
use crate :: syscalls;
6
7
use core:: marker:: PhantomData ;
@@ -41,14 +42,15 @@ impl<'a> ButtonsDriver<'a> {
41
42
self . num_buttons
42
43
}
43
44
44
- pub fn get ( & self , button_num : usize ) -> Option < Button > {
45
+ /// Returns the button at 0-based index `button_num`
46
+ pub fn get ( & self , button_num : usize ) -> Result < Button , OutOfRangeError > {
45
47
if button_num < self . num_buttons {
46
- Some ( Button {
48
+ Ok ( Button {
47
49
button_num,
48
50
lifetime : PhantomData ,
49
51
} )
50
52
} else {
51
- None
53
+ Err ( OutOfRangeError )
52
54
}
53
55
}
54
56
Original file line number Diff line number Diff line change @@ -46,7 +46,7 @@ pub struct CallbackSubscription<'a> {
46
46
}
47
47
48
48
impl < ' a > CallbackSubscription < ' a > {
49
- pub fn new ( driver_number : usize , subscribe_number : usize ) -> CallbackSubscription < ' a > {
49
+ pub ( crate ) fn new ( driver_number : usize , subscribe_number : usize ) -> CallbackSubscription < ' a > {
50
50
CallbackSubscription {
51
51
driver_number,
52
52
subscribe_number,
Original file line number Diff line number Diff line change
1
+ use crate :: result:: OutOfRangeError ;
1
2
use crate :: result:: TockResult ;
2
3
use crate :: syscalls:: command;
3
4
use core:: marker:: PhantomData ;
@@ -42,14 +43,15 @@ impl<'a> LedsDriver<'a> {
42
43
}
43
44
}
44
45
45
- pub fn get ( & self , led_num : usize ) -> Option < Led > {
46
+ /// Returns the led at 0-based index `led_num`
47
+ pub fn get ( & self , led_num : usize ) -> Result < Led , OutOfRangeError > {
46
48
if led_num < self . num_leds {
47
- Some ( Led {
49
+ Ok ( Led {
48
50
led_num,
49
51
lifetime : PhantomData ,
50
52
} )
51
53
} else {
52
- None
54
+ Err ( OutOfRangeError )
53
55
}
54
56
}
55
57
}
Original file line number Diff line number Diff line change @@ -72,6 +72,7 @@ pub enum OtherError {
72
72
TimerDriverDurationOutOfRange ,
73
73
TimerDriverErroneousClockFrequency ,
74
74
DriverAlreadyTaken ,
75
+ OutOfRangeError ,
75
76
}
76
77
77
78
impl From < OtherError > for TockError {
@@ -80,6 +81,14 @@ impl From<OtherError> for TockError {
80
81
}
81
82
}
82
83
84
+ pub struct OutOfRangeError ;
85
+
86
+ impl From < OutOfRangeError > for TockError {
87
+ fn from ( _other : OutOfRangeError ) -> Self {
88
+ TockError :: Other ( OtherError :: OutOfRangeError )
89
+ }
90
+ }
91
+
83
92
pub const SUCCESS : isize = 0 ;
84
93
pub const FAIL : isize = -1 ;
85
94
pub const EBUSY : isize = -2 ;
You can’t perform that action at this time.
0 commit comments