Added keyboard_test example

This commit is contained in:
ZacJW 2025-07-13 19:22:28 +01:00
parent 4853df4cc9
commit 15c7b0ef1e
4 changed files with 150 additions and 0 deletions

View file

@ -0,0 +1,24 @@
[package]
name = "keyboard_test"
version = "0.1.0"
edition = "2024"
[[bin]]
name = "keyboard_test"
harness = false # do not use the built in cargo test harness -> resolve rust-analyzer errors
[profile.release]
opt-level = "s"
debug = true
panic = "abort"
[profile.dev]
debug = true # Symbols are nice and they don't increase the size on Flash
opt-level = "z"
panic = "abort"
[dependencies]
cardputer-bsc-nostd = { version = "0.1.0", path = "../.." }
esp-hal = {version = "=1.0.0-beta.1", features = ["esp32s3", "unstable"]}
esp-println = { version = "0.14.0", features = ["esp32s3"] }
esp-bootloader-esp-idf = "0.1.0"

View file

@ -0,0 +1,52 @@
fn main() {
linker_be_nice();
// make sure linkall.x is the last linker script (otherwise might cause problems with flip-link)
println!("cargo:rustc-link-arg=-Tlinkall.x");
}
fn linker_be_nice() {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 {
let kind = &args[1];
let what = &args[2];
match kind.as_str() {
"undefined-symbol" => match what.as_str() {
"_defmt_timestamp" => {
eprintln!();
eprintln!("💡 `defmt` not found - make sure `defmt.x` is added as a linker script and you have included `use defmt_rtt as _;`");
eprintln!();
}
"_stack_start" => {
eprintln!();
eprintln!("💡 Is the linker script `linkall.x` missing?");
eprintln!();
}
"esp_wifi_preempt_enable"
| "esp_wifi_preempt_yield_task"
| "esp_wifi_preempt_task_create" => {
eprintln!();
eprintln!("💡 `esp-wifi` has no scheduler enabled. Make sure you have the `builtin-scheduler` feature enabled, or that you provide an external scheduler.");
eprintln!();
}
"embedded_test_linker_file_not_added_to_rustflags" => {
eprintln!();
eprintln!("💡 `embedded-test` not found - make sure `embedded-test.x` is added as a linker script for tests");
eprintln!();
}
_ => (),
},
// we don't have anything helpful for "missing-lib" yet
_ => {
std::process::exit(1);
}
}
std::process::exit(0);
}
println!(
"cargo:rustc-link-arg=-Wl,--error-handling-script={}",
std::env::current_exe().unwrap().display()
);
}

View file

@ -0,0 +1,11 @@
# keyboard_test example
This is a simple test of the cardputer's keyboard. It logs key presses and releases over the USB serial.
## Run this example
With this repo cloned, run the following from either the root or this directory:
```shell
cargo espflash flash --release --package keyboard_test --monitor
```

View file

@ -0,0 +1,63 @@
#![no_std]
#![no_main]
use core::panic::PanicInfo;
use esp_hal::main;
use esp_println::println;
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("{info}");
esp_hal::system::software_reset()
}
esp_bootloader_esp_idf::esp_app_desc!();
#[main]
fn entrypoint() -> ! {
_main();
println!("main exited, entering infinite loop");
loop {}
}
#[allow(static_mut_refs)]
fn _main() {
let peripherals = esp_hal::init(esp_hal::Config::default());
let mut keyboard = cardputer_bsc_nostd::keyboard::Keyboard::new(
peripherals.GPIO8,
peripherals.GPIO9,
peripherals.GPIO11,
peripherals.GPIO13,
peripherals.GPIO15,
peripherals.GPIO3,
peripherals.GPIO4,
peripherals.GPIO5,
peripherals.GPIO6,
peripherals.GPIO7,
);
loop {
keyboard.scan();
let key_set = keyboard.pressed_keys();
if !key_set.is_none() {
for &(key_name, key) in cardputer_bsc_nostd::keyboard::Key::flags() {
if key_set.contains(key) {
println!("Pressed {key_name}");
}
}
}
keyboard.clear_pressed_keys();
let key_set = keyboard.released_keys();
if !key_set.is_none() {
for &(key_name, key) in cardputer_bsc_nostd::keyboard::Key::flags() {
if key_set.contains(key) {
println!("Released {key_name}");
}
}
}
keyboard.clear_released_keys();
esp_hal::delay::Delay::new().delay_millis(10);
}
}