Split firmware inte left.rs and right.rs.

This commit is contained in:
2023-03-18 00:58:42 +01:00
parent 5c154141d0
commit 4d02d0825a
19 changed files with 416 additions and 245 deletions

View File

@ -3,7 +3,9 @@ use core::sync::atomic::{AtomicU16, Ordering};
use alloc::{boxed::Box, vec::Vec};
use embassy_executor::Spawner;
use embassy_rp::gpio::{AnyPin, Input, Pin, Pull};
use log::{error, warn};
use embassy_time::{Duration, Timer};
use futures::{select_biased, FutureExt};
use log::{debug, error, info, warn};
use tgnt::{button::Button, layer::Layer};
use crate::usb::keyboard::KB_REPORT;
@ -24,6 +26,11 @@ impl KeyboardConfig {
return;
}
info!(
"setting up keyboard layout with {} layer(s)",
self.layers.len()
);
let layers = Box::leak(self.layers.into_boxed_slice());
for (i, layer) in layers.iter().enumerate() {
if layer.buttons.len() != SWITCH_COUNT {
@ -43,7 +50,10 @@ impl KeyboardConfig {
}
}
const MOD_TAP_TIME: Duration = Duration::from_millis(100);
const SWITCH_COUNT: usize = 18;
/// Task for monitoring a single switch pin, and handling button presses.
#[embassy_executor::task(pool_size = 18)]
async fn switch_task(switch_num: usize, pin: AnyPin, layers: &'static [Layer]) -> ! {
let _pin_nr = pin.pin();
@ -51,27 +61,39 @@ async fn switch_task(switch_num: usize, pin: AnyPin, layers: &'static [Layer]) -
loop {
// pins are pull-up, so when the switch is pressed they are brought low.
pin.wait_for_low().await;
// TODO: do we need debouncing?
// get current layer
let mut current_layer = CURRENT_LAYER.load(Ordering::Relaxed);
let layer_count = layers.len() as u16;
if current_layer >= layer_count {
error!("current layer was out of bounds for some reason ({current_layer})");
current_layer = 0;
}
let Some(Layer { buttons }) = layers.get(usize::from(current_layer)) else {
error!("current layer was out of bounds for some reason ({current_layer})");
CURRENT_LAYER.store(0, Ordering::Relaxed);
continue;
};
// and current button
let Some(button) = buttons.get(switch_num) else {
warn!("no button defined for switch {switch_num}");
continue;
};
debug!("switch {switch_num} button {button:?} pressed");
let wait_for_release = async {
pin.wait_for_high().await;
debug!("switch {switch_num} button {button:?} released");
};
match button {
&Button::Key(key) => {
KB_REPORT.lock().await.press_key(key);
pin.wait_for_high().await;
wait_for_release.await;
KB_REPORT.lock().await.release_key(key);
continue;
}
@ -83,7 +105,19 @@ async fn switch_task(switch_num: usize, pin: AnyPin, layers: &'static [Layer]) -
//continue;
}
Button::ModTap { keycode, modifier } => {
// TODO
select_biased! {
_ = Timer::after(MOD_TAP_TIME).fuse() => {
// TODO: Modifier
pin.wait_for_high().await;
continue;
}
_ = wait_for_release.fuse() => {
KB_REPORT.lock().await.press_key(*keycode);
Timer::after(Duration::from_millis(10)).await;
KB_REPORT.lock().await.release_key(*keycode);
continue;
}
}
}
Button::NextLayer => {
let next_layer = (current_layer + 1) % layer_count;
@ -96,7 +130,7 @@ async fn switch_task(switch_num: usize, pin: AnyPin, layers: &'static [Layer]) -
Button::None => {}
}
pin.wait_for_high().await;
wait_for_release.await;
}
}