]> oss.titaniummirror.com Git - rgblamp.git/commitdiff
Merge new functionality
authorR. Steve McKown <rsmckown@gmail.com>
Sat, 17 Dec 2011 02:22:36 +0000 (19:22 -0700)
committerR. Steve McKown <rsmckown@gmail.com>
Sat, 17 Dec 2011 02:22:36 +0000 (19:22 -0700)
* New button functionality from latest picbtnisr
* Bright/dim ramping
* New color tracking to allow fade and dim at the same time

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

diff --git a/isr.c b/isr.c
index 957fc49ccf107c745c05eef37c7c7d73385d4855..9d80695df2e5f12808ae6091c0ee8f96c1e936b3 100644 (file)
--- a/isr.c
+++ b/isr.c
@@ -35,6 +35,8 @@ void interrupt isr()
   btn_isr();
   tmr32_isr();
 
+  if (tmr_fired(TMR_BTN_PB_HELD))
+    task_post(TASK_BTN_PB_HELD);
   if (tmr_fired(TMR_FADE))
     task_post(TASK_FADE);
   if (tmr_fired(TMR_INCOLOR))
diff --git a/main.c b/main.c
index cb05d747df629781ae1e3a1647440090f9e93ca9..c258972d20e9717696c8df660d9aa42fe502fef0 100644 (file)
--- a/main.c
+++ b/main.c
 #include "adc_random.h"
 
 #if 1
-#define AUTO_OFF_COUNT          1800U  /*  1 hr  in 2 sec units */
-#define AUTO_ON_COUNT           19800U /* 11 hrs in 2 sec units */
+#define AUTO_OFF_COUNT   450  /* 15 mins on*/
+#define AUTO_ON_COUNT    1350 /* 45 mins off */
 #else
-#define AUTO_OFF_COUNT          9000U  /*  5 hrs in 2 sec units */
-#define AUTO_ON_COUNT           34200U /* 19 hrs in 2 sec units */
+#define AUTO_OFF_COUNT   9000U  /*  5 hrs in 2 sec units */
+#define AUTO_ON_COUNT    34200U /* 19 hrs in 2 sec units */
 #endif
-#define leds_set(r,g,b,w)       rgb_set((r).value >> 7, (g).value >> 7, \
-                                    (b).value >> 7, 0)
-#define dbgpin_init()           do { \
-                                    /* Set RA2 as output low */ \
-                                    RA2 = 0; \
-                                    TRISA2 = 0; \
-                                } while (0)
+#define dbgpin_init()    do { \
+                             /* Set RA2 as output low */ \
+                             RA2 = 0; \
+                             TRISA2 = 0; \
+                         } while (0)
 #define dbgpin_high() (RA2 = 1)
 #define dbgpin_low()  (RA2 = 0)
 #define dbgpin_toggle()  (RA2 = (LATA2 == 0) ? 1 : 0)
 
+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_COUNT = 19,
+  BRIGHT_TOP = 100,
+  BRIGHT_BOTTOM = -5,
+  BRIGHT_MAX = 85,
+  BRIGHT_MIN = 1,
+  BRIGHT_INIT = 42, /* later, get from eeprom */
+
+  MODE_SOLID = 0,
+  //MODE_CANDLE,
+  MODE_FADE,
+  MODE_PARTY,
+  MODE_COUNT,
+
+  LED_RED = 0,
+  LED_GRN,
+  LED_BLU,
+  LED_WHT,
+  LED_COUNT,
+};
+
 typedef struct {
   int value;
   int increment;
   signed char remainder;
 } led_t;
 
