From 541892c2955660bdd068dc7bc0a2611592f408d7 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Thu, 28 Mar 2013 22:13:58 +0100 Subject: [PATCH] ambient light sensor: configurable # of readings Set the # of readings to 1, and decrease the shift value for running average in order to get faster reaction. --- firmware/adc.c | 2 +- firmware/ambient.c | 12 +++++++----- firmware/lights.h | 1 + 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/firmware/adc.c b/firmware/adc.c index f55a427..70c2aa1 100644 --- a/firmware/adc.c +++ b/firmware/adc.c @@ -19,7 +19,7 @@ struct { { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 1 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 2 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 3 - { 0, 1, 3 }, // ambient + { 0, 1, AMBIENT_ADC_SHIFT }, // ambient { 0, 1, 0 }, // battery { 0, 1, 0 }, // gain20 { 0, 1, 0 }, // buttons diff --git a/firmware/ambient.c b/firmware/ambient.c index 143ffc4..f626208 100644 --- a/firmware/ambient.c +++ b/firmware/ambient.c @@ -2,6 +2,7 @@ #include "lights.h" +#define AMBIENT_VAL_SHIFT 3 static uint16_t ambient_val; volatile unsigned char ambient_zone; @@ -15,10 +16,10 @@ typedef struct { * and having small overlaps in order to provide a bit of hysteresis. */ static ambient_zone_t ambient_zones[] = { - { 0x0000, 0xa400 }, // dark - { 0xa000, 0xc000 }, - { 0xbe00, 0xc800 }, - { 0xc600, 0xffff } + { 0x0000 , 0x0290<> 3); + ambient_val += adcval - (ambient_val + >> (AMBIENT_VAL_SHIFT - AMBIENT_ADC_SHIFT)); while (ambient_zones[ambient_zone].lo > ambient_val) ambient_zone--; diff --git a/firmware/lights.h b/firmware/lights.h index 2eb1f09..e9d48aa 100644 --- a/firmware/lights.h +++ b/firmware/lights.h @@ -59,6 +59,7 @@ void susp_gpio(); void gpio_set(unsigned char n, unsigned char on); /* ambient.c */ +#define AMBIENT_ADC_SHIFT 0 /* 1 measurement per callback */ void init_ambient(); extern volatile unsigned char ambient_zone; void ambient_adc(uint16_t adc_val); -- 2.39.3