Newer
Older
use embedded_hal::{
blocking::delay::{DelayMs, DelayUs},
digital::v2::{InputPin, OutputPin},
};
use rp2040_hal::Timer;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use rtt_target::{rprint, rprintln};
const NONE: char = '\0';
const CALL_END: char = '\x01';
const L_SHIFT: char = '\x02';
const R_SHIFT: char = '\x03';
const ALT: char = '\x04';
const SYM: char = '\x05';
const BACK: char = '\x06';
const MENU: char = '\x07';
const BACKSPACE: char = '\x08';
const CALL_START: char = '\x09';
const TOUCHPAD_BUTTON: char = '\x0B';
const ENTER: char = '\n';
const KEYMAP: [[char; 8]; 6] = [
[CALL_END, ENTER, 'b', 'y', 'n', 'j', 'u', 'h'],
[NONE, BACKSPACE, '$', 'i', 'm', 'k', 'o', 'l'],
[NONE, 'p', 'x', 'd', 'z', L_SHIFT, 'e', 's'],
[NONE, R_SHIFT, 'v', 't', 'c', 'f', 'r', 'g'],
[NONE, 'a', ALT, SYM, ' ', '0', 'q', 'w'],
[
NONE,
NONE,
BACK,
MENU,
NONE,
CALL_START,
NONE,
TOUCHPAD_BUTTON,
],
];
enum KeyCode {
CallStart,
Menu,
Touchpad,
Back,
CallEnd,
Backspace,
Enter,
LeftShift,
RightShift,
Alt,
Sym,
Char(char),
}
impl KeyCode {
fn from_keyboard(c: char, alt: bool, shift: bool) -> Self {
match (alt, shift, c) {
(_, _, CALL_END) => KeyCode::CallEnd,
(_, _, L_SHIFT) => KeyCode::LeftShift,
(_, _, R_SHIFT) => KeyCode::RightShift,
(_, _, ALT) => KeyCode::Alt,
(_, _, SYM) => KeyCode::Sym,
(_, _, BACK) => KeyCode::Back,
(_, _, MENU) => KeyCode::Menu,
(_, _, BACKSPACE) => KeyCode::Backspace,
(_, _, CALL_START) => KeyCode::CallStart,
(_, _, TOUCHPAD_BUTTON) => KeyCode::Touchpad,
(_, _, ENTER) => KeyCode::Enter,
(false, false, _) => KeyCode::Char(c),
(true, _, _) => KeyCode::Char(altify(c)),
(false, true, _) => KeyCode::Char(c.to_ascii_uppercase()),
}
}
}
impl Display for KeyCode {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
KeyCode::CallEnd => write!(f, "<CALLEND>"),
KeyCode::Backspace => write!(f, "<BACKSPACE>"),
KeyCode::Enter => write!(f, "\n"),
KeyCode::CallStart => write!(f, "<CALLSTART>"),
KeyCode::Menu => write!(f, "<MENU>"),
KeyCode::Touchpad => write!(f, "<TOUCHPAD>"),
KeyCode::Back => write!(f, "<BACK>"),
KeyCode::LeftShift => write!(f, "<LEFTSHIFT>"),
KeyCode::RightShift => write!(f, "<RIGHTSHIFT>"),
KeyCode::Alt => write!(f, "<ALT>"),
KeyCode::Sym => write!(f, "<SYM>"),
KeyCode::Char(c) => write!(f, "{c}"),
}
}
}
pub struct Keyboard<
// col
CCLK: OutputPin<Error = Infallible>,
CSER: OutputPin<Error = Infallible>,
CRCLK: OutputPin<Error = Infallible>,
// row
RCLK: OutputPin<Error = Infallible>,
RSER: InputPin,
RLD: OutputPin<Error = Infallible>,
> {
timer: Timer,
col_clk: CCLK,
col_ser: CSER,
col_rclk: CRCLK,
row_clk: RCLK,
row_ser: RSER,
row_ld: RLD,
impl<
CCLK: OutputPin<Error = Infallible>,
CSER: OutputPin<Error = Infallible>,
CRCLK: OutputPin<Error = Infallible>,
RCLK: OutputPin<Error = Infallible>,
RSER: InputPin<Error = Infallible>,
RLD: OutputPin<Error = Infallible>,
> Keyboard<CCLK, CSER, CRCLK, RCLK, RSER, RLD>
{
timer: Timer,
col_clk: CCLK,
col_ser: CSER,
col_rclk: CRCLK,
row_clk: RCLK,
row_ser: RSER,
row_ld: RLD,
col_clk,
col_ser,
col_rclk,
row_clk,
row_ser,
row_ld,
// +2 gets us to QF (col 6)
self.write_cols(1 << (x + 2));
&mut self.timer,
&mut self.row_clk,
&mut self.row_ser,
&mut self.row_ld,
);
state |= u64::from(y) << (x * 8);
}
let delta = (!self.last_state) & state;
for x in 0..6 {
for y in 0..8 {
if delta & (1 << (x * 8 + y)) > 0 {
let key = KEYMAP[usize::try_from(x).unwrap()][usize::try_from(y).unwrap()];
if key != NONE {
let keycode = KeyCode::from_keyboard(
key,
is_alt_pressed(state),
is_shift_pressed(state),
);
rprint!("{}", keycode);
} else {
rprint!("\n({}, {})", x, y);
}
}
}
// 1s in val are low
// 0s are high
// indexes from furthest pin (QH on second shift register)
fn write_cols(&mut self, val: u16) {
for i in 0..16 {
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
self.col_clk.set_low().unwrap();
self.col_clk.set_high().unwrap();
}
self.col_rclk.set_low().unwrap();
self.col_rclk.set_high().unwrap();
}
}
fn read_rows<
RCLK: OutputPin<Error = Infallible>,
RSER: InputPin<Error = Infallible>,
RLD: OutputPin<Error = Infallible>,
>(
timer: &mut Timer,
row_clk: &mut RCLK,
row_ser: &mut RSER,
row_ld: &mut RLD,
) -> u8 {
row_ld.set_low().unwrap();
row_ld.set_high().unwrap();
let mut out = 0;
for i in 0..8 {
timer.delay_us(10);
if row_ser.is_low().unwrap() {
out |= 1 << i;
}
row_clk.set_low().unwrap();
row_clk.set_high().unwrap();
}
out
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
fn is_alt_pressed(state: u64) -> bool {
(state & (1 << (4 * 8 + 2))) > 0
}
fn is_shift_pressed(state: u64) -> bool {
let left = (state & (1 << (2 * 8 + 5))) > 0;
let right = (state & (1 << (3 * 8 + 1))) > 0;
left || right
}
fn altify(c: char) -> char {
match c {
'q' => '#',
'w' => '1',
'e' => '2',
'r' => '3',
't' => '(',
'y' => ')',
'u' => '_',
'i' => '-',
'o' => '+',
'p' => '@',
'a' => '*',
's' => '4',
'd' => '5',
'f' => '6',
'g' => '/',
'h' => ':',
'j' => ';',
'k' => ',',
'l' => '"',
'z' => '7',
'x' => '8',
'c' => '9',
'v' => '?',
'b' => '!',
'n' => ',',
'm' => '.',
_ => c,
}
}