-
Notifications
You must be signed in to change notification settings - Fork 5
mapper
A very common issue one runs across when programming is unit translation and value limiting. You are given some value that covers an arbitrary range and you need to "map" this value to a unit that you are looking for. And, at the same time, limit the range of the inputted data to "sane" values.
That was really wordy, lets try an example. You would like to make a digital volt meter. You have an analog input that gives values from 0 to say.. 1024. Next you need to find what the limits are of your circuit. At your lowest voltage, what number do you read from the analog input? What's your highest voltage reading? This gives us 4 values.
Low voltage - corresponding analog value, analogA.
High voltage - corresponding analog value, analogB.
From this we plug all four into a mapper object.
mapper myMapper(analogA, analogB,lowVoltage,highVoltage);
Now that we have our mapper, we can read analog values and use our mapper object to calculate our voltage values.
float voltageValue = myMapper.Map(analogValue);
So, if this is such a handy thing, why doesn't the Arduino library have one already? It does! But the Arduino one is all done in integers and doesn't save any of its calculation data between mappings. This version is written to use doubles and does as much of its calculation in the constructor as possible. This is to save time during use. And yes, I know the Arduino can't do doubles. But no one said this is restricted to Arduinos did they? As chips get more powerful more systems will use double precision and this version will automatically use this if its available.
mapper(void);
No parameters, creates a mapper that maps 0..1 -> 0..1. This is default used when one does not know the domain or range of a mapper at creation time.
mapper(double x1,double x2,double y1,double y2);
The standard constructor. The inputted values will range from x1..x2 and these will be mapped to the output range of y1..y2. There are no restrictions on the entered values. A inputted range from high to low can just as easily map to a low to high as a high to low.
`