blob: 7befeb5df1fb0234c18fe967b996458d3d3515e7 [file] [log] [blame]
Woyten53911632018-01-01 22:07:30 +01001#![no_std]
2
torfmasterc84685a2019-02-08 19:23:22 +01003use libtock::electronics::ShiftRegister;
torfmaster3d605242019-12-16 22:36:39 +01004use libtock::result::TockResult;
torfmasterc84685a2019-02-08 19:23:22 +01005use libtock::timer::Duration;
Woyten53911632018-01-01 22:07:30 +01006
Johnathan Van Why074fa7d2020-10-14 17:06:50 -07007libtock_core::stack_size! {0x800}
8
Woyten53911632018-01-01 22:07:30 +01009fn number_to_bits(n: u8) -> [bool; 8] {
10 match n {
Woyten595f72f2018-01-05 00:11:34 +010011 1 => [false, false, false, true, false, true, false, false],
12 2 => [true, false, true, true, false, false, true, true],
13 3 => [true, false, true, true, false, true, true, false],
14 4 => [true, true, false, true, false, true, false, false],
15 5 => [true, true, true, false, false, true, true, false],
16 6 => [true, true, true, false, false, true, true, true],
17 7 => [false, false, true, true, false, true, false, false],
18 8 => [true, true, true, true, false, true, true, true],
19 9 => [true, true, true, true, false, true, true, false],
20 0 => [false, true, true, true, false, true, true, true],
21 _ => [false, false, false, false, true, false, false, false],
Woyten53911632018-01-01 22:07:30 +010022 }
23}
24
Woyten595f72f2018-01-05 00:11:34 +010025// Example works on a shift register on P0.03, P0.04, P0.28
torfmasterb06f6372019-12-17 09:20:48 +010026#[libtock::main]
torfmaster3d605242019-12-16 22:36:39 +010027async fn main() -> TockResult<()> {
Woytenb1226202020-01-13 22:47:48 +010028 let mut drivers = libtock::retrieve_drivers()?;
Woytenb50a9872019-12-01 01:12:57 +010029
Woytenb1226202020-01-13 22:47:48 +010030 let mut gpio_driver = drivers.gpio.init_driver()?;
Woytenb50a9872019-12-01 01:12:57 +010031 let mut gpios = gpio_driver.gpios();
32 let mut gpio0 = gpios.next().unwrap();
33 let gpio0 = gpio0.enable_output()?;
34 let mut gpio1 = gpios.next().unwrap();
35 let gpio1 = gpio1.enable_output()?;
36 let mut gpio2 = gpios.next().unwrap();
37 let gpio2 = gpio2.enable_output()?;
38 let mut shift_register = ShiftRegister::new(&gpio0, &gpio1, &gpio2);
Woyten53911632018-01-01 22:07:30 +010039
Woytenb1226202020-01-13 22:47:48 +010040 let mut driver = drivers.timer.create_timer_driver();
torfmasterf7ac7c92019-11-17 00:33:28 +010041 let timer_driver = driver.activate()?;
42
Woyten53911632018-01-01 22:07:30 +010043 let mut i = 0;
Woyten193a7af2019-11-14 21:19:48 +010044 loop {
45 i = (i + 1) % 11;
Woyten22bc2542019-11-15 23:18:40 +010046 shift_register.write_bits(&number_to_bits(i))?;
torfmaster6ecb72f2019-12-18 12:17:34 +010047 timer_driver.sleep(Duration::from_ms(200)).await?;
Woyten193a7af2019-11-14 21:19:48 +010048 }
Woyten53911632018-01-01 22:07:30 +010049}