Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Servo Pulse1
Map.cpp@0:7663c81cdf71, 2019-12-27 (annotated)
- Committer:
- kyucheol
- Date:
- Fri Dec 27 09:09:04 2019 +0000
- Revision:
- 0:7663c81cdf71
modified codes and added some explanation
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kyucheol | 0:7663c81cdf71 | 1 | #include "Map.h" |
| kyucheol | 0:7663c81cdf71 | 2 | //#include "mbed.h" |
| kyucheol | 0:7663c81cdf71 | 3 | |
| kyucheol | 0:7663c81cdf71 | 4 | float map(float in, float inMin, float inMax, float outMin, float outMax) { |
| kyucheol | 0:7663c81cdf71 | 5 | // check it's within the range |
| kyucheol | 0:7663c81cdf71 | 6 | if (inMin<inMax) { |
| kyucheol | 0:7663c81cdf71 | 7 | if (in <= inMin) |
| kyucheol | 0:7663c81cdf71 | 8 | return outMin; |
| kyucheol | 0:7663c81cdf71 | 9 | if (in >= inMax) |
| kyucheol | 0:7663c81cdf71 | 10 | return outMax; |
| kyucheol | 0:7663c81cdf71 | 11 | } else { // cope with input range being backwards. |
| kyucheol | 0:7663c81cdf71 | 12 | if (in >= inMin) |
| kyucheol | 0:7663c81cdf71 | 13 | return outMin; |
| kyucheol | 0:7663c81cdf71 | 14 | if (in <= inMax) |
| kyucheol | 0:7663c81cdf71 | 15 | return outMax; |
| kyucheol | 0:7663c81cdf71 | 16 | } |
| kyucheol | 0:7663c81cdf71 | 17 | // calculate how far into the range we are |
| kyucheol | 0:7663c81cdf71 | 18 | float scale = (in-inMin)/(inMax-inMin); |
| kyucheol | 0:7663c81cdf71 | 19 | // calculate the output. |
| kyucheol | 0:7663c81cdf71 | 20 | return outMin + scale*(outMax-outMin); |
| kyucheol | 0:7663c81cdf71 | 21 | } |