X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=bike-lights.git;a=blobdiff_plain;f=firmware%2Fambient.c;h=02377ccd46586ab1895083adeb09dbda77ff50e5;hp=59c85a79b2e17bb11b65b54d0d3fb79f170e3154;hb=65c3ad96cf307c3b77b36e6f6a2af5201c213a3c;hpb=b0572d0cae20a9fd351b197a0ea9b01fc658ee77 diff --git a/firmware/ambient.c b/firmware/ambient.c index 59c85a7..02377cc 100644 --- a/firmware/ambient.c +++ b/firmware/ambient.c @@ -1,9 +1,18 @@ #include +#include #include "lights.h" +#define AMBIENT_VAL_SHIFT 2 static uint16_t ambient_val; volatile unsigned char ambient_zone; +static unsigned char ambient_min, ambient_max; + +/* logging */ +#define AMBIENT_LOG_SIZE 128 +static unsigned char ambient_log_offset_stored EEMEM; +static unsigned char ambient_log_offset; +static unsigned char ambient_log[AMBIENT_LOG_SIZE] EEMEM; /* My photodiode reads 0x00C5 .. 0x033B */ typedef struct { @@ -15,17 +24,48 @@ typedef struct { * and having small overlaps in order to provide a bit of hysteresis. */ static ambient_zone_t ambient_zones[] = { - { 0x0000, 0xb000 }, // dark - { 0xa800, 0xc400 }, - { 0xc200, 0xcb00 }, - { 0xca80, 0xffff } + { 0x0000 , 0x0280<= AMBIENT_LOG_SIZE - 1) + return; + + eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_min); + eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_max); + + ambient_min = 0xFF; + ambient_max = 0; } void ambient_zone_changed() @@ -45,8 +85,10 @@ void ambient_zone_changed() void ambient_adc(uint16_t adcval) { unsigned char old_zone = ambient_zone; + unsigned char byte_val; - ambient_val += adcval - (ambient_val >> 3); + ambient_val += adcval - (ambient_val + >> (AMBIENT_VAL_SHIFT - AMBIENT_ADC_SHIFT)); while (ambient_zones[ambient_zone].lo > ambient_val) ambient_zone--; @@ -54,6 +96,13 @@ void ambient_adc(uint16_t adcval) while (ambient_zones[ambient_zone].hi < ambient_val) ambient_zone++; + byte_val = adcval >> 2; + + if (ambient_min > byte_val) + ambient_min = byte_val; + + if (ambient_max < byte_val) + ambient_max = byte_val; #if 0 if (old_zone != ambient_zone) { log_byte(0xab);