]> oss.titaniummirror.com Git - rgblamp.git/commitdiff
Add persistent configuration
authorR. Steve McKown <rsmckown@gmail.com>
Sat, 17 Dec 2011 18:30:53 +0000 (11:30 -0700)
committerR. Steve McKown <rsmckown@gmail.com>
Sat, 17 Dec 2011 18:30:53 +0000 (11:30 -0700)
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.

isr.c
main.c
task_defs.h
tmr_defs.h

diff --git a/isr.c b/isr.c
index 9d80695df2e5f12808ae6091c0ee8f96c1e936b3..31fd147ae80ad9881dd473ff7f79215281b2d2c3 100644 (file)
--- 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 0f98b6aada831887f1b8be768b02c62071892a44..64cf2f13f7d07c098ef28122194ec64e4f5ddd4d 100644 (file)
--- a/main.c
+++ b/main.c
 #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;
     }
   }
 }
index c3b31ca18c9ef110e2b554f678ad986de4e5a447..28262eae1a7d42668e2734ad661b41704914b933 100644 (file)
@@ -23,6 +23,7 @@ enum {
   TASK_FADE,
   TASK_INCOLOR,
   TASK_TMR32,
+  TASK_CFG,
 
   TASK_COUNT
 };
index aa37abeee630177f23b736dd89c3284dc708179a..37fc198a3ca979e8a342022797880edaf952bc26 100644 (file)
@@ -21,6 +21,7 @@ enum {
   TMR_FADE,
   TMR_INCOLOR,
   //TMR_DIM,
+  TMR_CFG,
 
   TMR_COUNT
 };