X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=firmware%2Fambient.c;h=f6e8ae5f5be088eba62d2efc6533b495e5a254bd;hb=5d0f6ceb47eed2282aeb11eb57eebc390add234e;hp=21c2a66d721ad54f647c96463cc5b9218d6fb06a;hpb=69ef9776904e9f3018d340a794cf8427bdc728ff;p=bike-lights.git diff --git a/firmware/ambient.c b/firmware/ambient.c index 21c2a66..f6e8ae5 100644 --- a/firmware/ambient.c +++ b/firmware/ambient.c @@ -4,47 +4,64 @@ static uint16_t ambient_val; volatile unsigned char ambient_zone; -static unsigned char ambient_zone_set; -static uint16_t ambient_zones[] = { - 0x0b70, 0x0b80, 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, 0xb000 }, // dark + { 0xa800, 0xc700 }, + { 0xc600, 0xcb00 }, + { 0xca80, 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() { +#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; - - if (!ambient_zone_set) - ambient_val = adcval << 4; - else // running sum - ambient_val += adcval - (ambient_val >> 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 >> 3); + + 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 }