diff --git a/keyboards/momokai/tap_trio_pro/analogkeys.h b/keyboards/momokai/tap_trio_pro/analogkeys.h index c5033af719c..dd3b969060a 100644 --- a/keyboards/momokai/tap_trio_pro/analogkeys.h +++ b/keyboards/momokai/tap_trio_pro/analogkeys.h @@ -1,4 +1,4 @@ -/* Copyright 2023 RephlexZero (@RephlexZero) +/* Copyright 2023 RephlexZero (@RephlexZero) 2024 peepeetee (@peepeetee) 2024 minisbett (@minisbett) SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once @@ -26,5 +26,11 @@ typedef struct { int16_t offset; bool is_analog; bool dynamic_actuation_bool; + // uint16_t SMA_buffer[1<<(SMA_FILTER_SAMPLE_EXPONENT)]; + uint8_t SMA_samplesExponent; + uint8_t SMA_samples; + uint16_t *SMA_buffer; + uint32_t SMA_sum; + uint8_t SMA_index; } hybrid_key_t; extern hybrid_key_t keys[MATRIX_ROWS][MATRIX_COLS]; diff --git a/keyboards/momokai/tap_trio_pro/config.h b/keyboards/momokai/tap_trio_pro/config.h index 7f3ac385716..00627bd4541 100644 --- a/keyboards/momokai/tap_trio_pro/config.h +++ b/keyboards/momokai/tap_trio_pro/config.h @@ -15,3 +15,6 @@ #define CALIBRATION_RANGE 255 #define DEBOUNCE 16 + +//this configuration for the SMA filter, default is 4 for 2^4 = 16 samples +#define SMA_FILTER_SAMPLE_EXPONENT 4 diff --git a/keyboards/momokai/tap_trio_pro/matrix.c b/keyboards/momokai/tap_trio_pro/matrix.c index 2c0711d0f73..ed316ffe362 100644 --- a/keyboards/momokai/tap_trio_pro/matrix.c +++ b/keyboards/momokai/tap_trio_pro/matrix.c @@ -10,12 +10,19 @@ SPDX-License-Identifier: GPL-2.0-or-later */ #include "lut.h" #include "scanfunctions.h" #include "debounce.c" +#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_t matrix_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS; hybrid_key_t keys[MATRIX_ROWS][MATRIX_COLS] = {0}; @@ -56,6 +63,8 @@ void matrix_init_custom(void) { 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++) { diff --git a/keyboards/momokai/tap_trio_pro/psudocode b/keyboards/momokai/tap_trio_pro/psudocode index 29ccab57ddf..1493a6892ad 100644 --- a/keyboards/momokai/tap_trio_pro/psudocode +++ b/keyboards/momokai/tap_trio_pro/psudocode @@ -1,8 +1,16 @@ matrix scanning: scan values - put values in SMA filter - get value out, update sensor boundaries if sma initialized + put values in SMA filter: + for each key, there is a sma filter instance + the filter takes in a 2^4=16 values in a circular buffer + 3 variables: 4 -> 2^4 -> uint16_t[2^4] + state variable: is filter initialized for this key? + pointer: where in the circular buffer are we? + sum: sum of values in the circular buffer, so that we don't have to access the whole buffer, just 2 elements. + + + get value out, update sensor boundaries only if sma initialized calebrate key if this is first pass @@ -10,8 +18,21 @@ matrix scanning: run algos on the key: regular(look at the methods already implemented) rapid trigger: - default boundaries are hysteresis values - if continuous rapid trigger, + 1. + default boundaries are hysteresis values, reset state once reached + (if continuous rapid trigger, reset state only if fully released) + 2. + when rapid trigger state is false + if value is in rapid trigger zone, press and set rapid trigger state + if state is false && lower than lower hysteresis, press key and set rapid trigger state + 3. + when rapid trigger state is true + if pressed && left rt zone or value is 'sensitivity' above peak, release + if not pressed and key is in trigger zone and value is sensitivity below peak, press + + 4. if a new peak is observed, update it in the key structure + peak here is a up or down, since keeping track of both doesn't make sense + diff --git a/keyboards/momokai/tap_trio_pro/sma.c b/keyboards/momokai/tap_trio_pro/sma.c index f0a4bea3e67..5b162ce80b8 100644 --- a/keyboards/momokai/tap_trio_pro/sma.c +++ b/keyboards/momokai/tap_trio_pro/sma.c @@ -1,22 +1,36 @@ -#include -#include "helpers/sma_filter.hpp" +/* Copyright 2024 peepeetee (@peepeetee) 2024 minisbett (@minisbett) +SPDX-License-Identifier: GPL-2.0-or-later */ -// On the call operator the next value is given into the filter, with the new average being returned. -uint16_t SMAFilter::operator()(uint16_t value) -{ - // Calculate the new sum by removing the oldest element and adding the new one. - sum = sum - buffer[index] + value; +#include +#include "analogkeys.h" - // Overwrite the oldest element in the circular buffer with the new one. - buffer[index] = value; - // Move the index by 1 or restart at 0 if the end is reached. - index = (index + 1) % samples; - // If the index is 0 here (meaning the circular index just reset), set the fully initialized state to true. - if(index == 0) - initialized = true; - - // Divide the number by the amount of samples using bitshifting and return it. - return sum >> samplesExponent; +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)); + printf("%s\n", "SMA_buffer test print"); + key.SMA_sum = 0; + key.SMA_index = 0; } + +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; + printf("%s\n", "SMA_filter test print"); + printf("%s\n", "SMA_sum and SMA_index:"); + printf("%lu\n", key.SMA_sum); + printf("%x\n", key.SMA_index); + for(int i = 0; i < key.SMA_samples; i++) { + printf("%u\n", key.SMA_buffer[i]); + } + printf("%s\n", "current value:"); + printf("%x\n", key.SMA_buffer[key.SMA_index]); + return key.SMA_sum >> key.SMA_samplesExponent; +} + + + + diff --git a/keyboards/momokai/tap_trio_pro/sma.h b/keyboards/momokai/tap_trio_pro/sma.h index b4837004a30..b22f5dccf40 100644 --- a/keyboards/momokai/tap_trio_pro/sma.h +++ b/keyboards/momokai/tap_trio_pro/sma.h @@ -1,35 +1,8 @@ + #pragma once +#include +#include "analogkeys.h" -#include +void initialize_SMA_filter(hybrid_key_t key, uint8_t samplesExponent); -class SMAFilter -{ -public: - // Initialize the SMAFilter instance with the specified sample exponent. - // (1 = 1 sample, 2 = 4 samples, 3 = 8 samples, ...) - SMAFilter(uint8_t samplesExponent) - : samplesExponent(samplesExponent) - , samples(1 << samplesExponent) - , buffer(new uint16_t[samples] {0}) - {} - - // The call operator for passing values through the filter. - uint16_t operator()(uint16_t value); - - // Bool whether the whole buffer has been written at least once. - bool initialized = false; - -private: - // The amount of samples and the exponent. - uint8_t samplesExponent; - uint8_t samples; - - // The buffer containing all values. - uint16_t *buffer; - - // The index of the oldest and thus next element to overwrite. - uint8_t index = 0; - - // The sum of all values in the buffer. - uint32_t sum = 0; -}; +uint16_t SMA_filter(hybrid_key_t key, uint16_t value);