-
Notifications
You must be signed in to change notification settings - Fork 190
/
AutoMap.h
77 lines (63 loc) · 1.91 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
74
75
76
77
/*
* AutoMap.h
/*
* AutoMap.h
*
* This file is part of Mozzi.
*
* Copyright 2012-2024 Tim Barrass and the Mozzi Team
*
* Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
*
*/
#ifndef AUTOMAP_H_
#define AUTOMAP_H_
// for map - maybe rewrite my own templated map for better efficiency
#include <Arduino.h> // for map
#include "AutoRange.h"
/** @defgroup sensortools Automatic range adjustment
*/
/** @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_