diff --git a/src/main.rs b/src/main.rs index 884fdfb..2535077 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, }, @@ -308,9 +311,10 @@ mod kb { let input_processors: &mut [&mut dyn InputProcessor< { ::KEY_MATRIX_ROW_COUNT }, { ::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(::get_input_map()); let events_processors: &mut [&mut dyn EventsProcessor< ::Layer, diff --git a/src/processor/input/flip.rs b/src/processor/input/flip.rs new file mode 100644 index 0000000..0e830f8 --- /dev/null +++ b/src/processor/input/flip.rs @@ -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 + InputProcessor for ConcurrentFlipProcessor +{ + fn process(&mut self, input: &mut Input) -> 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(()) + } +} diff --git a/src/processor/input/mod.rs b/src/processor/input/mod.rs index 1fabb2e..b165f50 100644 --- a/src/processor/input/mod.rs +++ b/src/processor/input/mod.rs @@ -1,2 +1,3 @@ pub mod debounce; +pub mod flip; pub mod none;