Woyten | 5391163 | 2018-01-01 22:07:30 +0100 | [diff] [blame] | 1 | #![no_std] |
| 2 | |
torfmaster | c84685a | 2019-02-08 19:23:22 +0100 | [diff] [blame] | 3 | use libtock::electronics::ShiftRegister; |
torfmaster | 3d60524 | 2019-12-16 22:36:39 +0100 | [diff] [blame] | 4 | use libtock::result::TockResult; |
torfmaster | c84685a | 2019-02-08 19:23:22 +0100 | [diff] [blame] | 5 | use libtock::timer::Duration; |
Woyten | 5391163 | 2018-01-01 22:07:30 +0100 | [diff] [blame] | 6 | |
Johnathan Van Why | 074fa7d | 2020-10-14 17:06:50 -0700 | [diff] [blame] | 7 | libtock_core::stack_size! {0x800} |
| 8 | |
Woyten | 5391163 | 2018-01-01 22:07:30 +0100 | [diff] [blame] | 9 | fn number_to_bits(n: u8) -> [bool; 8] { |
| 10 | match n { |
Woyten | 595f72f | 2018-01-05 00:11:34 +0100 | [diff] [blame] | 11 | 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], |
Woyten | 5391163 | 2018-01-01 22:07:30 +0100 | [diff] [blame] | 22 | } |
| 23 | } |
| 24 | |
Woyten | 595f72f | 2018-01-05 00:11:34 +0100 | [diff] [blame] | 25 | // Example works on a shift register on P0.03, P0.04, P0.28 |
torfmaster | b06f637 | 2019-12-17 09:20:48 +0100 | [diff] [blame] | 26 | #[libtock::main] |
torfmaster | 3d60524 | 2019-12-16 22:36:39 +0100 | [diff] [blame] | 27 | async fn main() -> TockResult<()> { |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 28 | let mut drivers = libtock::retrieve_drivers()?; |
Woyten | b50a987 | 2019-12-01 01:12:57 +0100 | [diff] [blame] | 29 | |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 30 | let mut gpio_driver = drivers.gpio.init_driver()?; |
Woyten | b50a987 | 2019-12-01 01:12:57 +0100 | [diff] [blame] | 31 | 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); |
Woyten | 5391163 | 2018-01-01 22:07:30 +0100 | [diff] [blame] | 39 | |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 40 | let mut driver = drivers.timer.create_timer_driver(); |
torfmaster | f7ac7c9 | 2019-11-17 00:33:28 +0100 | [diff] [blame] | 41 | let timer_driver = driver.activate()?; |
| 42 | |
Woyten | 5391163 | 2018-01-01 22:07:30 +0100 | [diff] [blame] | 43 | let mut i = 0; |
Woyten | 193a7af | 2019-11-14 21:19:48 +0100 | [diff] [blame] | 44 | loop { |
| 45 | i = (i + 1) % 11; |
Woyten | 22bc254 | 2019-11-15 23:18:40 +0100 | [diff] [blame] | 46 | shift_register.write_bits(&number_to_bits(i))?; |
torfmaster | 6ecb72f | 2019-12-18 12:17:34 +0100 | [diff] [blame] | 47 | timer_driver.sleep(Duration::from_ms(200)).await?; |
Woyten | 193a7af | 2019-11-14 21:19:48 +0100 | [diff] [blame] | 48 | } |
Woyten | 5391163 | 2018-01-01 22:07:30 +0100 | [diff] [blame] | 49 | } |