amend sma logic, polish some functions

This commit is contained in:
peepeetee 2024-04-28 19:32:59 -05:00
parent fe3629a05c
commit 8781b54ee1
3 changed files with 14 additions and 2 deletions

View File

@ -16,6 +16,7 @@ typedef struct {
uint8_t press_hysteresis;
uint8_t release_hysteresis;
} analog_config; /* 6 bytes */
//size defined in config.h
_Static_assert(sizeof(analog_config) == EECONFIG_KB_DATA_SIZE, "Size mismatch");
extern analog_config g_config;
@ -32,5 +33,6 @@ typedef struct {
uint16_t *SMA_buffer;
uint32_t SMA_sum;
uint8_t SMA_index;
bool SMA_filled;
} hybrid_key_t;
extern hybrid_key_t keys[MATRIX_ROWS][MATRIX_COLS];

View File

@ -1,4 +1,4 @@
/* Copyright 2023 RephlexZero (@RephlexZero)
/* Copyright 2023 RephlexZero (@RephlexZero) 2024 peepeetee (@peepeetee)
SPDX-License-Identifier: GPL-2.0-or-later */
#include "quantum.h"
#include "analog.h"
@ -7,6 +7,7 @@ SPDX-License-Identifier: GPL-2.0-or-later */
extern pin_t matrix_pins[MATRIX_ROWS][MATRIX_COLS];
//detects sensor offsets when the key is not pressed
void get_sensor_offsets(void) {
uint16_t rest_adc_value = distance_to_adc(0);
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {

View File

@ -9,7 +9,10 @@ SPDX-License-Identifier: GPL-2.0-or-later */
void initialize_SMA_filter(hybrid_key_t key, uint8_t samplesExponent) {
key.SMA_samplesExponent = samplesExponent;
key.SMA_samples = 1 << samplesExponent;
key.SMA_buffer = malloc(key.SMA_samples*sizeof(uint16_t));
key.SMA_buffer = malloc((key.SMA_samples)*sizeof(uint16_t));
for (int i = 0; i < key.SMA_samples; i++) {
key.SMA_buffer[i] = 0;
}
printf("%s\n", "SMA_buffer test print");
key.SMA_sum = 0;
key.SMA_index = 0;
@ -19,6 +22,12 @@ uint16_t SMA_filter(hybrid_key_t key, uint16_t value) {
key.SMA_sum = key.SMA_sum - key.SMA_buffer[key.SMA_index] + value;
key.SMA_buffer[key.SMA_index] = value;
key.SMA_index = (key.SMA_index + 1) % key.SMA_samples;
if (!key.SMA_filled) {
if (key.SMA_index == 0) {
key.SMA_filled = true;
}
}
printf("%s\n", "SMA_filter test print");
printf("%s\n", "SMA_sum and SMA_index:");
printf("%lu\n", key.SMA_sum);