-/* The index of all step arrays is the speed variable.  min values must be
- * at least one.  range values are bit-ANDed with rand() to generate a range, so
- * ensure they are one less than a power of 2 and at least 1.
- */
-const static unsigned min_incolor_steps[4] =   {   320,  32, 32,  1 };
-const static unsigned range_incolor_steps[4] = { 32767, 127, 31,  7 };
-const static int min_fade_steps[4] =           {    64,  32, 32,  8 };
-const static int range_fade_steps[4] =         {   511, 127, 31,  7 };
+/* 18 available colors plus white, 2 bits per color, no white mix */
+const unsigned char colors[COLOR_COUNT][LED_COUNT] = {
+  { 3, 0, 0, 0 },
+  { 3, 1, 0, 0 },
+  { 3, 2, 0, 0 },
+  { 3, 3, 0, 0 },
+  { 2, 3, 0, 0 },
+  { 1, 3, 0, 0 },
+  { 0, 3, 0, 0 },
+  { 0, 3, 1, 0 },
+  { 0, 3, 2, 0 },
+  { 0, 3, 3, 0 },
+  { 0, 2, 3, 0 },
+  { 0, 1, 3, 0 },
+  { 0, 0, 3, 0 },
+  { 1, 0, 3, 0 },
+  { 2, 0, 3, 0 },
+  { 3, 0, 3, 0 },
+  { 3, 0, 2, 0 },
+  { 3, 0, 1, 0 },
+  { 3, 3, 3, 3 },
+};
+
+const static int std_fade = 16;
+const static int std_incolor = 29491;
+const static int party_min = 8;
+const static int party_range = 8;
 
+unsigned char mode = MODE_SOLID;
+unsigned char color = COLOR_WHITE;
 led_t red;
 led_t grn;
 led_t blu;
 led_t wht;
 bit on;
-unsigned char speed;
 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()
