/*
* 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
+ *
*/
-#define RUNAT32MHZ /* else 16 MHz */
-
#include <htc.h>
#include <stdlib.h>
#include "picinit.h"
+#include "unused.h"
#include "buttons.h"
#include "rgb.h"
+#include "tmr.h"
+#include "adc_random.h"
-#define STEP_SIZE 32 /* ms */
#if 0
-#define MIN_INCOLOR_STEPS 320
-#define MAX_INCOLOR_STEPS 57600
-#define MIN_FADE_STEPS 32
-#define MAX_FADE_STEPS 480
+#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 MIN_INCOLOR_STEPS 32
-#define MAX_INCOLOR_STEPS 160
-#define MIN_FADE_STEPS 32
-#define MAX_FADE_STEPS 160
+#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 reset_steps() do { incolor_steps = 1; fade_steps = 0; } while (0)
+#define reset_steps() do { incolor_steps = 1; fade_steps = 0; } \
+ while (0)
#define rand_u8() (rand() & 0xff)
-#define rand_u16() ((rand() << 8) + (rand() & 0xff))
-#define rand_incolor_steps() (MIN_INCOLOR_STEPS + rand_u16() % \
- (MAX_INCOLOR_STEPS - MIN_INCOLOR_STEPS))
-#define rand_fade_steps() (MIN_FADE_STEPS + rand() % \
- (MAX_FADE_STEPS - MIN_FADE_STEPS))
+#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)
-// (w).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)
typedef struct {
int value;
int increment;
- int remainder;
+ signed char remainder;
} led_t;
#define INIT_LED { 0, 0, 0 }
+/* 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
+
int main(void)
{
led_t red = INIT_LED;
led_t grn = INIT_LED;
led_t blu = INIT_LED;
led_t wht = INIT_LED;
- unsigned char fast = 0;
+ unsigned char speed = 0;
unsigned incolor_steps;
int fade_steps;
+ unsigned char buttons, last_buttons;
pic_init();
+ unused_init();
buttons_init();
rgb_init();
- reset_steps();
+ tmr_init();
+ dbgpin_init();
+ srand((adc_random() << 8) + adc_random());
+ reset_steps();
if (buttons_on())
rgb_on();
+ dbgpin_high();
+ last_buttons = 0;
+ buttons = buttons_read();
while (1) {
- unsigned char buttons = buttons_read();
-
- if ((buttons & (IN_ROCKERA | IN_ROCKERB))) {
- if (buttons & IN_PUSHBTN) {
- if (!fast) {
- fast = 1;
- reset_steps();
- } else
- fast = 0;
- }
-
- if (fade_steps) {
- fade_steps--;
- /* Continue the in-progress fade */
- red.value += red.increment;
- grn.value += grn.increment;
- blu.value += blu.increment;
- wht.value += wht.increment;
- if (fade_steps == 0) {
- red.value += red.remainder;
- grn.value += grn.remainder;
- blu.value += blu.remainder;
- wht.value += wht.remainder;
- }
- leds_set(red, grn, blu, wht);
- } else if (--incolor_steps == 0) {
- int newr, newg, newb, neww, tmp;
-
- /* Next led color. All off is not a valid option. */
- do {
- /* RGB values are processed 15-bit values. This allows for
- * signedness when fading to a lesser value. The actual PWM
- * values are set from the most significant 8 bits, so
- * leds_set() does >> 7.
- */
- newr = rand();
- newg = rand();
- newb = rand();
- neww = rand();
- } while (newr == 0 && newg == 0 && newb == 0 && neww == 0);
-
- /* Next incolor and fade steps */
- incolor_steps = rand_incolor_steps();
- fade_steps = (buttons & IN_ROCKERA) ? 1 : rand_fade_steps();
- if (fast) {
- incolor_steps = 1 + incolor_steps / 8;
- if (buttons & IN_ROCKERB)
- fade_steps = 1 + fade_steps / 8;
- }
-
- /* Compute increment 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);
- }
- __delay_ms(STEP_SIZE); /* step should be start to start... */
- } else {
- rgb_off();
- buttons_sleep();
+ /* Wait for an event. Timer firing or button state change.
+ * When we have a crystal, we can sleep instead. Also, soon we
+ * can transition button state changes to the ISR.
+ */
+ dbgpin_low();
+ while (!tmr_events() && last_buttons == buttons) {
+ last_buttons = buttons;
+ buttons = buttons_read();
+ }
+ dbgpin_high();
+
+ if (((buttons & IN_ROCKERB) && !tmr_on(TMR_AUTO_OFFON)) ||
+ !(last_buttons & (IN_ROCKERB | IN_ROCKERA))) {
+ /* Event: off to on */
+ tmr_startPeriodic(TMR_AUTO_OFFON, AUTO_OFF_COUNT);
+ tmr_startPeriodic(TMR_FADE, 1); /* 32.768 msec */
+
+ } else if (((buttons & IN_ROCKERB) && tmr_fired(TMR_AUTO_OFFON)) ||
+ (!(buttons & (IN_ROCKERA | IN_ROCKERB)))) {
+ /* Event on to off, either by switch or auto-off timer */
+ rgb_off();
+ dbgpin_low();
+ tmr_stop(TMR_AUTO_OFFON);
+ tmr_stop(TMR_FADE);
+ buttons_sleep();
+ dbgpin_high();
+ rgb_on();
+ red.value = 0;
+ grn.value = 0;
+ blu.value = 0;
+ wht.value = 0;
+ reset_steps();
+ tmr_startPeriodic(TMR_FADE, 1); /* 32.768 msec */
+
+ } else if (tmr_fired(TMR_FADE)) {
+ /* Other actions, driven by the fade timer for now */
+
+ /* Crappy way to detect rising edges to change state of speed var */
+ if (!(speed & 4) && (buttons & IN_PUSHBTN))
+ speed |= 4;
+ else if ((speed & 4) && !(buttons & IN_PUSHBTN)) {
+ speed = (speed + 1) & ~4;
reset_steps();
- rgb_on();
- red.value = 0;
- grn.value = 0;
- blu.value = 0;
- wht.value = 0;
}
- }
- return 0;
+
+ if (fade_steps) {
+ /* Continue the in-progress fade */
+ fade_steps--;
+ red.value += red.increment;
+ grn.value += grn.increment;
+ blu.value += blu.increment;
+ wht.value += wht.increment;
+ if (fade_steps == 0) {
+ red.value += red.remainder;
+ grn.value += grn.remainder;
+ blu.value += blu.remainder;
+ wht.value += wht.remainder;
+ }
+ leds_set(red, grn, blu, wht);
+
+ } else if (--incolor_steps == 0) {
+ /* 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);
+
+ /* Next incolor and fade steps */
+ incolor_steps = rand_incolor_steps(speed);
+ fade_steps = rand_fade_steps(speed);
+
+ /* Compute increment 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);
+ }
+ }
+ }
+ return 0;
}