Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid division by zero when map() is called with incorrect parameters #1248

Merged
merged 2 commits into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion user/tests/wiring/api/wiring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ test(api_wire)

test(api_map)
{
map(0x01,0x00,0xFF,0,255);
int i = 0;
API_COMPILE(i = map(0x01, 0x00, 0xFF, 0, 255));
double d = 0;
API_COMPILE(d = map(5.0, 0.0, 10.0, 0.0, 20.0));
(void)i; // avoid unused variable warning
(void)d;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions user/tests/wiring/no_fixture/misc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2017 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include "application.h"
#include "unit-test/unit-test.h"

test(MISC_01_map) {
// int map(int, int, int, int, int)
assertEqual(map(5, 0, 10, 0, 10), 5);
assertEqual(map(5, 0, 10, 0, 20), 10);
assertEqual(map(5, 10, 10, 10, 10), 5); // Shouldn't cause division by zero
// double map(double, double, double, double, double)
assertEqual(map(5.0, 0.0, 10.0, 0.0, 10.0), 5);
assertEqual(map(5.0, 0.0, 10.0, 0.0, 15.0), 7.5);
assertEqual(map(5.5, 10.0, 10.0, 10.0, 10.0), 5.5); // Shouldn't cause division by zero
}
6 changes: 3 additions & 3 deletions wiring/inc/spark_wiring.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ bool pinAvailable(uint16_t pin);
void digitalWrite(uint16_t pin, uint8_t value);
int32_t digitalRead(uint16_t pin);


long map(long value, long fromStart, long fromEnd, long toStart, long toEnd);

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);

Expand All @@ -92,4 +89,7 @@ uint8_t analogWriteResolution(pin_t pin);
uint32_t analogWriteMaxFrequency(pin_t pin);
void setDACBufferred(pin_t pin, uint8_t state);

int map(int value, int fromStart, int fromEnd, int toStart, int toEnd);
double map(double value, double fromStart, double fromEnd, double toStart, double toEnd);

#endif /* SPARK_WIRING_H_ */
17 changes: 14 additions & 3 deletions wiring/src/spark_wiring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,23 @@
*/
void setADCSampleTime(uint8_t ADC_SampleTime)
{
HAL_ADC_Set_Sample_Time(ADC_SampleTime);
HAL_ADC_Set_Sample_Time(ADC_SampleTime);
}

long map(long value, long fromStart, long fromEnd, long toStart, long toEnd)
int map(int value, int fromStart, int fromEnd, int toStart, int toEnd)
{
return (value - fromStart) * (toEnd - toStart) / (fromEnd - fromStart) + toStart;
if (fromEnd == fromStart) {
return value;
}
return (value - fromStart) * (toEnd - toStart) / (fromEnd - fromStart) + toStart;
}

double map(double value, double fromStart, double fromEnd, double toStart, double toEnd)
{
if (fromEnd == fromStart) {
return value;
}
return (value - fromStart) * (toEnd - toStart) / (fromEnd - fromStart) + toStart;
}

void delay(unsigned long ms)
Expand Down