Skip to content

Commit

Permalink
add: concurrent flip input processor
Browse files Browse the repository at this point in the history
  • Loading branch information
daystram committed Jul 26, 2024
1 parent ab21109 commit 2e4d2d0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ mod kb {
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 Down Expand Up @@ -308,9 +311,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 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 2e4d2d0

Please sign in to comment.