From e5360facc2b16d902a9c891e9cd8aadcc3c21e39 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Tue, 28 Aug 2012 16:54:03 +0200 Subject: [PATCH] ambient light sensor --- Makefile | 2 +- adc.c | 10 ++++++++-- ambient.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ lights.h | 5 +++++ main.c | 1 + 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 ambient.c diff --git a/Makefile b/Makefile index c2e1d6a..e7b6355 100644 --- 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 a37e958..b758721 100644 --- 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 index 0000000..e91c1ae --- /dev/null +++ b/ambient.c @@ -0,0 +1,51 @@ +#include + +#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; + } +} + + + diff --git a/lights.h b/lights.h index 6619be7..74e887d 100644 --- 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 fe906e5..7b63ad8 100644 --- a/main.c +++ b/main.c @@ -16,6 +16,7 @@ int main(void) pwmled_init(); gpio_init(); + ambient_init(); log_set_state(3); -- 2.43.5