1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Fix "improved_map" from Servo.cpp and use this is in core implementation.

Greatly reduces error rate (half, or 0 zero errors, depends on in/out ranges) for round-trip mapping at the same performance.
This commit is contained in:
Dirk O. Kaar 2020-01-21 11:28:14 +01:00
parent 14f627218b
commit 58829022ed

View File

@ -70,11 +70,11 @@ long secureRandom(long howsmall, long howbig) {
} }
long map(long x, long in_min, long in_max, long out_min, long out_max) { long map(long x, long in_min, long in_max, long out_min, long out_max) {
long divisor = (in_max - in_min); const long dividend = out_max - out_min;
if(divisor == 0){ const long divisor = in_max - in_min;
return -1; //AVR returns -1, SAM returns 0 const long delta = x - in_min;
}
return (x - in_min) * (out_max - out_min) / divisor + out_min; return (delta * dividend + (divisor / 2)) / divisor + out_min;
} }
unsigned int makeWord(unsigned int w) { unsigned int makeWord(unsigned int w) {