 	delay(100);
 	if (iotuz.read_encoder_button() == ENC_RELEASED || !digitalRead(JOYSTICK_BUT_PIN)) break;
     }
+
+    Events.addHandler(LED_Handler, 50);
+    Events.addHandler(LED_Brightness_Handler, 333);
+    // Make use of my ISR driven mini port of Andy Gelme's Aiko
+    // Sadly, I cannot. Calling pixels.show() from an ISR causes crashes.
+    //iotuz.enable_aiko_ISR();
 }
 
 // vim:sts=4:sw=4

// 30 chars high, 5 boxes, 6 lines per box
char* opt_name[NVERT][NHORIZ][3] = {
    { { "", "Finger", "Paint"},  { "Adafruit", "Touch", "Paint"}, { "Joystick", "Absolute", "Paint"}, { "Joystick", "Relative", "Paint"}, { "", "Accel", "Paint"}, },
    { { "Select", "LEDs", "Color"}, { "", "LEDs", "Off"}, { "", "Rotary", "Encoder"}, { "", "Round", "Rects"}, { "Round", "Fill", "Rects"}, },
    { { "", "Text", ""}, { "", "Fill", ""}, { "", "Diagonal", "Lines"}, { "Horizon", "Vert", "Lines"}, { "", "Rectangle", ""}, },
    { { "", "Fill", "Rectangle"}, { "", "Circles", ""}, { "", "Fill", "Circles"}, { "", "Triangles", ""}, { "", "Fill", "Triangles"}, },
    { { "", "Tetris", ""}, { "", "Breakout", ""}, { "", "", ""}, { "", "", ""}, { "", "", ""}, },

    while(i  < 3)
    {
	uint8_t color_guess = random(0, 3);

	// Loop until we get a color slot that's not be assigned to
	if (led[color_guess] != -1.0) continue;
    	led[color_guess] = led_color[i++];
    }

    *red =   led[0];
    *green = led[1];
    *blue =  led[2];
#ifdef DEBUG_LED
    Serial.print("LED ");
    Serial.print(numled);
    Serial.print(": picked RGB Color: ");
    Serial.print((int)*red, HEX);
    Serial.print("/");
    Serial.print((int)*green, HEX);
    Serial.print("/");
    Serial.println((int)*blue, HEX);
#endif
}

bool rgbLedFade(uint8_t red[], uint8_t green[], uint8_t blue[]) {
    static uint8_t RGB_led_stage = 0;
    static float RGBa[NUMLED][3] = { 255, 255, 255 };
    static float RGBb[NUMLED][3];
    static float RGBdiff[NUMLED][3];

    if (RGB_led_stage < RGB_COLOR_CHANGE_STEPS)
    {
	for (uint8_t numled=0; numled < NUMLED; numled++) {
	    if (RGB_led_stage == 0) {
		LED_Color_Picker(numled, &RGBb[numled][0], &RGBb[numled][1], &RGBb[numled][2]);
	    }
		for (uint8_t i = 0; i <= 2; i++)
		{
		    if (RGB_led_stage == 0)
		    {
			RGBdiff[numled][i] = (RGBb[numled][i] - RGBa[numled][i]) / RGB_COLOR_CHANGE_STEPS;
		    }
		    RGBa[numled][i] = constrain(RGBa[numled][i] + RGBdiff[numled][i], 0, 255);
		}
		if (rgb_led_brightness) {
		    red[numled]   = int(RGBa[numled][0]) / (17-rgb_led_brightness);
		    green[numled] = int(RGBa[numled][1]) / (17-rgb_led_brightness);
		    blue[numled]  = int(RGBa[numled][2]) / (17-rgb_led_brightness);
		} else {
		    red[numled]   = 0;
		    green[numled] = 0;
		    blue[numled]  = 0;
		}
	}
	RGB_led_stage++;
	return 1;
    }
    if (RGB_led_stage == RGB_COLOR_CHANGE_STEPS) 
    {
	// Cannot use Serial in an ISR or things will crash
#ifdef DEBUG_LED
	Serial.print(millis());
	Serial.println(": Holding Color for LEDs");
#endif
    }
    if (RGB_led_stage < RGB_COLOR_CHANGE_STEPS + RGB_COLOR_HOLD_STEPS)
    {
	RGB_led_stage++;
	return 0;
    }
    else
    {
#ifdef DEBUG_LED
	Serial.print(millis());
	Serial.println(": Done Holding Color for LEDs");
#endif
	RGB_led_stage = 0;
	return 0;
    }
}


void LED_Handler() {
    uint8_t numled;
    // This needs to be static because some calls to rgbLedFade do not return a modified value
    // (holding a color at end of stage)
    static uint8_t red[NUMLED], green[NUMLED], blue[NUMLED];

    rgbLedFade(red, green, blue);
    for (uint8_t numled=0; numled<NUMLED; numled++) {
	// APA106 Mapping is actually Green, Red, Blue (not RGB)
	pixels.setPixelColor(numled, green[numled], red[numled], blue[numled]);
#ifdef DEBUG_LED
	Serial.print("Set LED ");
	Serial.print(numled);
	Serial.print(": ");
	Serial.print((int)red[numled], HEX);
	Serial.print("/");
	Serial.print((int)green[numled], HEX);
	Serial.print("/");
	Serial.println((int)blue[numled], HEX);
#endif
    }
    pixels.show();
}

void LED_Brightness_Handler() {
    // Start at -1 to force a screen update at init.
    static int16_t   last_encoder = -4;
    int16_t   encoder = iotuz.read_encoder();
    // Encoder jumps 4 notches for each detent click
    int16_t offset = (encoder - last_encoder)/4;
    last_encoder = encoder;

    if (offset) {
	rgb_led_brightness = constrain(rgb_led_brightness + offset, 0, 16);
	sprintf(iotuz.tft_str, "LED Bright:%d", rgb_led_brightness);
	iotuz.tftprint(40, 0, 13, iotuz.tft_str);
    }
}

void loop() {
