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=72e13323a9d6124ba863a70a71dc676af916a876;hp=025b0c4bd6dcb6839395a36f91a8fedd4dc97e73;hb=c30006aaf666f7cff3a6ab949c613c2f8cc6163b;hpb=647f353f4d8c87c5cf18cae905e9d600cd0e6bcb diff --git a/firmware/ambient.c b/firmware/ambient.c index 025b0c4..72e1332 100644 --- a/firmware/ambient.c +++ b/firmware/ambient.c @@ -2,51 +2,68 @@ #include "lights.h" +#define AMBIENT_VAL_SHIFT 2 static uint16_t ambient_val; volatile unsigned char ambient_zone; -static unsigned char ambient_zone_set; -static uint16_t ambient_zones[] = { - 0x0c00, 0x0d00, 0x1000, 0x1800, 0x2800, 0x2f80, 0xffff +/* My photodiode reads 0x00C5 .. 0x033B */ +typedef struct { + uint16_t lo, hi; +} ambient_zone_t; + +/* + * Note: these have to be sorted, starting with 0, ending with 0xFFFF + * and having small overlaps in order to provide a bit of hysteresis. + */ +static ambient_zone_t ambient_zones[] = { + { 0x0000 , 0x0290<> 4); - - newzone = 0; - while (newzone < N_AMBIENT_ZONES-1 - && ambient_zones[newzone] < ambient_val) - newzone++; - - // TODO: implement hysteresis? - if (!ambient_zone_set || newzone != ambient_zone) { - ambient_zone = newzone; - ambient_zone_set = 1; - // ambient_zone_changed(); + unsigned char old_zone = ambient_zone; + + ambient_val += adcval - (ambient_val + >> (AMBIENT_VAL_SHIFT - AMBIENT_ADC_SHIFT)); + + while (ambient_zones[ambient_zone].lo > ambient_val) + ambient_zone--; + + while (ambient_zones[ambient_zone].hi < ambient_val) + ambient_zone++; + +#if 0 + if (old_zone != ambient_zone) { + log_byte(0xab); + log_byte(ambient_zone); + log_word(adcval); + log_flush(); } + // ambient_zone_changed(); +#endif }