Mozzi  version v2.0
sound synthesis library for Arduino
AutoRange.h
1 /*
2  * AutoRange.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2013-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  */
11 
12 #ifndef AUTORANGE_H
13 #define AUTORANGE_H
14 
15 /** @ingroup sensortools
16 Keeps a running calculation of the range of the input values it receives.
17 */
18 template <class T>
19 class
20  AutoRange {
21 
22 public:
23  /** Constructor.
24  @tparam T the type of numbers to to use, eg. int, unsigned int, float etc.
25  @param min_expected the minimum possible input value.
26  @param max_expected the maximum possible input value.
27  */
28  AutoRange(T min_expected, T max_expected):range_min(max_expected),range_max(min_expected),range(0)
29  {
30  }
31 
32 
33  /** Updates the current range.
34  @param n the next value to include in the range calculation.
35  */
36  void next(T n)
37  {
38  if (n > range_max)
39  {
40  range_max = n;
41  range = range_max - range_min;
42  }
43  else
44  {
45  if (n< range_min)
46  {
47  range_min = n;
48  range = range_max - range_min;
49  }
50  }
51  }
52 
53  /** Returns the current minimum.
54  @return minimum
55  */
56  T getMin()
57  {
58  return range_min;
59  }
60 
61 
62  /** Returns the current maximum.
63  @return maximum
64  */
65  T getMax()
66  {
67  return range_max;
68  }
69 
70 
71  /** Returns the current range.
72  @return range
73  */
75  {
76  return range;
77  }
78 
79 private:
80  T range_max, range_min, range;
81 
82 };
83 
84 #endif // #ifndef AUTORANGE_H