2022-07-16 08:32:10 +00:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2022-07-16 14:42:41 +00:00
|
|
|
mod buttons;
|
|
|
|
mod state;
|
2022-07-16 08:32:10 +00:00
|
|
|
|
2022-07-16 14:42:41 +00:00
|
|
|
use core::panic::PanicInfo;
|
|
|
|
use ufmt::uDisplay;
|
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};
|
|
|
|
use buttons::Button;
|
|
|
|
use state::State;
|
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-16 14:42:41 +00:00
|
|
|
delay_ms(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PinId(usize);
|
|
|
|
|
|
|
|
impl uDisplay for PinId {
|
|
|
|
fn fmt<W>(&self, fmt: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
|
|
|
|
where
|
|
|
|
W: ufmt::uWrite + ?Sized,
|
|
|
|
{
|
|
|
|
// fmt.
|
|
|
|
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[arduino_hal::entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
let dp = Peripherals::take().unwrap();
|
|
|
|
let pins = pins!(dp);
|
|
|
|
|
|
|
|
let mut buttons = [
|
|
|
|
Button {
|
|
|
|
pin: pins.d5.downgrade(),
|
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
|
|
|
pin: pins.d6.downgrade(),
|
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
|
|
|
pin: pins.d7.downgrade(),
|
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
|
|
|
pin: pins.d8.downgrade(),
|
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
|
|
|
pin: pins.d9.downgrade(),
|
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
|
|
|
pin: pins.d10.downgrade(),
|
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
|
|
|
pin: pins.d11.downgrade(),
|
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
Button {
|
|
|
|
pin: pins.d12.downgrade(),
|
|
|
|
is_low: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut serial = default_serial!(dp, pins, 57600);
|
|
|
|
let mut current_state = State::WaitingForInput;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match current_state {
|
|
|
|
State::WaitingForInput => {
|
|
|
|
for (id, button) in buttons.iter_mut().enumerate() {
|
|
|
|
if button.pin.is_high() {
|
|
|
|
if button.is_low {
|
|
|
|
button.is_low = false;
|
|
|
|
|
|
|
|
// let s = (id + 1).to_string();
|
|
|
|
ufmt::uwriteln!(&mut serial, "HIGH: {}", id + 1).unwrap();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !button.is_low {
|
|
|
|
button.is_low = true;
|
|
|
|
|
|
|
|
// let s = (id + 1).to_string();
|
|
|
|
ufmt::uwriteln!(&mut serial, "LOW: {}\n ---", id + 1).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
State::WaitingForAcknowledge => (),
|
|
|
|
}
|
2022-07-16 08:32:10 +00:00
|
|
|
}
|
|
|
|
}
|