]> oss.titaniummirror.com Git - rgblamp.git/commitdiff
Add candle mode 1.1
authorR. Steve McKown <rsmckown@gmail.com>
Thu, 29 Dec 2011 18:32:14 +0000 (11:32 -0700)
committerR. Steve McKown <rsmckown@gmail.com>
Sat, 31 Dec 2011 21:53:33 +0000 (14:53 -0700)
Flicker by varying the brightness about the current brightness setting,
bright.  cbright tracks the flicker brightness variance, which is further
constrained by CBRIGHT_VARIANCE.  bright represents the max possible value
of cbright, so that when brightness is at max there remains a full range
of brightness for flicker.

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

diff --git a/isr.c b/isr.c
index 31fd147ae80ad9881dd473ff7f79215281b2d2c3..4fcdbe40ac80a389c2417b14f6aa3c2ee012b9bd 100644 (file)
--- a/isr.c
+++ b/isr.c
@@ -41,10 +41,8 @@ void interrupt isr()
     task_post(TASK_FADE);
   if (tmr_fired(TMR_INCOLOR))
     task_post(TASK_INCOLOR);
-#if 0
-  if (tmr_fired(TMR_DIM))
-    task_post(TMR_DIM);
-#endif
+  if (tmr_fired(TMR_CANDLE))
+    task_post(TASK_CANDLE);
   if (tmr_fired(TMR_CFG))
     task_post(TASK_CFG);
 }
