]> www.fi.muni.cz Git - bike-lights.git/blobdiff - firmware/ambient.c
ambient.c: fixup, disable shadow code
[bike-lights.git] / firmware / ambient.c
index 7352f6d5779359c2b1b2471091618e0a07641c78..3ae15d3c0e97080e1ac56957f06b67a883275305 100644 (file)
@@ -11,7 +11,7 @@ volatile unsigned char ambient_zone, ambient_shadow;
 static unsigned char ambient_min, ambient_max, ambient_drop;
 
 /* logging */
-#define AMBIENT_LOG_SIZE 128
+#define AMBIENT_LOG_SIZE 256
 static unsigned char ambient_log_offset_stored EEMEM;
 static unsigned char ambient_log_offset;
 static unsigned char ambient_log[AMBIENT_LOG_SIZE] EEMEM;
@@ -26,10 +26,17 @@ typedef struct {
  * and having small overlaps in order to provide a bit of hysteresis.
  */
 static ambient_zone_t ambient_zones[N_AMBIENT_ZONES] = {
+#ifdef PAVLINA
+       { 0x0000, 0x02d0 }, // dark
+       { 0x02b0, 0x0318 }, // evening
+       { 0x0308, 0x032c }, // dawn
+       { 0x0324, 0xffff }, // day
+#else
        { 0x0000, 0x0250 }, // dark
-       { 0x0240, 0x02e0 }, // evening
-       { 0x02c0, 0x0300 }, // dawn
-       { 0x02f8, 0xffff }, // day
+       { 0x0230, 0x02e8 }, // evening
+       { 0x02d0, 0x0302 }, // dawn
+       { 0x02fc, 0xffff }, // day
+#endif
 };
 
 #define SHADOW_DROP_LIMIT      0x20 // in ADC units (0..0x3ff)
@@ -65,7 +72,7 @@ void susp_ambient()
 void ambient_log_min_max()
 {
        if (ambient_log_offset >= AMBIENT_LOG_SIZE - 1)
-               return;
+               ambient_log_offset = 0; // start over
 
        ambient_max -= ambient_min;
        // ambient_max >>= 2;
@@ -95,6 +102,9 @@ static unsigned char val_to_zone(uint16_t ambient_val)
 {
        unsigned char new_zone = ambient_zone;
 
+       if (new_zone >= N_AMBIENT_ZONES)
+               new_zone = N_AMBIENT_ZONES-1;
+
        while (ambient_zones[new_zone].lo > ambient_val)
                new_zone--;
 
@@ -122,8 +132,8 @@ void ambient_adc(uint16_t adcval)
 
        slow_10bit = ambient_slow >> AMBIENT_SLOW_SHIFT;
 
-       if (new_zone > 1 && (
-               new_zone == ambient_zone-1 || new_zone == ambient_zone+1)) {
+       if ((new_zone > ambient_zone)
+               || (new_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(slow_10bit);
@@ -133,6 +143,7 @@ void ambient_adc(uint16_t adcval)
        if ((user_zone = get_user_param(0)) > 0)
                new_zone = user_zone - 1;
 
+#if 0  // ignore shadow-entering code altogether for now
        // are we entering the shadow?
        if (!user_zone && new_zone < ambient_zone
                && ambient_zone >= 2 && slow_10bit > fast_10bit
@@ -156,6 +167,7 @@ void ambient_adc(uint16_t adcval)
                new_zone = ambient_zone; // don't change while entering shadow
                ambient_zone_changed();  // notify others the first time
        }
+#endif
 
        // TODO: maybe use these values instead of 10-bit?
        byte_fast = fast_10bit >> 2;