FastLED 3.9.15
Loading...
Searching...
No Matches
moving_average_impl.h
Go to the documentation of this file.
1#pragma once
2
5#include "fl/stl/noexcept.h"
6
7namespace fl {
8namespace detail {
9
10template <typename T, fl::size N = 0>
12 public:
14 explicit MovingAverageImpl(fl::size capacity) : mBuf(capacity), mSum(T(0)) {}
15
16 T update(T input) {
17 if (mBuf.full()) {
18 mSum = mSum - mBuf.front();
19 }
20 mBuf.push_back(input);
21 mSum = mSum + input;
22 return value();
23 }
24
25 T value() const {
26 fl::size count = mBuf.size();
27 if (count == 0) {
28 return T(0);
29 }
30 return divByCount(mSum, count);
31 }
32
33 void reset() {
34 mBuf.clear();
35 mSum = T(0);
36 }
37
38 bool full() const { return mBuf.full(); }
39 fl::size size() const { return mBuf.size(); }
40 fl::size capacity() const { return mBuf.capacity(); }
41
42 void resize(fl::size new_capacity) {
43 mBuf = circular_buffer<T, N>(new_capacity);
44 mSum = T(0);
45 }
46
47 private:
48 template <typename U = T>
50 divByCount(U sum, fl::size count) {
51 return sum / static_cast<U>(count);
52 }
53
54 template <typename U = T>
56 divByCount(U sum, fl::size count) {
57 return sum / static_cast<U>(count);
58 }
59
60 template <typename U = T>
63 divByCount(U sum, fl::size count) {
64 return sum / U(static_cast<float>(count));
65 }
66
69};
70
71} // namespace detail
72} // namespace fl
static fl::enable_if< fl::is_integral< U >::value, U >::type divByCount(U sum, fl::size count)
static fl::enable_if< fl::is_floating_point< U >::value, U >::type divByCount(U sum, fl::size count)
void resize(fl::size new_capacity)
static fl::enable_if<!fl::is_floating_point< U >::value &&!fl::is_integral< U >::value, U >::type divByCount(U sum, fl::size count)
Compile-time linker keep-alive hook for a single fl::Bus.
Definition bus_traits.h:48
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT