cleanup code; debug

This commit is contained in:
peepeetee 2024-05-21 01:02:06 -05:00
parent 6cd2830537
commit 9b985746fc
6 changed files with 10 additions and 187 deletions

View File

@ -1,146 +0,0 @@
/*
Copyright 2017 Alex Ong<the.onga@gmail.com>
Copyright 2020 Andrei Purdea<andrei@purdea.ro>
Copyright 2021 Simon Arlott
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Basic symmetric per-key algorithm. Uses an 8-bit counter per key.
When no state changes have occured for DEBOUNCE milliseconds, we push the state.
*/
#pragma once
#include "debounce.h"
#include "timer.h"
#include <stdlib.h>
#ifdef PROTOCOL_CHIBIOS
# if CH_CFG_USE_MEMCORE == FALSE
# error ChibiOS is configured without a memory allocator. Your keyboard may have set `#define CH_CFG_USE_MEMCORE FALSE`, which is incompatible with this debounce algorithm.
# endif
#endif
#ifndef DEBOUNCE
# define DEBOUNCE 5
#endif
// Maximum debounce: 255ms
#if DEBOUNCE > UINT8_MAX
# undef DEBOUNCE
# define DEBOUNCE UINT8_MAX
#endif
#define ROW_SHIFTER ((matrix_row_t)1)
typedef uint8_t debounce_counter_t;
#if DEBOUNCE > 0
static debounce_counter_t *debounce_counters;
static fast_timer_t last_time;
static bool counters_need_update;
static bool cooked_changed;
# define DEBOUNCE_ELAPSED 0
static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time);
static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
// we use num_rows rather than MATRIX_ROWS to support split keyboards
void debounce_init(uint8_t num_rows) {
debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
int i = 0;
for (uint8_t r = 0; r < num_rows; r++) {
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
debounce_counters[i++] = DEBOUNCE_ELAPSED;
}
}
}
void debounce_free(void) {
free(debounce_counters);
debounce_counters = NULL;
}
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
bool updated_last = false;
cooked_changed = false;
if (counters_need_update) {
fast_timer_t now = timer_read_fast();
fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
last_time = now;
updated_last = true;
if (elapsed_time > UINT8_MAX) {
elapsed_time = UINT8_MAX;
}
if (elapsed_time > 0) {
update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, elapsed_time);
}
}
if (changed) {
if (!updated_last) {
last_time = timer_read_fast();
}
start_debounce_counters(raw, cooked, num_rows);
}
return cooked_changed;
}
static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time) {
counters_need_update = false;
debounce_counter_t *debounce_pointer = debounce_counters;
for (uint8_t row = 0; row < num_rows; row++) {
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
if (*debounce_pointer != DEBOUNCE_ELAPSED) {
if (*debounce_pointer <= elapsed_time) {
*debounce_pointer = DEBOUNCE_ELAPSED;
matrix_row_t cooked_next = (cooked[row] & ~(ROW_SHIFTER << col)) | (raw[row] & (ROW_SHIFTER << col));
cooked_changed |= cooked[row] ^ cooked_next;
cooked[row] = cooked_next;
} else {
*debounce_pointer -= elapsed_time;
counters_need_update = true;
}
}
debounce_pointer++;
}
}
}
static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {
debounce_counter_t *debounce_pointer = debounce_counters;
for (uint8_t row = 0; row < num_rows; row++) {
matrix_row_t delta = raw[row] ^ cooked[row];
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
if (delta & (ROW_SHIFTER << col)) {
if (*debounce_pointer == DEBOUNCE_ELAPSED) {
*debounce_pointer = DEBOUNCE;
counters_need_update = true;
}
} else {
*debounce_pointer = DEBOUNCE_ELAPSED;
}
debounce_pointer++;
}
}
}
#else
# include "none.c"
#endif

View File

@ -18,6 +18,7 @@
"bootmagic": { "bootmagic": {
"matrix": [2, 0] "matrix": [2, 0]
}, },
"debounce": 0,
"build": { "build": {
"lto": true "lto": true
}, },

View File

