|
| 1 | +#![no_main] |
| 2 | +#![no_std] |
| 3 | +use core::cell::Cell; |
| 4 | +use core::fmt::Write; |
| 5 | +use libtock::buttons::{ButtonListener, ButtonState, Buttons}; |
| 6 | +use libtock::console::Console; |
| 7 | +use libtock::runtime::{set_main, stack_size, TockSyscalls}; |
| 8 | +use libtock_platform::share; |
| 9 | +use libtock_platform::ErrorCode; |
| 10 | +use libtock_platform::Syscalls; |
| 11 | + |
| 12 | +use embedded_graphics_libtock::tock_screen::TockMonochrome8BitPage128x64Screen; |
| 13 | + |
| 14 | +use embedded_graphics::pixelcolor::BinaryColor; |
| 15 | +use embedded_graphics::prelude::Point; |
| 16 | +use embedded_graphics::prelude::Primitive; |
| 17 | +use embedded_graphics::primitives::{Circle, PrimitiveStyle}; |
| 18 | +use embedded_graphics::Drawable; |
| 19 | + |
| 20 | +set_main! {main} |
| 21 | +stack_size! {4000} |
| 22 | + |
| 23 | +fn run() -> Result<(), ErrorCode> { |
| 24 | + let mut screen = TockMonochrome8BitPage128x64Screen::new(); |
| 25 | + |
| 26 | + let width = screen.get_width(); |
| 27 | + let height = screen.get_height(); |
| 28 | + |
| 29 | + let button_count = Buttons::count()?; |
| 30 | + writeln!(Console::writer(), "[BUTTONS] Count: {}", button_count).unwrap(); |
| 31 | + |
| 32 | + // Calculate where the buttons should be drawn. |
| 33 | + let button_padding_px = (button_count - 1) * 2; |
| 34 | + let max_x = (width - button_padding_px) / button_count; |
| 35 | + let max_y = height - 2; |
| 36 | + let diameter = core::cmp::min(max_x, max_y); |
| 37 | + let buttons_width = (diameter * button_count) + button_padding_px; |
| 38 | + let padding_left_px = (width - buttons_width) / 2; |
| 39 | + let y = (height / 2) - (diameter / 2); |
| 40 | + |
| 41 | + // Draw the initial button outlines. |
| 42 | + for i in 0..button_count { |
| 43 | + let x = padding_left_px + ((diameter + 2) * i); |
| 44 | + |
| 45 | + let _ = Circle::new(Point::new(x as i32, y as i32), diameter) |
| 46 | + .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1)) |
| 47 | + .draw(&mut screen); |
| 48 | + } |
| 49 | + let _ = screen.flush(); |
| 50 | + |
| 51 | + // Now wait for button presses. Record what happened in the upcall. |
| 52 | + let buttons: [Cell<ButtonState>; 10] = [const { Cell::new(ButtonState::Released) }; 10]; |
| 53 | + let changed: Cell<bool> = Cell::new(false); |
| 54 | + |
| 55 | + let listener = ButtonListener(|button, state| { |
| 56 | + // If the button state changed, record it. |
| 57 | + if buttons[button as usize].get() != state { |
| 58 | + buttons[button as usize].set(state); |
| 59 | + changed.set(true); |
| 60 | + } |
| 61 | + }); |
| 62 | + share::scope(|subscribe| { |
| 63 | + // Subscribe to the button callback. |
| 64 | + Buttons::register_listener(&listener, subscribe).unwrap(); |
| 65 | + |
| 66 | + // Enable interrupts for each button press. |
| 67 | + for i in 0..button_count { |
| 68 | + Buttons::enable_interrupts(i).unwrap(); |
| 69 | + } |
| 70 | + |
| 71 | + // Wait for buttons to be pressed. |
| 72 | + loop { |
| 73 | + TockSyscalls::yield_wait(); |
| 74 | + |
| 75 | + // If a button state changed, re-draw the buttons. |
| 76 | + if changed.get() { |
| 77 | + changed.set(false); |
| 78 | + |
| 79 | + let mut screen = TockMonochrome8BitPage128x64Screen::new(); |
| 80 | + |
| 81 | + // Draw Circles |
| 82 | + for i in 0..button_count { |
| 83 | + let x = padding_left_px + ((diameter + 2) * i); |
| 84 | + |
| 85 | + // Draw outer circle |
| 86 | + let _ = Circle::new(Point::new(x as i32, y as i32), diameter) |
| 87 | + .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1)) |
| 88 | + .draw(&mut screen); |
| 89 | + |
| 90 | + match buttons[i as usize].get() { |
| 91 | + ButtonState::Pressed => { |
| 92 | + let _ = |
| 93 | + Circle::new(Point::new(x as i32 + 1, y as i32 + 1), diameter - 2) |
| 94 | + .into_styled(PrimitiveStyle::with_fill(BinaryColor::On)) |
| 95 | + .draw(&mut screen); |
| 96 | + } |
| 97 | + ButtonState::Released => { |
| 98 | + let _ = |
| 99 | + Circle::new(Point::new(x as i32 + 1, y as i32 + 1), diameter - 2) |
| 100 | + .into_styled(PrimitiveStyle::with_fill(BinaryColor::Off)) |
| 101 | + .draw(&mut screen); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + let _ = screen.flush(); |
| 106 | + } |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + Ok(()) |
| 111 | +} |
| 112 | + |
| 113 | +fn main() { |
| 114 | + match run() { |
| 115 | + Ok(()) => {} |
| 116 | + Err(_e) => { |
| 117 | + writeln!(Console::writer(), "[BUTTONS] Err could not run app").unwrap(); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments