#include "lights.h"
#define AMBIENT_VAL_SHIFT 2
-static uint16_t ambient_val;
+static uint16_t ambient_val, ambient_val16;
volatile unsigned char ambient_zone;
-static unsigned char ambient_min, ambient_max;
+static unsigned char ambient_min, ambient_max, ambient_16drop;
/* logging */
#define AMBIENT_LOG_SIZE 128
void init_ambient()
{
ambient_val = 0;
+ ambient_val16 = 0;
ambient_max = 0;
ambient_min = 0xFF;
ambient_zone = 1;
+ ambient_16drop = 0;
ambient_log_offset = eeprom_read_byte(&ambient_log_offset_stored);
return;
eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_min);
- eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_max);
+ // eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_max);
+ eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_16drop);
ambient_min = 0xFF;
ambient_max = 0;
+ ambient_16drop = 0;
}
void ambient_zone_changed()
void ambient_adc(uint16_t adcval)
{
unsigned char old_zone = ambient_zone;
- unsigned char byte_val;
+ unsigned char byte_val, byte_val16;
ambient_val += adcval - (ambient_val
>> (AMBIENT_VAL_SHIFT - AMBIENT_ADC_SHIFT));
while (ambient_zones[ambient_zone].hi < ambient_val)
ambient_zone++;
- byte_val = adcval >> 2;
+ byte_val = ambient_val >> (2 + AMBIENT_VAL_SHIFT - AMBIENT_ADC_SHIFT);
+
+ ambient_val16 += byte_val - (ambient_val16 >> 4);
+ byte_val16 = ambient_val16 >> 4;
+
+ if (byte_val16 > byte_val) {
+ byte_val16 -= byte_val;
+ if (byte_val16 > ambient_16drop)
+ ambient_16drop = byte_val16;
+ }
if (ambient_min > byte_val)
ambient_min = byte_val;