cleanup code, fix hall effect keys row off by one error

This commit is contained in:
peepeetee 2025-06-21 00:02:13 +08:00
parent d5cbafa745
commit 6325c8e568
8 changed files with 33 additions and 206 deletions

View File

@ -7,6 +7,7 @@ SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
#include <stdint.h>
#include <stdbool.h>
enum analog_key_modes {
dynamic_actuation = 0,

View File

@ -5,7 +5,6 @@
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = LAYOUT(
KC_MUTE,
KC_1, KC_2, KC_3,
KC_4, KC_5, KC_6
)

View File

@ -5,7 +5,6 @@
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = LAYOUT(
KC_MUTE,
KC_Z, KC_X, KC_C,
KC_GRV, KC_ESC, KC_F2
)

View File

@ -2,8 +2,8 @@
SPDX-License-Identifier: GPL-2.0-or-later */
#include <math.h>
#include <stdint.h>
// #include "scanfunctions.h"
// #include "util.h"
#include "scanfunctions.h"
#include "util.h"
#if !defined(MIN)
# define MIN(x, y) (((x) < (y)) ? (x) : (y))

View File

@ -8,6 +8,7 @@ extern uint16_t lut[ADC_RESOLUTION_MAX];
const double lut_a;
const double lut_b;
const double lut_c;
const double lut_d;
uint16_t distance_to_adc(uint16_t distance);

View File

@ -1,6 +1,6 @@
/* Copyright 2023 RephlexZero (@RephlexZero)
Copyright 2023 peepeetee
SPDX-License-Identifier: GPL-2.0-or-later */
Copyright 2023 peepeetee
SPDX-License-Identifier: GPL-2.0-or-later */
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
@ -11,97 +11,26 @@ SPDX-License-Identifier: GPL-2.0-or-later */
#include "debounce.h"
#include "scanfunctions.h"
#include "sma.c"
// #include "matrix_helpers.c"
#ifndef MATRIX_INPUT_PRESSED_STATE
# define MATRIX_INPUT_PRESSED_STATE 0
#endif
// //configuration for the SMA filter, default is 4 for 2^4 = 16 samples
// #ifndef SMA_FILTER_SAMPLE_EXPONENT
// # define SMA_FILTER_SAMPLE_EXPONENT 4
// #endif
// Pin and key matrix definitions
pin_t matrix_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
hybrid_key_t keys[MATRIX_ROWS][MATRIX_COLS] = {0};
static uint16_t pressedAdcValue = 0;
static uint16_t restAdcValue = 0;
/* matrix state(1:on, 0:off) */
/* Matrix state: 1 = on, 0 = off */
matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
matrix_row_t matrix[MATRIX_ROWS]; // debounced values
void matrix_init_custom(void) {
wait_ms(3500); //give time to try to have hid_listen spin up
printf("%s\n", "test print at start of matrix init");
wait_ms(250);
// printf("%s\n", "2nd test print at start of matrix init");
// wait_ms(250);
// printf("%u\n", MATRIX_COLS);
// wait_ms(250);
// printf("%u\n", MATRIX_ROWS);
// wait_ms(250);
// for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
// for (uint8_t col = 0; col < MATRIX_COLS; col++) {
// printf("%lu\n", matrix_pins[row][col]);
// wait_ms(250);
// }
// wait_ms(250);
// printf("\n");
// }
// wait_ms(250);
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
keys[1][i].is_analog = true;
//should really be done at compile time
// initialize_SMA_filter(keys[1][i], SMA_FILTER_SAMPLE_EXPONENT);
}
// for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
// for (uint8_t col = 0; col < MATRIX_COLS; col++) {
// printf("keys[%u][%u].value = %u\n", row, col, keys[row][col].value);
// wait_ms(250);
// printf("keys[%u][%u].extremum = %u\n", row, col, keys[row][col].extremum);
// wait_ms(250);
// printf("keys[%u][%u].offset = %u\n", row, col, keys[row][col].offset);
// wait_ms(250);
// printf("keys[%u][%u].is_analog = %s\n", row, col, keys[row][col].is_analog ? "true" : "false");
// wait_ms(250);
// printf("keys[%u][%u].dynamic_actuation_bool = %s\n", row, col, keys[row][col].dynamic_actuation_bool ? "true" : "false");
// }
// wait_ms(250);
// printf("\n");
// }
generate_lut();
pressedAdcValue = distance_to_adc(400);
restAdcValue = distance_to_adc(0);
// get_sensor_offsets();
wait_ms(200); // Let ADC reach steady state
get_sensor_offsets();
}
//setup only rows 0 and 2, leave row 1 untouched
__attribute__((weak)) void matrix_init_pins(void) {
// if (keys[row][0].is_analog == false)
// Setup only rows 1, leave row 0 untouched (for analog)
void matrix_init_pins(void) {
for (int row = 0; row < MATRIX_ROWS; row++) {
if (row != 1){
if (row != 0) {
for (int col = 0; col < MATRIX_COLS; col++) {
pin_t pin = matrix_pins[row][col];
if (pin != NO_PIN) {
@ -112,118 +41,41 @@ __attribute__((weak)) void matrix_init_pins(void) {
}
}
// user-defined overridable functions
void matrix_init_custom(void) {
// Analog row setup (should ideally be done at compile time)
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
keys[0][i].is_analog = true;
// initialize_SMA_filter(&keys[1][i], SMA_FILTER_SAMPLE_EXPONENT);
}
matrix_init_pins();
generate_lut();
pressedAdcValue = distance_to_adc(400);
restAdcValue = distance_to_adc(0);
wait_ms(100); // Let ADC reach steady state
get_sensor_offsets();
}
// User-defined overridable functions
__attribute__((weak)) void matrix_init_pins(void);
__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter);
// static inline uint8_t readMatrixPin(pin_t pin) {
// if (pin != NO_PIN) {
// return (readPin(pin) == MATRIX_INPUT_PRESSED_STATE) ? 0 : 1;
// } else {
// return 1;
// }
// }
matrix_row_t matrix_get_row(uint8_t row) {
// TODO: return the requested row data
//matrix_common.c row 68
return matrix[row];
}
# define print_matrix_header() print("\nr/c 01234567\n")
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
void matrix_print(void) {
// TODO: use print() to dump the current matrix state to console
//matrix_common.c row 84
print_matrix_header();
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
print_hex8(row);
print(": ");
print_matrix_row(row);
print("\n");
}
}
void matrix_init(void) {
// TODO: initialize hardware and global matrix state here
//matrix.c row 274
//matrix_common.c row 149
// initialize key pins
matrix_init_pins();
matrix_init_custom();
// initialize matrix state: all keys off
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
raw_matrix[i] = 0;
matrix[i] = 0;
}
// Unless hardware debouncing - Init the configured debounce routine
// debounce_init(MATRIX_ROWS);
// This *must* be called for correct keyboard behavior
matrix_init_kb();
}
static inline uint8_t readMatrixPin(pin_t pin) {
if (pin != NO_PIN) {
return (readPin(pin) == MATRIX_INPUT_PRESSED_STATE) ? 0 : 1;
} else {
return 1;
}
}
__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
// Start with a clear matrix row
matrix_row_t current_row_value = 0;
matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) {
pin_t pin = matrix_pins[current_row][col_index];
current_row_value |= readMatrixPin(pin) ? 0 : row_shifter;
}
// Update the matrix
current_matrix[current_row] = current_row_value;
}
matrix_row_t previous_matrix[MATRIX_ROWS];
static matrix_row_t previous_matrix[MATRIX_ROWS];
bool matrix_scan_custom(matrix_row_t current_matrix[]) {
// matrix_row_t is an alias for u_int8_t
memcpy(previous_matrix, current_matrix, sizeof(previous_matrix));
for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
if (keys[current_row][0].is_analog == false) {
if (!keys[current_row][0].is_analog) {
matrix_read_cols_on_row(current_matrix, current_row);
} else {
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
hybrid_key_t *key = &keys[current_row][current_col];
// TODO: current row has to be 1!!!!!!!!!!
key->value = lut[analogReadPin(matrix_pins[current_row][current_col]) + key->offset];
// limits our options, I would like to change hybrid_key_t to include weather it is a hall effect key
// key->value = MIN((key->value << 8) / lut[1100 + key->offset], 400);
switch (g_config.mode) {
case dynamic_actuation:
@ -247,28 +99,15 @@ bool matrix_scan_custom(matrix_row_t current_matrix[]) {
}
uint8_t matrix_scan(void) {
// TODO: add matrix scanning routine here
//matrix.c row 324
//matrix_common.c row 169
bool changed = matrix_scan_custom(raw_matrix);
// Unless hardware debouncing - use the configured debounce routine
changed = debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
// This *must* be called for correct keyboard behavior
matrix_scan_kb();
return (uint8_t)changed;
}
__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
__attribute__((weak)) void matrix_init_user(void) {}
__attribute__((weak)) void matrix_scan_user(void) {}

View File

@ -1,6 +1,6 @@
QUANTUM_LIB_SRC += analog.c
SRC += matrix.c lut.c scanfunctions.c
CUSTOM_MATRIX = yes
CUSTOM_MATRIX = lite
OPT = 3

View File

@ -56,7 +56,7 @@ bool debug_print(void) {
int raw_analog_value[3];
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
raw_analog_value[col] = analogReadPin(matrix_pins[1][col]);
raw_analog_value[col] = analogReadPin(matrix_pins[0][col]);
}
printf("raw_analog_value = %d, %d, %d\n", raw_analog_value[0], raw_analog_value[1], raw_analog_value[2]);
@ -82,11 +82,11 @@ uint32_t idle_recalibrate_callback(uint32_t trigger_time, void *cb_arg) {
#endif
void values_load(void) {
eeconfig_read_kb_datablock(&g_config);
eeconfig_read_kb_datablock(&g_config, 0, EECONFIG_KB_DATA_SIZE);
}
void values_save(void) {
eeconfig_update_kb_datablock(&g_config);
eeconfig_update_kb_datablock(&g_config, 0, EECONFIG_KB_DATA_SIZE);
}
void eeconfig_init_kb() {
@ -203,16 +203,4 @@ void via_config_get_value(uint8_t *data) {
#endif
#ifdef OLED_ENABLE
bool oled_task_kb() {
static const char image[] PROGMEM = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xf0, 0xfc, 0xff, 0xf8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xfc, 0xbf, 0xbf, 0x9f, 0x9f, 0x1f, 0x1f, 0x1f, 0x5f, 0x5f, 0x5f, 0x1f, 0x1f, 0x1f, 0x7f, 0xff, 0xff, 0xfe, 0xf8, 0xe0, 0x80, 0x87, 0xff, 0xff, 0xfe, 0xf8, 0xf0, 0xe0, 0x80, 0x00, 0x00, 0x00, 0x27, 0x27, 0x27, 0x27, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f, 0x0f, 0x0e, 0x08, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x80, 0xe0, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x37, 0xe0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf8, 0xf8, 0xf8, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0xf0, 0x30, 0xf0, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xf0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x7f, 0x7f, 0x3f, 0x9f, 0xbf, 0xbf, 0xbf, 0xbf, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x00, 0xe0, 0xe0, 0x80, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xf8, 0xf0, 0x00, 0x00, 0x00, 0xf0, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x07, 0x0f, 0x0f, 0x0f, 0x1f, 0x1f, 0x1f, 0x1f, 0x3f, 0x3f, 0x3f, 0x3f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x3f, 0x3f, 0x3f, 0x3f, 0x1f, 0x1f, 0x1f, 0x0f, 0x0f, 0x0f, 0x07, 0x03, 0x00, 0x00, 0x00, 0x80, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x3f, 0x3f, 0x1f, 0x80, 0xc0, 0xcf, 0xcf, 0xdf, 0x9f, 0x3f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
oled_write_raw_P(image, sizeof(image));
return false;
}
#endif