]> www.fi.muni.cz Git - bike-lights.git/blobdiff - firmware/ambient.c
ambient.c: rewrite
[bike-lights.git] / firmware / ambient.c
index c6c6a654ab67e1bd974366feefd575e98af95f55..904f2161f5fba06f4b27f38cf23b6a9c2b90417d 100644 (file)
 #include <avr/io.h>
+#include <avr/eeprom.h>
 
 #include "lights.h"
 
-static uint16_t ambient_val;
+#define AMBIENT_FAST_SHIFT 2   // running avg of ADC vals
+#define AMBIENT_SLOW_SHIFT 6   // 1 << AMBIENT_SLOW_SHIFT times slower
+
+static uint16_t ambient_fast, ambient_slow;
 volatile unsigned char ambient_zone;
+static unsigned char ambient_min, ambient_max, ambient_drop;
+
+/* 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 {
-       uint16_t lo, hi;
+       uint16_t lo, hi; // TODO: consider changing this to bytes
 } 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, 0x3120 }, // dark
-       { 0x30f0, 0x5000 },
-       { 0x4c00, 0x8000 },
-       { 0x7800, 0xffff }
+static ambient_zone_t ambient_zones[N_AMBIENT_ZONES] = {
+       { 0x0000, 0x0250 }, // dark
+       { 0x0240, 0x02e0 }, // evening
+       { 0x02c0, 0x0306 }, // dawn
+       { 0x02f8, 0xffff }, // day
 };
-#define N_AMBIENT_ZONES (sizeof(ambient_zones)/sizeof(ambient_zones[0]))
 
 void init_ambient()
 {
-       ambient_val = 0;
+       ambient_slow = 0;
+       ambient_fast = 0;
+       ambient_max = 0;
+       ambient_min = 0xFF;
        ambient_zone = 1;
+       ambient_drop = 0;
+
+       ambient_log_offset = eeprom_read_byte(&ambient_log_offset_stored);
+
+       if (ambient_log_offset == AMBIENT_LOG_SIZE)
+               ambient_log_offset = 0; // start over
 }
 
-void ambient_zone_changed()
+void susp_ambient()
 {
-#if 1
-       log_byte(0xab);
-       log_byte(ambient_zone);
-       log_word(ambient_val);
-       log_flush();
-#endif
+       unsigned char stored_offset;
+
+       ambient_log_min_max();
+
+       stored_offset = eeprom_read_byte(&ambient_log_offset_stored);
+       if (stored_offset != ambient_log_offset)
+               eeprom_write_byte(&ambient_log_offset_stored,
+                       ambient_log_offset);
+}
+
+void ambient_log_min_max()
+{
+       if (ambient_log_offset >= AMBIENT_LOG_SIZE - 1)
+               return;
+
+       ambient_max -= ambient_min;
+       ambient_max >>= 2;
+       if (ambient_max > 15)
+               ambient_max = 15;
+
+       ambient_drop >>= 2;
+       if (ambient_drop > 15)
+               ambient_drop = 15;
+
+       eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_min);
+       eeprom_write_byte(&ambient_log[ambient_log_offset++],
+               (ambient_max << 4) | ambient_drop);
+
+       ambient_min = 0xFF;
+       ambient_max = 0;
+       ambient_drop = 0;
+}
+
+static inline void ambient_zone_changed()
+{
+       pwmled_select_brightness();
+       pattern_reload();
+}
+
+static unsigned char val_to_zone(uint16_t ambient_val)
+{
+       unsigned char new_zone = ambient_zone;
 
-       // led_set_pattern(N_PWMLEDS, status_led_pattern_select());
-       // led_set_pattern(N_PWMLEDS+1, illumination_led_pattern_select());
-       // pattern_reload();
+       while (ambient_zones[new_zone].lo > ambient_val)
+               new_zone--;
+
+       while (ambient_zones[new_zone].hi < ambient_val)
+               new_zone++;
+
+       return new_zone;
 }
 
 void ambient_adc(uint16_t adcval)
 {
-       unsigned char old_zone = ambient_zone;
+       unsigned char new_zone, user_zone;
+       unsigned char byte_fast, byte_slow, drop;
+       uint16_t a_10bit;
+
+       // running avg - shorter timespan
+       ambient_fast += adcval - (ambient_fast >> AMBIENT_FAST_SHIFT);
+
+       // running avg - longer timespan
+       a_10bit = ambient_fast >> (AMBIENT_FAST_SHIFT + AMBIENT_ADC_SHIFT);
+       ambient_slow += a_10bit - (ambient_slow >> AMBIENT_SLOW_SHIFT);
 
-       ambient_val += adcval - (ambient_val >> 6);
+       // ambient zones are governed by shorter timespan by default
+       new_zone = val_to_zone(a_10bit);
 
-       while (ambient_zones[ambient_zone].lo > ambient_val)
-               ambient_zone--;
+       if (new_zone > 1 && (
+               new_zone == ambient_zone-1 || new_zone == ambient_zone+1)) {
+               // but change to the neighbouring zone is governed by _slow,
+               // except to the darkest zone, where we want fast reaction.
+               new_zone = val_to_zone(ambient_slow >> AMBIENT_SLOW_SHIFT);
+       }
+
+       // user_param ambient zone override
+       if ((user_zone = get_user_param(0)) > 0)
+               new_zone = user_zone - 1;
+
+       // TODO: maybe use these values instead of 10-bit?
+       byte_fast = a_10bit >> 2;
+       byte_slow = ambient_slow >> (AMBIENT_SLOW_SHIFT + 2);
+
+       if (byte_slow > byte_fast) {
+               drop = byte_slow - byte_fast;
+               if (drop > ambient_drop)
+                       ambient_drop = drop;
+       }
+
+       if (ambient_min > byte_fast)
+               ambient_min = byte_fast;
 
-       while (ambient_zones[ambient_zone].hi < ambient_val)
-               ambient_zone++;
+       if (ambient_max < byte_fast)
+               ambient_max = byte_fast;
 
+       if (new_zone != ambient_zone) {
+               ambient_zone = new_zone;
 #if 0
-       if (old_zone != ambient_zone) {
                log_byte(0xab);
                log_byte(ambient_zone);
                log_word(adcval);
                log_flush();
-       }
-               // ambient_zone_changed();
 #endif
+               ambient_zone_changed();
+       }
 }