PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
mapInt.c
1 /*
2  * @file mapInt.c
3  *
4  * @author Andy Lindsay
5  *
6  * @version 1.1.8
7  *
8  * @copyright Copyright (C) Parallax, Inc. 2018. See end of file for
9  * terms of use (MIT License).
10  *
11  * @brief mapInt function source, see simpletools.h for documentation.
12  *
13  * @detail Please submit bug reports, suggestions, and improvements to
14  * this code to editor@parallax.com.
15  */
16 
17 
18 
19 #include "simpletools.h"
20 
21 
22 
23 int mapInt(int value, int fromMin, int fromMax, int toMin, int toMax)
24 {
25  // n - numerator
26  // d - denomenator
27  // irt - integer rounding term
28 
29  int n = (value - fromMin) * (toMax - toMin);
30  int d = (fromMax - fromMin);
31 
32  int irt = d / 2;
33  if( ( (n > 0) && (d < 0) ) || ( (n < 0) && (d > 0)))
34  {
35  irt = - irt;
36  }
37 
38  int val = (n + irt) / d;
39 
40  return val + toMin;
41 }
42 
43 
44 
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
mapInt
int mapInt(int value, int fromMin, int fromMax, int toMin, int toMax)
Maps an integer value from its position in one range to its corresponding. position in a different ra...
Definition: mapInt.c:23