Skip to content

Commit

Permalink
Use a count-down timer instead of a delay for hopefully more accurate…
Browse files Browse the repository at this point in the history
… key-scan timing (#60)
  • Loading branch information
bschwind authored Jun 30, 2024
1 parent 11c0a56 commit 39b9c56
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions firmware/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions firmware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cortex-m = "0.7"
cortex-m-rt = "0.7"
embedded-hal = "1"
embedded-time = "0.12"
fugit = "0.3"
panic-reset = "0.1"
rp2040-boot2 = "0.3"
rp2040-hal = { version = "0.10", features = ["rt", "critical-section-impl"] }
Expand Down
18 changes: 13 additions & 5 deletions firmware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![no_std]

use crate::key_scan::TRANSPOSED_NORMAL_LAYER_MAPPING;
use cortex_m::prelude::_embedded_hal_timer_CountDown;
use usb_device::class::UsbClass;
mod debounce;
mod hid_descriptor;
Expand All @@ -17,6 +18,7 @@ use critical_section::Mutex;
use defmt::{error, info, warn};
use defmt_rtt as _;
use embedded_hal::digital::{InputPin, OutputPin};
use fugit::ExtU32;
use panic_probe as _;
use rp2040_hal::{
pac::{self, interrupt},
Expand Down Expand Up @@ -131,6 +133,7 @@ fn main() -> ! {

// Initialize a delay for accurate sleeping.
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
let timer = rp2040_hal::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks);

let mut modifier_mask = [[false; NUM_ROWS]; NUM_COLS];
for (col, mapping_col) in modifier_mask.iter_mut().zip(TRANSPOSED_NORMAL_LAYER_MAPPING) {
Expand Down Expand Up @@ -204,12 +207,17 @@ fn main() -> ! {
pac::NVIC::unmask(pac::Interrupt::USBCTRL_IRQ);
}
info!("Entering main loop");

let mut tick_count_down = timer.count_down();
tick_count_down.start(1.millis());

loop {
let scan = KeyScan::scan(&mut rows, &mut cols, &mut delay, &mut debounce);
critical_section::with(|cs| {
KEYBOARD_REPORT.replace(cs, scan.into());
});
delay.delay_ms(SCAN_LOOP_RATE_MS);
if tick_count_down.wait().is_ok() {
let scan = KeyScan::scan(&mut rows, &mut cols, &mut delay, &mut debounce);
critical_section::with(|cs| {
KEYBOARD_REPORT.replace(cs, scan.into());
});
}
}
}

Expand Down

0 comments on commit 39b9c56

Please sign in to comment.