MacroBoard-rs/arduino/src/main.rs

65 lines
1.9 KiB
Rust

#![no_std]
#![no_main]
mod buttons;
use core::panic::PanicInfo;
use arduino_hal::{default_serial, delay_ms, pins, Peripherals};
use buttons::{Button, BUTTON_COUNT};
const MESSAGE_START: char = '<';
const MESSAGE_END: char = '>';
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
// disable interrupts - firmware has panicked so no ISRs should continue running
avr_device::interrupt::disable();
// get the peripherals so we can access serial and the LED.
//
// SAFETY: Because main() already has references to the peripherals this is an unsafe
// operation - but because no other code can run after the panic handler was called,
// we know it is okay.
let dp = unsafe { Peripherals::steal() };
let pins = pins!(dp);
let mut led = pins.d13.into_output();
loop {
led.toggle();
delay_ms(500);
}
}
#[arduino_hal::entry]
fn main() -> ! {
let dp = Peripherals::take().unwrap();
let pins = pins!(dp);
pins.d13.into_output().set_high();
let mut buttons: [Button; BUTTON_COUNT] = [
Button::new(1, pins.d5.into_pull_up_input().downgrade()),
Button::new(2, pins.d6.into_pull_up_input().downgrade()),
Button::new(3, pins.d7.into_pull_up_input().downgrade()),
Button::new(4, pins.d8.into_pull_up_input().downgrade()),
Button::new(5, pins.d9.into_pull_up_input().downgrade()),
Button::new(6, pins.d10.into_pull_up_input().downgrade()),
Button::new(7, pins.d11.into_pull_up_input().downgrade()),
Button::new(8, pins.d12.into_pull_up_input().downgrade()),
];
let mut serial = default_serial!(dp, pins, 57600);
loop {
for button in buttons.iter_mut() {
button.check_state(|index, pressed| {
if !pressed {
ufmt::uwriteln!(&mut serial, "{}{}{}", MESSAGE_START, index, MESSAGE_END)
.unwrap();
}
})
}
}
}