add some scratchpad code and psudocode for my headspace

This commit is contained in:
peepeetee 2024-03-19 19:02:43 -05:00
parent 3755e50477
commit 1f96833780
3 changed files with 75 additions and 0 deletions

View File

@ -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,

View File

@ -0,0 +1,22 @@
#include <Arduino.h>
#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;
}

View File

@ -0,0 +1,35 @@
#pragma once
#include <cstdint>
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;
};