-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: concurrent flip input processor
- Loading branch information
Showing
3 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod debounce; | ||
pub mod flip; | ||
pub mod none; |