-
Notifications
You must be signed in to change notification settings - Fork 187
/
IntMap.h
49 lines (41 loc) · 1.14 KB
/
IntMap.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
/*
* IntMap.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 INTMAP_H_
#define INTMAP_H_
/** A faster version of Arduino's map() function.
This uses ints instead of longs internally and does some of the maths at compile time.
*/
class IntMap {
public:
/** Constructor.
@param in_min the minimum of the input range.
@param in_max the maximum of the input range.
@param out_min the minimum of the output range.
@param out_max the maximum of the output range.
*/
IntMap(int in_min, int in_max, int out_min, int out_max)
: _IN_MIN(in_min),_IN_MAX(in_max),_OUT_MIN(out_min),_OUT_MAX(out_max),
_MULTIPLIER((256L * (out_max-out_min)) / (in_max-in_min))
{
;
}
/** Process the next input value.
@param n the next integer to process.
@return the input integer mapped to the output range.
*/
int operator()(int n) const {
return (int) (((_MULTIPLIER*(n-_IN_MIN))>>8) + _OUT_MIN);
}
private:
const int _IN_MIN, _IN_MAX, _OUT_MIN, _OUT_MAX;
const long _MULTIPLIER;
};
#endif /* INTMAP_H_ */