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=48a752d4b35bf744fcf11992684a2b1fef521e44;hp=c6c6a654ab67e1bd974366feefd575e98af95f55;hb=94524330d5709845fe0bbe8562fd68deedcb818c;hpb=20177f823fc0effcc9c5c389a629294c5ed367a1 diff --git a/firmware/ambient.c b/firmware/ambient.c index c6c6a65..48a752d 100644 --- a/firmware/ambient.c +++ b/firmware/ambient.c @@ -1,9 +1,18 @@ #include +#include #include "lights.h" -static uint16_t ambient_val; +#define AMBIENT_VAL_SHIFT 2 +static uint16_t ambient_val, ambient_val16; volatile unsigned char ambient_zone; +static unsigned char ambient_min, ambient_max, ambient_16drop; + +/* 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,38 +24,67 @@ typedef struct { * and having small overlaps in order to provide a bit of hysteresis. */ static ambient_zone_t ambient_zones[] = { - { 0x0000, 0x3120 }, // dark - { 0x30f0, 0x5000 }, - { 0x4c00, 0x8000 }, - { 0x7800, 0xffff } + { 0x0000 , 0x0270<= 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); + eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_16drop); + + ambient_min = 0xFF; + ambient_max = 0; + ambient_16drop = 0; +} - // led_set_pattern(N_PWMLEDS, status_led_pattern_select()); - // led_set_pattern(N_PWMLEDS+1, illumination_led_pattern_select()); - // pattern_reload(); +static inline void ambient_zone_changed() +{ + pwmled_select_brightness(); + pattern_reload(); } void ambient_adc(uint16_t adcval) { unsigned char old_zone = ambient_zone; + unsigned char byte_val, byte_val16; - ambient_val += adcval - (ambient_val >> 6); + ambient_val += adcval - (ambient_val + >> (AMBIENT_VAL_SHIFT - AMBIENT_ADC_SHIFT)); while (ambient_zones[ambient_zone].lo > ambient_val) ambient_zone--; @@ -54,14 +92,30 @@ void ambient_adc(uint16_t adcval) while (ambient_zones[ambient_zone].hi < ambient_val) ambient_zone++; -#if 0 + 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; + + if (ambient_max < byte_val) + ambient_max = byte_val; if (old_zone != ambient_zone) { +#if 0 log_byte(0xab); log_byte(ambient_zone); log_word(adcval); log_flush(); - } - // ambient_zone_changed(); #endif + ambient_zone_changed(); + } }