From 3f38edc49796a8dd1d03c2dc302be42db2d97d50 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Mon, 20 May 2013 23:47:05 +0200 Subject: [PATCH] ambient: log minima and maxima In order to be able to tune the ambient light sensor better, I made it to log the minimum and maximum values read for every three minutes or so. When the log buffer fills, start over only after the next off/on cycle. --- firmware/ambient.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++ firmware/lights.h | 2 ++ firmware/main.c | 1 + firmware/tmr.c | 3 +++ 4 files changed, 53 insertions(+) diff --git a/firmware/ambient.c b/firmware/ambient.c index 72e1332..548ff69 100644 --- a/firmware/ambient.c +++ b/firmware/ambient.c @@ -1,10 +1,18 @@ #include +#include #include "lights.h" #define AMBIENT_VAL_SHIFT 2 static uint16_t ambient_val; volatile unsigned char ambient_zone; +static unsigned char ambient_min, ambient_max; + +/* logging */ +#define AMBIENT_LOG_SIZE 128 +static unsigned char ambient_log_offset_stored EEMEM; +static unsigned char ambient_log_offset; +static unsigned char ambient_log[AMBIENT_LOG_SIZE] EEMEM; /* My photodiode reads 0x00C5 .. 0x033B */ typedef struct { @@ -26,7 +34,38 @@ static ambient_zone_t ambient_zones[] = { void init_ambient() { ambient_val = 0; + ambient_max = 0; + ambient_min = 0xFF; ambient_zone = 1; + + ambient_log_offset = eeprom_read_byte(&ambient_log_offset_stored); + + if (ambient_log_offset == AMBIENT_LOG_SIZE) + ambient_log_offset = 0; // start over +} + +void susp_ambient() +{ + unsigned char stored_offset; + + ambient_log_min_max(); + + stored_offset = eeprom_read_byte(&ambient_log_offset_stored); + if (stored_offset != ambient_log_offset) + eeprom_write_byte(&ambient_log_offset_stored, + ambient_log_offset); +} + +void ambient_log_min_max() +{ + if (ambient_log_offset >= AMBIENT_LOG_SIZE - 1) + return; + + eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_min); + eeprom_write_byte(&ambient_log[ambient_log_offset++], ambient_max); + + ambient_min = 0xFF; + ambient_max = 0; } void ambient_zone_changed() @@ -46,6 +85,7 @@ void ambient_zone_changed() void ambient_adc(uint16_t adcval) { unsigned char old_zone = ambient_zone; + unsigned char byte_val; ambient_val += adcval - (ambient_val >> (AMBIENT_VAL_SHIFT - AMBIENT_ADC_SHIFT)); @@ -56,6 +96,13 @@ void ambient_adc(uint16_t adcval) while (ambient_zones[ambient_zone].hi < ambient_val) ambient_zone++; + byte_val = adcval >> 2; + + if (ambient_min > byte_val) + ambient_min = byte_val; + + if (ambient_max < byte_val) + ambient_max = byte_val; #if 0 if (old_zone != ambient_zone) { log_byte(0xab); diff --git a/firmware/lights.h b/firmware/lights.h index 772613a..6851509 100644 --- a/firmware/lights.h +++ b/firmware/lights.h @@ -61,6 +61,8 @@ void gpio_set(unsigned char n, unsigned char on); /* ambient.c */ #define AMBIENT_ADC_SHIFT 0 /* 1 measurement per callback */ void init_ambient(); +void susp_ambient(); +void ambient_log_min_max(); extern volatile unsigned char ambient_zone; void ambient_adc(uint16_t adc_val); diff --git a/firmware/main.c b/firmware/main.c index 90bea63..393f47d 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -32,6 +32,7 @@ static void hw_suspend() susp_adc(); susp_tmr(); susp_gpio(); + susp_ambient(); susp_buttons(); wdt_disable(); diff --git a/firmware/tmr.c b/firmware/tmr.c index 23b70b6..d53981a 100644 --- a/firmware/tmr.c +++ b/firmware/tmr.c @@ -37,5 +37,8 @@ ISR(TIMER0_COMPA_vect) timer_start_slow_adcs(); pattern_div = PATTERN_DIV; } + + if ((jiffies & 0x7FFF) == 0) + ambient_log_min_max(); } -- 2.39.3