]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/ambient.c
logic for setting brightness
[bike-lights.git] / firmware / ambient.c
1 #include <avr/io.h>
2 #include <avr/eeprom.h>
3
4 #include "lights.h"
5
6 #define AMBIENT_VAL_SHIFT 2
7 static uint16_t ambient_val, ambient_val16;
8 volatile unsigned char ambient_zone;
9 static unsigned char ambient_min, ambient_max, ambient_16drop;
10
11 /* logging */
12 #define AMBIENT_LOG_SIZE 128
13 static unsigned char ambient_log_offset_stored EEMEM;
14 static unsigned char ambient_log_offset;
15 static unsigned char ambient_log[AMBIENT_LOG_SIZE] EEMEM;
16
17 /* My photodiode reads 0x00C5 .. 0x033B */
18 typedef struct {
19         uint16_t lo, hi;
20 } ambient_zone_t;
21
22 /*
23  * Note: these have to be sorted, starting with 0, ending with 0xFFFF
24  * and having small overlaps in order to provide a bit of hysteresis.
25  */
26 static ambient_zone_t ambient_zones[] = {
27         { 0x0000                   , 0x0270<<AMBIENT_VAL_SHIFT }, // dark
28         { 0x0260<<AMBIENT_VAL_SHIFT, 0x02e0<<AMBIENT_VAL_SHIFT }, // evening
29         { 0x02d0<<AMBIENT_VAL_SHIFT, 0x0306<<AMBIENT_VAL_SHIFT }, // dawn
30         { 0x0302<<AMBIENT_VAL_SHIFT, 0xffff                    }, // day
31 };
32 #define N_AMBIENT_ZONES (sizeof(ambient_zones)/sizeof(ambient_zones[0]))
33
34 void init_ambient()
35 {
36         ambient_val = 0;
37         ambient_val16 = 0;
38         ambient_max = 0;
39         ambient_min = 0xFF;
40         ambient_zone = 1;
41         ambient_16drop = 0;
42
43         ambient_log_offset = eeprom_read_byte(&ambient_log_offset_stored);
44
45         if (ambient_log_offset == AMBIENT_LOG_SIZE)
46                 ambient_log_offset = 0; // start over
47 }
48
49 void susp_ambient()
50 {
51         unsigned char stored_offset;
52
53         ambient_log_min_max();
54
55         stored_offset = eeprom_read_byte(&ambient_log_offset_stored);
56         if (stored_offset != ambient_log_offset)
57                 eeprom_write_byte(&ambient_log_offset_stored,
58                         ambient_log_offset);
59 }
60
61 void ambient_log_min_max()
62 {
63         if (ambient_log_offset >= AMBIENT_LOG_SIZE - 1)
64                 return;
65
66         // eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_min);
67         eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_max);
68         eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_16drop);
69
70         ambient_min = 0xFF;
71         ambient_max = 0;
72         ambient_16drop = 0;
73 }
74
75 static inline void ambient_zone_changed()
76 {
77         pwmled_select_brightness();
78         pattern_reload();
79 }
80
81 void ambient_adc(uint16_t adcval)
82 {
83         unsigned char old_zone = ambient_zone;
84         unsigned char byte_val, byte_val16;
85
86         ambient_val += adcval - (ambient_val
87                 >> (AMBIENT_VAL_SHIFT - AMBIENT_ADC_SHIFT));
88
89         while (ambient_zones[ambient_zone].lo > ambient_val)
90                 ambient_zone--;
91
92         while (ambient_zones[ambient_zone].hi < ambient_val)
93                 ambient_zone++;
94
95         byte_val = ambient_val >> (2 + AMBIENT_VAL_SHIFT - AMBIENT_ADC_SHIFT);
96
97         ambient_val16 += byte_val - (ambient_val16 >> 4);
98         byte_val16 = ambient_val16 >> 4;
99
100         if (byte_val16 > byte_val) {
101                 byte_val16 -= byte_val;
102                 if (byte_val16 > ambient_16drop)
103                         ambient_16drop = byte_val16;
104         }
105
106         if (ambient_min > byte_val)
107                 ambient_min = byte_val;
108
109         if (ambient_max < byte_val)
110                 ambient_max = byte_val;
111         if (old_zone != ambient_zone) {
112 #if 0
113                 log_byte(0xab);
114                 log_byte(ambient_zone);
115                 log_word(adcval);
116                 log_flush();
117 #endif
118                 ambient_zone_changed();
119         }
120 }
121