| Mozzi version v1.1.0 sound synthesis library for Arduino |

AutoMap.h
1 /*
2 * AutoMap.h
3 *
4 * Copyright 2012 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
12 #ifndef AUTOMAP_H_
13 #define AUTOMAP_H_
14
15 // for map - maybe rewrite my own templated map for better efficiency
16 #if ARDUINO >= 100
17 #include "Arduino.h" // for map
18 #else
19 #include "WProgram.h"
20 #endif
21
22 #include "AutoRange.h"
23
24 /** @ingroup sensortools
25 Automatically map an input value to an output range without knowing the precise range of inputs beforehand.
26 */
27
28 class AutoMap : public AutoRange<int>
29 {
30 public:
31 /** Constructor.
32 @param min_expected the minimum possible input value.
33 @param max_expected the maximum possible input value.
34 */
35 AutoMap(int min_expected, int max_expected, int map_to_min, int map_to_max)
36 : inherited(min_expected,max_expected),map_min(map_to_min), map_max(map_to_max)
37 {
38 }
39
40
41 /** Process the next value and return it mapped to the range which was set in the constructor.
42 Can use the operator instead if you prefer, eg. myMap(n) instead of myMap.next(n).
43 @param n the next value to process.
44 @return the input value mapped to the range which was set in the constructor.
45 */
46 inline
47 int next(int n)
48 {
49 inherited::next(n);
50 return map(n,inherited::getMin(),inherited::getMax(),map_min,map_max);
51 }
52
53 /** Process the next value and return it mapped to the range which was set in the constructor.
54 This is an alternative to next() if you prefer, eg. myMap(n) instead of myMap.next(n).
55 @param n the next value to process.
56 @return the input value mapped to the range which was set in the constructor.
57 */
58 inline
59 int operator()(int n)
60 {
61 return next(n);
62 }
63
64
65 private:
66 typedef AutoRange <int> inherited;
67 int map_min, map_max;
68 };
69
70
71 /**
72 @example 03.Sensors/Knob_LDR_x2_WavePacket/Knob_LDR_x2_WavePacket.ino
73 This example demonstrates the AutoMap class.
74 */
75
76 #endif // #ifndef AUTOMAP_H_
AutoMap::next
int next(int n)
Process the next value and return it mapped to the range which was set in the constructor.
Definition: AutoMap.h:47
AutoMap::operator()
int operator()(int n)
Process the next value and return it mapped to the range which was set in the constructor.
Definition: AutoMap.h:59
AutoMap
Automatically map an input value to an output range without knowing the precise range of inputs befor...
Definition: AutoMap.h:28
AutoRange
Keeps a running calculation of the range of the input values it receives.
Definition: AutoRange.h:18
AutoMap::AutoMap
AutoMap(int min_expected, int max_expected, int map_to_min, int map_to_max)
Constructor.
Definition: AutoMap.h:35
AutoRange::next
void next(T n)
Updates the current range.
Definition: AutoRange.h:35
AutoRange::AutoRange
AutoRange(T min_expected, T max_expected)
Constructor.
Definition: AutoRange.h:27