Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
Hysteresis.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <stdint.h>
6
8
11
36template <uint8_t Bits, class T_in = uint16_t, class T_out = uint8_t>
38 public:
50 T_in prevLevelFull = (T_in(prevLevel) << Bits) | offset;
51 T_in lowerbound = prevLevel > 0 ? prevLevelFull - margin : 0;
52 T_in upperbound = prevLevel < max_out ? prevLevelFull + margin : max_in;
54 setValue(inputLevel);
55 return true;
56 }
57 return false;
58 }
59
65 T_out getValue() const { return prevLevel; }
66
70 void setValue(T_in inputLevel) { prevLevel = inputLevel >> Bits; }
71
72 private:
73 T_out prevLevel = 0;
74 constexpr static T_in margin = (1ul << Bits) - 1ul;
75 constexpr static T_in offset = Bits >= 1 ? 1ul << (Bits - 1) : 0;
76 constexpr static T_in max_in = static_cast<T_in>(-1);
77 constexpr static T_out max_out = static_cast<T_out>(max_in >> Bits);
78 static_assert(max_in > 0, "Error: only unsigned types are supported");
79};
80
81template <class T_in, class T_out>
82class Hysteresis<0, T_in, T_out> {
83 public:
85 bool changed = inputLevel != prevLevel;
86 prevLevel = inputLevel;
87 return changed;
88 }
89
90 T_out getValue() const { return prevLevel; }
91 void setValue(T_in inputLevel) { prevLevel = inputLevel; }
92
93 private:
94 T_in prevLevel = 0;
95};
96
98
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
bool update(T_in inputLevel)
void setValue(T_in inputLevel)
A class for applying hysteresis to a given input.
bool update(T_in inputLevel)
Update the hysteresis output with a new input value.
void setValue(T_in inputLevel)
Forcefully update the internal state to the given level.
T_out getValue() const
Get the current output level.
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...