]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - main.c
Verify EEPROM has changed before update
[rgblamp.git] / main.c
diff --git a/main.c b/main.c
index 64cf2f13f7d07c098ef28122194ec64e4f5ddd4d..bfc6b1e6d367348390eec8bd17575e71a1d3617c 100644 (file)
--- a/main.c
+++ b/main.c
@@ -38,6 +38,7 @@
 #include <stdlib.h>
 #include "picinit.h"
 #include "unused.h"
+#include "lfsr.h"
 #include "btn.h"
 #include "rgb.h"
 #include "tmr.h"
 #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 */
@@ -104,11 +92,10 @@ enum {
   STD_FADE = 16,         /* Fade time 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() */
+  PARTY_RANGE = 8,       /* Party fade/incolor range mask for lfsr_get() */
                          /* ... 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 */
+  CFG_ADDR = 0,          /* EEPROM address of start of configuration */
+  CFG_DELAY = 183,        /* 6 seconds in 32.768 msec units */
 };
 
 typedef struct {
@@ -134,61 +121,90 @@ const unsigned char colors[COLOR_COUNT][LED_COUNT] = {
 
 unsigned char mode = MODE_SOLID;
 unsigned char color = COLOR_WHITE;
+signed char bright = BRIGHT_INIT;
+bit bright_up;
 led_t red;
 led_t grn;
 led_t blu;
 led_t wht;
 bit on;
 int fade_steps;
-signed char bright = BRIGHT_INIT;
-bit bright_up;
 signed char pbHeldCount;
 
-/* Return the constrained brightness level for use in computing RGB values */
-unsigned char bright_get()
+void cfg_write()
 {
-  if (bright > BRIGHT_MAX)
-    return BRIGHT_MAX;
-  else if (bright < BRIGHT_MIN)
-    return BRIGHT_MIN;
-  else
-    return bright;
+  unsigned char addr = CFG_ADDR;
+  unsigned char m, c;
+  signed char b;
+
+  m = eeprom_read(addr++);
+  c = eeprom_read(addr++);
+  b = eeprom_read(addr);
+  if (mode != m || color != c || bright != b) {
+    addr = CFG_ADDR;
+    eeprom_write(addr++, mode);
+    eeprom_write(addr++, color);
+    eeprom_write(addr, bright);
+  }
 }
 
-/* RGB values will not illuminate the LED */
-unsigned char led_get(unsigned value)
+void cfg_read()
 {
-  value = (value * bright_get()) >> 8;
-#ifdef CEIL256
-  if (value && value < 0x26)
-    return 0x26;
-#else
-  if (value && value < 4)
-    return 4;
-#endif
-  return value;
+  unsigned char addr = CFG_ADDR;
+  unsigned char tmp;
+
+  tmp = eeprom_read(addr++);
+  if (tmp < MODE_COUNT)
+    mode = tmp;
+
+  tmp = eeprom_read(addr++);
+  if (tmp < COLOR_COUNT)
+    color = tmp;
+
+  tmp = eeprom_read(addr);
+  if ((signed char)tmp >= BRIGHT_BOTTOM && (signed char)tmp <= BRIGHT_TOP)
+    bright = tmp;
+}
+
+/* Combine LED color value and brightness level to generate an RGB value.
+ * bright will be BRIGHT_MIN...BRIGHT_MAX
+ *
+ * @param value 0...1023 (6 bit color, left shifted 4 bits, for 10 bits)
+ * @param b brightness value BRIGHT_MIN...BRIGHT_MAX
+ * @return An RGB drive value, 0...255
+ */
+unsigned char led_get(int value, signed char b)
+{
+  return (unsigned long)value * b / 4 / BRIGHT_MAX;
 }
 
-/* Set the LEDs using both color and brightness values. */
+/* Set the LEDs using color values and current brightness. */
 void leds_set()
 {
-  rgb_set(led_get((red).value), led_get((grn).value), led_get((blu).value),
-      led_get((wht).value));
+  signed char b;
+
+  if (bright > BRIGHT_MAX)
+    b = BRIGHT_MAX;
+  else if (bright < BRIGHT_MIN)
+    b = BRIGHT_MIN;
+  else
+    b = bright;
+  rgb_set(led_get((red).value, b), led_get((grn).value, b),
+      led_get((blu).value, b), led_get((wht).value, b));
 }
 
 void start_fade()
 {
-  /* RGB PWM values are 8 bits, but computations are done in 15
-   * (leaving room for a sign bit), so leds_set() uses >>7 to convert
-   * to PWM values.
+  /* RGB PWM values are 8 bits.  Color values are 12 bits.  See led_get()
+   * for the conversion of color values and brightness to RGB values.
    */
   int newr, newg, newb, neww;
 
   /* Select the destination color and fade-to time depending upon mode. */
   fade_steps = STD_FADE;
   if (mode == MODE_PARTY) {
-    color = rand() % COLOR_COUNT;
-    fade_steps = PARTY_MIN + (rand() % PARTY_RANGE);
+    color = lfsr_get() % COLOR_COUNT;
+    fade_steps = PARTY_MIN + (lfsr_get() % PARTY_RANGE);
   } else if (mode == MODE_CYCLE) {
     if (++color == COLOR_COUNT)
       color = 0;
@@ -240,8 +256,6 @@ void turnOff()
 void pb_clicked()
 {
   if (on) {
-    unsigned char omode = mode;
-
     if (mode == MODE_SOLID) {
       if (++color == COLOR_COUNT) {
         color = 0;
@@ -252,8 +266,7 @@ void pb_clicked()
       mode = 0;
       color = 0;
     }
-    if (mode != omode || mode == MODE_SOLID)
-      tmr_start(TMR_CFG, CFG_DELAY);
+    tmr_start(TMR_CFG, CFG_DELAY);
     start_fade();
   }
 }
@@ -272,6 +285,7 @@ void pb_held()
     else
       bright--;
   }
+  tmr_start(TMR_CFG, CFG_DELAY);
   leds_set();
 }
 
@@ -343,7 +357,7 @@ void fade_task()
     if (mode == MODE_CYCLE)
       tmr_start(TMR_INCOLOR, STD_INCOLOR);
     else if (mode == MODE_PARTY)
-      tmr_start(TMR_INCOLOR, PARTY_MIN + (rand() % PARTY_RANGE));
+      tmr_start(TMR_INCOLOR, PARTY_MIN + (lfsr_get() % PARTY_RANGE));
   }
   leds_set();
 }
@@ -364,8 +378,8 @@ void auto_offon_task()
 void user_boot()
 {
   dbgpin_high();
-  srand((adc_random() << 8) + adc_random());
-  cfg_read(mode, color);
+  lfsr_init((adc_random() << 8) + adc_random());
+  cfg_read();
   rs_task();
 }
 
@@ -394,7 +408,7 @@ void user_tasks(unsigned char block)
         auto_offon_task();
         break;
       case TASK_CFG:            /* Save config to EEPROM event */
-        cfg_write(mode, color);
+        cfg_write();
         break;
     }
   }