Control Surface  1.1.1
MIDI Control Surface library for Arduino
Hysteresis.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
5 
6 #include <stdint.h>
7 
10 
35 template <uint8_t BITS, class T_in = uint16_t, class T_out = uint8_t>
36 class Hysteresis {
37  public:
48  bool update(T_in inputLevel) {
49  T_in prevLevelFull = (T_in(prevLevel) << BITS) | offset;
50  T_in lowerbound = prevLevel > 0 ? prevLevelFull - margin : 0;
51  T_in upperbound = prevLevel < max_out ? prevLevelFull + margin : max_in;
52  if (inputLevel < lowerbound || inputLevel > upperbound) {
53  prevLevel = inputLevel >> BITS;
54  return true;
55  }
56  return false;
57  }
58 
64  T_out getValue() const { return prevLevel; }
65 
66  private:
67  T_out prevLevel = 0;
68  constexpr static T_in margin = (1UL << BITS) - 1;
69  constexpr static T_in offset = 1UL << (BITS - 1);
70  constexpr static T_in max_in = -1;
71  constexpr static T_out max_out = static_cast<T_out>(max_in >> BITS);
72  static_assert(max_in > 0, "Error: only unsigned types are supported");
73 };
74 
76 
Warnings.hpp
Hysteresis::margin
constexpr static T_in margin
Definition: Hysteresis.hpp:68
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
Hysteresis::getValue
T_out getValue() const
Get the current output level.
Definition: Hysteresis.hpp:64
Hysteresis::max_out
constexpr static T_out max_out
Definition: Hysteresis.hpp:71
Hysteresis
A class for applying hysteresis to a given input.
Definition: Hysteresis.hpp:36
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
Hysteresis::offset
constexpr static T_in offset
Definition: Hysteresis.hpp:69
Hysteresis::update
bool update(T_in inputLevel)
Update the hysteresis output with a new input value.
Definition: Hysteresis.hpp:48
Hysteresis::prevLevel
T_out prevLevel
Definition: Hysteresis.hpp:67
Hysteresis::max_in
constexpr static T_in max_in
Definition: Hysteresis.hpp:70