Remove light sensor auto-normalisation

This commit is contained in:
fruchti 2020-09-20 19:00:55 +02:00
parent 9f230125d8
commit e13d522f4e
3 changed files with 14 additions and 46 deletions

View file

@ -1 +1 @@
459
482

View file

@ -4,11 +4,7 @@ volatile unsigned int LightSensor_Measurement;
volatile bool LightSensor_NewMeasurement = false;
// Rolling average of the brightness measurement
unsigned int LightSensor_AbsoluteBrightness = LIGHTSENSOR_MAX / 2;
// Maximum and minimum encountered so far
unsigned int LightSensor_MinimumBrightness = LIGHTSENSOR_MAX;
unsigned int LightSensor_MaximumBrightness = 0;
unsigned int LightSensor_AbsoluteBrightness = 0;
int LightSensor_RelativeBrightness;
@ -68,29 +64,16 @@ void LightSensor_Poll(void)
LightSensor_NewMeasurement = false;
unsigned int brightness = ((1 << 31)
/ (measurement + 1)) >> (31 - 16);
// LightSensor_AbsoluteBrightness = LIGHTSENSOR_LAMBDA * LightSensor_AbsoluteBrightness
// + (1.0f - LIGHTSENSOR_LAMBDA) * brightness;
LightSensor_AbsoluteBrightness -= LightSensor_AbsoluteBrightness >> LIGHTSENSOR_LAMBDA_BITS;
LightSensor_AbsoluteBrightness += brightness >> LIGHTSENSOR_LAMBDA_BITS;
LightSensor_AbsoluteBrightness -= LightSensor_AbsoluteBrightness
>> LIGHTSENSOR_LAMBDA_BITS;
LightSensor_AbsoluteBrightness += brightness
>> LIGHTSENSOR_LAMBDA_BITS;
if(brightness < LightSensor_MinimumBrightness)
{
LightSensor_MinimumBrightness = brightness;
}
if(brightness > LightSensor_MaximumBrightness)
{
LightSensor_MaximumBrightness = brightness;
}
LightSensor_RelativeBrightness =
((int)LightSensor_AbsoluteBrightness - LIGHTSENSOR_LOW_BOUND)
* LIGHTSENSOR_MAX
/ (LIGHTSENSOR_HIGH_BOUND - LIGHTSENSOR_LOW_BOUND);
// Scale and saturate to get relative brightness value
int range = LightSensor_MaximumBrightness
- LightSensor_MinimumBrightness;
int low = LightSensor_MinimumBrightness
+ LIGHTSENSOR_LOW_BOUND * range / LIGHTSENSOR_MAX;
int high = LightSensor_MinimumBrightness
+ LIGHTSENSOR_HIGH_BOUND * range / LIGHTSENSOR_MAX;
LightSensor_RelativeBrightness = ((int)LightSensor_AbsoluteBrightness - low)
* LIGHTSENSOR_MAX / (high - low);
if(LightSensor_RelativeBrightness < 0)
{
LightSensor_RelativeBrightness = 0;
@ -99,17 +82,6 @@ void LightSensor_Poll(void)
{
LightSensor_RelativeBrightness = LIGHTSENSOR_MAX;
}
// Slowly reset limit values
static int decay_counter = 0;
decay_counter++;
if(decay_counter == LIGHTSENSOR_LIMIT_RESET_TIME * 1000
/ LIGHTSENSOR_INTERVAL / LIGHTSENSOR_MAX)
{
decay_counter = 0;
LightSensor_MaximumBrightness -= 1;
LightSensor_MinimumBrightness += 1;
}
}
}

View file

@ -15,14 +15,10 @@
// 'Forgetting factor' of the rolling brightness average
#define LIGHTSENSOR_LAMBDA_BITS 2
// Time until minimum and maximum value are completely reset (in seconds)
#define LIGHTSENSOR_LIMIT_RESET_TIME \
(48 * 60 * 60)
// Bounds for converting absolute to relative brightness: Consider everything
// near the minimum or maximum 0.0 or 1.0, respectively
#define LIGHTSENSOR_LOW_BOUND ((unsigned int)(0.013 * LIGHTSENSOR_MAX))
#define LIGHTSENSOR_HIGH_BOUND ((unsigned int)(1.0 * LIGHTSENSOR_MAX))
// Bounds for converting absolute to relative brightness (empirically
// determined)
#define LIGHTSENSOR_LOW_BOUND ((int)(0.003 * LIGHTSENSOR_MAX))
#define LIGHTSENSOR_HIGH_BOUND ((int)(0.65 * LIGHTSENSOR_MAX))
extern int LightSensor_RelativeBrightness;