From 1f96833780f766f675aae8ae3af15a63c5cd3d67 Mon Sep 17 00:00:00 2001 From: peepeetee Date: Tue, 19 Mar 2024 19:02:43 -0500 Subject: [PATCH] add some scratchpad code and psudocode for my headspace --- keyboards/momokai/tap_trio_pro/psudocode | 18 ++++++++++++ keyboards/momokai/tap_trio_pro/sma.c | 22 +++++++++++++++ keyboards/momokai/tap_trio_pro/sma.h | 35 ++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 keyboards/momokai/tap_trio_pro/psudocode create mode 100644 keyboards/momokai/tap_trio_pro/sma.c create mode 100644 keyboards/momokai/tap_trio_pro/sma.h diff --git a/keyboards/momokai/tap_trio_pro/psudocode b/keyboards/momokai/tap_trio_pro/psudocode new file mode 100644 index 00000000000..29ccab57ddf --- /dev/null +++ b/keyboards/momokai/tap_trio_pro/psudocode @@ -0,0 +1,18 @@ +matrix scanning: + + scan values + put values in SMA filter + get value out, update sensor boundaries if sma initialized + + calebrate key if this is first pass + + + run algos on the key: + regular(look at the methods already implemented) + rapid trigger: + default boundaries are hysteresis values + if continuous rapid trigger, + + + + diff --git a/keyboards/momokai/tap_trio_pro/sma.c b/keyboards/momokai/tap_trio_pro/sma.c new file mode 100644 index 00000000000..f0a4bea3e67 --- /dev/null +++ b/keyboards/momokai/tap_trio_pro/sma.c @@ -0,0 +1,22 @@ +#include +#include "helpers/sma_filter.hpp" + +// 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; + + // 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; +} diff --git a/keyboards/momokai/tap_trio_pro/sma.h b/keyboards/momokai/tap_trio_pro/sma.h new file mode 100644 index 00000000000..b4837004a30 --- /dev/null +++ b/keyboards/momokai/tap_trio_pro/sma.h @@ -0,0 +1,35 @@ +#pragma once + +#include + +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; +};