Skip to content

Commit

Permalink
feat(core): add api to simply run the keyboard, update examples
Browse files Browse the repository at this point in the history
Signed-off-by: Haobo Gu <haobogu@outlook.com>
  • Loading branch information
HaoboGu committed Jan 22, 2024
1 parent e7eb197 commit 7ff6cac
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ members = [
"boards/stm32h7",
# "boards/stm32f4"
]
default-members = ["boards/stm32h7", "rmk"]
default-members = [
"rmk",
# Uncomment to choose one chip you want to use as default
# "boards/rp2040",
"boards/stm32h7",
# "boards/stm32f4"
]

# Profile dev: optimize for fast compiling with debug info
[profile.dev]
Expand Down
26 changes: 4 additions & 22 deletions boards/rp2040/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ mod vial;

use core::cell::RefCell;
use embassy_executor::Spawner;
use embassy_futures::join::join;
use embassy_rp::{
bind_interrupts,
flash::{Blocking, Flash},
gpio::{AnyPin, Input, Output},
peripherals::{self, USB},
usb::{Driver, InterruptHandler},
};
use embassy_time::Timer;

use defmt::*;
use defmt_rtt as _;
use panic_probe as _;
use rmk::{eeprom::EepromStorageConfig, initialize_keyboard_and_usb_device, keymap::KeyMap};
use rmk::{eeprom::EepromStorageConfig, initialize_keyboard_and_run, keymap::KeyMap};
use static_cell::StaticCell;

use crate::keymap::{COL, NUM_LAYER, ROW};
Expand Down Expand Up @@ -78,7 +76,7 @@ async fn main(_spawner: Spawner) {
)));

// Initialize all utilities: keyboard, usb and keymap
let (mut keyboard, mut usb_device, vial) = initialize_keyboard_and_usb_device::<
initialize_keyboard_and_run::<
Driver<'_, USB>,
Input<'_, AnyPin>,
Output<'_, AnyPin>,
Expand All @@ -94,22 +92,6 @@ async fn main(_spawner: Spawner) {
keymap,
&vial::VIAL_KEYBOARD_ID,
&vial::VIAL_KEYBOARD_DEF,
);

let usb_fut = usb_device.device.run();
let keyboard_fut = async {
loop {
let _ = keyboard.keyboard_task().await;
keyboard.send_report(&mut usb_device.keyboard_hid).await;
keyboard.send_media_report(&mut usb_device.other_hid).await;
}
};

let via_fut = async {
loop {
vial.process_via_report(&mut usb_device.via_hid).await;
Timer::after_millis(1).await;
}
};
join(usb_fut, join(keyboard_fut, via_fut)).await;
)
.await;
}
8 changes: 2 additions & 6 deletions boards/stm32f4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ embassy-executor = { version = "0.5.0", git = "https://github.com/embassy-rs/emb
"defmt",
"arch-cortex-m",
"executor-thread",
"executor-interrupt",
"integrated-timers",
] }
embassy-futures = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy" }
rtt-target = "0.5.0"
log = "0.4.19"
packed_struct = { version = "0.10.1", default-features = false }
Expand All @@ -49,11 +47,9 @@ static_cell = { version = "2" }
# defmt deps
defmt = "0.3.5"
defmt-rtt = "0.4.0"
panic-probe = {version = "0.3", features = ["print-defmt"] }
panic-probe = { version = "0.3", features = ["print-defmt"] }

[[bin]]
name = "rmk-stm32f4"
test = false
bench = false

[workspace]
bench = false
25 changes: 3 additions & 22 deletions boards/stm32f4/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use core::{cell::RefCell, sync::atomic::AtomicBool};
use defmt::*;
use defmt_rtt as _;
use embassy_executor::Spawner;
use embassy_futures::join::join;
use embassy_stm32::{
bind_interrupts,
flash::{Blocking, Flash},
Expand All @@ -21,9 +20,8 @@ use embassy_stm32::{
usb_otg::{Driver, InterruptHandler},
Config,
};
use embassy_time::Timer;
use panic_probe as _;
use rmk::{eeprom::EepromStorageConfig, initialize_keyboard_and_usb_device, keymap::KeyMap};
use rmk::{eeprom::EepromStorageConfig, initialize_keyboard_and_run, keymap::KeyMap};
use static_cell::StaticCell;

use crate::keymap::{COL, NUM_LAYER, ROW};
Expand Down Expand Up @@ -80,7 +78,7 @@ async fn main(_spawner: Spawner) {
)));

// Initialize all utilities: keyboard, usb and keymap
let (mut keyboard, mut usb_device, vial) = initialize_keyboard_and_usb_device::<
initialize_keyboard_and_run::<
Driver<'_, USB_OTG_FS>,
Input<'_, AnyPin>,
Output<'_, AnyPin>,
Expand All @@ -96,22 +94,5 @@ async fn main(_spawner: Spawner) {
keymap,
&vial::VIAL_KEYBOARD_ID,
&vial::VIAL_KEYBOARD_DEF,
);

let usb_fut = usb_device.device.run();
let keyboard_fut = async {
loop {
let _ = keyboard.keyboard_task().await;
keyboard.send_report(&mut usb_device.keyboard_hid).await;
keyboard.send_media_report(&mut usb_device.other_hid).await;
}
};

