qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

sym_defer_pr.c (4121B)


      1 // Copyright 2017 Alex Ong<the.onga@gmail.com>
      2 // Copyright 2020 Andrei Purdea<andrei@purdea.ro>
      3 // Copyright 2021 Simon Arlott
      4 // Copyright @filterpaper
      5 // SPDX-License-Identifier: GPL-2.0-or-later
      6 //
      7 // Basic symmetric per-row algorithm. Uses an 8-bit counter per row.
      8 // When no state changes have occured for DEBOUNCE milliseconds, we push the state.
      9 
     10 #include "debounce.h"
     11 #include "timer.h"
     12 #include "util.h"
     13 
     14 #ifndef DEBOUNCE
     15 #    define DEBOUNCE 5
     16 #endif
     17 
     18 // Maximum debounce: 255ms
     19 #if DEBOUNCE > UINT8_MAX
     20 #    undef DEBOUNCE
     21 #    define DEBOUNCE UINT8_MAX
     22 #endif
     23 
     24 #define DEBOUNCE_ELAPSED 0
     25 
     26 #if DEBOUNCE > 0
     27 typedef uint8_t debounce_counter_t;
     28 // Uses MATRIX_ROWS_PER_HAND instead of MATRIX_ROWS to support split keyboards
     29 static debounce_counter_t debounce_counters[MATRIX_ROWS_PER_HAND] = {DEBOUNCE_ELAPSED};
     30 static bool               counters_need_update;
     31 static bool               cooked_changed;
     32 
     33 static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time);
     34 static inline void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]);
     35 
     36 void debounce_init(void) {}
     37 
     38 bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
     39     static fast_timer_t last_time;
     40     bool                updated_last = false;
     41     cooked_changed                   = false;
     42 
     43     if (counters_need_update) {
     44         fast_timer_t now          = timer_read_fast();
     45         fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
     46 
     47         last_time    = now;
     48         updated_last = true;
     49 
     50         if (elapsed_time > 0) {
     51             // Update debounce counters with elapsed timer clamped to UINT8_MAX
     52             update_debounce_counters_and_transfer_if_expired(raw, cooked, MIN(elapsed_time, UINT8_MAX));
     53         }
     54     }
     55 
     56     if (changed) {
     57         if (!updated_last) {
     58             last_time = timer_read_fast();
     59         }
     60 
     61         start_debounce_counters(raw, cooked);
     62     }
     63 
     64     return cooked_changed;
     65 }
     66 
     67 /**
     68  * @brief Updates debounce counters and transfers debounced row states if the debounce period has expired.
     69  *
     70  * Iterates through each row in the matrix and checks its debounce counter. If the debounce period has expired
     71  * for a row, the debounced state is updated to match the raw state. Otherwise, the debounce counter is decremented
     72  * by the elapsed time and marked for further updates.
     73  *
     74  * @param raw The current raw key state matrix.
     75  * @param cooked The debounced key state matrix to be updated.
     76  * @param elapsed_time The time elapsed since the last debounce update, in milliseconds.
     77  */
     78 static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time) {
     79     counters_need_update = false;
     80     for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
     81         if (debounce_counters[row] != DEBOUNCE_ELAPSED) {
     82             if (debounce_counters[row] <= elapsed_time) {
     83                 debounce_counters[row] = DEBOUNCE_ELAPSED;
     84                 cooked_changed |= cooked[row] ^ raw[row];
     85                 cooked[row] = raw[row];
     86             } else {
     87                 debounce_counters[row] -= elapsed_time;
     88                 counters_need_update = true;
     89             }
     90         }
     91     }
     92 }
     93 
     94 /**
     95  * @brief Initializes debounce counters for rows with changed states.
     96  *
     97  * For each row in the matrix, this function checks if the raw state differs from the debounced state.
     98  * If a change is detected and the debounce counter has elapsed, the counter is set to the debounce period
     99  * and marked for update. Otherwise, the counter is cleared.
    100  *
    101  * @param raw The current raw key state matrix.
    102  * @param cooked The debounced key state matrix.
    103  */
    104 static inline void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]) {
    105     for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
    106         if (raw[row] != cooked[row]) {
    107             debounce_counters[row] = DEBOUNCE;
    108             counters_need_update   = true;
    109         } else {
    110             debounce_counters[row] = DEBOUNCE_ELAPSED;
    111         }
    112     }
    113 }
    114 
    115 #else
    116 #    include "none.c"
    117 #endif