]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - main.c
Fixes to pushbutton handling in main.
[rgblamp.git] / main.c
diff --git a/main.c b/main.c
index 7fa14f2fab32eb238dd7f46eefd16f91731e43d6..9f910dfbe6841e0dd0d29fac0d01418fbd1ca450 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, 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
+ *
  */
 
-
 #include <htc.h>
-//#include <pic16f1827.h>
-
-#define RUNAT32MHZ /* else 16 MHz */
-#define LEDBIT          0x01 /* Port RB0 */
-#define RED_CHAN    1
-#define GRN_CHAN    2
-#define BLU_CHAN    4
-#define WHT_CHAN    8
-#define ALL_CHAN    15
-
-#if defined(RUNAT32MHZ)
-__CONFIG(WDTE_OFF & FOSC_INTOSC);
-__CONFIG(LVP_OFF & PLLEN_ON);
+#include <stdlib.h>
+#include "picinit.h"
+#include "unused.h"
+#include "btn.h"
+#include "rgb.h"
+#include "tmr.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
-__CONFIG(WDTE_OFF);
-__CONFIG(LVP_OFF);
+#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] + \
+                                    (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 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)
 
-void pic_init()
-{
-#if defined(RUNAT32MHZ)
-    OSCCON = 0b11110000;
-#else /* 16 MHz */
-    OSCCON = 0b01111010;
+typedef struct {
+  int value;
+  int increment;
+  signed char remainder;
+} 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
 
-    /* OSCSTAT.HFIOFL is set when oscillator is locked (accurate within 2%) */
-    while (!HFIOFL);
-}
+led_t red;
+led_t grn;
+led_t blu;
+led_t wht;
+bit on;
+unsigned char speed;
+int fade_steps;
 
-void pwm_init()
+void start_fade()
 {
-    /* Initialize PWM
-     * CCP1 on RB3, CCP2 on RA7, CCP3 on RA3, CCP4 on RA4
-     * - Fosc = 32MHz
-     * - Prescale = 16
-     * - PRx value = 0xff
-     * = f(pwm) = 1.95 kHz
-     */
+  /* 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;
 
-    /* Disable output on PWM Rxn pins */
-    TRISA |= 0b10011000;
-    TRISB |= 0b00001000;
-
-    /* Configure ECCP1 */
-    CCP1CON = 0b00001100;
-    CCPR1L = 0; /* Initial PWM value; only using 8 LSBs */
-
-    /* Configure ECCP2 */
-    APFCON0 |= 0b00001000; /* Use alternate output pin RA7 */
-    CCP2CON = 0b00001100;
-    CCPR2L = 0; /* Initial PWM value; only using 8 LSBs */
-
-    /* Configure CCP3 */
-    CCP3CON = 0b00001100;
-    CCPR3L = 0; /* Initial PWM value; only using 8 LSBs */
-
-    /* Configure CCP4 */
-    CCP4CON = 0b00001100;
-    CCPR4L = 0; /* Initial PWM value; only using 8 LSBs */
-
-    /* Configure Timer2 */
-    CCPTMRS = 0; /* All CCPx use Timer 2 */
-    TMR2IF = 0;
-    PR2 = 0xff;
-    T2CON = 0b00000111;
-
-    /* Enable PWM outputs after Timer 2 overflows */
-    while (!TMR2IF);
-    TRISA &= ~0b10011000;
-    TRISB &= ~0b00001000;
+  /* 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 = rand_fade_steps(speed);
+
+  /* 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 led_init()
+void turnOn()
 {
-    PORTB &= ~LEDBIT; /* Led is RB0 */
-    TRISB &= ~LEDBIT;
+  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 led_set(unsigned char led)
+void turnOff()
 {
-    if (led)
-        PORTB |= LEDBIT;
-    else
-        PORTB &= ~LEDBIT;
+  /* Event on to off, either by switch or auto-off timer */
+  tmr_stop(TMR_INCOLOR);
+  tmr_stop(TMR_FADE);
+  rgb_off();
+  dbgpin_low();
+  on = 0;
 }
 
-void pwm_set(unsigned char channels, unsigned char step)
+void pb_task()
 {
-    if (channels & RED_CHAN)
-        CCPR1L = step;
-    if (channels & GRN_CHAN)
-        CCPR2L = step;
-    if (channels & BLU_CHAN)
-        CCPR3L = step;
-    if (channels & WHT_CHAN)
-        CCPR4L = step;
+  /* Is this task running nearly continuously? */
+  if (on) {
+    if (btn_pb() == BTN_PB_UP) {
+      speed = (speed + 1) & 3;
+      start_fade();
+    }
+    btn_pben();
+  }
 }
 
-void delay()
+void rs_task()
 {
-    for (unsigned counter = 0; counter < 10000; counter++);
+  switch (btn_rs()) {
+    case BTN_RS_OFF:
+      tmr_stop(TMR_AUTO_OFFON);
+      turnOff();
+      break;
+    case BTN_RS_RIGHT:
+      tmr_start(TMR_AUTO_OFFON, AUTO_OFF_COUNT);
+      /* fall through */
+    case BTN_RS_LEFT:
+      turnOn();
+      break;
+  }
+  btn_rsen();
 }
 
-void ramp_up(unsigned char channels)
+void fade_task()
 {
-    unsigned char step = 0;
-
-    do {
-        pwm_set(channels, step++);
-        delay();
-    } while (step != 0);
+  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, rand_incolor_steps(speed));
+  }
+  leds_set(red, grn, blu, wht);
 }
 
-void ramp_down(unsigned char channels)
+void auto_offon_task()
 {
-    unsigned char step = 255;
+  if (on) {
+    turnOff();
+    if (btn_rs() == BTN_RS_RIGHT)
+      tmr_start(TMR_AUTO_OFFON, AUTO_ON_COUNT);
+  } else /* off */ {
+    turnOn();
+    if (btn_rs() == BTN_RS_RIGHT)
+      tmr_start(TMR_AUTO_OFFON, AUTO_OFF_COUNT);
+  }
+}
 
-    do {
-        pwm_set(channels, step--);
-        delay();
-    } while (step != 255);
+void user_boot()
+{
+  dbgpin_high();
+  srand((adc_random() << 8) + adc_random());
+  rs_task();
 }
 
-void ramp_up_down(unsigned char channels)
+void user_tasks(unsigned char block)
 {
-    ramp_up(channels);
-    ramp_down(channels);
+  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_AUTO_OFFON:     /* auto on/off timer has fired */
+        auto_offon_task();
+        break;
+    }
+  }
 }
 
 int main(void)
 {
-    unsigned char fwd = 1;
-    unsigned char step = 0;
-
-    pic_init();
-    led_init();
-    pwm_init();
-    while (1) {
-        ramp_up(RED_CHAN);
-        ramp_up(GRN_CHAN);
-        ramp_down(RED_CHAN);
-        ramp_up(BLU_CHAN);
-        ramp_down(GRN_CHAN);
-        ramp_up(RED_CHAN);
-        ramp_up(GRN_CHAN + BLU_CHAN);
-        ramp_down(ALL_CHAN);
-        ramp_up_down(WHT_CHAN);
-    }
-    return 0;
+  /* Platform initialization */
+  pic_init();
+  unused_init();
+  btn_init();
+  rgb_init();
+  tmr_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);
 }
+