microbit: modified the range of read() MicroBitLightSensor

Previously the range of MicrobitLightSensor read() was 0 - 100,
this didn't align with other APIs.

The new range is 0-255 which aligns with display brightness to give
a more consistent overall API.
master
James Devine 2016-02-01 23:58:33 +00:00
parent 104493299e
commit a27b2314be
2 changed files with 9 additions and 5 deletions

View File

@ -85,10 +85,10 @@ class MicroBitLightSensor
*
* Where each number represents a different section on the 5 x 5 matrix display.
*
* @return returns a value in the range 0 - 100 where 0 is dark, and 100
* @return returns a value in the range 0 - 255 where 0 is dark, and 255
* is very bright
*
* @note currently returns a value in the range 0 - 100 where 0 is dark, and 100
* @note currently returns a value in the range 0 - 255 where 0 is dark, and 255
* is very bright perhaps we should normalise the returned values into an SI unit!
* TODO.
*/

View File

@ -105,10 +105,10 @@ MicroBitLightSensor::MicroBitLightSensor() : analogTrigger()
*
* Where each number represents a different section on the 5 x 5 matrix display.
*
* @return returns a value in the range 0 - 100 where 0 is dark, and 100
* @return returns a value in the range 0 - 255 where 0 is dark, and 255
* is very bright
*
* @note currently returns a value in the range 0 - 100 where 0 is dark, and 100
* @note currently returns a value in the range 0 - 255 where 0 is dark, and 255
* is very bright perhaps we should normalise the returned values into an SI unit!
* TODO.
*/
@ -127,7 +127,11 @@ int MicroBitLightSensor::read()
int inverted = (MICROBIT_LIGHT_SENSOR_MAX_VALUE - average) + MICROBIT_LIGHT_SENSOR_MIN_VALUE;
int normalised = ((inverted - MICROBIT_LIGHT_SENSOR_MIN_VALUE) * 100) / (MICROBIT_LIGHT_SENSOR_MAX_VALUE - MICROBIT_LIGHT_SENSOR_MIN_VALUE);
int a = 0;
int b = 255;
int normalised = a + ((((inverted - MICROBIT_LIGHT_SENSOR_MIN_VALUE)) * (b - a))/ (MICROBIT_LIGHT_SENSOR_MAX_VALUE - MICROBIT_LIGHT_SENSOR_MIN_VALUE));
return normalised;
}