diff --git a/main.c b/main.c
index b09dcc44021597bfa6ac2c37cfbdba3f77734043..3c3066075c1bc9983bc5c62a6e41727133d19137 100644 (file)
--- a/main.c
+++ b/main.c
@@ -60,7 +60,6 @@
 enum {
   /* Operating modes */
   MODE_SOLID = 0,        /* Cycle through colors[][] before next mode */
-  //MODE_CANDLE,
   MODE_CYCLE,             /* Auto cycle through colors */
   MODE_PARTY,            /* Random yet fast incolor and fade */
   MODE_COUNT,
@@ -74,13 +73,15 @@ enum {
 
   HELD_TMR_PERIODS = 1,  /* Held timer fires every 32.768 msec */
   HELD_PERIODS = 16,     /* Held this long before held events fire */
-  COLOR_WHITE = 0,       /* Color index for white */
-  COLOR_COUNT = 11,
+  COLOR_COUNT = 12,
+  COLOR_CANDLE = 0,      /* Color index for white */
+  COLOR_WHITE = 11,      /* Color index for white */
   BRIGHT_TOP = 100,      /* Bright ramps up to top ... */
   BRIGHT_BOTTOM = -5,    /* ... then down to bottom */
   BRIGHT_MAX = 85,       /* Bright values are constrained for computations */
   BRIGHT_MIN = 1,        /* ... by min and max. */
   BRIGHT_INIT = 42,      /* later, get from eeprom */
+  CBRIGHT_VARIANCE = 20, /* brightness range for candle flicker */
   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 */
@@ -96,8 +97,12 @@ typedef struct {
   signed char remainder;
 } led_t;
 
+/* Offsets to brightness used for candle color */
+const signed char offsets[4] = { -1, -1, 1, 1 };
+
 /* Available colors, 6 bits per color (no values above 0x3f) */
 const unsigned char colors[COLOR_COUNT][LED_COUNT] = {
+  { 0x2c, 0x20, 0x00, 0x18 }, /* candle */
   { 0x28, 0x00, 0x00, 0x00 }, /* red */
   { 0x30, 0x20, 0x00, 0x00 }, /* orange */
   { 0x2c, 0x2c, 0x00, 0x00 }, /* yellow */
@@ -112,7 +117,7 @@ const unsigned char colors[COLOR_COUNT][LED_COUNT] = {
 };
 
 unsigned char mode = MODE_SOLID;
-unsigned char color = COLOR_WHITE;
+unsigned char color = COLOR_CANDLE;
 signed char bright = BRIGHT_INIT;
 bit bright_up;
 led_t red;
@@ -122,6 +127,7 @@ led_t wht;
 bit on;
 int fade_steps;
 signed char pbHeldCount;
+signed char cbright;
 
 void cfg_write()
 {
@@ -175,12 +181,16 @@ void leds_set()
 {
   signed char b;
 
-  if (bright > BRIGHT_MAX)
-    b = BRIGHT_MAX;
-  else if (bright < BRIGHT_MIN)
-    b = BRIGHT_MIN;
+  /* Use correct brightness value, then constrain it */
+  if (mode == MODE_SOLID && color == COLOR_CANDLE)
+    b = cbright;
   else
     b = bright;
+  if (b > BRIGHT_MAX)
+    b = BRIGHT_MAX;
+  else if (b < BRIGHT_MIN)
+    b = BRIGHT_MIN;
+
   rgb_set(led_get((red).value, b), led_get((grn).value, b),
       led_get((blu).value, b), led_get((wht).value, b));
 }
@@ -219,6 +229,7 @@ void start_fade()
   wht.remainder = neww - (wht.value + wht.increment * fade_steps);
 
   /* Start the fade timer */
+  tmr_stop(TMR_CANDLE);
   tmr_stop(TMR_INCOLOR);
   tmr_startPeriodic(TMR_FADE, 1);  /* 32.768 msec */
 }
@@ -236,6 +247,7 @@ void turnOff()
 {
   /* Event on to off, either by switch or auto-off timer */
   tmr_stop(TMR_CFG);
+  tmr_stop(TMR_CANDLE);
   tmr_stop(TMR_INCOLOR);
   tmr_stop(TMR_FADE);
   rgb_off();
@@ -331,6 +343,21 @@ void rs_task()
   }
 }
 
+void candle_task()
+{
+  /* Flicker by using the brightness value */
+  cbright += offsets[lfsr_get() & 0x03];
+
+  /* Constraight cbright to a range about bright */
+  if (cbright > bright)
+    cbright = bright;
+  else if (cbright < bright - CBRIGHT_VARIANCE)
+    cbright = bright - CBRIGHT_VARIANCE;
+
+  /* Adjust the LEDs */
+  leds_set();
+}
+
 void fade_task()
 {
   red.value += red.increment;
@@ -345,11 +372,16 @@ void fade_task()
     grn.value += grn.remainder;
     blu.value += blu.remainder;
     wht.value += wht.remainder;
+    tmr_stop(TMR_CANDLE);
     tmr_stop(TMR_FADE);
     if (mode == MODE_CYCLE)
       tmr_start(TMR_INCOLOR, STD_INCOLOR);
     else if (mode == MODE_PARTY)
       tmr_start(TMR_INCOLOR, PARTY_MIN + (lfsr_get() % PARTY_RANGE));
+    else if (mode == MODE_SOLID && color == COLOR_CANDLE) {
+      cbright = bright - CBRIGHT_VARIANCE / 2;
+      tmr_startPeriodic(TMR_CANDLE, 1);
+    }
   }
   leds_set();
 }
@@ -396,6 +428,9 @@ void user_tasks(unsigned char block)
       case TASK_INCOLOR:        /* in-color timer has fired */
         start_fade();
         break;
+      case TASK_CANDLE:         /* periodic adjustment for candle flicker */
+        candle_task();
+        break;
       case TASK_TMR32:          /* auto on/off event */
         auto_offon_task();
         break;
index 28262eae1a7d42668e2734ad661b41704914b933..5130fba51debb2fed893dd5459aaf2ec4fd798e6 100644 (file)
@@ -23,6 +23,7 @@ enum {
   TASK_FADE,
   TASK_INCOLOR,
   TASK_TMR32,
+  TASK_CANDLE,
   TASK_CFG,
 
   TASK_COUNT
index 37fc198a3ca979e8a342022797880edaf952bc26..43ba6aa4228d813b81187c512ab8440b75b06bbd 100644 (file)
@@ -20,7 +20,7 @@ enum {
   TMR_AUTO_OFFON,
   TMR_FADE,
   TMR_INCOLOR,
-  //TMR_DIM,
+  TMR_CANDLE,
   TMR_CFG,
 
   TMR_COUNT