]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - main.c
Remove debug auto off/on and fade/incolor options
[rgblamp.git] / main.c
diff --git a/main.c b/main.c
index 637879f2cab0c18349df39617866dc8eea61c4fe..ac75428bce7ca04780b8eae92afbd45dbd24d986 100644 (file)
--- a/main.c
+++ b/main.c
 #include "btn.h"
 #include "rgb.h"
 #include "tmr.h"
-#include "adc_random.h"
 #include "task.h"
+#include "adc_random.h"
 
-#if 0
 #define AUTO_OFF_COUNT          549316UL  /*  5 hrs in 32.768 ms units */
 #define AUTO_ON_COUNT           2087402UL /* 19 hrs in 32.768 ms units */
-#else
-#define AUTO_OFF_COUNT          1831 /* 1 minute in 32.768 ms units */
-#define AUTO_ON_COUNT           3662 /* 2 minutes in 32.768 ms units */
-#endif
 #define rand_u8()               (rand() & 0xff)
 #define rand_u16()              ((rand() << 8) + rand_u8())
 #define rand_incolor_steps(s)   (min_incolor_steps[s & 3] + \
@@ -68,17 +63,10 @@ typedef struct {
 } led_t;
 
 /* The index of all step arrays is the speed variable */
-#if 1
 const static unsigned min_incolor_steps[4] =   {   320,  32, 32,  1 };
 const static unsigned range_incolor_steps[4] = { 32768, 128, 32,  8 };
 const static int min_fade_steps[4] =           {    64,  32, 32,  1 };
 const static int range_fade_steps[4] =         {   416, 128, 32,  8 };
-#else /* for debugging */
-const static unsigned min_incolor_steps[4] =   { 64, 32, 16,  8 };
-const static unsigned range_incolor_steps[4] = {  1,  1,  1,  1 };
-const static int min_fade_steps[4] =           { 64, 32, 16,  8 };
-const static int range_fade_steps[4] =         {  1,  1,  1,  1 };
-#endif
 
 led_t red;
 led_t grn;
@@ -118,6 +106,7 @@ void start_fade()
   wht.remainder = neww - (wht.value + wht.increment * fade_steps);
 
   /* Start the fade timer */
+  tmr_stop(TMR_INCOLOR);
   tmr_startPeriodic(TMR_FADE, 1);  /* 32.768 msec */
 }
 
@@ -125,12 +114,14 @@ void turnOn()
 {
   dbgpin_high();
   on = 1;
-  rgb_on();
   red.value = 0;
   grn.value = 0;
   blu.value = 0;
   wht.value = 0;
+  leds_set(red, grn, blu, wht);
+  rgb_on();
   start_fade();
+  btn_pben();
 }
 
 void turnOff()
@@ -145,11 +136,14 @@ void turnOff()
 
 void pb_task()
 {
-  if (btn_pb() == BTN_PB_UP) {
-    speed = (speed + 1) & ~4;
-    fade_steps = 0;
+  /* Is this task running nearly continuously? */
+  if (on) {
+    if (btn_pb() == BTN_PB_UP) {
+      speed = (speed + 1) & 3;
+      start_fade();
+    }
+    btn_pben();
   }
-  btn_pben();
 }
 
 void rs_task()
@@ -189,16 +183,16 @@ void fade_task()
   leds_set(red, grn, blu, wht);
 }
 
-void auto_onoff_task()
+void auto_offon_task()
 {
   if (on) {
     turnOff();
     if (btn_rs() == BTN_RS_RIGHT)
-      tmr_start(TMR_AUTO_OFFON, AUTO_OFF_COUNT);
+      tmr_start(TMR_AUTO_OFFON, AUTO_ON_COUNT);
   } else /* off */ {
     turnOn();
     if (btn_rs() == BTN_RS_RIGHT)
-      tmr_start(TMR_AUTO_OFFON, AUTO_ON_COUNT);
+      tmr_start(TMR_AUTO_OFFON, AUTO_OFF_COUNT);
   }
 }
 
@@ -206,7 +200,6 @@ void user_boot()
 {
   dbgpin_high();
   srand((adc_random() << 8) + adc_random());
-  pb_task();
   rs_task();
 }
 
@@ -214,7 +207,7 @@ void user_tasks(unsigned char block)
 {
   task_id_t tid;
 
-  while ((tid = task_get(block))) {
+  while ((tid = task_get(block)) >= 0) {
     switch (tid) {
       case TASK_BTN_PB:         /* pushbutton state change */
         pb_task();
@@ -229,7 +222,7 @@ void user_tasks(unsigned char block)
         start_fade();
         break;
       case TASK_AUTO_OFFON:     /* auto on/off timer has fired */
-        auto_onoff_task();
+        auto_offon_task();
         break;
     }
   }
@@ -237,6 +230,7 @@ void user_tasks(unsigned char block)
 
 int main(void)
 {
+  /* Platform initialization */
   pic_init();
   unused_init();
   btn_init();
@@ -264,5 +258,8 @@ int main(void)
 
   /* Process tasks forever */
   user_tasks(1);
+
+  /* Prevent return from main, which causes a device reset */
+  while (1);
 }