Mozzi  version v2.0
sound synthesis library for Arduino
AutoMap.h
1 /*
2  * AutoMap.h
3 /*
4  * AutoMap.h
5  *
6  * This file is part of Mozzi.
7  *
8  * Copyright 2012-2024 Tim Barrass and the Mozzi Team
9  *
10  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
11  *
12  */
13 
14 #ifndef AUTOMAP_H_
15 #define AUTOMAP_H_
16 
17 // for map - maybe rewrite my own templated map for better efficiency
18 #include <Arduino.h> // for map
19 
20 #include "AutoRange.h"
21 
22 /** @defgroup sensortools Automatic range adjustment
23 */
24 
25 /** @ingroup sensortools
26 Automatically map an input value to an output range without knowing the precise range of inputs beforehand.
27 */
28 
29 class AutoMap : public AutoRange<int>
30 {
31 public:
32  /** Constructor.
33  @param min_expected the minimum possible input value.
34  @param max_expected the maximum possible input value.
35  */
36  AutoMap(int min_expected, int max_expected, int map_to_min, int map_to_max)
37  : inherited(min_expected,max_expected),map_min(map_to_min), map_max(map_to_max)
38  {
39  }
40 
41 
42  /** Process the next value and return it mapped to the range which was set in the constructor.
43  Can use the operator instead if you prefer, eg. myMap(n) instead of myMap.next(n).
44  @param n the next value to process.
45  @return the input value mapped to the range which was set in the constructor.
46  */
47  inline
48  int next(int n)
49  {
50  inherited::next(n);
51  return map(n,inherited::getMin(),inherited::getMax(),map_min,map_max);
52  }
53 
54  /** Process the next value and return it mapped to the range which was set in the constructor.
55  This is an alternative to next() if you prefer, eg. myMap(n) instead of myMap.next(n).
56  @param n the next value to process.
57  @return the input value mapped to the range which was set in the constructor.
58  */
59  inline
60  int operator()(int n)
61  {
62  return next(n);
63  }
64 
65 
66 private:
67  typedef AutoRange <int> inherited;
68  int map_min, map_max;
69 };
70 
71 
72 /**
73 @example 03.Sensors/Knob_LDR_x2_WavePacket/Knob_LDR_x2_WavePacket.ino
74 This example demonstrates the AutoMap class.
75 */
76 
77 #endif // #ifndef AUTOMAP_H_