From 77c0fcaac2c97a15c8b10a06a5ccc328f0319eeb Mon Sep 17 00:00:00 2001 From: Haobo Gu Date: Wed, 24 Jan 2024 21:18:33 +0800 Subject: [PATCH] refactor: expose what's really needed Signed-off-by: Haobo Gu --- rmk/src/action.rs | 10 +++---- rmk/src/debounce.rs | 12 ++++---- rmk/src/eeprom.rs | 22 +++++++------- rmk/src/eeprom/eeconfig.rs | 40 +++++++++++++------------- rmk/src/eeprom/eekeymap.rs | 17 ++++++++--- rmk/src/keyboard.rs | 16 +++++------ rmk/src/keycode.rs | 52 +++++++++++++++++----------------- rmk/src/keymap.rs | 43 +++++++++++++++++++--------- rmk/src/lib.rs | 21 ++++++-------- rmk/src/matrix.rs | 36 ++++++++++------------- rmk/src/usb.rs | 16 +++++------ rmk/src/usb/descriptor.rs | 18 ++++++------ rmk/src/via.rs | 4 +-- rmk/src/via/keycode_convert.rs | 4 +-- rmk/src/via/process.rs | 13 +++++---- rmk/src/via/protocol.rs | 4 +-- rmk/src/via/vial.rs | 8 ++++-- 17 files changed, 178 insertions(+), 158 deletions(-) diff --git a/rmk/src/action.rs b/rmk/src/action.rs index b31700a2..6f91a042 100644 --- a/rmk/src/action.rs +++ b/rmk/src/action.rs @@ -53,7 +53,7 @@ pub enum KeyAction { impl KeyAction { /// Convert a `KeyAction` to corresponding key action code. - pub fn to_key_action_code(&self) -> u16 { + pub(crate) fn to_key_action_code(&self) -> u16 { match self { KeyAction::No => 0x0000, KeyAction::Transparent => 0x0001, @@ -87,7 +87,7 @@ impl KeyAction { } } - pub fn from_key_action_code(code: u16) -> KeyAction { + pub(crate) fn from_key_action_code(code: u16) -> KeyAction { match code { 0x0..=0xFFF => KeyAction::Single(Action::from_action_code(code)), 0x1000..=0x1FFF => KeyAction::Tap(Action::from_action_code(code & 0xFFF)), @@ -146,7 +146,7 @@ pub enum Action { impl Action { /// Convert an `Action` to 12-bit action code - pub fn to_action_code(&self) -> u16 { + pub(crate) fn to_action_code(&self) -> u16 { match self { Action::Key(k) => *k as u16, Action::Modifier(m) => 0xE00 | (m.to_bits() as u16), @@ -157,7 +157,7 @@ impl Action { } /// Create an `Action` from action_code, returns Key(KeyCode::No) if the action code is not valid. - pub fn from_action_code(action_code: u16) -> Action { + pub(crate) fn from_action_code(action_code: u16) -> Action { match action_code { 0x000..=0xCFF => Action::Key(KeyCode::from_primitive(action_code)), 0xE00..=0xE1F => { @@ -184,7 +184,7 @@ impl Action { } /// Convert an `Action` to 8-bit basic action code, only applicable for `Key(BasicKeyCode)` - pub fn to_basic_action_code(&self) -> u16 { + pub(crate) fn to_basic_action_code(&self) -> u16 { match self { Action::Key(kc) => { if kc.is_basic() { diff --git a/rmk/src/debounce.rs b/rmk/src/debounce.rs index 66ed9017..7810ddef 100644 --- a/rmk/src/debounce.rs +++ b/rmk/src/debounce.rs @@ -7,8 +7,8 @@ static DEBOUNCE_THRESHOLD: u16 = 10; /// Debounce info for each key. #[derive(Copy, Clone, Debug)] -pub struct DebounceState { - pub counter: u16, +pub(crate) struct DebounceState { + pub(crate) counter: u16, } impl DebounceState { @@ -31,16 +31,16 @@ impl DebounceState { } /// Default per-key debouncer. The debouncing algorithm is same as ZMK's [default debouncer](https://github.com/zmkfirmware/zmk/blob/19613128b901723f7b78c136792d72e6ca7cf4fc/app/module/lib/zmk_debounce/debounce.c) -pub struct Debouncer { +pub(crate) struct Debouncer { last_tick: u32, - pub debounce_state: [[DebounceState; INPUT_PIN_NUM]; OUTPUT_PIN_NUM], + pub(crate) debounce_state: [[DebounceState; INPUT_PIN_NUM]; OUTPUT_PIN_NUM], } impl Debouncer { /// Create a default debouncer - pub fn new() -> Self { + pub(crate) fn new() -> Self { Debouncer { debounce_state: [[DebounceState { counter: 0 }; INPUT_PIN_NUM]; OUTPUT_PIN_NUM], last_tick: 0, @@ -48,7 +48,7 @@ impl } /// Per-key debounce, same with zmk's debounce algorithm - pub fn debounce( + pub(crate) fn debounce( &mut self, in_idx: usize, out_idx: usize, diff --git a/rmk/src/eeprom.rs b/rmk/src/eeprom.rs index a9f20eda..813032e4 100644 --- a/rmk/src/eeprom.rs +++ b/rmk/src/eeprom.rs @@ -1,5 +1,5 @@ -pub mod eeconfig; -pub mod eekeymap; +pub(crate) mod eeconfig; +pub(crate) mod eekeymap; extern crate alloc; @@ -12,7 +12,7 @@ use embedded_storage::nor_flash::NorFlash; /// A record in the eeprom, with 2-byte address and 2-byte data /// A record is 4-byte long, so the tracking pos in the `Eeprom` implementation must be a multiple of 4 -pub struct EepromRecord { +pub(crate) struct EepromRecord { address: u16, data: u16, } @@ -34,21 +34,21 @@ impl EepromRecord { /// Configuration of eeprom's backend storage. #[derive(Default)] -pub struct EepromStorageConfig { +pub(crate) struct EepromStorageConfig { /// The start address in the backend storage. - pub start_addr: u32, + pub(crate) start_addr: u32, /// Total used size in backend storage for eeprom. - pub storage_size: u32, + pub(crate) storage_size: u32, /// Minimal write size of backend storage. /// For example, stm32h7's internal flash allows 256-bit(32 bytes) or 128-bit(16 bytes) write, so page_size should be 32/16 for stm32h7. - pub page_size: u32, + pub(crate) page_size: u32, } /// Eeprom based on any storage device which implements `embedded-storage::NorFlash` trait /// Data in eeprom is saved in a 4-byte `record`, with 2-byte address in the first 16 bits and 2-byte data in the next 16 bits. /// Eeprom struct maintains a cache in ram to speed up reads, whose size is same as the logical eeprom capacity. /// User can specify the size of the logical size of eeprom(maximum 64KB), Eeprom struct maintains a cache in ram to speed up reads, whose size is same as the user defined logical eeprom capacity. -pub struct Eeprom { +pub(crate) struct Eeprom { /// Current position in the storage pos: u32, /// Backend storage, implements `embedded-storage::NorFlash` trait @@ -71,7 +71,7 @@ pub struct Eeprom { } impl Eeprom { - pub fn new( + pub(crate) fn new( storage: F, storage_config: EepromStorageConfig, eeconfig: Option, @@ -143,7 +143,7 @@ impl Eeprom { Some(eeprom) } - pub fn write_byte(&mut self, mut address: u16, data: &[u8]) { + pub(crate) fn write_byte(&mut self, mut address: u16, data: &[u8]) { if data.len() == 0 { warn!("No data to write to eeprom, skip"); return; @@ -189,7 +189,7 @@ impl Eeprom { /// Read bytes from eeprom, starting from the given address, and reading `read_size` bytes. /// Returns a slice of eeprom cache, which is immutable - pub fn read_byte(&self, address: u16, read_size: usize) -> &[u8] { + pub(crate) fn read_byte(&self, address: u16, read_size: usize) -> &[u8] { &self.cache[address as usize..(address as usize + read_size)] } diff --git a/rmk/src/eeprom/eeconfig.rs b/rmk/src/eeprom/eeconfig.rs index 6a4a7776..0bacb8f7 100644 --- a/rmk/src/eeprom/eeconfig.rs +++ b/rmk/src/eeprom/eeconfig.rs @@ -43,7 +43,7 @@ pub(crate) const DYNAMIC_KEYMAP_ADDR: u16 = 16; impl Eeprom { /// Initialize eeprom with default eeconfig - pub fn init_with_default_config(&mut self) { + pub(crate) fn init_with_default_config(&mut self) { self.set_enable(true); self.set_default_layer(0); self.set_keymap_config(EeKeymapConfig::default()); @@ -54,7 +54,7 @@ impl Eeprom { } /// Initialize eeprom with given eeconfig - pub fn init_with_config(&mut self, config: Eeconfig) { + pub(crate) fn init_with_config(&mut self, config: Eeconfig) { self.set_enable(config.eeprom_enable); self.set_default_layer(config.default_layer); self.set_keymap_config(config.keymap_config); @@ -65,7 +65,7 @@ impl Eeprom { } /// Enable or disable eeprom by writing magic value - pub fn set_enable(&mut self, enabled: bool) { + pub(crate) fn set_enable(&mut self, enabled: bool) { let magic = if enabled { EEPROM_MAGIC } else { @@ -78,7 +78,7 @@ impl Eeprom { } /// Returns eeprom magic value stored in EEPROM - pub fn get_magic(&mut self) -> u16 { + pub(crate) fn get_magic(&mut self) -> u16 { // ALWAYS read magic from the start address of the backend store let mut bytes = [0_u8; 4]; match self @@ -94,17 +94,17 @@ impl Eeprom { } /// Set default layer - pub fn set_default_layer(&mut self, default_layer: u8) { + pub(crate) fn set_default_layer(&mut self, default_layer: u8) { self.write_byte(DEFAULT_LAYER_START as u16, &[default_layer]); } /// Returns current default layer - pub fn get_default_layer(&self) -> u8 { + pub(crate) fn get_default_layer(&self) -> u8 { self.cache[DEFAULT_LAYER_START] } /// Set keymap config - pub fn set_keymap_config(&mut self, config: EeKeymapConfig) { + pub(crate) fn set_keymap_config(&mut self, config: EeKeymapConfig) { let mut buf = match config.pack() { Ok(b) => b, Err(_) => { @@ -116,7 +116,7 @@ impl Eeprom { } /// Returns keymap config as `EeKeymapConfig` - pub fn get_keymap_config(&self) -> Option { + pub(crate) fn get_keymap_config(&self) -> Option { match EeKeymapConfig::unpack_from_slice( self.read_byte(KEYMAP_CONFIG_ADDR, KEYMAP_CONFIG_SIZE), ) { @@ -129,7 +129,7 @@ impl Eeprom { } /// Set backlight config - pub fn set_backlight_config(&mut self, config: EeBacklightConfig) { + pub(crate) fn set_backlight_config(&mut self, config: EeBacklightConfig) { let mut buf = match config.pack() { Ok(b) => b, Err(_) => { @@ -141,7 +141,7 @@ impl Eeprom { } /// Returns backlight config as `EeBacklightConfig` - pub fn get_backlight_config(&self) -> Option { + pub(crate) fn get_backlight_config(&self) -> Option { match EeBacklightConfig::unpack_from_slice( self.read_byte(BACKLIGHT_CONFIG_ADDR, BACKLIGHT_CONFIG_SIZE), ) { @@ -154,7 +154,7 @@ impl Eeprom { } /// Set audio config - pub fn set_audio_config(&mut self, config: EeAudioConfig) { + pub(crate) fn set_audio_config(&mut self, config: EeAudioConfig) { let mut buf = match config.pack() { Ok(b) => b, Err(_) => { @@ -166,7 +166,7 @@ impl Eeprom { } /// Returns audio config as `EeAudioConfig` - pub fn get_audio_config(&self) -> Option { + pub(crate) fn get_audio_config(&self) -> Option { match EeAudioConfig::unpack_from_slice(self.read_byte(AUDIO_CONFIG_ADDR, AUDIO_CONFIG_SIZE)) { Ok(config) => Some(config), @@ -178,7 +178,7 @@ impl Eeprom { } /// Set rgb light config - pub fn set_rgb_light_config(&mut self, config: EeRgbLightConfig) { + pub(crate) fn set_rgb_light_config(&mut self, config: EeRgbLightConfig) { let mut buf = match config.pack() { Ok(b) => b, Err(_) => { @@ -190,7 +190,7 @@ impl Eeprom { } /// Returns rgb light config as `EeRgbLightConfig` - pub fn get_rgb_light_config(&self) -> Option { + pub(crate) fn get_rgb_light_config(&self) -> Option { match EeRgbLightConfig::unpack_from_slice(self.read_byte(RGB_CONFIG_ADDR, RGB_CONFIG_SIZE)) { Ok(config) => Some(config), @@ -202,14 +202,14 @@ impl Eeprom { } /// Set layout option - pub fn set_layout_option(&mut self, option: u32) { + pub(crate) fn set_layout_option(&mut self, option: u32) { let mut buf = [0xFF; 4]; BigEndian::write_u32(&mut buf, option); self.write_byte(LAYOUT_OPTION_ADDR, &mut buf); } /// Returns layout option - pub fn get_layout_option(&self) -> u32 { + pub(crate) fn get_layout_option(&self) -> u32 { BigEndian::read_u32(self.read_byte(LAYOUT_OPTION_ADDR, LAYOUT_OPTION_SIZE)) } } @@ -227,7 +227,7 @@ pub struct Eeconfig { #[derive(PackedStruct, Debug, Default)] #[packed_struct(bit_numbering = "msb0", bytes = "2")] -pub struct EeKeymapConfig { +pub(crate) struct EeKeymapConfig { #[packed_field(bits = "0")] swap_control_capslock: bool, #[packed_field(bits = "1")] @@ -259,7 +259,7 @@ pub struct EeKeymapConfig { #[derive(PackedStruct, Debug, Default)] #[packed_struct(bit_numbering = "msb0")] -pub struct EeBacklightConfig { +pub(crate) struct EeBacklightConfig { #[packed_field(bits = "0")] enable: bool, #[packed_field(bits = "1")] @@ -272,7 +272,7 @@ pub struct EeBacklightConfig { #[derive(PackedStruct, Debug, Default)] #[packed_struct(bit_numbering = "msb0")] -pub struct EeAudioConfig { +pub(crate) struct EeAudioConfig { #[packed_field(bits = "0")] enable: bool, #[packed_field(bits = "1")] @@ -283,7 +283,7 @@ pub struct EeAudioConfig { #[derive(PackedStruct, Debug, Default)] #[packed_struct(bit_numbering = "msb0")] -pub struct EeRgbLightConfig { +pub(crate) struct EeRgbLightConfig { #[packed_field(bits = "0")] enable: bool, #[packed_field(bits = "1..=7")] diff --git a/rmk/src/eeprom/eekeymap.rs b/rmk/src/eeprom/eekeymap.rs index 477da9f5..045feee3 100644 --- a/rmk/src/eeprom/eekeymap.rs +++ b/rmk/src/eeprom/eekeymap.rs @@ -1,11 +1,14 @@ -use crate::{action::KeyAction, via::keycode_convert::{from_via_keycode, to_via_keycode}}; +use crate::{ + action::KeyAction, + via::keycode_convert::{from_via_keycode, to_via_keycode}, +}; use byteorder::{BigEndian, ByteOrder}; use embedded_storage::nor_flash::NorFlash; use super::{eeconfig::DYNAMIC_KEYMAP_ADDR, Eeprom}; impl Eeprom { - pub fn set_keymap( + pub(crate) fn set_keymap( &mut self, keymap: &[[[KeyAction; COL]; ROW]; NUM_LAYER], ) { @@ -23,14 +26,20 @@ impl Eeprom { }); } - pub fn set_keymap_action(&mut self, row: usize, col: usize, layer: usize, action: KeyAction) { + pub(crate) fn set_keymap_action( + &mut self, + row: usize, + col: usize, + layer: usize, + action: KeyAction, + ) { let addr = self.get_keymap_addr(row, col, layer); let mut buf: [u8; 2] = [0xFF; 2]; BigEndian::write_u16(&mut buf, to_via_keycode(action)); self.write_byte(addr, &buf); } - pub fn read_keymap( + pub(crate) fn read_keymap( &self, keymap: &mut [[[KeyAction; COL]; ROW]; NUM_LAYER], ) { diff --git a/rmk/src/keyboard.rs b/rmk/src/keyboard.rs index 326e5e13..c34f1bbd 100644 --- a/rmk/src/keyboard.rs +++ b/rmk/src/keyboard.rs @@ -13,7 +13,7 @@ use embedded_hal::digital::{InputPin, OutputPin}; use embedded_storage::nor_flash::NorFlash; use usbd_hid::descriptor::{KeyboardReport, MediaKeyboardReport, SystemControlReport}; -pub struct Keyboard< +pub(crate) struct Keyboard< 'a, In: InputPin, Out: OutputPin, @@ -25,12 +25,12 @@ pub struct Keyboard< > { /// Keyboard matrix, use COL2ROW by default #[cfg(feature = "col2row")] - pub matrix: Matrix, + pub(crate) matrix: Matrix, #[cfg(not(feature = "col2row"))] matrix: Matrix, /// Keymap - pub keymap: &'a RefCell>, + pub(crate) keymap: &'a RefCell>, /// Keyboard internal hid report buf report: KeyboardReport, @@ -66,7 +66,7 @@ impl< > Keyboard<'a, In, Out, F, EEPROM_SIZE, ROW, COL, NUM_LAYER> { #[cfg(feature = "col2row")] - pub fn new( + pub(crate) fn new( input_pins: [In; ROW], output_pins: [Out; COL], keymap: &'a RefCell>, @@ -93,7 +93,7 @@ impl< } #[cfg(not(feature = "col2row"))] - pub fn new( + pub(crate) fn new( input_pins: [In; COL], output_pins: [Out; ROW], keymap: [[[KeyAction; COL]; ROW]; NUM_LAYER], @@ -136,7 +136,7 @@ impl< } /// Send hid report. The report is sent only when key state changes. - pub async fn send_report<'d, D: Driver<'d>>( + pub(crate) async fn send_report<'d, D: Driver<'d>>( &mut self, hid_interface: &mut HidReaderWriter<'d, D, 1, 8>, ) { @@ -154,7 +154,7 @@ impl< } } - pub async fn send_media_report<'d, D: Driver<'d>>( + pub(crate) async fn send_media_report<'d, D: Driver<'d>>( &mut self, hid_interface: &mut HidReaderWriter<'d, D, 1, 8>, ) { @@ -170,7 +170,7 @@ impl< /// Main keyboard task, it scans matrix, processes active keys /// If there is any change of key states, set self.changed=true - pub async fn keyboard_task(&mut self) -> Result<(), Infallible> { + pub(crate) async fn keyboard_task(&mut self) -> Result<(), Infallible> { // Matrix scan self.matrix.scan().await?; diff --git a/rmk/src/keycode.rs b/rmk/src/keycode.rs index 3b7c8c33..d8647827 100644 --- a/rmk/src/keycode.rs +++ b/rmk/src/keycode.rs @@ -23,7 +23,7 @@ pub struct ModifierCombination { } impl ModifierCombination { - pub fn new(right: bool, gui: bool, alt: bool, shift: bool, ctrl: bool) -> Self { + pub(crate) fn new(right: bool, gui: bool, alt: bool, shift: bool, ctrl: bool) -> Self { ModifierCombination { ctrl, shift, @@ -35,7 +35,7 @@ impl ModifierCombination { /// Convert modifier combination to a list of modifier keycodes. /// Returns a list of modifiers keycodes, and the length of the list. - pub fn to_modifier_keycodes(&self) -> ([KeyCode; 8], usize) { + pub(crate) fn to_modifier_keycodes(&self) -> ([KeyCode; 8], usize) { let mut keycodes = [KeyCode::No; 8]; let mut i = 0; if self.right { @@ -78,7 +78,7 @@ impl ModifierCombination { } /// Get modifier hid report bits from modifier combination - pub fn to_hid_modifier_bits(&self) -> u8 { + pub(crate) fn to_hid_modifier_bits(&self) -> u8 { let (keycodes, n) = self.to_modifier_keycodes(); let mut hid_modifier_bits = 0; for i in 0..n { @@ -89,12 +89,12 @@ impl ModifierCombination { } /// Convert modifier combination to bits - pub fn to_bits(&self) -> u8 { + pub(crate) fn to_bits(&self) -> u8 { ModifierCombination::pack(&self).unwrap_or_default()[0] } /// Convert from bits - pub fn from_bits(bits: u8) -> Self { + pub(crate) fn from_bits(bits: u8) -> Self { ModifierCombination::unpack_from_slice(&[bits]).unwrap_or_default() } } @@ -819,18 +819,18 @@ pub enum KeyCode { impl KeyCode { /// Returns `true` if the keycode is basic keycode - pub fn is_basic(self) -> bool { + pub(crate) fn is_basic(self) -> bool { KeyCode::No <= self && self <= KeyCode::RGui } /// Returns `true` if the keycode is a modifier keycode - pub fn is_modifier(self) -> bool { + pub(crate) fn is_modifier(self) -> bool { KeyCode::LCtrl <= self && self <= KeyCode::RGui } /// Returns the byte with the bit corresponding to the USB HID /// modifier bitfield set. - pub fn as_modifier_bit(self) -> u8 { + pub(crate) fn as_modifier_bit(self) -> u8 { if self.is_modifier() { 1 << (self as u16 as u8 - KeyCode::LCtrl as u16 as u8) } else { @@ -839,88 +839,88 @@ impl KeyCode { } /// Returns `true` if the keycode is a system keycode - pub fn is_system(self) -> bool { + pub(crate) fn is_system(self) -> bool { KeyCode::SystemPower <= self && self <= KeyCode::SystemWake } /// Returns `true` if the keycode is a keycode in consumer page - pub fn is_consumer(self) -> bool { + pub(crate) fn is_consumer(self) -> bool { KeyCode::AudioMute <= self && self <= KeyCode::Launchpad } /// Returns `true` if the keycode is a mouse keycode - pub fn is_mouse_key(self) -> bool { + pub(crate) fn is_mouse_key(self) -> bool { KeyCode::MouseUp <= self && self <= KeyCode::MouseAccel2 } /// Returns `true` if the keycode is a magic keycode - pub fn is_magic(self) -> bool { + pub(crate) fn is_magic(self) -> bool { KeyCode::MagicSwapControlCapsLock <= self && self <= KeyCode::MagicToggleEscapeCapsLock } /// Returns `true` if the keycode is a midi keycode - pub fn is_midi(self) -> bool { + pub(crate) fn is_midi(self) -> bool { KeyCode::MidiOn <= self && self <= KeyCode::MidiPitchBendUp } /// Returns `true` if the keycode is a sequencer keycode - pub fn is_sequencer(self) -> bool { + pub(crate) fn is_sequencer(self) -> bool { KeyCode::SequencerOn <= self && self <= KeyCode::SequencerStepsClear } /// Returns `true` if the keycode is a joystick keycode - pub fn is_joystick(self) -> bool { + pub(crate) fn is_joystick(self) -> bool { KeyCode::JoystickButton0 <= self && self <= KeyCode::JoystickButton31 } /// Returns `true` if the keycode is a programmable button keycode - pub fn is_programmable_button(self) -> bool { + pub(crate) fn is_programmable_button(self) -> bool { KeyCode::ProgrammableButton1 <= self && self <= KeyCode::ProgrammableButton32 } /// Returns `true` if the keycode is a audio keycode /// Note: Basic audio keycodes are not included - pub fn is_audio(self) -> bool { + pub(crate) fn is_audio(self) -> bool { KeyCode::AudioOn <= self && self <= KeyCode::AudioVoicePrevious } /// Returns `true` if the keycode is a steno keycode - pub fn is_steno(self) -> bool { + pub(crate) fn is_steno(self) -> bool { KeyCode::StenoBolt <= self && self <= KeyCode::StenoCombMax } /// Returns `true` if the keycode is a macro keycode - pub fn is_macro(self) -> bool { + pub(crate) fn is_macro(self) -> bool { KeyCode::Macro0 <= self && self <= KeyCode::Macro31 } /// Returns `true` if the keycode is a backlight keycode - pub fn is_backlight(self) -> bool { + pub(crate) fn is_backlight(self) -> bool { KeyCode::BacklightOn <= self && self <= KeyCode::BacklightToggleBreathing } /// Returns `true` if the keycode is a rgb keycode - pub fn is_rgb(self) -> bool { + pub(crate) fn is_rgb(self) -> bool { KeyCode::RgbTog <= self && self <= KeyCode::RgbModeTwinkle } /// Returns `true` if the keycode is defined by rmk to achieve special functionalities, such as reboot keyboard, goto bootloader, etc. - pub fn is_rmk(self) -> bool { + pub(crate) fn is_rmk(self) -> bool { KeyCode::Bootloader <= self && self <= KeyCode::AltRepeatKey } /// Returns `true` if the keycode is a kb keycode - pub fn is_kb(self) -> bool { + pub(crate) fn is_kb(self) -> bool { KeyCode::Kb0 <= self && self <= KeyCode::Kb31 } /// Returns `true` if the keycode is a user keycode - pub fn is_user(self) -> bool { + pub(crate) fn is_user(self) -> bool { KeyCode::User0 <= self && self <= KeyCode::User31 } /// Convert a keycode to usb hid media key - pub fn as_consumer_control_usage_id(self) -> MediaKey { + pub(crate) fn as_consumer_control_usage_id(self) -> MediaKey { match self { KeyCode::AudioMute => MediaKey::Mute, KeyCode::AudioVolUp => MediaKey::VolumeIncrement, @@ -944,7 +944,7 @@ impl KeyCode { } /// Convert a keycode to usb hid media key - pub fn as_system_control_usage_id(self) -> Option { + pub(crate) fn as_system_control_usage_id(self) -> Option { match self { KeyCode::SystemPower => Some(SystemControlKey::PowerDown), KeyCode::SystemSleep => Some(SystemControlKey::Sleep), diff --git a/rmk/src/keymap.rs b/rmk/src/keymap.rs index 0f8dd4ff..c2096fb7 100644 --- a/rmk/src/keymap.rs +++ b/rmk/src/keymap.rs @@ -3,20 +3,20 @@ use crate::{ eeprom::{eeconfig::Eeconfig, Eeprom, EepromStorageConfig}, matrix::KeyState, }; +use defmt::warn; use embedded_alloc::Heap; use embedded_storage::nor_flash::NorFlash; -use defmt::warn; #[global_allocator] static HEAP: Heap = Heap::empty(); pub struct KeyMapConfig { /// Number of rows. - pub row: usize, + pub(crate) row: usize, /// Number of columns. - pub col: usize, + pub(crate) col: usize, /// Number of layer - pub layer: usize, + pub(crate) layer: usize, } /// KeyMap represents the stack of layers. @@ -102,21 +102,27 @@ impl< } } - pub fn get_keymap_config(&self) -> (usize, usize, usize) { + pub(crate) fn get_keymap_config(&self) -> (usize, usize, usize) { (ROW, COL, NUM_LAYER) } - pub fn set_action_at(&mut self, row: usize, col: usize, layer_num: usize, action: KeyAction) { + pub(crate) fn set_action_at( + &mut self, + row: usize, + col: usize, + layer_num: usize, + action: KeyAction, + ) { self.layers[layer_num][row][col] = action; } /// Fetch the action in keymap, with layer cache - pub fn get_action_at(&mut self, row: usize, col: usize, layer_num: usize) -> KeyAction { + pub(crate) fn get_action_at(&mut self, row: usize, col: usize, layer_num: usize) -> KeyAction { self.layers[layer_num][row][col] } /// Fetch the action in keymap, with layer cache - pub fn get_action_with_layer_cache( + pub(crate) fn get_action_with_layer_cache( &mut self, row: usize, col: usize, @@ -168,27 +174,36 @@ impl< } /// Activate given layer - pub fn activate_layer(&mut self, layer_num: u8) { + pub(crate) fn activate_layer(&mut self, layer_num: u8) { if layer_num as usize >= NUM_LAYER { - warn!("Not a valid layer {}, keyboard supports only {} layers", layer_num, NUM_LAYER); + warn!( + "Not a valid layer {}, keyboard supports only {} layers", + layer_num, NUM_LAYER + ); return; } self.layer_state[layer_num as usize] = true; } /// Deactivate given layer - pub fn deactivate_layer(&mut self, layer_num: u8) { + pub(crate) fn deactivate_layer(&mut self, layer_num: u8) { if layer_num as usize >= NUM_LAYER { - warn!("Not a valid layer {}, keyboard supports only {} layers", layer_num, NUM_LAYER); + warn!( + "Not a valid layer {}, keyboard supports only {} layers", + layer_num, NUM_LAYER + ); return; } self.layer_state[layer_num as usize] = false; } /// Toggle given layer - pub fn toggle_layer(&mut self, layer_num: u8) { + pub(crate) fn toggle_layer(&mut self, layer_num: u8) { if layer_num as usize >= NUM_LAYER { - warn!("Not a valid layer {}, keyboard supports only {} layers", layer_num, NUM_LAYER); + warn!( + "Not a valid layer {}, keyboard supports only {} layers", + layer_num, NUM_LAYER + ); return; } diff --git a/rmk/src/lib.rs b/rmk/src/lib.rs index 85ea737a..8f3ceb9e 100644 --- a/rmk/src/lib.rs +++ b/rmk/src/lib.rs @@ -16,24 +16,21 @@ use keymap::KeyMap; use usb::KeyboardUsbDevice; use via::process::VialService; -pub use embassy_usb; -pub use usbd_hid; - pub mod action; -pub mod debounce; -pub mod eeprom; -pub mod flash; -pub mod keyboard; +mod debounce; +mod eeprom; +mod flash; +mod keyboard; pub mod keycode; pub mod keymap; -pub mod layout_macro; -pub mod matrix; -pub mod usb; -pub mod via; +mod layout_macro; +mod matrix; +mod usb; +mod via; /// DEPRECIATED: Use `initialize_keyboard_and_run` instead. /// Initialize keyboard core and keyboard usb device -pub fn initialize_keyboard_and_usb_device< +pub(crate) fn initialize_keyboard_and_usb_device< D: Driver<'static>, In: InputPin, Out: OutputPin, diff --git a/rmk/src/matrix.rs b/rmk/src/matrix.rs index f64a268a..76e79771 100644 --- a/rmk/src/matrix.rs +++ b/rmk/src/matrix.rs @@ -1,14 +1,14 @@ use crate::debounce::Debouncer; use core::convert::Infallible; +use embassy_time::{Duration, Instant, Timer}; use embedded_hal::digital::{InputPin, OutputPin}; -use embassy_time::{Instant, Duration, Timer}; /// KeyState represents the state of a key. #[derive(Copy, Clone, Debug)] -pub struct KeyState { - pub pressed: bool, - pub changed: bool, - pub hold_start: Option, +pub(crate) struct KeyState { + pub(crate) pressed: bool, + pub(crate) changed: bool, + hold_start: Option, } impl Default for KeyState { @@ -18,7 +18,7 @@ impl Default for KeyState { } impl KeyState { - pub fn new() -> Self { + fn new() -> Self { KeyState { pressed: false, changed: false, @@ -26,28 +26,22 @@ impl KeyState { } } - pub fn start_timer(&mut self) { + fn start_timer(&mut self) { self.hold_start = Some(Instant::now()); } - pub fn elapsed(&self) -> Option { + fn elapsed(&self) -> Option { match self.hold_start { Some(t) => Instant::now().checked_duration_since(t), None => None, } } - pub fn clear_timer(&mut self) { + fn clear_timer(&mut self) { self.hold_start = None; } } -/// Key's position in the matrix -pub struct KeyPos { - row: u8, - col: u8, -} - /// Matrix is the physical pcb layout of the keyboard matrix. pub struct Matrix< In: InputPin, @@ -60,9 +54,9 @@ pub struct Matrix< /// Output pins of the pcb matrix output_pins: [Out; OUTPUT_PIN_NUM], /// Debouncer - pub debouncer: Debouncer, + debouncer: Debouncer, /// Key state matrix - pub key_states: [[KeyState; INPUT_PIN_NUM]; OUTPUT_PIN_NUM], + key_states: [[KeyState; INPUT_PIN_NUM]; OUTPUT_PIN_NUM], } impl< @@ -73,7 +67,7 @@ impl< > Matrix { /// Create a matrix from input and output pins. - pub fn new(input_pins: [In; INPUT_PIN_NUM], output_pins: [Out; OUTPUT_PIN_NUM]) -> Self { + pub(crate) fn new(input_pins: [In; INPUT_PIN_NUM], output_pins: [Out; OUTPUT_PIN_NUM]) -> Self { Matrix { input_pins, output_pins, @@ -83,7 +77,7 @@ impl< } /// Do matrix scanning, the result is stored in matrix's key_state field. - pub async fn scan(&mut self) -> Result<(), Infallible> { + pub(crate) async fn scan(&mut self) -> Result<(), Infallible> { for (out_idx, out_pin) in self.output_pins.iter_mut().enumerate() { // Pull up output pin, wait 1us ensuring the change comes into effect out_pin.set_high()?; @@ -103,7 +97,7 @@ impl< } /// When a key is pressed, some callbacks some be called, such as `start_timer` - pub fn key_pressed(&mut self, row: usize, col: usize) { + pub(crate) fn key_pressed(&mut self, row: usize, col: usize) { // COL2ROW #[cfg(feature = "col2row")] self.key_states[col][row].start_timer(); @@ -113,7 +107,7 @@ impl< self.key_states[row][col].start_timer(); } - pub fn get_key_state(&mut self, row: usize, col: usize) -> KeyState { + pub(crate) fn get_key_state(&mut self, row: usize, col: usize) -> KeyState { // COL2ROW #[cfg(feature = "col2row")] return self.key_states[col][row]; diff --git a/rmk/src/usb.rs b/rmk/src/usb.rs index e27d0649..9b6b6cc2 100644 --- a/rmk/src/usb.rs +++ b/rmk/src/usb.rs @@ -1,15 +1,15 @@ use core::sync::atomic::{AtomicBool, Ordering}; +use defmt::info; use embassy_usb::{ class::hid::{Config, HidReaderWriter, ReportId, RequestHandler, State}, control::OutResponse, driver::Driver, Builder, Handler, UsbDevice, }; -use defmt::info; use static_cell::StaticCell; use usbd_hid::descriptor::{KeyboardReport, MediaKeyboardReport, SerializedDescriptor}; -pub mod descriptor; +pub(crate) mod descriptor; use crate::usb::descriptor::ViaReport; @@ -21,15 +21,15 @@ static SUSPENDED: AtomicBool = AtomicBool::new(false); // 1. Boot keyboard: 1 endpoint in // 2. Other: Mouse + System control + Consumer control: 1 endpoint in // 3. Via: used to communicate with via: 2 endpoints(in/out) -pub struct KeyboardUsbDevice<'d, D: Driver<'d>> { - pub device: UsbDevice<'d, D>, - pub keyboard_hid: HidReaderWriter<'d, D, 1, 8>, - pub other_hid: HidReaderWriter<'d, D, 1, 8>, - pub via_hid: HidReaderWriter<'d, D, 32, 32>, +pub(crate) struct KeyboardUsbDevice<'d, D: Driver<'d>> { + pub(crate) device: UsbDevice<'d, D>, + pub(crate) keyboard_hid: HidReaderWriter<'d, D, 1, 8>, + pub(crate) other_hid: HidReaderWriter<'d, D, 1, 8>, + pub(crate) via_hid: HidReaderWriter<'d, D, 32, 32>, } impl> KeyboardUsbDevice<'static, D> { - pub fn new(driver: D) -> Self { + pub(crate) fn new(driver: D) -> Self { // Create embassy-usb Config let mut usb_config = embassy_usb::Config::new(0x4c4b, 0x4643); usb_config.manufacturer = Some("Haobo"); diff --git a/rmk/src/usb/descriptor.rs b/rmk/src/usb/descriptor.rs index 0cbe5f77..9187fa12 100644 --- a/rmk/src/usb/descriptor.rs +++ b/rmk/src/usb/descriptor.rs @@ -10,9 +10,9 @@ use usbd_hid::descriptor::generator_prelude::*; }; } )] -pub struct ViaReport { - pub input_data: [u8; 32], - pub output_data: [u8; 32], +pub(crate) struct ViaReport { + pub(crate) input_data: [u8; 32], + pub(crate) output_data: [u8; 32], } // TODO: Composite hid report @@ -47,13 +47,13 @@ pub struct ViaReport { // )] // #[allow(dead_code)] // pub struct MyKeyboardReport { -// pub modifier: u8, -// pub reserved: u8, -// pub leds: u8, -// pub keycodes: [u8; 6], -// pub usage_id: u16, +// pub(crate) modifier: u8, +// pub(crate) reserved: u8, +// pub(crate) leds: u8, +// pub(crate) keycodes: [u8; 6], +// pub(crate) usage_id: u16, // } // impl AsInputReport for MyKeyboardReport { -// } \ No newline at end of file +// } diff --git a/rmk/src/via.rs b/rmk/src/via.rs index f2b4e4f8..d1ff5579 100644 --- a/rmk/src/via.rs +++ b/rmk/src/via.rs @@ -1,4 +1,4 @@ pub(crate) mod keycode_convert; -pub mod process; +pub(crate) mod process; mod protocol; -mod vial; \ No newline at end of file +mod vial; diff --git a/rmk/src/via/keycode_convert.rs b/rmk/src/via/keycode_convert.rs index c4b89bb8..1d96b43b 100644 --- a/rmk/src/via/keycode_convert.rs +++ b/rmk/src/via/keycode_convert.rs @@ -6,7 +6,7 @@ use crate::{ keycode::{KeyCode, ModifierCombination}, }; -pub fn to_via_keycode(key_action: KeyAction) -> u16 { +pub(crate) fn to_via_keycode(key_action: KeyAction) -> u16 { match key_action { KeyAction::No => 0x0000, KeyAction::Transparent => 0x0001, @@ -64,7 +64,7 @@ pub fn to_via_keycode(key_action: KeyAction) -> u16 { } /// Convert via keycode to KeyAction. -pub fn from_via_keycode(via_keycode: u16) -> KeyAction { +pub(crate) fn from_via_keycode(via_keycode: u16) -> KeyAction { match via_keycode { 0x0000 => KeyAction::No, 0x0001 => KeyAction::Transparent, diff --git a/rmk/src/via/process.rs b/rmk/src/via/process.rs index a8957a00..9a5c42e4 100644 --- a/rmk/src/via/process.rs +++ b/rmk/src/via/process.rs @@ -16,7 +16,7 @@ use embassy_usb::{ use embedded_storage::nor_flash::NorFlash; use num_enum::{FromPrimitive, TryFromPrimitive}; -pub struct VialService< +pub(crate) struct VialService< 'a, F: NorFlash, const EEPROM_SIZE: usize, @@ -25,7 +25,7 @@ pub struct VialService< const NUM_LAYER: usize, > { // VialService holds a reference of keymap, for updating - pub keymap: &'a RefCell>, + keymap: &'a RefCell>, // Vial config vial_keyboard_id: &'a [u8], @@ -42,7 +42,7 @@ impl< const NUM_LAYER: usize, > VialService<'a, F, EEPROM_SIZE, ROW, COL, NUM_LAYER> { - pub fn new( + pub(crate) fn new( keymap: &'a RefCell>, vial_keyboard_id: &'a [u8], vial_keyboard_def: &'a [u8], @@ -54,7 +54,7 @@ impl< } } - pub async fn process_via_report>( + pub(crate) async fn process_via_report>( &self, hid_interface: &mut HidReaderWriter<'a, D, 32, 32>, ) { @@ -84,7 +84,7 @@ impl< } } - pub fn process_via_packet( + fn process_via_packet( &self, report: &mut ViaReport, keymap: &mut KeyMap, @@ -286,7 +286,8 @@ impl< } } } -pub fn get_position_from_offset( + +fn get_position_from_offset( offset: usize, max_row: usize, max_col: usize, diff --git a/rmk/src/via/protocol.rs b/rmk/src/via/protocol.rs index 858ea88b..dd627f35 100644 --- a/rmk/src/via/protocol.rs +++ b/rmk/src/via/protocol.rs @@ -6,7 +6,7 @@ pub(crate) const VIA_FIRMWARE_VERSION: u32 = 0x0001; /// Via communication commands. Check [qmk/quantum/via.h`](https://github.com/qmk/qmk_firmware/blob/2fad45132f0777002934e07d17bfe8ec7aa95740/quantum/via.h#L74) #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)] #[repr(u8)] -pub enum ViaCommand { +pub(crate) enum ViaCommand { GetProtocolVersion = 0x01, // always 0x01 GetKeyboardValue = 0x02, SetKeyboardValue = 0x03, @@ -36,7 +36,7 @@ pub enum ViaCommand { /// Information of a via keyboard. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, TryFromPrimitive)] #[repr(u8)] -pub enum ViaKeyboardInfo { +pub(crate) enum ViaKeyboardInfo { Uptime = 0x01, LayoutOptions = 0x02, SwitchMatrixState = 0x03, diff --git a/rmk/src/via/vial.rs b/rmk/src/via/vial.rs index 738b83f3..8c31c830 100644 --- a/rmk/src/via/vial.rs +++ b/rmk/src/via/vial.rs @@ -6,7 +6,7 @@ use crate::usb::descriptor::ViaReport; #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)] #[repr(u8)] -pub enum VialCommand { +enum VialCommand { GetKeyboardId = 0x00, GetSize = 0x01, GetKeyboardDef = 0x02, @@ -29,7 +29,11 @@ const VIAL_PROTOCOL_VERSION: u32 = 6; const VIAL_EP_SIZE: usize = 32; /// /// Note: vial uses litte endian, while via uses big endian -pub fn process_vial(report: &mut ViaReport, vial_keyboard_Id: &[u8], vial_keyboard_def: &[u8]) { +pub(crate) fn process_vial( + report: &mut ViaReport, + vial_keyboard_Id: &[u8], + vial_keyboard_def: &[u8], +) { // report.output_data[0] == 0xFE -> vial commands let vial_command = VialCommand::from_primitive(report.output_data[1]); match vial_command {