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=143ffc49e1bf98d8ad5c5ae42566aac38c907e52;hp=ab28a20faf1ce95719cdd8ff4386ef9b90790af2;hb=1e3c9f876c88582e3da20a51f43d340c40c372ab;hpb=acd7fc45b8751444605da3c464e49ab16148e9c1 diff --git a/firmware/ambient.c b/firmware/ambient.c index ab28a20..143ffc4 100644 --- a/firmware/ambient.c +++ b/firmware/ambient.c @@ -4,45 +4,64 @@ static uint16_t ambient_val; volatile unsigned char ambient_zone; -static unsigned char ambient_zone_set; -static uint16_t ambient_zones[] = { - 0x60, 0x68, 0x70, 0xa0, 0x100, 0x1c0, 0x270, 0x290, 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, 0xa400 }, // dark + { 0xa000, 0xc000 }, + { 0xbe00, 0xc800 }, + { 0xc600, 0xffff } }; #define N_AMBIENT_ZONES (sizeof(ambient_zones)/sizeof(ambient_zones[0])) -void ambient_init() +void init_ambient() { ambient_val = 0; - ambient_zone = 0; - ambient_zone_set = 0; + ambient_zone = 1; } void ambient_zone_changed() { - log_byte(0xCC); +#if 1 + log_byte(0xab); log_byte(ambient_zone); log_word(ambient_val); + log_flush(); +#endif + + // led_set_pattern(N_PWMLEDS, status_led_pattern_select()); + // led_set_pattern(N_PWMLEDS+1, illumination_led_pattern_select()); + // pattern_reload(); } void ambient_adc(uint16_t adcval) { - unsigned char newzone; + unsigned char old_zone = ambient_zone; - if (!ambient_zone_set) - ambient_val = adcval << 4; - else // running sum - ambient_val += adcval - (ambient_val >> 4); + ambient_val += adcval - (ambient_val >> 3); - newzone = 0; - while (newzone < N_AMBIENT_ZONES-1 - && ambient_zones[newzone] < ambient_val) - newzone++; + while (ambient_zones[ambient_zone].lo > ambient_val) + ambient_zone--; - if (!ambient_zone_set || newzone != ambient_zone) { - ambient_zone = newzone; - ambient_zone_set = 1; - // ambient_zone_changed(); + 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 }