2016-03-10 23:01:04 +01:00
|
|
|
//% color=351 weight=30
|
|
|
|
namespace pins {
|
|
|
|
/**
|
|
|
|
* Re-maps a number from one range to another. That is, a value of ``from low`` would get mapped to ``to low``, a value of ``from high`` to ``to high``, values in-between to values in-between, etc.
|
|
|
|
* @param value value to map in ranges
|
|
|
|
* @param fromLow the lower bound of the value's current range
|
|
|
|
* @param fromHigh the upper bound of the value's current range, eg: 1023
|
|
|
|
* @param toLow the lower bound of the value's target range
|
|
|
|
* @param toHigh the upper bound of the value's target range, eg: 4
|
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=pins/map weight=15
|
2016-03-10 23:01:04 +01:00
|
|
|
//% blockId=math_map block="map %value|from low %fromLow|from high %fromHigh|to low %toLow|to high %toHigh"
|
|
|
|
export function map(value: number, fromLow: number, fromHigh: number, toLow: number, toHigh: number): number {
|
|
|
|
return ((value - fromLow) * (toHigh - toLow)) / (fromHigh - fromLow) + toLow;
|
|
|
|
}
|
|
|
|
}
|