qmk_firmware

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

sym_eager_pr.c (4158B)


      1 // Copyright 2017 Alex Ong<the.onga@gmail.com>
      2 // Copyright 2021 Simon Arlott
      3 // SPDX-License-Identifier: GPL-2.0-or-later
      4 //
      5 // Basic per-row algorithm. Uses an 8-bit counter per key.
      6 // After pressing a key, it immediately changes state, and sets a counter.
      7 // No further inputs are accepted until DEBOUNCE milliseconds have occurred.
      8 
      9 #include "debounce.h"
     10 #include "timer.h"
     11 #include "util.h"
     12 
     13 #ifndef DEBOUNCE
     14 #    define DEBOUNCE 5
     15 #endif
     16 
     17 // Maximum debounce: 255ms
     18 #if DEBOUNCE > UINT8_MAX
     19 #    undef DEBOUNCE
     20 #    define DEBOUNCE UINT8_MAX
     21 #endif
     22 
     23 #define DEBOUNCE_ELAPSED 0
     24 
     25 #if DEBOUNCE > 0
     26 typedef uint8_t debounce_counter_t;
     27 // Uses MATRIX_ROWS_PER_HAND instead of MATRIX_ROWS to support split keyboards
     28 static debounce_counter_t debounce_counters[MATRIX_ROWS_PER_HAND] = {DEBOUNCE_ELAPSED};
     29 static bool               counters_need_update;
     30 static bool               matrix_need_update;
     31 static bool               cooked_changed;
     32 
     33 static inline void update_debounce_counters(uint8_t elapsed_time);
     34 static inline void transfer_matrix_values(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(MIN(elapsed_time, UINT8_MAX));
     53         }
     54     }
     55 
     56     if (changed || matrix_need_update) {
     57         if (!updated_last) {
     58             last_time = timer_read_fast();
     59         }
     60 
     61         transfer_matrix_values(raw, cooked);
     62     }
     63 
     64     return cooked_changed;
     65 }
     66 
     67 /**
     68  * @brief Updates per-row debounce counters and determines if matrix needs updating.
     69  *
     70  * Iterates through each row in the matrix and checks its debounce counter. If the debounce
     71  * period has elapsed, the counter is reset and the matrix is marked for update. Otherwise,
     72  * the counter is decremented by the elapsed time and marked for further updates if needed.
     73  *
     74  * @param elapsed_time The time elapsed since the last debounce update, in milliseconds.
     75  */
     76 static inline void update_debounce_counters(uint8_t elapsed_time) {
     77     counters_need_update = false;
     78     matrix_need_update   = false;
     79 
     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                 matrix_need_update     = true;
     85             } else {
     86                 debounce_counters[row] -= elapsed_time;
     87                 counters_need_update = true;
     88             }
     89         }
     90     }
     91 }
     92 
     93 /**
     94  * @brief Transfers debounced key states from the raw matrix to the cooked matrix.
     95  *
     96  * For each row in the matrix, this function checks if its state has changed and if its
     97  * debounce counter has elapsed. If so, the debounce counter is reset, the cooked matrix
     98  * is updated to reflect the new state, and the matrix is marked for further updates.
     99  *
    100  * @param raw The current raw key state matrix.
    101  * @param cooked The debounced key state matrix
    102  */
    103 static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]) {
    104     matrix_need_update = false;
    105 
    106     for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
    107         matrix_row_t existing_row = cooked[row];
    108         matrix_row_t raw_row      = raw[row];
    109 
    110         // determine new value basd on debounce pointer + raw value
    111         if (existing_row != raw_row) {
    112             if (debounce_counters[row] == DEBOUNCE_ELAPSED) {
    113                 debounce_counters[row] = DEBOUNCE;
    114                 cooked_changed |= cooked[row] ^ raw_row;
    115                 cooked[row]          = raw_row;
    116                 counters_need_update = true;
    117             }
    118         }
    119     }
    120 }
    121 
    122 #else
    123 #    include "none.c"
    124 #endif