Read key inputs

This commit is contained in:
2021-05-07 01:20:32 +02:00
parent 684dbbd882
commit 5418c1b232
4 changed files with 116 additions and 4 deletions

View File

@ -15,6 +15,10 @@ mod usb_shell;
mod util; mod util;
mod wait_delay; mod wait_delay;
use crate::ring_buffer::RingBuffer;
use crate::util::ByteStr;
use core::fmt::Write;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use hal::clock::GenericClockController; use hal::clock::GenericClockController;
use hal::entry; use hal::entry;
use hal::pac::{interrupt, CorePeripherals, Peripherals}; use hal::pac::{interrupt, CorePeripherals, Peripherals};
@ -34,9 +38,16 @@ fn main() -> ! {
); );
let mut pins = hal::Pins::new(peripherals.PORT); let mut pins = hal::Pins::new(peripherals.PORT);
let mut red_led = pins.d13.into_open_drain_output(&mut pins.port); let mut red_led = pins.d13.into_open_drain_output(&mut pins.port);
//let mut delay = Delay::new(core.SYST, &mut clocks);
let mut delay = WaitDelay::new(core.SYST, &mut clocks); let mut delay = WaitDelay::new(core.SYST, &mut clocks);
let col0 = pins.d9.into_pull_up_input(&mut pins.port);
let col1 = pins.d12.into_pull_up_input(&mut pins.port);
let cols = [&col0 as &dyn InputPin<Error = ()>, &col1];
let mut row0 = pins.d11.into_open_drain_output(&mut pins.port);
let mut row1 = pins.d7.into_open_drain_output(&mut pins.port);
let mut rows = [&mut row0 as &mut dyn OutputPin<Error = ()>, &mut row1];
let mut blip = || { let mut blip = || {
red_led.set_low().ok(); red_led.set_low().ok();
delay.delay_ms(100u16); delay.delay_ms(100u16);
@ -54,11 +65,59 @@ fn main() -> ! {
&mut blip, &mut blip,
); );
let mut matrix = [[false; 2]; 2];
// Flash the LED in a spin loop to demonstrate that USB is // Flash the LED in a spin loop to demonstrate that USB is
// entirely interrupt driven. // entirely interrupt driven.
loop { loop {
blip(); let mut switched_buttons = RingBuffer::<(usize, usize, bool), 4>::new();
for (i, row) in rows.iter_mut().enumerate() {
row.set_low().ok();
for (j, col) in cols.iter().enumerate() {
let state = col.is_low().unwrap_or(false);
if matrix[i][j] != state {
matrix[i][j] = state;
switched_buttons.push_back((i, j, state)).ok();
}
}
row.set_high().ok();
}
if matrix.iter().flatten().any(|&f| f) {
red_led.set_high().ok();
} else {
red_led.set_low().ok();
}
while let Some((i, j, pressed)) = switched_buttons.pop_front() {
let mut buf = ByteStr::<255>::new();
write!(
&mut buf,
"button [{}, {}] {}",
i,
j,
if pressed { "pressed" } else { "released" }
)
.ok();
usb.println(&buf);
}
/*
n += buf.write(b"buttons: ").unwrap_or(0);
for &pressed in matrix.iter().flatten() {
if pressed {
n += buf.write("DOWN").unwrap_or(0);
} else {
n += buf.write("UP").unwrap_or(0);
}
n += buf.write(" ").unwrap_or(0);
}
*/
delay.delay_ms(50u16);
usb.poll(); usb.poll();
//usb.println(b"woop"); //usb.println(b"woop");
} }
} }

View File

@ -5,7 +5,7 @@ pub struct RingBuffer<T, const N: usize> {
} }
impl<const N: usize> RingBuffer<u8, N> { impl<const N: usize> RingBuffer<u8, N> {
pub const fn new() -> Self { pub const fn new_u8() -> Self {
RingBuffer { RingBuffer {
buffer: [0; N], buffer: [0; N],
start: 0, start: 0,
@ -14,6 +14,19 @@ impl<const N: usize> RingBuffer<u8, N> {
} }
} }
impl<T, const N: usize> RingBuffer<T, N>
where
T: Default + Copy,
{
pub fn new() -> Self {
RingBuffer {
buffer: [Default::default(); N],
start: 0,
end: 0,
}
}
}
impl<T, const N: usize> RingBuffer<T, N> impl<T, const N: usize> RingBuffer<T, N>
where where
T: Copy, T: Copy,

View File

@ -28,7 +28,7 @@ static USB_READY: AtomicBool = AtomicBool::new(false);
static mut USB_BUS: Option<UsbDevice<UsbBus>> = None; static mut USB_BUS: Option<UsbDevice<UsbBus>> = None;
static mut USB_SERIAL: Option<SerialPort<UsbBus>> = None; static mut USB_SERIAL: Option<SerialPort<UsbBus>> = None;
static INPUT_BUFFER: RacyCell<RingBuffer<u8, 255>> = RacyCell::new(RingBuffer::new()); static INPUT_BUFFER: RacyCell<RingBuffer<u8, 255>> = RacyCell::new(RingBuffer::new_u8());
const PROMPT: &[u8] = b"[neowatch]# "; const PROMPT: &[u8] = b"[neowatch]# ";

View File

@ -1 +1,41 @@
pub mod cell; pub mod cell;
use core::fmt::{self, Write};
use core::ops::Deref;
pub struct ByteStr<const N: usize> {
bytes: [u8; N],
len: usize,
}
impl<const N: usize> ByteStr<N> {
pub fn new() -> Self {
ByteStr {
bytes: [0; N],
len: 0,
}
}
}
impl<const N: usize> Deref for ByteStr<N> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.bytes[..self.len]
}
}
impl<const N: usize> Write for ByteStr<N> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let b = s.as_bytes();
if (self.bytes.len() - self.len) < b.len() {
return Err(fmt::Error);
}
self.bytes[self.len..self.len + b.len()].copy_from_slice(b);
self.len += b.len();
Ok(())
}
}