Start implementing arduino code

This commit is contained in:
hodasemi 2022-07-16 16:42:41 +02:00
parent f07932f225
commit eb876b3468
4 changed files with 129 additions and 12 deletions

View file

@ -12,8 +12,9 @@ bench = false
[dependencies] [dependencies]
panic-halt = "0.2" panic-halt = "0.2"
avr-device = "*"
arduino-hal = { git = "https://github.com/Rahix/avr-hal", features = ["arduino-uno"], rev = "1aacefb335517f85d0de858231e11055d9768cdf" } arduino-hal = { git = "https://github.com/Rahix/avr-hal", features = ["arduino-uno"], rev = "1aacefb335517f85d0de858231e11055d9768cdf" }
# branch = "main", ufmt = "*"
# Configure the build for minimal size - AVRs have very little program memory # Configure the build for minimal size - AVRs have very little program memory
[profile.dev] [profile.dev]

21
arduino/src/buttons.rs Normal file
View file

@ -0,0 +1,21 @@
use arduino_hal::{
hal::port::Dynamic,
port::{
mode::{Floating, Input},
Pin,
},
};
pub const BUTTON_COUNT: usize = 8;
// pub const BUTTONS: [Button; BUTTON_COUNT] = {
// Button {
// pin_id: 6,
// // last_state:
// }
// };
pub struct Button {
pub pin: Pin<Input<Floating>, Dynamic>,
pub is_low: bool,
}

View file

@ -1,24 +1,115 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
// use core::panic::PanicInfo; mod buttons;
mod state;
// #[panic_handler] use core::panic::PanicInfo;
// fn panic(info: &PanicInfo) -> ! { use ufmt::uDisplay;
// loop {}
// }
use panic_halt as _; use arduino_hal::{default_serial, delay_ms, pins, Peripherals};
use buttons::Button;
use state::State;
#[arduino_hal::entry] #[panic_handler]
fn main() -> ! { fn panic(_info: &PanicInfo) -> ! {
let dp = arduino_hal::Peripherals::take().unwrap(); // disable interrupts - firmware has panicked so no ISRs should continue running
let pins = arduino_hal::pins!(dp); 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(); let mut led = pins.d13.into_output();
loop { loop {
led.toggle(); led.toggle();
arduino_hal::delay_ms(1000); 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 => (),
}
} }
} }

4
arduino/src/state.rs Normal file
View file

@ -0,0 +1,4 @@
pub enum State {
WaitingForInput,
WaitingForAcknowledge,
}