- unsigned char red = 0, grn = 0, blu = 0; //, wht = 0;
- char fadestep_red, fadestep_grn, fadestep_blu; //, fadestep_wht;
- unsigned char incolor_time = 0;
- unsigned char fade_time = 0;
- unsigned char fast = 0;
-
- pic_init();
- buttons_init();
- rgb_init();
- srand(27);
-
- if (buttons_on())
- rgb_on();
-
- while (1) {
- unsigned char buttons = buttons_read();
-
- if ((buttons & (IN_ROCKERA | IN_ROCKERB))) {
- if (buttons & IN_PUSHBTN) {
- if (!fast) {
- incolor_time = 0;
- fade_time = 0;
- fast = 1;
- } else
- fast = 0;
- }
-
- if (fade_time) {
- red += fadestep_red;
- grn += fadestep_grn;
- blu += fadestep_blu;
- //wht += fadestep_wht;
- rgb_set(red, grn, blu, 0); // wht);
- fade_time--;
- } else if (incolor_time)
- incolor_time--;
- else /* fade_time == 0 && incolor_time == 0 */ {
- unsigned char newr, newg, newb, neww;
-
- /* Determine next color state */
- newr = random_rgb();
- newg = random_rgb();
- newb = random_rgb();
- //neww = random_rgb();
-
- /* Calculate new incolor_time and fade_time */
- if (buttons & IN_ROCKERA) {
- incolor_time = 1 + random_time();
- fade_time = 1;
- } else /* (buttons & IN_ROCKERB) */ {
- incolor_time = min_incolor_time() + random_time() / 2;
- fade_time = min_fade_time() + random_time() / 2;
- }
-
- if (fast) {
- incolor_time = 1 + incolor_time / 8;
- fade_time = 1 + fade_time / 8;
- }
-
- fadestep_red = ((int)newr - red) / fade_time;
- fadestep_grn = ((int)newg - red) / fade_time;
- fadestep_blu = ((int)newb - red) / fade_time;
- //fadestep_wht = ((int)neww - red) / fade_time;
- }
- __delay_ms(10);
- } else {
- rgb_off();
- buttons_sleep();
- incolor_time = 0;
- fade_time = 0;
- rgb_on();
- }
+ 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()
+{
+ 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;
+ }
+ 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;