From 09e6e8633a90fcee0478e418de8611ee04da2721 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Tue, 23 Feb 2021 08:56:32 -0700 Subject: [PATCH 1/3] Add div by zero check back into WMath::map --- cores/esp32/WMath.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cores/esp32/WMath.cpp b/cores/esp32/WMath.cpp index d351b194e23..83f9c198f35 100644 --- a/cores/esp32/WMath.cpp +++ b/cores/esp32/WMath.cpp @@ -69,7 +69,9 @@ 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 divisor = in_max - in_min; const long delta = x - in_min; - + if(divisor == 0){ + return -1; //AVR returns -1, SAM returns 0 + } return (delta * dividend + (divisor / 2)) / divisor + out_min; } From 30447f4d2e992d6c00ee1a5aba9a4f4e4d9d4082 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Tue, 23 Feb 2021 12:03:33 -0700 Subject: [PATCH 2/3] Add a log_e on map div0 --- cores/esp32/WMath.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cores/esp32/WMath.cpp b/cores/esp32/WMath.cpp index 83f9c198f35..96f68f0d6ce 100644 --- a/cores/esp32/WMath.cpp +++ b/cores/esp32/WMath.cpp @@ -70,6 +70,7 @@ long map(long x, long in_min, long in_max, long out_min, long out_max) { const long divisor = in_max - 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; From d8c09752f5fd47de3fa832d267eab070705d0b14 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Wed, 24 Feb 2021 18:54:49 +0200 Subject: [PATCH 3/3] include esp32-hal-log.h --- cores/esp32/WMath.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cores/esp32/WMath.cpp b/cores/esp32/WMath.cpp index 96f68f0d6ce..d70171655ef 100644 --- a/cores/esp32/WMath.cpp +++ b/cores/esp32/WMath.cpp @@ -27,6 +27,7 @@ extern "C" { #include #include "esp_system.h" } +#include "esp32-hal-log.h" void randomSeed(unsigned long seed) {