Skip to content

Commit e22bc2b

Browse files
committed
Review comments
1 parent d007bb9 commit e22bc2b

File tree

7 files changed

+28
-10
lines changed

7 files changed

+28
-10
lines changed

examples/ble_scanning.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ async fn main() -> TockResult<()> {
2929
ble_parser::find(&my_buffer, simple_ble::gap_data::SERVICE_DATA as u8)
3030
.and_then(|service_data| ble_parser::extract_for_service([91, 79], service_data))
3131
.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+
});
3338
});
3439

3540
let _subscription = drivers.ble_scanning.start(&mut callback)?;

examples/blink_random.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async fn main() -> TockResult<()> {
3131
// Takes the 4 least-significant bits of x, and turn the 4 leds on/off accordingly.
3232
fn blink_nibble(leds_driver: &LedsDriver, x: u8) -> TockResult<()> {
3333
for i in 0..4 {
34-
let led = leds_driver.get(i).unwrap();
34+
let led = leds_driver.get(i)?;
3535
if (x >> i) & 1 != 0 {
3636
led.on()?;
3737
} else {

examples/button_leds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async fn main() -> TockResult<()> {
1212
let leds_driver = drivers.leds.init_driver()?;
1313

1414
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)) {
1616
led.toggle().ok().unwrap();
1717
}
1818
};

src/buttons.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::callback::CallbackSubscription;
22
use crate::callback::Consumer;
33
use crate::result::OtherError;
4+
use crate::result::OutOfRangeError;
45
use crate::result::TockResult;
56
use crate::syscalls;
67
use core::marker::PhantomData;
@@ -41,14 +42,15 @@ impl<'a> ButtonsDriver<'a> {
4142
self.num_buttons
4243
}
4344

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> {
4547
if button_num < self.num_buttons {
46-
Some(Button {
48+
Ok(Button {
4749
button_num,
4850
lifetime: PhantomData,
4951
})
5052
} else {
51-
None
53+
Err(OutOfRangeError)
5254
}
5355
}
5456

src/callback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct CallbackSubscription<'a> {
4646
}
4747

4848
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> {
5050
CallbackSubscription {
5151
driver_number,
5252
subscribe_number,

src/leds.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::result::OutOfRangeError;
12
use crate::result::TockResult;
23
use crate::syscalls::command;
34
use core::marker::PhantomData;
@@ -42,14 +43,15 @@ impl<'a> LedsDriver<'a> {
4243
}
4344
}
4445

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> {
4648
if led_num < self.num_leds {
47-
Some(Led {
49+
Ok(Led {
4850
led_num,
4951
lifetime: PhantomData,
5052
})
5153
} else {
52-
None
54+
Err(OutOfRangeError)
5355
}
5456
}
5557
}

src/result.rs

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub enum OtherError {
7272
TimerDriverDurationOutOfRange,
7373
TimerDriverErroneousClockFrequency,
7474
DriverAlreadyTaken,
75+
OutOfRangeError,
7576
}
7677

7778
impl From<OtherError> for TockError {
@@ -80,6 +81,14 @@ impl From<OtherError> for TockError {
8081
}
8182
}
8283

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+
8392
pub const SUCCESS: isize = 0;
8493
pub const FAIL: isize = -1;
8594
pub const EBUSY: isize = -2;

0 commit comments

Comments
 (0)