]> oss.titaniummirror.com Git - rgblamp.git/commitdiff
Making changes...
authorR. Steve McKown <rsmckown@gmail.com>
Thu, 8 Dec 2011 22:38:32 +0000 (15:38 -0700)
committerR. Steve McKown <rsmckown@gmail.com>
Thu, 8 Dec 2011 22:38:32 +0000 (15:38 -0700)
main.c
tmr.c
tmr.h
tmr_defs.h

diff --git a/main.c b/main.c
index 65cff5565ab684b1d875cdc5e0667a5c10dab461..46fe66aae3e7b118465853ef1ee9e99e5d8f4019 100644 (file)
--- a/main.c
+++ b/main.c
@@ -91,7 +91,7 @@ int main(void)
     unsigned char speed = 0;
     unsigned incolor_steps;
     int fade_steps;
-    unsigned long auto_off = 0;
+    unsigned char buttons, last_buttons;
 
     pic_init();
     unused_init();
@@ -106,85 +106,99 @@ int main(void)
       rgb_on();
 
     dbgpin_high();
-    tmr_startPeriodic(TMR_FADE, 1);  /* 32.768 msec */
+    last_buttons = 0;
+    buttons = buttons_read();
     while (1) {
-        unsigned char buttons = buttons_read();
-
-        if ((buttons & IN_ROCKERB) && auto_off == 0)
-            tmr_startPeriodic(TMR_AUTO_OFFON, AUTO_OFF_COUNT);
-
-        if ((buttons & IN_ROCKERB) && tmr_fired(TMR_AUTO_OFFON) ||
-            (!(buttons & (IN_ROCKERA | IN_ROCKERB)))) {
-            /* Sleep when auto-off time has expired or if rocker switch is
-             * turned off.
-             */
-            rgb_off();
-            dbgpin_low();
-            auto_off = 0;
-            buttons_sleep();
-            dbgpin_high();
+      /* Wait for an event.  Timer firing or button state change.
+       * When we have a crystal, we can sleep instead.  Also, soon we
+       * can transition button state changes to the ISR.
+       */
+      dbgpin_low();
+      while (!tmr_events() && last_buttons == buttons) {
+        last_buttons = buttons;
+        buttons = buttons_read();
+      }
+      dbgpin_high();
+
+      if (((buttons & IN_ROCKERB) && !tmr_on(TMR_AUTO_OFFON)) ||
+          !(last_buttons & (IN_ROCKERB | IN_ROCKERA))) {
+        /* Event: off to on */
+        tmr_startPeriodic(TMR_AUTO_OFFON, AUTO_OFF_COUNT);
+        tmr_startPeriodic(TMR_FADE, 1);  /* 32.768 msec */
+
+      } else if (((buttons & IN_ROCKERB) && tmr_fired(TMR_AUTO_OFFON)) ||
+          (!(buttons & (IN_ROCKERA | IN_ROCKERB)))) {
+        /* Event on to off, either by switch or auto-off timer */
+        rgb_off();
+        dbgpin_low();
+        tmr_stop(TMR_AUTO_OFFON);
+        tmr_stop(TMR_FADE);
+        buttons_sleep();
+        dbgpin_high();
+        rgb_on();
+        red.value = 0;
+        grn.value = 0;
+        blu.value = 0;
+        wht.value = 0;
+        reset_steps();
+        tmr_startPeriodic(TMR_FADE, 1);  /* 32.768 msec */
+
+      } else if (tmr_fired(TMR_FADE)) {
+        /* Other actions, driven by the fade timer for now */
+
+        /* Crappy way to detect rising edges to change state of speed var */
+        if (!(speed & 4) && (buttons & IN_PUSHBTN))
+            speed |= 4;
+        else if ((speed & 4) && !(buttons & IN_PUSHBTN)) {
+            speed = (speed + 1) & ~4;
             reset_steps();
-            rgb_on();
-            red.value = 0;
-            grn.value = 0;
-            blu.value = 0;
-            wht.value = 0;
-        } else {
-            /* Crappy way to detect rising edges to change state of speed var */
-            if (!(speed & 4) && (buttons & IN_PUSHBTN))
-                speed |= 4;
-            else if ((speed & 4) && !(buttons & IN_PUSHBTN)) {
-                speed = (speed + 1) & ~4;
-                reset_steps();
-            }
-
-            if (fade_steps) {
-              fade_steps--;
-              /* Continue the in-progress fade */
-              red.value += red.increment;
-              grn.value += grn.increment;
-              blu.value += blu.increment;
-              wht.value += wht.increment;
-              if (fade_steps == 0) {
-                red.value += red.remainder;
-                grn.value += grn.remainder;
-                blu.value += blu.remainder;
-                wht.value += wht.remainder;
-              }
-              leds_set(red, grn, blu, wht);
-            } else if (--incolor_steps == 0) {
-              int newr, newg, newb, neww;
-
-              /* Next led color.  All off is not a valid option.
-               * RGB values are stored and processed as 15-bit non-negative
-               * integers.  Since the RGB pwm values are 8 bits in width,
-               * leds_set() uses >> 7 to convert each color's value.
-               */
-              do {
-                newr = rand();
-                newg = rand();
-                newb = rand();
-                neww = rand();
-              } while (newr == 0 && newg == 0 && newb == 0 && neww == 0);
-
-              /* Next incolor and fade steps */
-              incolor_steps = rand_incolor_steps(speed);
-              fade_steps = rand_fade_steps(speed);
-
-              /* Compute increment and remainder for each led */
-              red.increment = (newr - red.value) / fade_steps;
-              red.remainder = newr - (red.value + red.increment * fade_steps);
-              grn.increment = (newg - grn.value) / fade_steps;
-              grn.remainder = newg - (grn.value + grn.increment * fade_steps);
-              blu.increment = (newb - blu.value) / fade_steps;
-              blu.remainder = newb - (blu.value + blu.increment * fade_steps);
-              wht.increment = (neww - wht.value) / fade_steps;
-              wht.remainder = neww - (wht.value + wht.increment * fade_steps);
-            }
-            dbgpin_low();
-            while (!tmr_fired(TMR_FADE));
-            dbgpin_high();
         }
-    }
-    return 0;
+
+        if (fade_steps) {
+          /* Continue the in-progress fade */
+          fade_steps--;
+          red.value += red.increment;
+          grn.value += grn.increment;
+          blu.value += blu.increment;
+          wht.value += wht.increment;
+          if (fade_steps == 0) {
+            red.value += red.remainder;
+            grn.value += grn.remainder;
+            blu.value += blu.remainder;
+            wht.value += wht.remainder;
+          }
+          leds_set(red, grn, blu, wht);
+
+        } else if (--incolor_steps == 0) {
+          /* 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.
+           */
+          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);
+
+          /* Next incolor and fade steps */
+          incolor_steps = rand_incolor_steps(speed);
+          fade_steps = rand_fade_steps(speed);
+
+          /* Compute increment and remainder for each led */
+          red.increment = (newr - red.value) / fade_steps;
+          red.remainder = newr - (red.value + red.increment * fade_steps);
+          grn.increment = (newg - grn.value) / fade_steps;
+          grn.remainder = newg - (grn.value + grn.increment * fade_steps);
+          blu.increment = (newb - blu.value) / fade_steps;
+          blu.remainder = newb - (blu.value + blu.increment * fade_steps);
+          wht.increment = (neww - wht.value) / fade_steps;
+          wht.remainder = neww - (wht.value + wht.increment * fade_steps);
+        }
+      }
+  }
+  return 0;
 }
