Add div by zero check back into WMath::map (#4853)

* Add div by zero check back into WMath::map
* include esp32-hal-log.h

Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
This commit is contained in:
lbernstone 2021-02-24 10:04:36 -07:00 committed by GitHub
parent 5d00b6eb16
commit 3fe7c2e8cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,6 +27,7 @@ extern "C" {
#include <stdlib.h> #include <stdlib.h>
#include "esp_system.h" #include "esp_system.h"
} }
#include "esp32-hal-log.h"
void randomSeed(unsigned long seed) void randomSeed(unsigned long seed)
{ {
@ -69,7 +70,10 @@ long map(long x, long in_min, long in_max, long out_min, long out_max) {
const long dividend = out_max - out_min; const long dividend = out_max - out_min;
const long divisor = in_max - in_min; const long divisor = in_max - in_min;
const long delta = x - in_min; const long delta = x - in_min;
if(divisor == 0){
log_e("Invalid map input range, min == max");
return -1; //AVR returns -1, SAM returns 0
}
return (delta * dividend + (divisor / 2)) / divisor + out_min; return (delta * dividend + (divisor / 2)) / divisor + out_min;
} }