+{
+  if (bright > BRIGHT_MAX)
+    return BRIGHT_MAX;
+  else if (bright < BRIGHT_MIN)
+    return BRIGHT_MIN;
+  else
+    return bright;
+}
+
+/* RGB values will not illuminate the LED */
+unsigned char led_get(unsigned value)
+{
+  value = (value * bright_get()) >> 8;
+#ifdef CEIL256
+  if (value && value < 0x26)
+    return 0x26;
+#else
+  if (value && value < 4)
+    return 4;
+#endif
+  return 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),
+      led_get((wht).value));
+}
 
 void start_fade()
 {
@@ -94,17 +172,21 @@ void start_fade()
     */
   int newr, newg, newb, neww;
 
-  /* New RGB values; all zero is not a valid option */
-  do {
-    newr = rand();
-    newg = rand();
-    newb = rand();
-    neww = rand();
-  } while (newr == 0 && newg == 0 && newb == 0 && neww == 0);
+  /* 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);
+  } else if (mode == MODE_FADE) {
+    if (++color == COLOR_COUNT)
+      color = 0;
+  }
 
-  /* Random # of steps to reach the new color */
-  fade_steps = min_fade_steps[speed & 3] +
-      (rand() & range_fade_steps[speed & 3]);
+  /* Retrieve the destination color */
+  newr = colors[color][LED_RED] << 8;
+  newg = colors[color][LED_GRN] << 8;
+  newb = colors[color][LED_BLU] << 8;
+  neww = colors[color][LED_WHT] << 8;
 
   /* Compute increment per fade step, and remainder, for each led */
   red.increment = (newr - red.value) / fade_steps;
@@ -125,11 +207,6 @@ void turnOn()
 {
   dbgpin_high();
   on = 1;
-  red.value = 0;
-  grn.value = 0;
-  blu.value = 0;
-  wht.value = 0;
-  leds_set(red, grn, blu, wht);
   rgb_on();
   start_fade();
   btn_pben();
@@ -141,23 +218,79 @@ void turnOff()
   tmr_stop(TMR_INCOLOR);
   tmr_stop(TMR_FADE);
   rgb_off();
+  red.value = grn.value = blu.value = wht.value = 0;
+  on = 0;
   dbgpin_low();
   SLEEP();
-  on = 0;
 }
 
-void pb_task()
+void pb_clicked()
 {
-  /* Is this task running nearly continuously? */
   if (on) {
-    if (btn_pb() == BTN_PB_UP) {
-      speed = (speed + 1) & 3;
-      start_fade();
+    if (mode == MODE_SOLID) {
+      if (++color == COLOR_COUNT) {
+        color = 0;
+        if (++mode == MODE_COUNT)
+          mode = 0;
+      }
+    } else if (++mode == MODE_COUNT) {
+      mode = 0;
+      color = 0;
     }
-    btn_pben();
+    start_fade();
   }
 }
 
+void pb_held()
+{
+  /* The button has been held.  Change the color magnitude. */
+  if (bright_up) {
+    if (bright == BRIGHT_TOP)
+      bright_up = 0;
+    else
+      bright++;
+  } else {
+    if (bright == BRIGHT_BOTTOM)
+      bright_up = 1;
+    else
+      bright--;
+  }
+  leds_set();
+}
+
+void pb_held_task()
+{
+  pbHeldCount++;
+  if (pbHeldCount >= 0) {
+    pbHeldCount = 0;
+    pb_held();
+  }
+}
+
+void pb_task()
+{
+  static unsigned char pbPrev = BTN_PB_UP;
+  unsigned char pb;
+
+  pb = btn_pb();
+  if (pbPrev == BTN_PB_UP) {
+    if (pb == BTN_PB_UP)
+      pb_clicked();
+    else /* (pb == BTN_PB_DOWN) */ {
+      tmr_startPeriodic(TMR_BTN_PB_HELD, HELD_TMR_PERIODS);
+      pbHeldCount = -HELD_PERIODS;
+    }
+  } else /* if (pbPrev == BTN_PB_DOWN) */ {
+    if (pb == BTN_PB_UP) {
+      tmr_stop(TMR_BTN_PB_HELD);
+      if (pbHeldCount < 0)
+        pb_clicked();
+    } /* nothing to do if button is down */
+  }
+  pbPrev = pb;
+  btn_pben();
+}
+
 void rs_task()
 {
   btn_rsen();
@@ -190,10 +323,12 @@ void fade_task()
     blu.value += blu.remainder;
     wht.value += wht.remainder;
     tmr_stop(TMR_FADE);
-    tmr_start(TMR_INCOLOR, min_incolor_steps[speed & 3] +
-          (rand() & range_incolor_steps[speed & 3]));
+    if (mode == MODE_FADE)
+      tmr_start(TMR_INCOLOR, std_incolor);
+    else if (mode == MODE_PARTY)
+      tmr_start(TMR_INCOLOR, party_min + (rand() % party_range));
   }
-  leds_set(red, grn, blu, wht);
+  leds_set();
 }
 
 void auto_offon_task()
@@ -209,10 +344,19 @@ 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();
   rs_task();
 }
 
@@ -228,6 +372,9 @@ void user_tasks(unsigned char block)
       case TASK_BTN_RS:         /* rocker switch state change */
         rs_task();
         break;
+      case TASK_BTN_PB_HELD: /* pushbutton is being held down */
+        pb_held_task();
+        break;
       case TASK_FADE:           /* fade timer has fired */
         fade_task();
         break;
index dac07dddb2228fb33851641c10c59efda7761125..c3b31ca18c9ef110e2b554f678ad986de4e5a447 100644 (file)
@@ -19,6 +19,7 @@
 enum {
   TASK_BTN_PB = 0,
   TASK_BTN_RS,
+  TASK_BTN_PB_HELD,
   TASK_FADE,
   TASK_INCOLOR,
   TASK_TMR32,
index ea94e9c16a2e7af2d3fbea94f14ac7c2b0a2b25f..aa37abeee630177f23b736dd89c3284dc708179a 100644 (file)
@@ -16,6 +16,7 @@
 enum {
   TMR_BTN_PB = 0,
   TMR_BTN_RS,
+  TMR_BTN_PB_HELD,
   TMR_AUTO_OFFON,
   TMR_FADE,
   TMR_INCOLOR,