]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - main.c
Perceived intensities via the CIE Lighting formula
[rgblamp.git] / main.c
diff --git a/main.c b/main.c
index 9f910dfbe6841e0dd0d29fac0d01418fbd1ca450..cb05d747df629781ae1e3a1647440090f9e93ca9 100644 (file)
--- a/main.c
+++ b/main.c
@@ -3,26 +3,35 @@
  *
  * PWM test program
  *
- * PIC resources in use, 18-pin DIP, by pin:
- * - ( 1) RA2 - unused
- * - ( 2) RA3 - CCP3 PWM for blu LED
- * - ( 3) RA4 - CCP4 PWM for wht LED.  ICSP pin 6, PGM/LVP.
- * - ( 4) RA5 -                        ICSP pin 1, MCLR#/Vpp
- * - ( 5) Vss
- * - ( 6) RB0 - pushbutton
- * - ( 7) RB1 - unused
- * - ( 8) RB2 - unused
- * - ( 9) RB3 - CCP1 PWM for red LED
- * - (10) RB4 - rocker switch position B (right)
- * - (11) RB5 - rocker switch position A (left)
- * - (12) RB6 - unused (future crystal)
- * - (13) RB7 - unused (future crystal)
- * - (14) Vdd
- * - (15) RA6 - unused
- * - (16) RA7 - CCP2 PWM for grn LED
- * - (17) RA0 - unused
- * - (18) RA1 - unused
- *
+ * PIC resources in use, 28-pin DIP, by pin:
+ * - ( 1) RE3 - Vpp/MCLR#
+ * - ( 2) RA0 - unused
+ * - ( 3) RA1 - unused
+ * - ( 4) RA2 - unused
+ * - ( 5) RA3 - unused
+ * - ( 6) RA4 - unused (could become PGM/LVP, but apparently not needed)
+ * - ( 7) RA5 - unused
+ * - ( 8) Vss
+ * - ( 9) RA7 - unused
+ * - (10) RA6 - unused
+ * - (11) RC0 - T1OSO
+ * - (12) RC1 - T1OSI
+ * - (13) RC2 - CCP1 PWM for wht LED
+ * - (14) RC3 - unused
+ * - (15) RC4 - unused
+ * - (16) RC5 - unused
+ * - (17) RC6 - unused
+ * - (18) RC7 - unused
+ * - (19) Vss
+ * - (20) Vdd
+ * - (21) RB0 - CCP4 PWM for blu LED
+ * - (22) RB1 - Pushbutton
+ * - (23) RB2 - Rocker switch, right
+ * - (24) RB3 - CCP2 PWM for grn LED
+ * - (25) RB4 - Rocker switch, left
+ * - (26) RB5 - CCP3 PWM for red LED
+ * - (27) RB6 - ICSPCLK
+ * - (28) RB7 - ICSPDAT
  */
 
 #include <htc.h>
 #include "btn.h"
 #include "rgb.h"
 #include "tmr.h"
+#include "tmr32.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 */
+#if 1
+#define AUTO_OFF_COUNT          1800U  /*  1 hr  in 2 sec units */
+#define AUTO_ON_COUNT           19800U /* 11 hrs in 2 sec 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 */
+#define AUTO_OFF_COUNT          9000U  /*  5 hrs in 2 sec units */
+#define AUTO_ON_COUNT           34200U /* 19 hrs in 2 sec units */
 #endif
-#define rand_u8()               (rand() & 0xff)
-#define rand_u16()              ((rand() << 8) + rand_u8())
-#define rand_incolor_steps(s)   (min_incolor_steps[s & 3] + \
-                                    (rand() % range_incolor_steps[s & 3]))
-#define rand_fade_steps(s)      (min_fade_steps[s & 3] + \
-                                    (rand() % range_fade_steps[s & 3]))
-#define leds_set(r,g,b,w)       rgb_set((r).value >> 7, \
-                                    (g).value >> 7, \
-                                    (b).value >> 7, \
-                                    0)
+#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; \
@@ -67,18 +69,14 @@ typedef struct {
   signed char remainder;
 } led_t;
 
-/* The index of all step arrays is the speed variable */
-#if 1
+/* 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] = { 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
+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 };
 
 led_t red;
 led_t grn;
@@ -105,7 +103,8 @@ void start_fade()
   } while (newr == 0 && newg == 0 && newb == 0 && neww == 0);
 
   /* Random # of steps to reach the new color */
-  fade_steps = rand_fade_steps(speed);
+  fade_steps = min_fade_steps[speed & 3] +
+      (rand() & range_fade_steps[speed & 3]);
 
   /* Compute increment per fade step, and remainder, for each led */
   red.increment = (newr - red.value) / fade_steps;
@@ -143,6 +142,7 @@ void turnOff()
   tmr_stop(TMR_FADE);
   rgb_off();
   dbgpin_low();
+  SLEEP();
   on = 0;
 }
 
@@ -160,19 +160,19 @@ void pb_task()
 
 void rs_task()
 {
+  btn_rsen();
   switch (btn_rs()) {
     case BTN_RS_OFF:
-      tmr_stop(TMR_AUTO_OFFON);
+      tmr32_set(0);
       turnOff();
       break;
     case BTN_RS_RIGHT:
-      tmr_start(TMR_AUTO_OFFON, AUTO_OFF_COUNT);
+      tmr32_set(AUTO_OFF_COUNT);
       /* fall through */
     case BTN_RS_LEFT:
       turnOn();
       break;
   }
-  btn_rsen();
 }
 
 void fade_task()
@@ -190,7 +190,8 @@ void fade_task()
     blu.value += blu.remainder;
     wht.value += wht.remainder;
     tmr_stop(TMR_FADE);
-    tmr_start(TMR_INCOLOR, rand_incolor_steps(speed));
+    tmr_start(TMR_INCOLOR, min_incolor_steps[speed & 3] +
+          (rand() & range_incolor_steps[speed & 3]));
   }
   leds_set(red, grn, blu, wht);
 }
@@ -200,11 +201,11 @@ void auto_offon_task()
   if (on) {
     turnOff();
     if (btn_rs() == BTN_RS_RIGHT)
-      tmr_start(TMR_AUTO_OFFON, AUTO_ON_COUNT);
+      tmr32_set(AUTO_ON_COUNT);
   } else /* off */ {
     turnOn();
     if (btn_rs() == BTN_RS_RIGHT)
-      tmr_start(TMR_AUTO_OFFON, AUTO_OFF_COUNT);
+      tmr32_set(AUTO_OFF_COUNT);
   }
 }
 
@@ -233,7 +234,7 @@ void user_tasks(unsigned char block)
       case TASK_INCOLOR:        /* in-color timer has fired */
         start_fade();
         break;
-      case TASK_AUTO_OFFON:     /* auto on/off timer has fired */
+      case TASK_TMR32:          /* auto on/off event */
         auto_offon_task();
         break;
     }
@@ -248,6 +249,7 @@ int main(void)
   btn_init();
   rgb_init();
   tmr_init();
+  tmr32_init();
   task_init();
   dbgpin_init();