-
Notifications
You must be signed in to change notification settings - Fork 109
/
blink_random.rs
42 lines (35 loc) · 1.13 KB
/
blink_random.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
#![no_std]
use libtock::leds::LedsDriver;
use libtock::result::TockResult;
use libtock::timer::Duration;
#[libtock::main]
async fn main() -> TockResult<()> {
let mut drivers = libtock::retrieve_drivers()?;
let leds_driver = drivers.leds.init_driver()?;
let mut timer_driver = drivers.timer.create_timer_driver();
let timer_driver = timer_driver.activate()?;
// blink_nibble assumes 4 leds.
assert_eq!(leds_driver.num_leds(), 4);
let mut buf = [0; 64];
loop {
drivers.rng.fill_buffer(&mut buf).await?;
for &x in buf.iter() {
blink_nibble(&leds_driver, x)?;
timer_driver.sleep(Duration::from_ms(100)).await?;
blink_nibble(&leds_driver, x >> 4)?;
timer_driver.sleep(Duration::from_ms(100)).await?;
}
}
}
// Takes the 4 least-significant bits of x, and turn the 4 leds on/off accordingly.
fn blink_nibble(leds_driver: &LedsDriver, x: u8) -> TockResult<()> {
for i in 0..4 {
let led = leds_driver.get(i)?;
if (x >> i) & 1 != 0 {
led.on()?;
} else {
led.off()?;
}
}
Ok(())
}