From: R. Steve McKown Date: Sat, 17 Dec 2011 18:30:53 +0000 (-0700) Subject: Add persistent configuration X-Git-Tag: 1.0~1 X-Git-Url: https://oss.titaniummirror.com/gitweb?p=rgblamp.git;a=commitdiff_plain;h=c3567b7908dd3b08dd7a8c2d051991bf50670a42 Add persistent configuration Save mode and color any time mode changes, or if in MODE_SOLID and color changes. Configuration is saved in internal EEPROM. The PIC16LF1933 claims 100K writes. At 5 hrs per day, thats over 13 years of config writes assuming MODE_SOLID and a color change 4 times per year. When the configuration changes, a 3 second timer is set. When the timer expires, then the configuration is written. This should help reduce reduce unnecessary EEPROM writes when the uesr is cycling through the modes to find the one they want. --- diff --git a/isr.c b/isr.c index 9d80695..31fd147 100644 --- a/isr.c +++ b/isr.c @@ -45,4 +45,6 @@ void interrupt isr() if (tmr_fired(TMR_DIM)) task_post(TMR_DIM); #endif + if (tmr_fired(TMR_CFG)) + task_post(TASK_CFG); } diff --git a/main.c b/main.c index 0f98b6a..64cf2f1 100644 --- a/main.c +++ b/main.c @@ -64,6 +64,19 @@ #define dbgpin_low() (RA2 = 0) #define dbgpin_toggle() (RA2 = (LATA2 == 0) ? 1 : 0) +#define cfg_write(mode, color) do { \ + eeprom_write(CFG_MODE_ADDR, (mode)); \ + eeprom_write(CFG_COLOR_ADDR, (color)); \ + } while (0) + +#define cfg_read(mode, color) do { \ + unsigned char tmp; \ + tmp = eeprom_read(CFG_MODE_ADDR); \ + mode = (tmp < MODE_COUNT) ? tmp : MODE_SOLID; \ + tmp = eeprom_read(CFG_COLOR_ADDR); \ + color = (tmp < COLOR_COUNT) ? tmp : 0; \ + } while (0) + enum { /* Operating modes */ MODE_SOLID = 0, /* Cycle through colors[][] before next mode */ @@ -89,10 +102,13 @@ enum { BRIGHT_MIN = 1, /* ... by min and max. */ BRIGHT_INIT = 42, /* later, get from eeprom */ STD_FADE = 16, /* Fade time in 32.768 ms units */ - STD_INCOLOR = 29491, /* Time in color when MODE_FADE, in 32.768 ms units */ + STD_INCOLOR = 29491, /* Time in color when MODE_CYCLE, in 32.768 ms units */ PARTY_MIN = 8, /* Min party fade and incolor units */ PARTY_RANGE = 8, /* Party fade/incolor range mask for rand() */ /* ... see start_fade() */ + CFG_MODE_ADDR = 0, /* EEPROM address of mode variable */ + CFG_COLOR_ADDR, /* EEPROM address of color variable */ + CFG_DELAY = 92, /* 3 seconds in 32.768 msec units */ }; typedef struct { @@ -156,8 +172,7 @@ unsigned char led_get(unsigned value) /* Set the LEDs using both color and brightness values. */ void leds_set() { - rgb_set(led_get((red).value), - led_get((grn).value), led_get((blu).value), + rgb_set(led_get((red).value), led_get((grn).value), led_get((blu).value), led_get((wht).value)); } @@ -212,6 +227,7 @@ void turnOn() void turnOff() { /* Event on to off, either by switch or auto-off timer */ + tmr_stop(TMR_CFG); tmr_stop(TMR_INCOLOR); tmr_stop(TMR_FADE); rgb_off(); @@ -224,6 +240,8 @@ void turnOff() void pb_clicked() { if (on) { + unsigned char omode = mode; + if (mode == MODE_SOLID) { if (++color == COLOR_COUNT) { color = 0; @@ -234,6 +252,8 @@ void pb_clicked() mode = 0; color = 0; } + if (mode != omode || mode == MODE_SOLID) + tmr_start(TMR_CFG, CFG_DELAY); start_fade(); } } @@ -341,19 +361,11 @@ void auto_offon_task() } } -void config_read() -{ - /* Read configuration fields from eeprom, notably current mode and - * brightness level. - */ - /* FIXME: implement this */ -} - void user_boot() { dbgpin_high(); srand((adc_random() << 8) + adc_random()); - config_read(); + cfg_read(mode, color); rs_task(); } @@ -381,6 +393,9 @@ void user_tasks(unsigned char block) case TASK_TMR32: /* auto on/off event */ auto_offon_task(); break; + case TASK_CFG: /* Save config to EEPROM event */ + cfg_write(mode, color); + break; } } } diff --git a/task_defs.h b/task_defs.h index c3b31ca..28262ea 100644 --- a/task_defs.h +++ b/task_defs.h @@ -23,6 +23,7 @@ enum { TASK_FADE, TASK_INCOLOR, TASK_TMR32, + TASK_CFG, TASK_COUNT }; diff --git a/tmr_defs.h b/tmr_defs.h index aa37abe..37fc198 100644 --- a/tmr_defs.h +++ b/tmr_defs.h @@ -21,6 +21,7 @@ enum { TMR_FADE, TMR_INCOLOR, //TMR_DIM, + TMR_CFG, TMR_COUNT };