]> oss.titaniummirror.com Git - rgblamp.git/commitdiff
Implement auto-off
authorR. Steve McKown <rsmckown@gmail.com>
Wed, 7 Dec 2011 02:09:17 +0000 (19:09 -0700)
committerR. Steve McKown <rsmckown@gmail.com>
Wed, 7 Dec 2011 02:09:17 +0000 (19:09 -0700)
After 5 hours, approximately, the lamp will automatically turn off.
Toggle the rocker to wake it back up.  An auto-wake feature will
be added later.

main.c

diff --git a/main.c b/main.c
index 232cc6a3a40a205063748e394332ac7db6999a0b..840d83aab4542727f239d6614fca593239885f84 100644 (file)
--- a/main.c
+++ b/main.c
@@ -33,6 +33,7 @@
 #include "rgb.h"
 #include "timer.h"
 
+#define AUTO_OFF_COUNT          549316UL /* 5 hrs in 32.768 ms units */
 #define reset_steps()           do { incolor_steps = 1; fade_steps = 0; } \
                                   while (0)
 #define rand_u8()               (rand() & 0xff)
                                   (b).value >> 7, \
                                   0)
 
+#define dbgpin_init() \
+do { \
+  /* Set RA2 as output low */ \
+  PORTA &= ~0x04; \
+  TRISA &= ~0x04; \
+} while (0)
+
+#define dbgpin_high() PORTA |= 0x04;
+#define dbgpin_low()  PORTA &= ~0x04;
+
 typedef struct {
   int value;
   int increment;
@@ -66,16 +77,6 @@ const static int min_fade_steps[4] =           { 64, 32, 16,  8 };
 const static int range_fade_steps[4] =         {  1,  1,  1,  1 };
 #endif
 
-#define dbgpin_init() \
-do { \
-  /* Set RA2 as output low */ \
-  PORTA &= ~0x04; \
-  TRISA &= ~0x04; \
-} while (0)
-
-#define dbgpin_high() PORTA |= 0x04;
-#define dbgpin_low()  PORTA &= ~0x04;
-
 int main(void)
 {
     led_t red = INIT_LED;
@@ -85,6 +86,8 @@ int main(void)
     unsigned char speed = 0;
     unsigned incolor_steps;
     int fade_steps;
+    unsigned long auto_off = 0;
+
 
     pic_init();
     unused_init();
@@ -101,11 +104,30 @@ int main(void)
     while (1) {
         unsigned char buttons = buttons_read();
 
-        if ((buttons & (IN_ROCKERA | IN_ROCKERB))) {
+        if ((buttons & IN_ROCKERB) && auto_off == 0)
+            auto_off = AUTO_OFF_COUNT;
+
+        if (((buttons & IN_ROCKERB) && auto_off && --auto_off == 0) ||
+            (!(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();
+            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)) {
+            if (!(speed & 4) && (buttons & IN_PUSHBTN))
                 speed |= 4;
-            else if ((speed & 4) && !(buttons & IN_PUSHBTN)) {
+            else if ((speed & 4) && !(buttons & IN_PUSHBTN)) {
                 speed = (speed + 1) & ~4;
                 reset_steps();
             }
@@ -156,17 +178,6 @@ int main(void)
             dbgpin_low();
             timer_owait(); /* wait 32 ms since last return from last call() */
             dbgpin_high();
-        } else {
-            rgb_off();
-            dbgpin_low();
-            buttons_sleep();
-            dbgpin_high();
-            reset_steps();
-            rgb_on();
-            red.value = 0;
-            grn.value = 0;
-            blu.value = 0;
-            wht.value = 0;
         }
     }
     return 0;