stashing progress

This commit is contained in:
peepeetee 2024-04-26 18:17:39 -05:00
parent 1f96833780
commit 8807498057
6 changed files with 80 additions and 54 deletions

View File

@ -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 */ SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once #pragma once
@ -26,5 +26,11 @@ typedef struct {
int16_t offset; int16_t offset;
bool is_analog; bool is_analog;
bool dynamic_actuation_bool; 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; } hybrid_key_t;
extern hybrid_key_t keys[MATRIX_ROWS][MATRIX_COLS]; extern hybrid_key_t keys[MATRIX_ROWS][MATRIX_COLS];

View File

@ -15,3 +15,6 @@
#define CALIBRATION_RANGE 255 #define CALIBRATION_RANGE 255
#define DEBOUNCE 16 #define DEBOUNCE 16
//this configuration for the SMA filter, default is 4 for 2^4 = 16 samples
#define SMA_FILTER_SAMPLE_EXPONENT 4

View File

@ -10,12 +10,19 @@ SPDX-License-Identifier: GPL-2.0-or-later */
#include "lut.h" #include "lut.h"
#include "scanfunctions.h" #include "scanfunctions.h"
#include "debounce.c" #include "debounce.c"
#include "sma.c"
// #include "matrix_helpers.c" // #include "matrix_helpers.c"
#ifndef MATRIX_INPUT_PRESSED_STATE #ifndef MATRIX_INPUT_PRESSED_STATE
# define MATRIX_INPUT_PRESSED_STATE 0 # define MATRIX_INPUT_PRESSED_STATE 0
#endif #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; pin_t matrix_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
hybrid_key_t keys[MATRIX_ROWS][MATRIX_COLS] = {0}; 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++) { 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
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++) {

View File

@ -1,8 +1,16 @@
matrix scanning: matrix scanning:
scan values scan values
put values in SMA filter put values in SMA filter:
get value out, update sensor boundaries if sma initialized 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 calebrate key if this is first pass
@ -10,8 +18,21 @@ matrix scanning:
run algos on the key: run algos on the key:
regular(look at the methods already implemented) regular(look at the methods already implemented)
rapid trigger: rapid trigger:
default boundaries are hysteresis values 1.
if continuous rapid trigger, 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

View File

@ -1,22 +1,36 @@
#include <Arduino.h> /* Copyright 2024 peepeetee (@peepeetee) 2024 minisbett (@minisbett)
#include "helpers/sma_filter.hpp" 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. #include <stdint.h>
uint16_t SMAFilter::operator()(uint16_t value) #include "analogkeys.h"
{
// Calculate the new sum by removing the oldest element and adding the new one.
sum = sum - buffer[index] + value;
// 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. void initialize_SMA_filter(hybrid_key_t key, uint8_t samplesExponent) {
if(index == 0) key.SMA_samplesExponent = samplesExponent;
initialized = true; key.SMA_samples = 1 << samplesExponent;
key.SMA_buffer = malloc(key.SMA_samples*sizeof(uint16_t));
// Divide the number by the amount of samples using bitshifting and return it. printf("%s\n", "SMA_buffer test print");
return sum >> samplesExponent; 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;
}

View File

@ -1,35 +1,8 @@
#pragma once #pragma once
#include <stdint.h>
#include "analogkeys.h"
#include <cstdint> void initialize_SMA_filter(hybrid_key_t key, uint8_t samplesExponent);
class SMAFilter uint16_t SMA_filter(hybrid_key_t key, uint16_t value);
{
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;
};