]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/ambient.c
ambient light: consistent init function naming
[bike-lights.git] / firmware / ambient.c
1 #include <avr/io.h>
2
3 #include "lights.h"
4
5 static uint16_t ambient_val;
6 volatile unsigned char ambient_zone;
7 static unsigned char ambient_zone_set;
8
9 static uint16_t ambient_zones[] = {
10         0x0c00, 0x0d00, 0x1000, 0x1800, 0x2800, 0x2f80, 0xffff
11 };
12 #define N_AMBIENT_ZONES (sizeof(ambient_zones)/sizeof(ambient_zones[0]))
13
14 void init_ambient()
15 {
16         ambient_val = 0;
17         ambient_zone = 0;
18         ambient_zone_set = 0;
19 }
20
21 void ambient_zone_changed()
22 {
23         log_byte(0xab);
24         log_byte(ambient_zone);
25         log_word(ambient_val);
26         log_flush();
27 }
28
29 void ambient_adc(uint16_t adcval)
30 {
31         unsigned char newzone;
32
33         if (!ambient_zone_set)
34                 ambient_val = adcval << 4;
35         else // running sum
36                 ambient_val += adcval - (ambient_val >> 4);
37
38         newzone = 0;
39         while (newzone < N_AMBIENT_ZONES-1
40                 && ambient_zones[newzone] < ambient_val)
41                 newzone++;
42
43         // TODO: implement hysteresis?
44         if (!ambient_zone_set || newzone != ambient_zone) {
45                 ambient_zone = newzone;
46                 ambient_zone_set = 1;
47                 // ambient_zone_changed();
48         }
49 }
50