microbit: modified as per suggestions

* This commit fixes an exposed member variable
  that was not intended to be public 'tickPeriod'

* normalised the light level to return values between 0 - 100
This commit is contained in:
James Devine 2016-02-01 21:46:15 +00:00
parent 6b0e9cf489
commit 8150b2ac67
3 changed files with 28 additions and 11 deletions

View file

@ -68,6 +68,8 @@ class MicroBit
void compassCalibrator(MicroBitEvent e);
uint32_t randomValue;
//the current tick period in MS
int tickPeriod;
public:
@ -77,8 +79,6 @@ class MicroBit
// Periodic callback
Ticker systemTicker;
int tickPeriod;
// I2C Interface
MicroBitI2C i2c;

View file

@ -8,6 +8,9 @@
#define MICROBIT_LIGHT_SENSOR_AN_SET_TIME 4000
#define MICROBIT_LIGHT_SENSOR_TICK_PERIOD 5
#define MICROBIT_LIGHT_SENSOR_MAX_VALUE 338
#define MICROBIT_LIGHT_SENSOR_MIN_VALUE 75
/**
* Class definition for MicroBitLightSensor.
*
@ -82,10 +85,12 @@ class MicroBitLightSensor
*
* Where each number represents a different section on the 5 x 5 matrix display.
*
* @note currently values are inverted to how one would think:
* - Lower is brighter
* - Higher is darker
* TODO: Normalise the returned values into an SI unit!
* @return returns a value in the range 0 - 100 where 0 is dark, and 100
* is very bright
*
* @note currently returns a value in the range 0 - 100 where 0 is dark, and 100
* is very bright perhaps we should normalise the returned values into an SI unit!
* TODO.
*/
int read();

View file

@ -105,10 +105,12 @@ MicroBitLightSensor::MicroBitLightSensor() : analogTrigger()
*
* Where each number represents a different section on the 5 x 5 matrix display.
*
* @note currently values are inverted to how one would think:
* - Lower is brighter
* - Higher is darker
* TODO: Normalise the returned values into an SI unit!
* @return returns a value in the range 0 - 100 where 0 is dark, and 100
* is very bright
*
* @note currently returns a value in the range 0 - 100 where 0 is dark, and 100
* is very bright perhaps we should normalise the returned values into an SI unit!
* TODO.
*/
int MicroBitLightSensor::read()
{
@ -117,7 +119,17 @@ int MicroBitLightSensor::read()
for(int i = 0; i < MICROBIT_LIGHT_SENSOR_CHAN_NUM; i++)
sum += results[i];
return sum/MICROBIT_LIGHT_SENSOR_CHAN_NUM;
int average = sum / MICROBIT_LIGHT_SENSOR_CHAN_NUM;
average = min(average, MICROBIT_LIGHT_SENSOR_MAX_VALUE);
average = max(average, MICROBIT_LIGHT_SENSOR_MIN_VALUE);
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);
return normalised;
}