Skip to content

Commit

Permalink
refactor: expose what's really needed
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 24, 2024
1 parent eae2df4 commit 77c0fca
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 158 deletions.
10 changes: 5 additions & 5 deletions rmk/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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),
Expand All @@ -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 => {
Expand All @@ -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() {
Expand Down
12 changes: 6 additions & 6 deletions rmk/src/debounce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -31,24 +31,24 @@ 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<const INPUT_PIN_NUM: usize, const OUTPUT_PIN_NUM: usize> {
pub(crate) struct Debouncer<const INPUT_PIN_NUM: usize, const OUTPUT_PIN_NUM: usize> {
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<const INPUT_PIN_NUM: usize, const OUTPUT_PIN_NUM: usize>
Debouncer<INPUT_PIN_NUM, OUTPUT_PIN_NUM>
{
/// 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,
}
}

/// Per-key debounce, same with zmk's debounce algorithm
pub fn debounce(
pub(crate) fn debounce(
&mut self,
in_idx: usize,
out_idx: usize,
Expand Down
22 changes: 11 additions & 11 deletions rmk/src/eeprom.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod eeconfig;
pub mod eekeymap;
pub(crate) mod eeconfig;
pub(crate) mod eekeymap;

extern crate alloc;

Expand All @@ -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,
}
Expand All @@ -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<F: NorFlash, const EEPROM_SIZE: usize> {
pub(crate) struct Eeprom<F: NorFlash, const EEPROM_SIZE: usize> {
/// Current position in the storage
pos: u32,
/// Backend storage, implements `embedded-storage::NorFlash` trait
Expand All @@ -71,7 +71,7 @@ pub struct Eeprom<F: NorFlash, const EEPROM_SIZE: usize> {
}

impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
pub fn new<const ROW: usize, const COL: usize, const NUM_LAYER: usize>(
pub(crate) fn new<const ROW: usize, const COL: usize, const NUM_LAYER: usize>(
storage: F,
storage_config: EepromStorageConfig,
eeconfig: Option<Eeconfig>,
Expand Down Expand Up @@ -143,7 +143,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
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;
Expand Down Expand Up @@ -189,7 +189,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {

/// 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)]
}

Expand Down
40 changes: 20 additions & 20 deletions rmk/src/eeprom/eeconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) const DYNAMIC_KEYMAP_ADDR: u16 = 16;

impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
/// 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());
Expand All @@ -54,7 +54,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// 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);
Expand All @@ -65,7 +65,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// 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 {
Expand All @@ -78,7 +78,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// 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
Expand All @@ -94,17 +94,17 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// 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(_) => {
Expand All @@ -116,7 +116,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// Returns keymap config as `EeKeymapConfig`
pub fn get_keymap_config(&self) -> Option<EeKeymapConfig> {
pub(crate) fn get_keymap_config(&self) -> Option<EeKeymapConfig> {
match EeKeymapConfig::unpack_from_slice(
self.read_byte(KEYMAP_CONFIG_ADDR, KEYMAP_CONFIG_SIZE),
) {
Expand All @@ -129,7 +129,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// 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(_) => {
Expand All @@ -141,7 +141,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// Returns backlight config as `EeBacklightConfig`
pub fn get_backlight_config(&self) -> Option<EeBacklightConfig> {
pub(crate) fn get_backlight_config(&self) -> Option<EeBacklightConfig> {
match EeBacklightConfig::unpack_from_slice(
self.read_byte(BACKLIGHT_CONFIG_ADDR, BACKLIGHT_CONFIG_SIZE),
) {
Expand All @@ -154,7 +154,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// 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(_) => {
Expand All @@ -166,7 +166,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// Returns audio config as `EeAudioConfig`
pub fn get_audio_config(&self) -> Option<EeAudioConfig> {
pub(crate) fn get_audio_config(&self) -> Option<EeAudioConfig> {
match EeAudioConfig::unpack_from_slice(self.read_byte(AUDIO_CONFIG_ADDR, AUDIO_CONFIG_SIZE))
{
Ok(config) => Some(config),
Expand All @@ -178,7 +178,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// 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(_) => {
Expand All @@ -190,7 +190,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// Returns rgb light config as `EeRgbLightConfig`
pub fn get_rgb_light_config(&self) -> Option<EeRgbLightConfig> {
pub(crate) fn get_rgb_light_config(&self) -> Option<EeRgbLightConfig> {
match EeRgbLightConfig::unpack_from_slice(self.read_byte(RGB_CONFIG_ADDR, RGB_CONFIG_SIZE))
{
Ok(config) => Some(config),
Expand All @@ -202,14 +202,14 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

/// 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))
}
}
Expand All @@ -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")]
Expand Down Expand Up @@ -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")]
Expand All @@ -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")]
Expand All @@ -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")]
Expand Down
17 changes: 13 additions & 4 deletions rmk/src/eeprom/eekeymap.rs
Original file line number Diff line number Diff line change
@@ -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<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
pub fn set_keymap<const ROW: usize, const COL: usize, const NUM_LAYER: usize>(
pub(crate) fn set_keymap<const ROW: usize, const COL: usize, const NUM_LAYER: usize>(
&mut self,
keymap: &[[[KeyAction; COL]; ROW]; NUM_LAYER],
) {
Expand All @@ -23,14 +26,20 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
});
}

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<const ROW: usize, const COL: usize, const NUM_LAYER: usize>(
pub(crate) fn read_keymap<const ROW: usize, const COL: usize, const NUM_LAYER: usize>(
&self,
keymap: &mut [[[KeyAction; COL]; ROW]; NUM_LAYER],
) {
Expand Down
Loading

0 comments on commit 77c0fca

Please sign in to comment.