+/*
+ * Copyright © 2011, 2012, Titanium Mirror, Inc..
+ * All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - Neither the name of Titanium Mirror, Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
/*
* File: main.c
*
* 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 <stdlib.h>
#include "picinit.h"
#include "unused.h"
-#include "buttons.h"
+#include "lfsr.h"
+#include "btn.h"
#include "rgb.h"
-#include "timer.h"
+#include "tmr.h"
+#include "tmr32.h"
+#include "task.h"
#include "adc_random.h"
-#define AUTO_OFF_COUNT 549316UL /* 5 hrs in 32.768 ms units */
-#define reset_steps() do { incolor_steps = 1; fade_steps = 0; } \
- while (0)
-#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 */ \
- PORTA &= ~0x04; \
- TRISA &= ~0x04; \
-} while (0)
-
-#define dbgpin_high() PORTA |= 0x04;
-#define dbgpin_low() PORTA &= ~0x04;
+#define AUTO_OFF_COUNT 9000U /* 5 hrs in 2 sec units */
+#define AUTO_ON_COUNT 34200U /* 19 hrs in 2 sec units */
+#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)
+
+enum {
+ /* Operating modes */
+ MODE_SOLID = 0, /* Cycle through colors[][] before next mode */
+ MODE_CYCLE, /* Auto cycle through colors */
+ MODE_PARTY, /* Random yet fast incolor and fade */
+ MODE_COUNT,
+
+ /* Indexes in colors[][] for LEDs */
+ LED_RED = 0,
+ LED_GRN,
+ LED_BLU,
+ LED_WHT,
+ LED_COUNT,
+
+ HELD_TMR_PERIODS = 1, /* Held timer fires every 32.768 msec */
+ HELD_PERIODS = 16, /* Held this long before held events fire */
+ COLOR_COUNT = 12,
+ COLOR_CANDLE = 0, /* Color index for the candle color */
+ COLOR_WHITE = 11, /* Color index for white */
+ BRIGHT_TOP = 100, /* Bright ramps up to top ... */
+ BRIGHT_BOTTOM = -5, /* ... then down to bottom */
+ BRIGHT_MAX = 85, /* Bright values are constrained for computations */
+ BRIGHT_MIN = 1, /* ... by min and max. */
+ BRIGHT_INIT = 42, /* Initial value is replaced by value from eeprom */
+ CBRIGHT_VARIANCE = 20, /* Brightness range for candle flicker */
+ STD_FADE = 16, /* Fade time in 32.768 ms units */
+ STD_INCOLOR = 29491, /* Time in color when MODE_CYCLE, in 32.768 ms units */
+ PARTY_MIN = 8, /* Min party fade and incolor units */
+ PARTY_RANGE = 8, /* Party fade/incolor range mask for lfsr_get() */
+ /* ... see start_fade() */
+ CFG_ADDR = 0, /* EEPROM address of start of configuration */
+ CFG_DELAY = 183, /* 6 seconds in 32.768 msec units */
+};
typedef struct {
int value;
int increment;
- char 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)
+/* Offsets to brightness used for candle color */
+const signed char offsets[4] = { -1, -1, 1, 1 };
+
+/* Available colors, 6 bits per color (no values above 0x3f) */
+const unsigned char colors[COLOR_COUNT][LED_COUNT] = {
+ { 0x2c, 0x20, 0x00, 0x18 }, /* candle */
+ { 0x28, 0x00, 0x00, 0x00 }, /* red */
+ { 0x30, 0x20, 0x00, 0x00 }, /* orange */
+ { 0x2c, 0x2c, 0x00, 0x00 }, /* yellow */
+ { 0x20, 0x2c, 0x00, 0x00 }, /* yellow-green */
+ { 0x00, 0x30, 0x00, 0x00 }, /* green */
+ { 0x00, 0x30, 0x18, 0x00 }, /* green-cyan */
+ { 0x00, 0x30, 0x28, 0x00 }, /* cyan */
+ { 0x00, 0x18, 0x30, 0x00 }, /* cyan-blue */
+ { 0x00, 0x00, 0x30, 0x00 }, /* blue */
+ { 0x1c, 0x00, 0x30, 0x00 }, /* magenta */
+ { 0x3f, 0x3f, 0x3f, 0x3f }, /* white */
+};
+
+unsigned char mode = MODE_SOLID;
+unsigned char color = COLOR_CANDLE;
+signed char bright = BRIGHT_INIT;
+bit bright_up;
+led_t red;
+led_t grn;
+led_t blu;
+led_t wht;
+bit on;
+int fade_steps;
+signed char pbHeldCount;
+signed char cbright;
+
+void cfg_write()
+{
+ unsigned char addr = CFG_ADDR;
+ unsigned char m, c;
+ signed char b;
+
+ m = eeprom_read(addr++);
+ c = eeprom_read(addr++);
+ b = eeprom_read(addr);
+ if (mode != m || color != c || bright != b) {
+ addr = CFG_ADDR;
+ eeprom_write(addr++, mode);
+ eeprom_write(addr++, color);
+ eeprom_write(addr, bright);
+ }
+}
+
+void cfg_read()
+{
+ unsigned char addr = CFG_ADDR;
+ unsigned char tmp;
+
+ tmp = eeprom_read(addr++);
+ if (tmp < MODE_COUNT)
+ mode = tmp;
+
+ tmp = eeprom_read(addr++);
+ if (tmp < COLOR_COUNT)
+ color = tmp;
+
+ tmp = eeprom_read(addr);
+ if ((signed char)tmp >= BRIGHT_BOTTOM && (signed char)tmp <= BRIGHT_TOP)
+ bright = tmp;
+}
+
+/* Combine LED color value and brightness level to generate an RGB value.
+ * bright will be BRIGHT_MIN...BRIGHT_MAX
+ *
+ * @param value 0...1023 (6 bit color, left shifted 4 bits, for 10 bits)
+ * @param b brightness value BRIGHT_MIN...BRIGHT_MAX
+ * @return An RGB drive value, 0...255
+ */
+unsigned char led_get(int value, signed char b)
+{
+ return (unsigned long)value * b / 4 / BRIGHT_MAX;
+}
+
+/* Set the LEDs using color values and current brightness. */
+void leds_set()
+{
+ signed char b;
+
+ /* Use correct brightness value, then constrain it */
+ if (mode != MODE_PARTY && color != COLOR_WHITE)
+ b = cbright;
+ else
+ b = bright;
+ if (b > BRIGHT_MAX)
+ b = BRIGHT_MAX;
+ else if (b < BRIGHT_MIN)
+ b = BRIGHT_MIN;
+
+ rgb_set(led_get((red).value, b), led_get((grn).value, b),
+ led_get((blu).value, b), led_get((wht).value, b));
+}
+
+void start_fade()
+{
+ /* RGB PWM values are 8 bits. Color values are 12 bits. See led_get()
+ * for the conversion of color values and brightness to RGB values.
+ */
+ int newr, newg, newb, neww;
+
+ /* Select the destination color and fade-to time depending upon mode. */
+ fade_steps = STD_FADE;
+ if (mode == MODE_PARTY) {
+ color = lfsr_get() % COLOR_COUNT;
+ fade_steps = PARTY_MIN + (lfsr_get() % PARTY_RANGE);
+ } else if (mode == MODE_CYCLE) {
+ if (++color == COLOR_COUNT)
+ color = 0;
+ }
+
+ /* Retrieve the destination color */
+ newr = colors[color][LED_RED] << 4;
+ newg = colors[color][LED_GRN] << 4;
+ newb = colors[color][LED_BLU] << 4;
+ neww = colors[color][LED_WHT] << 4;
+
+ /* 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_CANDLE);
+ tmr_stop(TMR_INCOLOR);
+ tmr_startPeriodic(TMR_FADE, 1); /* 32.768 msec */
+}
+
+void turnOn()
+{
+ dbgpin_high();
+ on = 1;
+ rgb_on();
+ start_fade();
+ btn_pben();
+}
+
+/* Event on to off, either by switch or auto-off timer */
+void turnOff()
+{
+ tmr_stop(TMR_CFG);
+ tmr_stop(TMR_CANDLE);
+ tmr_stop(TMR_INCOLOR);
+ tmr_stop(TMR_FADE);
+ rgb_off();
+ red.value = grn.value = blu.value = wht.value = 0;
+ on = 0;
+ dbgpin_low();
+ SLEEP();
+}
+
+void pb_clicked()
{
- led_t red = INIT_LED;
- led_t grn = INIT_LED;
- led_t blu = INIT_LED;
- led_t wht = INIT_LED;
- unsigned char speed = 0;
- unsigned incolor_steps;
- int fade_steps;
- unsigned long auto_off = 0;
-
- pic_init();
- unused_init();
- buttons_init();
- rgb_init();
- timer_init();
- dbgpin_init();
-
- srand((adc_random() << 8) + adc_random());
- reset_steps();
- if (buttons_on())
- rgb_on();
-
- dbgpin_high();
- while (1) {
- unsigned char buttons = buttons_read();
-
- if ((buttons & IN_ROCKERB) && auto_off == 0)
- auto_off = AUTO_OFF_COUNT;
-
- if (((buttons & IN_ROCKERB) && auto_off && --auto_off == 0) ||
- (!(buttons & (IN_ROCKERA | IN_ROCKERB)))) {
- /* Sleep when auto-off time has expired or if rocker switch is
- * turned off.
- */
- rgb_off();
- dbgpin_low();
- auto_off = 0;
- buttons_sleep();
- dbgpin_high();
- reset_steps();
- rgb_on();
- red.value = 0;
- grn.value = 0;
- blu.value = 0;
- wht.value = 0;
- } else {
- /* 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();
- }
-
- 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.
- * RGB values are stored and processed as 15-bit non-negative
- * integers. Since the RGB pwm values are 8 bits in width,
- * leds_set() uses >> 7 to convert each color's value.
- */
- 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);
- }
- dbgpin_low();
- timer_owait(); /* wait 32 ms since last return from last call() */
- dbgpin_high();
- }
+ if (on) {
+ if (mode == MODE_SOLID) {
+ if (++color == COLOR_COUNT) {
+ color = 0;
+ if (++mode == MODE_COUNT)
+ mode = 0;
+ }
+ } else if (++mode == MODE_COUNT) {
+ mode = 0;
+ color = 0;
}
- return 0;
+ tmr_start(TMR_CFG, CFG_DELAY);
+ start_fade();
+ }
}
+
+/* The button is being held. Change the color magnitude. */
+void pb_held()
+{
+ if (bright_up) {
+ if (bright == BRIGHT_TOP)
+ bright_up = 0;
+ else
+ bright++;
+ } else {
+ if (bright == BRIGHT_BOTTOM)
+ bright_up = 1;
+ else
+ bright--;
+ }
+ tmr_start(TMR_CFG, CFG_DELAY);
+ leds_set();
+}
+
+void pb_held_task()
+{
+ pbHeldCount++;
+ if (pbHeldCount >= 0) {
+ pbHeldCount = 0;
+ pb_held();
+ }
+}
+
+void pb_task()
+{
+ static unsigned char pbPrev = BTN_PB_UP;
+ unsigned char pb;
+
+ pb = btn_pb();
+ if (pbPrev == BTN_PB_UP) {
+ if (pb == BTN_PB_UP)
+ pb_clicked();
+ else /* (pb == BTN_PB_DOWN) */ {
+ tmr_startPeriodic(TMR_BTN_PB_HELD, HELD_TMR_PERIODS);
+ pbHeldCount = -HELD_PERIODS;
+ }
+ } else /* if (pbPrev == BTN_PB_DOWN) */ {
+ if (pb == BTN_PB_UP) {
+ tmr_stop(TMR_BTN_PB_HELD);
+ if (pbHeldCount < 0)
+ pb_clicked();
+ } /* nothing to do if button is down */
+ }
+ pbPrev = pb;
+ btn_pben();
+}
+
+void rs_task()
+{
+ 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 candle_task()
+{
+ /* Flicker by using the brightness value */
+ cbright += offsets[lfsr_get() & 0x03];
+
+ /* Constraight cbright to a range about bright */
+ if (cbright > bright)
+ cbright = bright;
+ else if (cbright < bright - CBRIGHT_VARIANCE)
+ cbright = bright - CBRIGHT_VARIANCE;
+
+ /* Adjust the LEDs */
+ leds_set();
+}
+
+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_CANDLE);
+ tmr_stop(TMR_FADE);
+ if (mode == MODE_PARTY)
+ tmr_start(TMR_INCOLOR, PARTY_MIN + (lfsr_get() % PARTY_RANGE));
+ else {
+ if (mode == MODE_CYCLE)
+ tmr_start(TMR_INCOLOR, STD_INCOLOR);
+ if (color != COLOR_WHITE) {
+ cbright = bright - CBRIGHT_VARIANCE / 2;
+ tmr_startPeriodic(TMR_CANDLE, 1);
+ }
+ }
+ }
+ leds_set();
+}
+
+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();
+ lfsr_init((adc_random() << 8) + adc_random());
+ cfg_read();
+ rs_task();
+}
+
+void user_tasks(unsigned char block)
+{
+ task_id_t tid;
+
+ while ((tid = task_get(block)) >= 0) {
+ switch (tid) {
+ case TASK_BTN_PB:
+ pb_task();
+ break;
+ case TASK_BTN_RS:
+ rs_task();
+ break;
+ case TASK_BTN_PB_HELD:
+ pb_held_task();
+ break;
+ case TASK_FADE:
+ fade_task();
+ break;
+ case TASK_INCOLOR:
+ start_fade();
+ break;
+ case TASK_CANDLE:
+ candle_task();
+ break;
+ case TASK_TMR32:
+ auto_offon_task();
+ break;
+ case TASK_CFG:
+ cfg_write();
+ break;
+ }
+ }
+}
+
+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);
+}
+