2022-07-16 08:32:10 +00:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2022-07-16 14:42:41 +00:00
|
|
|
mod buttons;
|
2022-07-16 08:32:10 +00:00
|
|
|
|
2022-07-16 14:42:41 +00:00
|
|
|
use core::panic::PanicInfo;
|
2022-07-16 08:32:10 +00:00
|
|
|
|
2022-07-16 14:42:41 +00:00
|
|
|
use arduino_hal::{default_serial, delay_ms, pins, Peripherals};
|
2022-07-18 10:11:17 +00:00
|
|
|
use buttons::{Button, BUTTON_COUNT};
|
|
|
|
|
|
|
|
const MESSAGE_START: char = '<';
|
|
|
|
const MESSAGE_END: char = '>';
|
2022-07-16 08:32:10 +00:00
|
|
|
|
2022-07-16 14:42:41 +00:00
|
|
|
#[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);
|
2022-07-16 08:32:10 +00:00
|
|
|
|
|
|
|
let mut led = pins.d13.into_output();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
led.toggle();
|
2022-07-18 10:11:17 +00:00
|
|
|
delay_ms(500);
|
2022-07-16 14:42:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[arduino_hal::entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
let dp = Peripherals::take().unwrap();
|
|
|
|
let pins = pins!(dp);
|
|
|
|
|
2022-07-18 10:11:17 +00:00
|
|
|
pins.d13.into_output().set_high();
|
|
|
|
|
|
|
|
let mut buttons: [Button; BUTTON_COUNT] = [
|
2022-07-16 14:42:41 +00:00
|
|
|
Button {
|
2022-07-18 10:11:17 +00:00
|
|
|
index: 1,
|
|
|
|
pin: pins.d5.into_pull_up_input().downgrade(),
|
2022-07-16 14:42:41 +00:00
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
2022-07-18 10:11:17 +00:00
|
|
|
index: 2,
|
|
|
|
pin: pins.d6.into_pull_up_input().downgrade(),
|
2022-07-16 14:42:41 +00:00
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
2022-07-18 10:11:17 +00:00
|
|
|
index: 3,
|
|
|
|
pin: pins.d7.into_pull_up_input().downgrade(),
|
2022-07-16 14:42:41 +00:00
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
2022-07-18 10:11:17 +00:00
|
|
|
index: 4,
|
|
|
|
pin: pins.d8.into_pull_up_input().downgrade(),
|
2022-07-16 14:42:41 +00:00
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
2022-07-18 10:11:17 +00:00
|
|
|
index: 5,
|
|
|
|
pin: pins.d9.into_pull_up_input().downgrade(),
|
2022-07-16 14:42:41 +00:00
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
2022-07-18 10:11:17 +00:00
|
|
|
index: 6,
|
|
|
|
pin: pins.d10.into_pull_up_input().downgrade(),
|
2022-07-16 14:42:41 +00:00
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
2022-07-18 10:11:17 +00:00
|
|
|
index: 7,
|
|
|
|
pin: pins.d11.into_pull_up_input().downgrade(),
|
2022-07-16 14:42:41 +00:00
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
2022-07-18 10:11:17 +00:00
|
|
|
index: 8,
|
|
|
|
pin: pins.d12.into_pull_up_input().downgrade(),
|
2022-07-16 14:42:41 +00:00
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut serial = default_serial!(dp, pins, 57600);
|
|
|
|
|
|
|
|
loop {
|
2022-07-18 10:11:17 +00:00
|
|
|
for button in buttons.iter_mut() {
|
|
|
|
button.check_state(|index, pressed| {
|
|
|
|
if !pressed {
|
|
|
|
ufmt::uwriteln!(&mut serial, "{}{}{}", MESSAGE_START, index, MESSAGE_END)
|
|
|
|
.unwrap();
|
2022-07-16 14:42:41 +00:00
|
|
|
}
|
2022-07-18 10:11:17 +00:00
|
|
|
})
|
2022-07-16 14:42:41 +00:00
|
|
|
}
|
2022-07-16 08:32:10 +00:00
|
|
|
}
|
|
|
|
}
|