]> 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 4d11ee352810a4d6f5874eb9d69df1eaf4dfc951..cb05d747df629781ae1e3a1647440090f9e93ca9 100644 (file)
--- a/main.c
+++ b/main.c
 /*
  * File:   main.c
  *
- * Created on August 16, 2010, 12:09 PM
+ * PWM test program
+ *
+ * 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
  */
 
-#define RUNAT32MHZ /* else 16 MHz */
-
 #include <htc.h>
 #include <stdlib.h>
 #include "picinit.h"
-#include "buttons.h"
+#include "unused.h"
+#include "btn.h"
 #include "rgb.h"
+#include "tmr.h"
+#include "tmr32.h"
+#include "task.h"
+#include "adc_random.h"
 
-#define random_rgb()        rand()         /* ~ (0...255) << 7 */
-#define random_time()       (rand() >> 7)  /* 0...255 */
-#define min_incolor_time()  100
-#define min_fade_time()     100
+#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          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_high() (RA2 = 1)
+#define dbgpin_low()  (RA2 = 0)
+#define dbgpin_toggle()  (RA2 = (LATA2 == 0) ? 1 : 0)
 
-int main(void)
+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 };
+
+led_t red;
+led_t grn;
+led_t blu;
+led_t wht;
+bit on;
+unsigned char speed;
+int fade_steps;
+
+void start_fade()
+{
+  /* 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);
+
+  /* Random # of steps to reach the new color */
+  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;
+  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);
+
+  /* Start the fade timer */
+  tmr_stop(TMR_INCOLOR);
+  tmr_startPeriodic(TMR_FADE, 1);  /* 32.768 msec */
+}
+
+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();
+}
+
+void turnOff()
+{
+  /* Event on to off, either by switch or auto-off timer */
+  tmr_stop(TMR_INCOLOR);
+  tmr_stop(TMR_FADE);
+  rgb_off();
+  dbgpin_low();
+  SLEEP();
+  on = 0;
+}
+
+void pb_task()
+{
+  /* Is this task running nearly continuously? */
+  if (on) {
+    if (btn_pb() == BTN_PB_UP) {
+      speed = (speed + 1) & 3;
+      start_fade();
+    }
+    btn_pben();
+  }
+}
+
+void rs_task()
 {
-    int red = 0, grn = 0, blu = 0; //, wht = 0;
-    int fadestep_red, fadestep_grn, fadestep_blu; //, fadestep_wht;
-    unsigned incolor_time = 0;
-    unsigned fade_time = 0;
-    unsigned char fast = 0;
-
-    pic_init();
-    buttons_init();
-    rgb_init();
-    srand(27);
-
-    if (buttons_on())
-      rgb_on();
-
-    while (1) {
-        unsigned char buttons = buttons_read();
-
-        if ((buttons & (IN_ROCKERA | IN_ROCKERB))) {
-            if (buttons & IN_PUSHBTN) {
-              if (!fast) {
-                fast = 1;
-                incolor_time = 0;
-                fade_time = 0;
-              } else
-                fast = 0;
-            }
-
-            if (fade_time) {
-              red += fadestep_red;
-              grn += fadestep_grn;
-              blu += fadestep_blu;
-              //wht += fadestep_wht;
-              rgb_set(red >> 7, grn >> 7, blu >> 7, 0); // wht >> 7);
-              fade_time--;
-            } else if (incolor_time)
-              incolor_time--;
-            else /* fade_time == 0 && incolor_time == 0 */ {
-              int newr, newg, newb, neww;
-
-              /* Determine next color state */
-              newr = random_rgb();
-              newg = random_rgb();
-              newb = random_rgb();
-              //neww = random_rgb();
-
-              /* Calculate new incolor_time and fade_time */
-              if (buttons & IN_ROCKERA) {
-                incolor_time = 1 + random_time();
-                fade_time = 1;
-              } else /* (buttons & IN_ROCKERB) */ {
-                incolor_time = min_incolor_time() + random_time() / 2;
-                fade_time = min_fade_time() + random_time() / 2;
-              }
-
-              if (fast) {
-                incolor_time = 1 + incolor_time / 8;
-                fade_time = 1 + fade_time / 8;
-              }
-
-              fadestep_red = (newr - red) / fade_time;
-              fadestep_grn = (newg - red) / fade_time;
-              fadestep_blu = (newb - red) / fade_time;
-              //fadestep_wht = (neww - red) / fade_time;
-            }
-            __delay_ms(50);
-        } else {
-            rgb_off();
-            buttons_sleep();
-            incolor_time = 0;
-            fade_time = 0;
-            rgb_on();
-        }
+  btn_rsen();
+  switch (btn_rs()) {
+    case BTN_RS_OFF:
+      tmr32_set(0);
+      turnOff();
+      break;
+    case BTN_RS_RIGHT:
+      tmr32_set(AUTO_OFF_COUNT);
+      /* fall through */
+    case BTN_RS_LEFT:
+      turnOn();
+      break;
+  }
+}
+
+void fade_task()
+{
+  red.value += red.increment;
+  grn.value += grn.increment;
+  blu.value += blu.increment;
+  wht.value += wht.increment;
+  if (--fade_steps == 0) {
+    /* This is the last fade step.  Finalize the RGB led states and decide how
+     * long to stay in this color.
+     */
+    red.value += red.remainder;
+    grn.value += grn.remainder;
+    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]));
+  }
+  leds_set(red, grn, blu, wht);
+}
+
+void auto_offon_task()
+{
+  if (on) {
+    turnOff();
+    if (btn_rs() == BTN_RS_RIGHT)
+      tmr32_set(AUTO_ON_COUNT);
+  } else /* off */ {
+    turnOn();
+    if (btn_rs() == BTN_RS_RIGHT)
+      tmr32_set(AUTO_OFF_COUNT);
+  }
+}
+
+void user_boot()
+{
+  dbgpin_high();
+  srand((adc_random() << 8) + adc_random());
+  rs_task();
+}
+
+void user_tasks(unsigned char block)
+{
+  task_id_t tid;
+
+  while ((tid = task_get(block)) >= 0) {
+    switch (tid) {
+      case TASK_BTN_PB:         /* pushbutton state change */
+        pb_task();
+        break;
+      case TASK_BTN_RS:         /* rocker switch state change */
+        rs_task();
+        break;
+      case TASK_FADE:           /* fade timer has fired */
+        fade_task();
+        break;
+      case TASK_INCOLOR:        /* in-color timer has fired */
+        start_fade();
+        break;
+      case TASK_TMR32:          /* auto on/off event */
+        auto_offon_task();
+        break;
     }
-    return 0;
+  }
+}
+
+int main(void)
+{
+  /* Platform initialization */
+  pic_init();
+  unused_init();
+  btn_init();
+  rgb_init();
+  tmr_init();
+  tmr32_init();
+  task_init();
+  dbgpin_init();
+
+#if 0
+  /* Execute tasks until the queue empties */
+  user_tasks(0);
+#endif
+
+#if 0
+  /* Software initialization */
+#endif
+
+#if 0
+  /* Execute tasks until the queue empties */
+  user_tasks(0);
+#endif
+
+  /* Run user boot code */
+  user_boot();
+
+  /* Process tasks forever */
+  user_tasks(1);
+
+  /* Prevent return from main, which causes a device reset */
+  while (1);
 }
+