Skip to content

Commit

Permalink
Merge pull request #11 from daystram/feat/concurrent-flip
Browse files Browse the repository at this point in the history
Add concurrent flip input processor
  • Loading branch information
daystram committed Jul 26, 2024
2 parents 661014e + 4fa66b4 commit 4f09d9d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ mod kb {

use crate::{
heartbeat::HeartbeatLED,
key::{Action, Key},
key::{Action, Edge, Key},
keyboard::{Keyboard, KeyboardConfiguration},
matrix::{BasicVerticalSwitchMatrix, Scanner},
processor::{
events::rgb::{FrameIterator, RGBMatrix, RGBProcessor},
input::debounce::KeyMatrixRisingFallingDebounceProcessor,
input::{
debounce::KeyMatrixRisingFallingDebounceProcessor,
flip::{ConcurrentFlipProcessor, Pos},
},
mapper::{Input, Mapper},
Event, EventsProcessor, InputProcessor,
},
Expand All @@ -81,6 +84,7 @@ mod kb {
const DEBUG_LOG_INPUT_SCANNER_INTERVAL: u64 = 50;
const DEBUG_LOG_PROCESSOR_ENABLE_TIMING: bool = false;
const DEBUG_LOG_PROCESSOR_INTERVAL: u64 = 50;
const DEBUG_LOG_EVENTS: bool = true;
const DEBUG_LOG_SENT_KEYS: bool = false;

#[shared]
Expand Down Expand Up @@ -308,9 +312,10 @@ mod kb {
let input_processors: &mut [&mut dyn InputProcessor<
{ <Keyboard as KeyboardConfiguration>::KEY_MATRIX_ROW_COUNT },
{ <Keyboard as KeyboardConfiguration>::KEY_MATRIX_COL_COUNT },
>] = &mut [&mut KeyMatrixRisingFallingDebounceProcessor::new(
10.millis(),
)];
>] = &mut [
&mut KeyMatrixRisingFallingDebounceProcessor::new(10.millis()),
&mut ConcurrentFlipProcessor::new(Pos { row: 2, col: 1 }, Pos { row: 2, col: 3 }),
];
let mut mapper = Mapper::new(<Keyboard as KeyboardConfiguration>::get_input_map());
let events_processors: &mut [&mut dyn EventsProcessor<
<Keyboard as KeyboardConfiguration>::Layer,
Expand All @@ -334,6 +339,13 @@ mod kb {
Vec::<Event<<Keyboard as KeyboardConfiguration>::Layer>>::with_capacity(10);
mapper.map(&input, &mut events);

if DEBUG_LOG_EVENTS {
events
.iter()
.filter(|e| e.edge != Edge::None)
.for_each(|e| debug!("[{}] event: action: {} edge: {}", n, e.action, e.edge));
}

if events_processors
.iter_mut()
.try_for_each(|p| p.process(&mut events))
Expand Down
2 changes: 1 addition & 1 deletion src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Result<const ROW_COUNT: usize, const COL_COUNT: usize> {
pub matrix: [[Bit; COL_COUNT]; ROW_COUNT],
}

#[derive(Clone, Copy, Debug, Format)]
#[derive(Clone, Copy, Debug, Default, Format)]
pub struct Bit {
pub edge: Edge,
pub pressed: bool,
Expand Down
73 changes: 73 additions & 0 deletions src/processor/input/flip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use defmt::Format;

use crate::{
key::Edge,
matrix::Bit,
processor::{mapper::Input, InputProcessor, Result},
};

pub struct ConcurrentFlipProcessor {
key1_pos: Pos,
key2_pos: Pos,
previous_pressed_state: (bool, bool),
}

#[derive(Clone, Copy, Debug, Format)]
pub struct Pos {
pub row: usize,
pub col: usize,
}

#[allow(dead_code)]
impl ConcurrentFlipProcessor {
pub fn new(key1_pos: Pos, key2_pos: Pos) -> Self {
ConcurrentFlipProcessor {
key1_pos,
key2_pos,
previous_pressed_state: (false, false),
}
}
}

impl<const KEY_MATRIX_ROW_COUNT: usize, const KEY_MATRIX_COL_COUNT: usize>
InputProcessor<KEY_MATRIX_ROW_COUNT, KEY_MATRIX_COL_COUNT> for ConcurrentFlipProcessor
{
fn process(&mut self, input: &mut Input<KEY_MATRIX_ROW_COUNT, KEY_MATRIX_COL_COUNT>) -> Result {
let key1_bit = input.key_matrix_result.matrix[self.key1_pos.row][self.key1_pos.col];
let key2_bit = input.key_matrix_result.matrix[self.key2_pos.row][self.key2_pos.col];

let pressed_state = if key1_bit.edge == Edge::Rising {
(true, false)
} else if key1_bit.edge == Edge::Falling {
if key2_bit.pressed {
(false, true)
} else {
(false, false)
}
} else if key2_bit.edge == Edge::Rising {
(false, true)
} else if key2_bit.edge == Edge::Falling {
if key1_bit.pressed {
(true, false)
} else {
(false, false)
}
} else if !key1_bit.pressed && !key2_bit.pressed {
(false, false) // reset to idle
} else {
self.previous_pressed_state // no edge changes, maintain pressed state
};

input.key_matrix_result.matrix[self.key1_pos.row][self.key1_pos.col] = Bit {
edge: Edge::from((self.previous_pressed_state.0, pressed_state.0)),
pressed: pressed_state.0,
};
input.key_matrix_result.matrix[self.key2_pos.row][self.key2_pos.col] = Bit {
edge: Edge::from((self.previous_pressed_state.1, pressed_state.1)),
pressed: pressed_state.1,
};

self.previous_pressed_state = pressed_state;
Ok(())
}
}
1 change: 1 addition & 0 deletions src/processor/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod debounce;
pub mod flip;
pub mod none;

0 comments on commit 4f09d9d

Please sign in to comment.