19 #ifndef ROLLINGAVERAGE_H
20 #define ROLLINGAVERAGE_H
22 #include "mozzi_utils.h"
35 template <
class T,
int WINDOW_LENGTH>
53 for (
int thisReading = 0; thisReading < WINDOW_LENGTH; thisReading++)
54 readings[thisReading] = 0;
65 return add(input)>>WINDOW_LENGTH_AS_RSHIFT;
74 total -= readings[index];
77 readings[index] = input;
80 ++index &= WINDOW_LENGTH -1;
86 T readings[WINDOW_LENGTH];
89 const uint8_t WINDOW_LENGTH_AS_RSHIFT;
99 template <
int WINDOW_LENGTH>
113 for (
int thisReading = 0; thisReading < WINDOW_LENGTH; thisReading++)
114 readings[thisReading] = 0;
122 unsigned int next(
unsigned int input)
127 return (
unsigned)add(input)>>WINDOW_LENGTH_AS_RSHIFT;
134 unsigned int add(
unsigned int input){
136 total -= readings[index];
139 readings[index] = input;
142 ++index &= WINDOW_LENGTH -1;
148 unsigned int readings[WINDOW_LENGTH];
151 const uint8_t WINDOW_LENGTH_AS_RSHIFT;
158 template <
int WINDOW_LENGTH>
172 for (
int thisReading = 0; thisReading < WINDOW_LENGTH; thisReading++)
173 readings[thisReading] = 0.0;
181 float next(
float input)
184 total -= readings[index];
187 readings[index] = input;
190 ++index &= WINDOW_LENGTH -1;
193 return total/WINDOW_LENGTH;
197 float readings[WINDOW_LENGTH];
constexpr uint8_t trailingZerosConst(unsigned long v)
Given a power of 2, work out the number to shift right by to do a divide by the number,...