]> www.fi.muni.cz Git - bike-lights.git/commitdiff
ambient light sensor
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 28 Aug 2012 14:54:03 +0000 (16:54 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 28 Aug 2012 14:58:23 +0000 (16:58 +0200)
Makefile
adc.c
ambient.c [new file with mode: 0644]
lights.h
main.c

index c2e1d6a89a54d2d9351acdc662f475af9f760130..e7b6355cd2086cfca28cfc5884dd983a2f7a9f94 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 
 PROGRAM=lights
-SRC=main.c logging.c adc.c pwm.c tmr.c pwmled.c gpio.c
+SRC=main.c logging.c adc.c pwm.c tmr.c pwmled.c gpio.c ambient.c
 OBJ=$(SRC:.c=.o)
 
 
diff --git a/adc.c b/adc.c
index a37e95886f237828537666d5bb209048a521eaeb..b758721d73e4c298ff3561b0be773b7336879894 100644 (file)
--- a/adc.c
+++ b/adc.c
@@ -16,6 +16,8 @@ static unsigned char adc_mux[] = { // pwmleds should be first
        _BV(REFS1) | _BV(MUX2) | _BV(MUX0),
 };
 
+#define AMBIENT_ADC N_PWMLEDS
+
 #define LAST_ADC (sizeof(adc_mux)/sizeof(char))
 volatile static unsigned char current_adc = LAST_ADC;
 
@@ -27,7 +29,9 @@ static void start_next_adc()
                // test if current_adc should be measured
                if (current_adc < N_PWMLEDS && pwmled_is_on(current_adc))
                        goto found;
-               // TODO ambient light, battery sense, etc.
+               if (current_adc == AMBIENT_ADC)
+                       goto found;
+               // TODO battery sense, etc.
        }
 
        // all ADCs have been handled
@@ -66,7 +70,9 @@ ISR(ADC_vect) { // IRQ handler
 
        if (current_adc < N_PWMLEDS)
                pwmled_adc(current_adc, adcval);
-       // TODO ambient light, battery sense, etc.
+       if (current_adc == AMBIENT_ADC)
+               ambient_adc(adcval);
+       // TODO battery sense, etc.
        
        start_next_adc();
 }
diff --git a/ambient.c b/ambient.c
new file mode 100644 (file)
index 0000000..e91c1ae
--- /dev/null
+++ b/ambient.c
@@ -0,0 +1,51 @@
+#include <avr/io.h>
+
+#include "lights.h"
+
+static uint16_t ambient_val;
+unsigned char ambient_zone;
+unsigned char ambient_zone_set;
+
+static uint16_t ambient_zones[] = {
+       1, 2, 10, 20, 256
+};
+#define N_AMBIENT_ZONES (sizeof(ambient_zones)/sizeof(ambient_zones[0]))
+
+void ambient_init()
+{
+       ambient_val = 0;
+       ambient_zone = 0;
+       ambient_zone_set = 0;
+}
+
+void ambient_zone_changed()
+{
+       log_byte(0xCC);
+       log_byte(ambient_zone);
+       log_word(ambient_val);
+}
+
+void ambient_adc(uint16_t adcval)
+{
+       unsigned char newzone;
+
+       if (!ambient_zone_set)
+               ambient_val = adcval << 4;
+
+       // running sum
+       ambient_val += adcval - (ambient_val >> 4);
+
+       newzone = 0;
+       while (newzone < N_AMBIENT_ZONES-1
+               && ambient_zones[newzone] <= ambient_val)
+               newzone++;
+
+       if (!ambient_zone_set || newzone != ambient_zone) {
+               ambient_zone = newzone;
+               ambient_zone_changed();
+               ambient_zone_set = 1;
+       }
+}
+
+
+
index 6619be73b66ff4181116473d155e1bf40bcae8d6..74e887d671eb3bcdd75f7c74a024f70cc08daccd 100644 (file)
--- a/lights.h
+++ b/lights.h
@@ -47,5 +47,10 @@ unsigned char pwmled_is_on(unsigned char n);
 void gpio_init();
 void gpio_set(unsigned char n, unsigned char on);
 
+/* ambient.c */
+void ambient_init();
+extern unsigned char ambient_zone;
+void ambient_adc(uint16_t adc_val);
+
 #endif /* !LIGHTS_H__ */
 
diff --git a/main.c b/main.c
index fe906e51424643cefd6bf108ce804a334370557d..7b63ad824f5f83f3db8865a7123497a376b4af68 100644 (file)
--- a/main.c
+++ b/main.c
@@ -16,6 +16,7 @@ int main(void)
 
        pwmled_init();
        gpio_init();
+       ambient_init();
 
        log_set_state(3);