2 #include <avr/eeprom.h>
6 #define AMBIENT_FAST_SHIFT 2 // running avg of ADC vals
7 #define AMBIENT_SLOW_SHIFT 6 // 1 << AMBIENT_SLOW_SHIFT times slower
9 static uint16_t ambient_fast, ambient_slow;
10 volatile unsigned char ambient_zone, ambient_shadow;
11 static unsigned char ambient_min, ambient_max, ambient_drop;
14 #define AMBIENT_LOG_SIZE 128
15 static unsigned char ambient_log_offset_stored EEMEM;
16 static unsigned char ambient_log_offset;
17 static unsigned char ambient_log[AMBIENT_LOG_SIZE] EEMEM;
19 /* My photodiode reads 0x00C5 .. 0x033B */
21 uint16_t lo, hi; // TODO: consider changing this to bytes
25 * Note: these have to be sorted, starting with 0, ending with 0xFFFF
26 * and having small overlaps in order to provide a bit of hysteresis.
28 static ambient_zone_t ambient_zones[N_AMBIENT_ZONES] = {
30 { 0x0000, 0x0290 }, // dark
31 { 0x0278, 0x02f8 }, // evening
32 { 0x02d8, 0x0310 }, // dawn
33 { 0x0308, 0xffff }, // day
35 { 0x0000, 0x0250 }, // dark
36 { 0x0240, 0x02e0 }, // evening
37 { 0x02c0, 0x0300 }, // dawn
38 { 0x02f8, 0xffff }, // day
42 #define SHADOW_DROP_LIMIT 0x20 // in ADC units (0..0x3ff)
54 ambient_log_offset = eeprom_read_byte(&ambient_log_offset_stored);
56 if (ambient_log_offset == AMBIENT_LOG_SIZE)
57 ambient_log_offset = 0; // start over
62 unsigned char stored_offset;
64 ambient_log_min_max();
66 stored_offset = eeprom_read_byte(&ambient_log_offset_stored);
67 if (stored_offset != ambient_log_offset)
68 eeprom_write_byte(&ambient_log_offset_stored,
72 void ambient_log_min_max()
74 if (ambient_log_offset >= AMBIENT_LOG_SIZE - 1)
75 ambient_log_offset = 0; // start over
77 ambient_max -= ambient_min;
83 if (ambient_drop > 15)
86 eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_min);
87 eeprom_write_byte(&ambient_log[ambient_log_offset++],
88 (ambient_max << 4) | ambient_drop);
95 static inline void ambient_zone_changed()
97 pwmled_select_brightness();
101 static unsigned char val_to_zone(uint16_t ambient_val)
103 unsigned char new_zone = ambient_zone;
105 while (ambient_zones[new_zone].lo > ambient_val)
108 while (ambient_zones[new_zone].hi < ambient_val)
114 void ambient_adc(uint16_t adcval)
116 unsigned char new_zone, user_zone, new_shadow = 0;
117 unsigned char byte_fast, byte_slow, drop;
118 uint16_t fast_10bit, slow_10bit;
120 // running avg - shorter timespan
121 ambient_fast += adcval - (ambient_fast >> AMBIENT_FAST_SHIFT);
123 // running avg - longer timespan
124 fast_10bit = ambient_fast >> (AMBIENT_FAST_SHIFT + AMBIENT_ADC_SHIFT);
125 ambient_slow += fast_10bit - (ambient_slow >> AMBIENT_SLOW_SHIFT);
127 // ambient zones are governed by shorter timespan by default
128 new_zone = val_to_zone(fast_10bit);
130 slow_10bit = ambient_slow >> AMBIENT_SLOW_SHIFT;
132 if (new_zone > 1 && (
133 new_zone == ambient_zone-1 || new_zone > ambient_zone)) {
134 // but change to the neighbouring zone is governed by _slow,
135 // except to the darkest zone, where we want fast reaction.
136 new_zone = val_to_zone(slow_10bit);
139 // user_param ambient zone override
140 if ((user_zone = get_user_param(0)) > 0)
141 new_zone = user_zone - 1;
143 // are we entering the shadow?
144 if (!user_zone && new_zone < ambient_zone
145 && ambient_zone >= 2 && slow_10bit > fast_10bit
146 && slow_10bit - fast_10bit >= SHADOW_DROP_LIMIT) {
147 // we are entering the shadow
151 if (ambient_shadow) {
153 new_zone = ambient_zone; // don't change while entering shadow
154 ambient_shadow = new_shadow; // update the timeout
157 if (!ambient_shadow) { // leaving the shadow
158 ambient_zone_changed();
161 } else if (new_shadow) {
162 ambient_shadow = new_shadow; // set up the timeout
163 new_zone = ambient_zone; // don't change while entering shadow
164 ambient_zone_changed(); // notify others the first time
167 // TODO: maybe use these values instead of 10-bit?
168 byte_fast = fast_10bit >> 2;
169 byte_slow = slow_10bit >> 2;
171 if (byte_slow > byte_fast) {
172 drop = byte_slow - byte_fast;
173 if (drop > ambient_drop)
177 if (ambient_min > byte_slow)
178 ambient_min = byte_slow;
180 if (ambient_max < byte_slow)
181 ambient_max = byte_slow;
183 if (new_zone != ambient_zone) {
184 ambient_zone = new_zone;
187 log_byte(ambient_zone);
191 ambient_zone_changed();