@ -8,8 +8,8 @@ SPDX-License-Identifier: GPL-2.0-or-later */
#include "quantum.h" #include "quantum.h"
#include "analog.h" #include "analog.h"
#include "lut.h" #include "lut.h"
#include "debounce.h"
#include "scanfunctions.h" #include "scanfunctions.h"
#include "debounce.c"
#include "sma.c" #include "sma.c"
// #include "matrix_helpers.c" // #include "matrix_helpers.c"
@ -64,7 +64,7 @@ void matrix_init_custom(void) {
for (uint8_t i = 0; i < MATRIX_COLS; i++) { for (uint8_t i = 0; i < MATRIX_COLS; i++) {
keys[1][i].is_analog = true; keys[1][i].is_analog = true;
//should really be done at compile time //should really be done at compile time
initialize_SMA_filter(keys[1][i], SMA_FILTER_SAMPLE_EXPONENT); // initialize_SMA_filter(keys[1][i], SMA_FILTER_SAMPLE_EXPONENT);
} }
// for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
@ -94,15 +94,16 @@ void matrix_init_custom(void) {
//setup only rows 0 and 2, leave row 1 untouched //setup only rows 0 and 2, leave row 1 untouched
__attribute__((weak)) void matrix_init_pins(void) { __attribute__((weak)) void matrix_init_pins(void) {
// if (keys[row][0].is_analog == false)
for (int row = 0; row < MATRIX_ROWS; row++) { for (int row = 0; row < MATRIX_ROWS; row++) {
// if (row != 1){ if (row != 1){
for (int col = 0; col < MATRIX_COLS; col++) { for (int col = 0; col < MATRIX_COLS; col++) {
pin_t pin = matrix_pins[row][col]; pin_t pin = matrix_pins[row][col];
if (pin != NO_PIN) { if (pin != NO_PIN) {
setPinInputHigh(pin); setPinInputHigh(pin);
} }
} }
// } }
} }
} }
@ -156,16 +157,6 @@ void matrix_init(void) {
// initialize key pins // initialize key pins
matrix_init_pins(); matrix_init_pins();
for (int row = 0; row < MATRIX_ROWS; row++) {
// if (row != 1){
for (int col = 0; col < MATRIX_COLS; col++) {
pin_t pin = matrix_pins[row][col];
if (pin != NO_PIN) {
setPinInputHigh(pin);
}
}
// }
}
matrix_init_custom(); matrix_init_custom();
// initialize matrix state: all keys off // initialize matrix state: all keys off
@ -175,7 +166,7 @@ void matrix_init(void) {
} }
// Unless hardware debouncing - Init the configured debounce routine // Unless hardware debouncing - Init the configured debounce routine
debounce_init(MATRIX_ROWS); // debounce_init(MATRIX_ROWS);
// This *must* be called for correct keyboard behavior // This *must* be called for correct keyboard behavior
matrix_init_kb(); matrix_init_kb();
@ -205,18 +196,13 @@ __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[]
current_matrix[current_row] = current_row_value; current_matrix[current_row] = current_row_value;
} }
matrix_row_t previous_matrix[MATRIX_ROWS];
bool matrix_scan_custom(matrix_row_t current_matrix[]) { bool matrix_scan_custom(matrix_row_t current_matrix[]) {
matrix_row_t previous_matrix[MATRIX_ROWS];
// matrix_row_t is an alias for u_int8_t // matrix_row_t is an alias for u_int8_t
memcpy(previous_matrix, current_matrix, sizeof(previous_matrix)); memcpy(previous_matrix, current_matrix, sizeof(previous_matrix));
for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {

View File

@ -1,16 +0,0 @@
/*
Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
Copyright 2023 peepeetee
SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "quantum.h"
#include "analog.h"
#include "lut.h"
#include "scanfunctions.h"

View File

@ -3,6 +3,4 @@ SRC += matrix.c lut.c scanfunctions.c
CUSTOM_MATRIX = yes CUSTOM_MATRIX = yes
DEBOUNCE_TYPE = custom
OPT = 3 OPT = 3

View File

@ -12,7 +12,7 @@ SPDX-License-Identifier: GPL-2.0-or-later */
analog_config g_config = { analog_config g_config = {
.mode = static_actuation, .mode = dynamic_actuation,
.actuation_point = 32, .actuation_point = 32,
.press_sensitivity = 32, .press_sensitivity = 32,
.release_sensitivity = 32, .release_sensitivity = 32,