diff --git a/tmr.c b/tmr.c
index fec23f925482a4776f7580fcf254a5cacd3f12d7..aa3f58df722b644e007b1dc98118991827af5354 100644 (file)
--- a/tmr.c
+++ b/tmr.c
 #include "bit.h"
 
 persistent tmr_time_t _tmr_ticks;
+tmr_bitno_t _tmr_on;
+tmr_bitno_t _tmr_periodic;
+static tmr_bitno_t _tmr_fired;
 static tmr_time_t _tmr_t0[TMR_COUNT];
 static tmr_time_t _tmr_elapsed[TMR_COUNT];
-static tmr_bitno_t _tmr_on;
-static tmr_bitno_t _tmr_periodic;
-static tmr_bitno_t _tmr_fired;
 
 /* Timer0 with 1:256 prescale given Fosc (_XTAL_FREQ).
  * Timer0 is clocked by Fosc/4.
diff --git a/tmr.h b/tmr.h
index 716ff65a6d5b68d24f6f937efe5d100811bb74c0..5e554316b084aae2f9e32e42bb871463e1ca9a0b 100644 (file)
--- a/tmr.h
+++ b/tmr.h
 #include "isr.h"
 #include "bit.h"
 
-/* Only access when in ISR or if interrupts are disabled */
+/* Used by 'inline' functions, etc.  User code should not access _tmr_ticks
+ * directly unless interrupts are off; otherwise use tmr_time().
+ */
 extern persistent tmr_time_t _tmr_ticks;
+extern tmr_bitno_t _tmr_on;
+extern tmr_bitno_t _tmr_periodic;
 
 /* Initialize the tmr subsystem */
 void tmr_init();
 
+/* Return non-zero if any timer events are pending */
+#define tmr_events() (_tmr_on)
+
 /* Return non-zero if the timer is on */
 /* FIXME: this may not be atomic WRT ISR */
 #define tmr_on(t) (bit_get(_tmr_on, (t)))
index cc6971165875c54f36f9606da196f49286934fe8..830da3eb33fff78d4df7c264551ccd21b3824711 100644 (file)
@@ -16,6 +16,7 @@
 enum {
   TMR_AUTO_OFFON = 0,
   TMR_FADE,
+  TMR_CHANGE,
   TMR_DIM,
   TMR_BTN,
   TMR_ROCKER,