Save mode and color any time mode changes, or if in MODE_SOLID and
color changes. Configuration is saved in internal EEPROM. The
PIC16LF1933 claims 100K writes. At 5 hrs per day, thats over 13
years of config writes assuming MODE_SOLID and a color change 4
times per year.
When the configuration changes, a 3 second timer is set. When the
timer expires, then the configuration is written. This should help
reduce reduce unnecessary EEPROM writes when the uesr is cycling
through the modes to find the one they want.
if (tmr_fired(TMR_DIM))
task_post(TMR_DIM);
#endif
if (tmr_fired(TMR_DIM))
task_post(TMR_DIM);
#endif
+ if (tmr_fired(TMR_CFG))
+ task_post(TASK_CFG);
#define dbgpin_low() (RA2 = 0)
#define dbgpin_toggle() (RA2 = (LATA2 == 0) ? 1 : 0)
#define dbgpin_low() (RA2 = 0)
#define dbgpin_toggle() (RA2 = (LATA2 == 0) ? 1 : 0)
+#define cfg_write(mode, color) do { \
+ eeprom_write(CFG_MODE_ADDR, (mode)); \
+ eeprom_write(CFG_COLOR_ADDR, (color)); \
+ } while (0)
+
+#define cfg_read(mode, color) do { \
+ unsigned char tmp; \
+ tmp = eeprom_read(CFG_MODE_ADDR); \
+ mode = (tmp < MODE_COUNT) ? tmp : MODE_SOLID; \
+ tmp = eeprom_read(CFG_COLOR_ADDR); \
+ color = (tmp < COLOR_COUNT) ? tmp : 0; \
+ } while (0)
+
enum {
/* Operating modes */
MODE_SOLID = 0, /* Cycle through colors[][] before next mode */
enum {
/* Operating modes */
MODE_SOLID = 0, /* Cycle through colors[][] before next mode */
BRIGHT_MIN = 1, /* ... by min and max. */
BRIGHT_INIT = 42, /* later, get from eeprom */
STD_FADE = 16, /* Fade time in 32.768 ms units */
BRIGHT_MIN = 1, /* ... by min and max. */
BRIGHT_INIT = 42, /* later, get from eeprom */
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() */
/* ... see start_fade() */
PARTY_MIN = 8, /* Min party fade and incolor units */
PARTY_RANGE = 8, /* Party fade/incolor range mask for rand() */
/* ... see start_fade() */
+ CFG_MODE_ADDR = 0, /* EEPROM address of mode variable */
+ CFG_COLOR_ADDR, /* EEPROM address of color variable */
+ CFG_DELAY = 92, /* 3 seconds in 32.768 msec units */
/* Set the LEDs using both color and brightness values. */
void leds_set()
{
/* Set the LEDs using both color and brightness values. */
void leds_set()
{
- rgb_set(led_get((red).value),
- led_get((grn).value), led_get((blu).value),
+ rgb_set(led_get((red).value), led_get((grn).value), led_get((blu).value),
void turnOff()
{
/* Event on to off, either by switch or auto-off timer */
void turnOff()
{
/* Event on to off, either by switch or auto-off timer */
tmr_stop(TMR_INCOLOR);
tmr_stop(TMR_FADE);
rgb_off();
tmr_stop(TMR_INCOLOR);
tmr_stop(TMR_FADE);
rgb_off();
void pb_clicked()
{
if (on) {
void pb_clicked()
{
if (on) {
+ unsigned char omode = mode;
+
if (mode == MODE_SOLID) {
if (++color == COLOR_COUNT) {
color = 0;
if (mode == MODE_SOLID) {
if (++color == COLOR_COUNT) {
color = 0;
+ if (mode != omode || mode == MODE_SOLID)
+ tmr_start(TMR_CFG, CFG_DELAY);
-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());
void user_boot()
{
dbgpin_high();
srand((adc_random() << 8) + adc_random());
case TASK_TMR32: /* auto on/off event */
auto_offon_task();
break;
case TASK_TMR32: /* auto on/off event */
auto_offon_task();
break;
+ case TASK_CFG: /* Save config to EEPROM event */
+ cfg_write(mode, color);
+ break;
TASK_FADE,
TASK_INCOLOR,
TASK_TMR32,
TASK_FADE,
TASK_INCOLOR,
TASK_TMR32,
TMR_FADE,
TMR_INCOLOR,
//TMR_DIM,
TMR_FADE,
TMR_INCOLOR,
//TMR_DIM,