#include <stdlib.h>
#include "picinit.h"
#include "unused.h"
+#include "lfsr.h"
#include "btn.h"
#include "rgb.h"
#include "tmr.h"
#include "task.h"
#include "adc_random.h"
-#define AUTO_OFF_COUNT 150 /* 5 mins on*/
-#define AUTO_ON_COUNT 300 /* 10 mins off */
-//#define AUTO_OFF_COUNT 450 /* 15 mins on*/
-//#define AUTO_ON_COUNT 1350 /* 45 mins off */
-//#define AUTO_OFF_COUNT 600 /* 20 mins on*/
-//#define AUTO_ON_COUNT 1200 /* 40 mins off */
-//#define AUTO_OFF_COUNT 450 /* 2 hours on*/
-//#define AUTO_ON_COUNT 1350 /* 22 hours off */
-//#define AUTO_OFF_COUNT 9000U /* 5 hrs in 2 sec units */
-//#define AUTO_ON_COUNT 34200U /* 19 hrs in 2 sec units */
+#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; \
enum {
/* Operating modes */
MODE_SOLID = 0, /* Cycle through colors[][] before next mode */
- //MODE_CANDLE,
- MODE_FADE, /* Auto cycle through colors */
+ MODE_CYCLE, /* Auto cycle through colors */
MODE_PARTY, /* Random yet fast incolor and fade */
MODE_COUNT,
HELD_TMR_PERIODS = 1, /* Held timer fires every 32.768 msec */
HELD_PERIODS = 16, /* Held this long before held events fire */
- COLOR_WHITE = 0, /* Color index for white */
- COLOR_COUNT = 11,
+ COLOR_COUNT = 12,
+ COLOR_CANDLE = 0, /* Color index for white */
+ 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, /* later, get 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_FADE, 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 rand() */
+ 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 {
signed char remainder;
} led_t;
+/* 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 */
};
unsigned char mode = MODE_SOLID;
-unsigned char color = COLOR_WHITE;
+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 bright = BRIGHT_INIT;
-bit bright_up;
signed char pbHeldCount;
+signed char cbright;
-/* Return the constrained brightness level for use in computing RGB values */
-unsigned char bright_get()
+void cfg_write()
{
- if (bright > BRIGHT_MAX)
- return BRIGHT_MAX;
- else if (bright < BRIGHT_MIN)
- return BRIGHT_MIN;
- else
- return bright;
+ 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);
+ }
}
-/* RGB values will not illuminate the LED */
-unsigned char led_get(unsigned value)
+void cfg_read()
{
- value = (value * bright_get()) >> 8;
-#ifdef CEIL256
- if (value && value < 0x26)
- return 0x26;
-#else
- if (value && value < 4)
- return 4;
-#endif
- return value;
+ 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 both color and brightness values. */
+/* Set the LEDs using color values and current brightness. */
void leds_set()
{
- rgb_set(led_get((red).value),
- led_get((grn).value), led_get((blu).value),
- led_get((wht).value));
+ signed char b;
+
+ /* Use correct brightness value, then constrain it */
+ if (mode == MODE_SOLID && color == COLOR_CANDLE)
+ 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, but computations are done in 15
- * (leaving room for a sign bit), so leds_set() uses >>7 to convert
- * to PWM values.
+ /* 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 = rand() % COLOR_COUNT;
- fade_steps = PARTY_MIN + (rand() % PARTY_RANGE);
- } else if (mode == MODE_FADE) {
+ color = lfsr_get() % COLOR_COUNT;
+ fade_steps = PARTY_MIN + (lfsr_get() % PARTY_RANGE);
+ } else if (mode == MODE_CYCLE) {
if (++color == COLOR_COUNT)
color = 0;
}
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 turnOff()
{
/* Event on to off, either by switch or auto-off timer */
+ tmr_stop(TMR_CFG);
+ tmr_stop(TMR_CANDLE);
tmr_stop(TMR_INCOLOR);
tmr_stop(TMR_FADE);
rgb_off();
mode = 0;
color = 0;
}
+ tmr_start(TMR_CFG, CFG_DELAY);
start_fade();
}
}
else
bright--;
}
+ tmr_start(TMR_CFG, CFG_DELAY);
leds_set();
}
}
}
+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.remainder;
blu.value += blu.remainder;
wht.value += wht.remainder;
+ tmr_stop(TMR_CANDLE);
tmr_stop(TMR_FADE);
- if (mode == MODE_FADE)
+ if (mode == MODE_CYCLE)
tmr_start(TMR_INCOLOR, STD_INCOLOR);
else if (mode == MODE_PARTY)
- tmr_start(TMR_INCOLOR, PARTY_MIN + (rand() % PARTY_RANGE));
+ tmr_start(TMR_INCOLOR, PARTY_MIN + (lfsr_get() % PARTY_RANGE));
+ else if (mode == MODE_SOLID && color == COLOR_CANDLE) {
+ cbright = bright - CBRIGHT_VARIANCE / 2;
+ tmr_startPeriodic(TMR_CANDLE, 1);
+ }
}
leds_set();
}
}
}
-void config_read()
-{
- /* Read configuration fields from eeprom, notably current mode and
- * brightness level.
- */
- /* FIXME: implement this */
-}
-
void user_boot()
{
dbgpin_high();
- srand((adc_random() << 8) + adc_random());
- config_read();
+ lfsr_init((adc_random() << 8) + adc_random());
+ cfg_read();
rs_task();
}
case TASK_INCOLOR: /* in-color timer has fired */
start_fade();
break;
+ case TASK_CANDLE: /* periodic adjustment for candle flicker */
+ candle_task();
+ break;
case TASK_TMR32: /* auto on/off event */
auto_offon_task();
break;
+ case TASK_CFG: /* Save config to EEPROM event */
+ cfg_write();
+ break;
}
}
}