Skip to content

Commit

Permalink
implement peripheral direct pin
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineGod committed Nov 14, 2024
1 parent f318062 commit a69882a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 24 deletions.
76 changes: 52 additions & 24 deletions rmk-macro/src/split/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
ChipModel, ChipSeries,
};

/// Parse split central mod and generate a valid RMK main function with all needed code
/// Parse split peripheral mod and generate a valid RMK main function with all needed code
pub(crate) fn parse_split_peripheral_mod(
id: usize,
_attr: proc_macro::TokenStream,
Expand Down Expand Up @@ -112,7 +112,6 @@ fn expand_split_peripheral(
));
// `generic_arg_infer` is a nightly feature. Const arguments cannot yet be inferred with `_` in stable now.
// So we need to declaring them in advance.
/*
let rows = keyboard_config.layout.rows as usize;
let cols = keyboard_config.layout.cols as usize;
let size = keyboard_config.layout.rows as usize * keyboard_config.layout.cols as usize;
Expand All @@ -125,7 +124,6 @@ fn expand_split_peripheral(
pub(crate) const LAYER_NUM: usize = #layers;
let low_active = #low_active;
});
*/
}
}

Expand Down Expand Up @@ -155,19 +153,38 @@ fn expand_split_peripheral_entry(
let peripheral_addr = peripheral_config.ble_addr.expect(
"Peripheral should have a ble address, please check the `ble_addr` field in `keyboard.toml`",
);
quote! {
::rmk::split::peripheral::run_rmk_split_peripheral::<
::embassy_nrf::gpio::Input<'_>,
::embassy_nrf::gpio::Output<'_>,
#row,
#col
> (
input_pins,
output_pins,
[#(#central_addr), *],
[#(#peripheral_addr), *],
spawner,
).await
match peripheral_config.matrix.matrix_type {
MatrixType::direct_pin => {
quote! {
::rmk::split::peripheral::run_rmk_split_peripheral_direct_pin::<
::embassy_nrf::gpio::Input<'_>,
::embassy_nrf::gpio::Output<'_>,
#row,
#col
> (
direct_pins,
[#(#central_addr), *],
[#(#peripheral_addr), *],
spawner,
).await
}
}
MatrixType::normal => {
quote! {
::rmk::split::peripheral::run_rmk_split_peripheral::<
::embassy_nrf::gpio::Input<'_>,
::embassy_nrf::gpio::Output<'_>,
#row,
#col
> (
input_pins,
output_pins,
[#(#central_addr), *],
[#(#peripheral_addr), *],
spawner,
).await
}
}
}
}
ChipSeries::Rp2040 => {
Expand All @@ -182,14 +199,25 @@ fn expand_split_peripheral_entry(

let row = peripheral_config.rows as usize;
let col = peripheral_config.cols as usize;
let peripheral_run = quote! {
::rmk::split::peripheral::run_rmk_split_peripheral::<
::embassy_rp::gpio::Input<'_>,
::embassy_rp::gpio::Output<'_>,
_,
#row,
#col,
>(input_pins, output_pins, uart0).await;
let peripheral_run = match peripheral_config.matrix.matrix_type {
MatrixType::normal => quote! {
::rmk::split::peripheral::run_rmk_split_peripheral::<
::embassy_rp::gpio::Input<'_>,
::embassy_rp::gpio::Output<'_>,
_,
#row,
#col,
>(input_pins, output_pins, uart0).await;
},
MatrixType::direct_pin => quote! {
::rmk::split::peripheral::run_rmk_split_peripheral_direct_pin::<
::embassy_rp::gpio::Input<'_>,
::embassy_rp::gpio::Output<'_>,
_,
#row,
#col,
>(direct_pins, uart0).await;
},
};
quote! {
#serial_init
Expand Down
53 changes: 53 additions & 0 deletions rmk/src/split/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use super::{
driver::{SplitReader, SplitWriter},
SplitMessage,
};
use crate::direct_pin::DirectPinMatrix;

#[cfg(not(feature = "_nrf_ble"))]
use {
Expand Down Expand Up @@ -87,6 +88,58 @@ pub async fn run_rmk_split_peripheral<
.await;
}

/// Run the split peripheral service with direct pin matrix.
///
/// # Arguments
///
/// * `direct_pins` - direct gpio pins, if `async_matrix` is enabled, the input pins should implement `embedded_hal_async::digital::Wait` trait
/// * `central_addr` - (optional) central's BLE static address. This argument is enabled only for nRF BLE split now
/// * `peripheral_addr` - (optional) peripheral's BLE static address. This argument is enabled only for nRF BLE split now
/// * `low_active`: pin active level
/// * `serial` - (optional) serial port used to send peripheral split message. This argument is enabled only for serial split now
/// * `spawner`: (optional) embassy spawner used to spawn async tasks. This argument is enabled for non-esp microcontrollers
pub async fn run_rmk_split_peripheral_direct_pin<
#[cfg(feature = "async_matrix")] In: Wait + InputPin,
#[cfg(not(feature = "async_matrix"))] In: InputPin,
Out: OutputPin,
#[cfg(not(feature = "_nrf_ble"))] S: Write + Read,
const ROW: usize,
const COL: usize,
const SIZE: usize,
>(
#[cfg(feature = "col2row")] direct_pins: [[Option<In>; COL]; ROW],
#[cfg(feature = "_nrf_ble")] central_addr: [u8; 6],
#[cfg(feature = "_nrf_ble")] peripheral_addr: [u8; 6],
low_active: bool,
#[cfg(not(feature = "_nrf_ble"))] serial: S,
#[cfg(feature = "_nrf_ble")] spawner: Spawner,
) {
// Create the debouncer, use COL2ROW by default
#[cfg(all(feature = "col2row", feature = "rapid_debouncer"))]
let debouncer = RapidDebouncer::<ROW, COL>::new();
#[cfg(all(feature = "col2row", not(feature = "rapid_debouncer")))]
let debouncer = DefaultDebouncer::<ROW, COL>::new();
#[cfg(all(not(feature = "col2row"), feature = "rapid_debouncer"))]
let debouncer = RapidDebouncer::<COL, ROW>::new();
#[cfg(all(not(feature = "col2row"), not(feature = "rapid_debouncer")))]
let debouncer = DefaultDebouncer::<COL, ROW>::new();

// Keyboard matrix
let matrix = DirectPinMatrix::<_, _, ROW, COL, SIZE>::new(direct_pins, debouncer, low_active);

#[cfg(not(feature = "_nrf_ble"))]
initialize_serial_split_peripheral_and_run::<_, S, ROW, COL>(matrix, serial).await;

#[cfg(feature = "_nrf_ble")]
initialize_nrf_ble_split_peripheral_and_run::<_, ROW, COL>(
matrix,
central_addr,
peripheral_addr,
spawner,
)
.await;
}

/// Initialize and run the nRF peripheral keyboard service via BLE.
///
/// # Arguments
Expand Down

0 comments on commit a69882a

Please sign in to comment.