// Hardware Timer sketch 4: A whiter shade of blinky
//
// (Note: For the RGB leds specifically, this could also be done, and
// better, by using the PWM support.  But this timer+interrupt
// approach can be used with _any_ pins, not just certain ones.)
//
// This sketch uses 100 ticks per lap, which implies only 100
// different intensities per color can be displayed.  A shorter tick
// with a larger lap would provide finer control, if desired.

#define PINS 3
const u32 pins[PINS] = { BODY_RGB_RED_PIN, BODY_RGB_GREEN_PIN, BODY_RGB_BLUE_PIN };
const u32 off[PINS] =  { 15,               100,                45 };

bool on = true;                 // Controls blink on vs blink off

void allSet() { for (u32 i = 0; i < PINS; ++i) ledSet(pins[i],on); }
void offOne() { ledOff(pins[Timer1.getSplitIndex()-1]); }

void setup() {
  Timer1.begin(100, 100);       // 10KHz tick, 100Hz lap -> no flicker!
  Timer1.setLapHandler(allSet); // Reset RGBs at the lap
  for (u32 i = 0; i < PINS; ++i) // Put same handler on all splits, with..
    Timer1.setSplitHandler(i+1,off[i],offOne); // ..different split times
  Timer1.start();               // And start the timer
}

void loop() { 
  on = !on;                     // Flip blink state
  delay(500);                   // Wait about a half second
}