let via_fut = async {
loop {
vial.process_via_report(&mut usb_device.via_hid).await;
Timer::after_millis(1).await;
}
};
join(usb_fut, join(keyboard_fut, via_fut)).await;
).await;
}
1 change: 0 additions & 1 deletion boards/stm32h7/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ embassy-executor = { version = "0.5.0", git = "https://github.com/embassy-rs/emb
"executor-thread",
"integrated-timers",
] }
embassy-futures = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy" }
rtt-target = "0.5.0"
log = "0.4.19"
packed_struct = { version = "0.10.1", default-features = false }
Expand Down
28 changes: 5 additions & 23 deletions boards/stm32h7/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod vial;

use core::{cell::RefCell, sync::atomic::AtomicBool};
use embassy_executor::Spawner;
use embassy_futures::join::join;
use embassy_stm32::{
bind_interrupts,
flash::{Blocking, Flash},
Expand All @@ -22,10 +21,9 @@ use embassy_stm32::{
usb_otg::{Driver, InterruptHandler},
Config,
};
use embassy_time::Timer;
use log::info;
use panic_rtt_target as _;
use rmk::{eeprom::EepromStorageConfig, initialize_keyboard_and_usb_device, keymap::KeyMap};
use rmk::{eeprom::EepromStorageConfig, initialize_keyboard_and_run, keymap::KeyMap};
use static_cell::StaticCell;

use crate::keymap::{COL, NUM_LAYER, ROW};
Expand Down Expand Up @@ -113,8 +111,8 @@ async fn main(_spawner: Spawner) {
None,
)));

// Initialize all utilities: keyboard, usb and keymap
let (mut keyboard, mut usb_device, vial) = initialize_keyboard_and_usb_device::<
// Start serving
initialize_keyboard_and_run::<
Driver<'_, USB_OTG_HS>,
Input<'_, AnyPin>,
Output<'_, AnyPin>,
Expand All @@ -130,22 +128,6 @@ async fn main(_spawner: Spawner) {
keymap,
&vial::VIAL_KEYBOARD_ID,
&vial::VIAL_KEYBOARD_DEF,
);

let usb_fut = usb_device.device.run();
let keyboard_fut = async {
loop {
let _ = keyboard.keyboard_task().await;
keyboard.send_report(&mut usb_device.keyboard_hid).await;
keyboard.send_media_report(&mut usb_device.other_hid).await;
}
};

let via_fut = async {
loop {
vial.process_via_report(&mut usb_device.via_hid).await;
Timer::after_millis(1).await;
}
};
join(usb_fut, join(keyboard_fut, via_fut)).await;
)
.await;
}
1 change: 1 addition & 0 deletions rmk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ embassy-usb = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy"
"max-handler-count-8",
] }
embassy-sync = { version = "0.5.0", git = "https://github.com/embassy-rs/embassy" }
embassy-futures = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy" }
num_enum = { version = "0.7", default-features = false }
log = "0.4"
static_cell = { version = "2" }
Expand Down
49 changes: 49 additions & 0 deletions rmk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#![cfg_attr(not(test), no_std)]

use core::{cell::RefCell, convert::Infallible};
use embassy_futures::join::join;
use embassy_time::Timer;
use embassy_usb::driver::Driver;
use embedded_hal::digital::{InputPin, OutputPin};
use embedded_storage::nor_flash::NorFlash;
Expand All @@ -30,6 +32,7 @@ pub mod matrix;
pub mod usb;
pub mod via;

/// DEPRECIATED: Use `initialize_keyboard_and_run` instead.
/// Initialize keyboard core and keyboard usb device
pub fn initialize_keyboard_and_usb_device<
D: Driver<'static>,
Expand Down Expand Up @@ -58,3 +61,49 @@ pub fn initialize_keyboard_and_usb_device<
VialService::new(keymap, vial_keyboard_Id, vial_keyboard_def),
)
}

/// Initialize and run the keyboard service, this function never returns.
pub async fn initialize_keyboard_and_run<
D: Driver<'static>,
In: InputPin<Error = Infallible>,
Out: OutputPin<Error = Infallible>,
F: NorFlash,
const EEPROM_SIZE: usize,
const ROW: usize,
const COL: usize,
const NUM_LAYER: usize,
>(
driver: D,
input_pins: [In; ROW],
output_pins: [Out; COL],
keymap: &'static RefCell<KeyMap<F, EEPROM_SIZE, ROW, COL, NUM_LAYER>>,
vial_keyboard_Id: &'static [u8],
vial_keyboard_def: &'static [u8],
) -> ! {
let (mut keyboard, mut usb_device, vial_service) = (
Keyboard::new(input_pins, output_pins, keymap),
KeyboardUsbDevice::new(driver),
VialService::new(keymap, vial_keyboard_Id, vial_keyboard_def),
);

let usb_fut = usb_device.device.run();
let keyboard_fut = async {
loop {
let _ = keyboard.keyboard_task().await;
keyboard.send_report(&mut usb_device.keyboard_hid).await;
keyboard.send_media_report(&mut usb_device.other_hid).await;
}
};

let via_fut = async {
loop {
vial_service
.process_via_report(&mut usb_device.via_hid)
.await;
Timer::after_millis(1).await;
}
};
join(usb_fut, join(keyboard_fut, via_fut)).await;

panic!("Keyboard service is died")
}

0 comments on commit 7ff6cac

Please sign in to comment.