From 2ef4cd7bbdbfb0d8d1b71ccc7b72e9d1e448dd30 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 1 Mar 2021 10:59:32 -0700 Subject: [PATCH] Add matrix command --- .../system76/common/include/board/kbscan.h | 4 ++ src/board/system76/common/kbscan.c | 8 ++-- src/board/system76/common/smfi.c | 16 +++++++- src/common/include/common/command.h | 2 + tool/src/ec.rs | 5 +++ tool/src/main.rs | 37 +++++++++++++++++++ 6 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/board/system76/common/include/board/kbscan.h b/src/board/system76/common/include/board/kbscan.h index f24d806..516d734 100644 --- a/src/board/system76/common/include/board/kbscan.h +++ b/src/board/system76/common/include/board/kbscan.h @@ -5,6 +5,7 @@ #include +#include #include // EC config reset key combo: Fn+Esc @@ -18,6 +19,9 @@ extern uint16_t kbscan_repeat_period; // ms between pressing key and repeating extern uint16_t kbscan_repeat_delay; +// Debounced kbscan matrix +extern uint8_t kbscan_matrix[KM_OUT]; + void kbscan_init(void); void kbscan_event(void); diff --git a/src/board/system76/common/kbscan.c b/src/board/system76/common/kbscan.c index 6fb3a80..eb00bbd 100644 --- a/src/board/system76/common/kbscan.c +++ b/src/board/system76/common/kbscan.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -26,6 +25,8 @@ bool kbscan_enabled = false; uint16_t kbscan_repeat_period = 91; uint16_t kbscan_repeat_delay = 500; +uint8_t kbscan_matrix[KM_OUT] = { 0 }; + uint8_t sci_extra = 0; static inline bool matrix_position_is_esc(int row, int col) { @@ -289,7 +290,6 @@ static inline bool key_should_repeat(uint16_t key) { void kbscan_event(void) { static uint8_t kbscan_layer = 0; uint8_t layer = kbscan_layer; - static uint8_t kbscan_last[KM_OUT] = { 0 }; static uint8_t kbscan_last_layer[KM_OUT][KM_IN] = { { 0 } }; static bool kbscan_ghost[KM_OUT] = { false }; @@ -312,7 +312,7 @@ void kbscan_event(void) { int i; for (i = 0; i < KM_OUT; i++) { uint8_t new = kbscan_get_row(i); - uint8_t last = kbscan_last[i]; + uint8_t last = kbscan_matrix[i]; if (new != last) { if (kbscan_has_ghost_in_row(i, new)) { kbscan_ghost[i] = true; @@ -388,7 +388,7 @@ void kbscan_event(void) { } } - kbscan_last[i] = new; + kbscan_matrix[i] = new; } else if (new && repeat_key != 0 && key_should_repeat(repeat_key)) { // A key is being pressed uint32_t time = time_get(); diff --git a/src/board/system76/common/smfi.c b/src/board/system76/common/smfi.c index e6a0749..c48e926 100644 --- a/src/board/system76/common/smfi.c +++ b/src/board/system76/common/smfi.c @@ -20,7 +20,7 @@ #ifndef __SCRATCH__ #include #include - #include + #include #endif #include #include @@ -232,6 +232,17 @@ static enum Result cmd_led_set_color(void) { return RES_ERR; } } + +static enum Result cmd_matrix_get(void) { + smfi_cmd[SMFI_CMD_DATA] = KM_OUT; + smfi_cmd[SMFI_CMD_DATA + 1] = KM_IN; + for (uint8_t row = 0; row < KM_OUT; row++) { + if ((SMFI_CMD_DATA + 2 + row) < ARRAY_SIZE(smfi_cmd)) { + smfi_cmd[SMFI_CMD_DATA + 2 + row] = kbscan_matrix[row]; + } + } + return RES_OK; +} #endif // !defined(__SCRATCH__) #if defined(__SCRATCH__) @@ -357,6 +368,9 @@ void smfi_event(void) { case CMD_LED_SET_COLOR: smfi_cmd[SMFI_CMD_RES] = cmd_led_set_color(); break; + case CMD_MATRIX_GET: + smfi_cmd[SMFI_CMD_RES] = cmd_matrix_get(); + break; #endif // !defined(__SCRATCH__) case CMD_SPI: smfi_cmd[SMFI_CMD_RES] = cmd_spi(); diff --git a/src/common/include/common/command.h b/src/common/include/common/command.h index 1e74bef..66052dc 100644 --- a/src/common/include/common/command.h +++ b/src/common/include/common/command.h @@ -38,6 +38,8 @@ enum Command { CMD_LED_GET_MODE = 15, // Set LED matrix mode and speed CMD_LED_SET_MODE = 16, + // Get key matrix state + CMD_MATRIX_GET = 17, //TODO }; diff --git a/tool/src/ec.rs b/tool/src/ec.rs index 9e0738d..1cdd639 100644 --- a/tool/src/ec.rs +++ b/tool/src/ec.rs @@ -33,6 +33,7 @@ enum Cmd { LedSetColor = 14, LedGetMode = 15, LedSetMode = 16, + MatrixGet = 17, } const CMD_SPI_FLAG_READ: u8 = 1 << 0; @@ -269,6 +270,10 @@ impl Ec { self.command(Cmd::LedSetMode, &mut data) } + pub unsafe fn matrix_get(&mut self, matrix: &mut [u8]) -> Result<(), Error> { + self.command(Cmd::MatrixGet, matrix) + } + pub fn into_dyn(self) -> Ec> where A: 'static { Ec { diff --git a/tool/src/main.rs b/tool/src/main.rs index 5ee206a..bd9d383 100644 --- a/tool/src/main.rs +++ b/tool/src/main.rs @@ -222,6 +222,35 @@ unsafe fn info(ec: &mut Ec>) -> Result<(), Error> { Ok(()) } +unsafe fn matrix(ec: &mut Ec>) -> Result<(), Error> { + let data_size = ec.access().data_size(); + + let mut data = vec![0; data_size]; + ec.matrix_get(&mut data)?; + let rows = *data.get(0).unwrap_or(&0); + let cols = *data.get(1).unwrap_or(&0); + let mut byte = 2; + let mut bit = 0; + for row in 0..rows { + for col in 0..cols { + if (data.get(byte).unwrap_or(&0) & (1 << bit)) != 0 { + print!("#"); + } else { + print!("-"); + } + + bit += 1; + if bit >= 8 { + byte += 1; + bit = 0; + } + } + println!(); + } + + Ok(()) +} + unsafe fn print(ec: &mut Ec>, message: &[u8]) -> Result<(), Error> { ec.print(message)?; @@ -338,6 +367,7 @@ fn main() { .validator(validate_from_str::) ) ) + .subcommand(SubCommand::with_name("matrix")) .subcommand(SubCommand::with_name("print") .arg(Arg::with_name("message") .required(true) @@ -536,6 +566,13 @@ fn main() { } } }, + ("matrix", Some(_sub_m)) => match unsafe { matrix(&mut ec) } { + Ok(()) => (), + Err(err) => { + eprintln!("failed to read matrix: {:X?}", err); + process::exit(1); + }, + }, ("print", Some(sub_m)) => for arg in sub_m.values_of("message").unwrap() { let mut arg = arg.to_owned(); arg.push('\n');