suspend: update wake up matrix after wake up delay

If USB_SUSPEND_WAKEUP_DELAY is set, the keyboard sleeps during wake up -
which can be up to multiple seconds. To handle key presses and releases
in that time frame we have to handle the following cases:

1. Key not pressed before suspend, and not pressed after wakeup → do
   nothing (normal case).
2. Key not pressed before suspend, but pressed after wakeup → set the
   wakeup_matrix bit to 1 (so that the press and release events would be
   suppressed).
3. Key pressed before suspend, but not pressed after wakeup → do nothing
   (the release event will be generated on the first matrix_task() call
   after the wakeup).
4. Key pressed before suspend, and still pressed after wakeup → do
   nothing (the release event will be generated some time later).

Signed-off-by: Stefan Kerkmann <karlk90@pm.me>
Co-authored-by: Sergey Vlasov <sigprof@gmail.com>
This commit is contained in:
Stefan Kerkmann 2024-04-01 18:00:36 +02:00
parent 3da2111df6
commit f9dc545b83
6 changed files with 28 additions and 5 deletions

View File

@ -4,6 +4,7 @@
#include "suspend.h" #include "suspend.h"
#include "matrix.h" #include "matrix.h"
extern matrix_row_t matrix_previous[MATRIX_ROWS];
static matrix_row_t wakeup_matrix[MATRIX_ROWS]; static matrix_row_t wakeup_matrix[MATRIX_ROWS];
// TODO: Move to more correct location // TODO: Move to more correct location
@ -48,14 +49,26 @@ bool suspend_wakeup_condition(void) {
matrix_power_down(); matrix_power_down();
bool wakeup = false; bool wakeup = false;
for (uint8_t r = 0; r < MATRIX_ROWS; r++) { for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
wakeup_matrix[r] = matrix_get_row(r); wakeup_matrix[row] = matrix_get_row(row);
wakeup |= wakeup_matrix[r] != 0; wakeup |= wakeup_matrix[row] != 0;
} }
return wakeup; return wakeup;
} }
void update_matrix_state_after_wakeup(void) {
matrix_power_up();
matrix_scan();
matrix_power_down();
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
const matrix_row_t current_row = matrix_get_row(row);
wakeup_matrix[row] |= current_row & ~matrix_previous[row];
matrix_previous[row] |= current_row;
}
}
bool keypress_is_wakeup_key(uint8_t row, uint8_t col) { bool keypress_is_wakeup_key(uint8_t row, uint8_t col) {
return (wakeup_matrix[row] & ((matrix_row_t)1 << col)); return (wakeup_matrix[row] & ((matrix_row_t)1 << col));
} }

View File

@ -15,6 +15,7 @@ void suspend_power_down_kb(void);
void suspend_power_down_quantum(void); void suspend_power_down_quantum(void);
bool keypress_is_wakeup_key(uint8_t row, uint8_t col); bool keypress_is_wakeup_key(uint8_t row, uint8_t col);
void update_matrix_state_after_wakeup(void);
void wakeup_matrix_handle_key_event(uint8_t row, uint8_t col, bool pressed); void wakeup_matrix_handle_key_event(uint8_t row, uint8_t col, bool pressed);
#ifndef USB_SUSPEND_WAKEUP_DELAY #ifndef USB_SUSPEND_WAKEUP_DELAY

View File

@ -571,6 +571,8 @@ static inline void generate_tick_event(void) {
} }
} }
matrix_row_t matrix_previous[MATRIX_ROWS];
/** /**
* @brief This task scans the keyboards matrix and processes any key presses * @brief This task scans the keyboards matrix and processes any key presses
* that occur. * that occur.
@ -584,8 +586,6 @@ static bool matrix_task(void) {
return false; return false;
} }
static matrix_row_t matrix_previous[MATRIX_ROWS];
matrix_scan(); matrix_scan();
bool matrix_changed = false; bool matrix_changed = false;
for (uint8_t row = 0; row < MATRIX_ROWS && !matrix_changed; row++) { for (uint8_t row = 0; row < MATRIX_ROWS && !matrix_changed; row++) {

View File

@ -193,6 +193,9 @@ void protocol_pre_task(void) {
// //
// Pause for a while to let things settle... // Pause for a while to let things settle...
wait_ms(USB_SUSPEND_WAKEUP_DELAY); wait_ms(USB_SUSPEND_WAKEUP_DELAY);
// ...and then update the wakeup matrix again as the waking key
// might have been released during the delay
update_matrix_state_after_wakeup();
# endif # endif
} }
} }

View File

@ -836,6 +836,9 @@ void protocol_pre_task(void) {
// //
// Pause for a while to let things settle... // Pause for a while to let things settle...
wait_ms(USB_SUSPEND_WAKEUP_DELAY); wait_ms(USB_SUSPEND_WAKEUP_DELAY);
// ...and then update the wakeup matrix again as the waking key
// might have been released during the delay
update_matrix_state_after_wakeup();
# endif # endif
} }
} }

View File

@ -145,6 +145,9 @@ void protocol_pre_task(void) {
// //
// Pause for a while to let things settle... // Pause for a while to let things settle...
wait_ms(USB_SUSPEND_WAKEUP_DELAY); wait_ms(USB_SUSPEND_WAKEUP_DELAY);
// ...and then update the wakeup matrix again as the waking key
// might have been released during the delay
update_matrix_state_after_wakeup();
# endif # endif
} }
} }