Skip to content
Snippets Groups Projects
Commit be6b345e authored by Stephen D's avatar Stephen D
Browse files

keyboard

parent 0a30e7db
No related branches found
No related tags found
No related merge requests found
use embedded_hal::digital::v2::{InputPin, OutputPin};
use rp2040_hal::gpio::{DynPinId, FunctionSio, Pin, PullUp, SioInput};
use shift_register_driver::sipo::ShiftRegisterPin;
pub struct Keyboard {
cols: [ShiftRegisterPin<'static>; 7],
rows: [Pin<DynPinId, FunctionSio<SioInput>, PullUp>; 7],
}
impl Keyboard {
pub fn new(
mut cols: [ShiftRegisterPin<'static>; 7],
rows: [Pin<DynPinId, FunctionSio<SioInput>, PullUp>; 7],
) -> Self {
for c in &mut cols {
c.set_high().unwrap();
}
Self { cols, rows }
}
pub fn read_state(&mut self) -> u64 {
let mut out = 0;
for (x, c) in self.cols.iter_mut().enumerate() {
c.set_low().unwrap();
// TODO we might need a delay in here
for (y, r) in self.rows.iter_mut().enumerate() {
if r.is_low().unwrap() {
out |= 1 << (x * 7 + y);
}
}
c.set_high().unwrap();
}
out
}
}
#![no_std] #![no_std]
#![no_main] #![no_main]
mod keyboard;
#[rtic::app( #[rtic::app(
device = rp2040_hal::pac, device = rp2040_hal::pac,
dispatchers = [TIMER_IRQ_1] dispatchers = [TIMER_IRQ_1]
)] )]
mod app { mod app {
use crate::keyboard::Keyboard;
use cortex_m::delay::Delay; use cortex_m::delay::Delay;
use display_interface_spi::SPIInterfaceNoCS; use display_interface_spi::SPIInterfaceNoCS;
use embedded_graphics::{ use embedded_graphics::{
...@@ -19,7 +22,13 @@ mod app { ...@@ -19,7 +22,13 @@ mod app {
}; };
use embedded_hal::PwmPin; use embedded_hal::PwmPin;
use fugit::RateExtU32; use fugit::RateExtU32;
use hal::Clock; use hal::{
gpio::{
bank0::{Gpio0, Gpio1, Gpio2},
DynPinId, FunctionSio, Pin, PullDown, PullUp, SioInput, SioOutput,
},
Clock,
};
use mipidsi::Builder; use mipidsi::Builder;
use panic_probe as _; use panic_probe as _;
use rp2040_hal as hal; use rp2040_hal as hal;
...@@ -28,6 +37,12 @@ mod app { ...@@ -28,6 +37,12 @@ mod app {
const XTAL_FREQ_HZ: u32 = 12_000_000u32; const XTAL_FREQ_HZ: u32 = 12_000_000u32;
type MyShiftRegister = ShiftRegister16<
Pin<Gpio1, FunctionSio<SioOutput>, PullDown>,
Pin<Gpio2, FunctionSio<SioOutput>, PullDown>,
Pin<Gpio0, FunctionSio<SioOutput>, PullDown>,
>;
#[shared] #[shared]
struct Shared {} struct Shared {}
...@@ -77,11 +92,26 @@ mod app { ...@@ -77,11 +92,26 @@ mod app {
let sr_clk = pins.gpio1.into_push_pull_output(); let sr_clk = pins.gpio1.into_push_pull_output();
let sr_ser = pins.gpio0.into_push_pull_output(); let sr_ser = pins.gpio0.into_push_pull_output();
let sr_rclk = pins.gpio2.into_push_pull_output(); let sr_rclk = pins.gpio2.into_push_pull_output();
let sr = ShiftRegister16::new(sr_clk, sr_rclk, sr_ser); let sr =
cortex_m::singleton!(: MyShiftRegister = ShiftRegister16::new(sr_clk, sr_rclk, sr_ser))
.unwrap();
let sr_pins = sr.decompose(); let sr_pins = sr.decompose();
let [_led_b, _led_g, _led_r, _tp_reset, disp_reset, _, _, _, _, _, _, _, _, _, _, _] = let [_led_b, _led_g, _led_r, _tp_reset, disp_reset, _, _, _, col1, col2, col3, col4, col5, col6, col7, _unused] =
sr_pins; sr_pins;
// Keyboard
let cols = [col1, col2, col3, col4, col5, col6, col7];
let rows: [Pin<DynPinId, FunctionSio<SioInput>, PullUp>; 7] = [
pins.gpio9.into_pull_up_input().into_dyn_pin(),
pins.gpio8.into_pull_up_input().into_dyn_pin(),
pins.gpio7.into_pull_up_input().into_dyn_pin(),
pins.gpio6.into_pull_up_input().into_dyn_pin(),
pins.gpio5.into_pull_up_input().into_dyn_pin(),
pins.gpio4.into_pull_up_input().into_dyn_pin(),
pins.gpio3.into_pull_up_input().into_dyn_pin(),
];
let keyboard = Keyboard::new(cols, rows);
// SPI Display // SPI Display
let display_cs = pins.gpio16.into_push_pull_output(); let display_cs = pins.gpio16.into_push_pull_output();
let spi_mosi = pins.gpio15.into_function::<hal::gpio::FunctionSpi>(); let spi_mosi = pins.gpio15.into_function::<hal::gpio::FunctionSpi>();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment