Skip to content

Instantly share code, notes, and snippets.

@hidsh
Created August 27, 2018 16:12
Show Gist options
  • Select an option

  • Save hidsh/9487688c3a5c6d5b388553d6f4638bd6 to your computer and use it in GitHub Desktop.

Select an option

Save hidsh/9487688c3a5c6d5b388553d6f4638bd6 to your computer and use it in GitHub Desktop.
`map' macro in c-language from arduino
// `map' function from arduino
//
// thx to "http://www.musashinodenpa.com/arduino/ref/index.php?f=0&pos=2743"
#include <stdio.h>
#define MAP(x, in_min, in_max, out_min, out_max) \
((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
typedef __int16_t int16_t;
int main(void)
{
int16_t a[] = {-800, 800, 0, 1023, -1023};
for(unsigned long i=0; i<(sizeof(a)/sizeof(a[0])); i++) {
int16_t x = a[i];
printf("%d: %d\n", x, MAP(x, -1023, 1023, -127, 127));
}
}
/* result
~/Dropbox/test/c ❯❯❯ ./a.out
-800: -100
800: 99
0: 0
1023: 127
-1023: -127
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment