+ unsigned char buttons = buttons_read();
+
+ if ((buttons & (IN_ROCKERA | IN_ROCKERB))) {
+ /* Crappy way to detect rising edges to change state of fast var */
+ if (!(fast & 2) && (buttons & IN_PUSHBTN)) {
+ fast |= 2;
+ } else if ((fast & 2) && !(buttons & IN_PUSHBTN)) {
+ fast &= ~2;
+ fast = 1 - fast;
+ 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(fast);
+ fade_steps = (buttons & IN_ROCKERA) ? 1 : rand_fade_steps(fast);
+
+ /* 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();
+ reset_steps();
+ rgb_on();
+ red.value = 0;
+ grn.value = 0;
+ blu.value = 0;
+ wht.value = 0;
+ }