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