forked from sensorium/Mozzi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutoMap.h
executable file
·74 lines (61 loc) · 1.89 KB
/
AutoMap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* AutoMap.h
*
* Copyright 2012 Tim Barrass.
*
* This file is part of Mozzi.
*
* Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
*
*/
#ifndef AUTOMAP_H_
#define AUTOMAP_H_
// for map - maybe rewrite my own templated map for better efficiency
#if ARDUINO >= 100
#include "Arduino.h" // for map
#else
#include "WProgram.h"
#endif
#include "AutoRange.h"
/** @ingroup sensortools
Automatically map an input value to an output range without knowing the precise range of inputs beforehand.
*/
class AutoMap : public AutoRange<int>
{
public:
/** Constructor.
@param min_expected the minimum possible input value.
@param max_expected the maximum possible input value.
*/
AutoMap(int min_expected, int max_expected, int map_to_min, int map_to_max)
: inherited(min_expected,max_expected),map_min(map_to_min), map_max(map_to_max)
{
}
/** Process the next value and return it mapped to the range which was set in the constructor.
Can use the operator instead if you prefer, eg. myMap(n) instead of myMap.next(n).
@param n the next value to process.
@return the input value mapped to the range which was set in the constructor.
*/
inline
int next(int n){
inherited::next(n);
return map(n,inherited::getMin(),inherited::getMax(),map_min,map_max);
}
/** Process the next value and return it mapped to the range which was set in the constructor.
This is an alternative to next() if you prefer, eg. myMap(n) instead of myMap.next(n).
@param n the next value to process.
@return the input value mapped to the range which was set in the constructor.
*/
inline
int operator()(int n) {
return next(n);
}
private:
typedef AutoRange <int> inherited;
int map_min, map_max;
};
/**
@example 03.Sensors/Knob_LDR_x2_WavePacket/Knob_LDR_x2_WavePacket.ino
This example demonstrates the AutoMap class.
*/
#endif // #ifndef AUTOMAP_H_