/*
  WS2812FX segment sequence demo. Creates a
*/

#include <WS2812FX.h>

#define LED_PIN    2  // digital pin used to drive the LED strip
#define LED_COUNT 8  // number of LEDs on the strip

#define NUM_SEGMENTS        8  // maximum total number of segments that can be created
#define NUM_ACTIVE_SEGMENTS 6  // maximum number of segments that can be actively running

// create helper macros that define the start and end LEDs for each of our two segments
#define LOWER_SEG_RANGE 0, LED_COUNT/2 - 1
#define UPPER_SEG_RANGE LED_COUNT/2, LED_COUNT - 1

// example of the new WS2812FX constructor that adds parameters for the number of
// segments allowed and the number of active segments allowed.
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800, NUM_SEGMENTS, NUM_ACTIVE_SEGMENTS);

void setup() {
  Serial.begin(115200);

  ws2812fx.init();
  ws2812fx.setBrightness(32);

  // create two active segments
  ws2812fx.setSegment(0, LOWER_SEG_RANGE, FX_MODE_BLINK, YELLOW,             2000, NO_OPTIONS);
  ws2812fx.setSegment(1, UPPER_SEG_RANGE, FX_MODE_BLINK, COLORS(RED), 2000, NO_OPTIONS);

  // create additional "idle" segments that will be activated later
  ws2812fx.setIdleSegment(2, LOWER_SEG_RANGE, FX_MODE_BLINK, YELLOW,               2000, NO_OPTIONS);
  ws2812fx.setIdleSegment(3, UPPER_SEG_RANGE, FX_MODE_BLINK, COLORS(RED), 2000, NO_OPTIONS);

  ws2812fx.start();
}

void loop() {
  static unsigned long timer = millis();
  static unsigned int  blink_counter = 0;

  ws2812fx.service();

  /* the lower segment is updated based on a timer.
     the lower segment will change every 10 seconds
  */
  if(millis() > timer + 10000) {        // every 10 seconds...
    if(ws2812fx.isActiveSegment(0)) {   // if seg[0] is active, switch to seg[2]
      ws2812fx.swapActiveSegment(0, 2);
    } else {                            // else, switch to seg[0]
      ws2812fx.swapActiveSegment(2, 0);
    }
    timer = millis();
  }

  /* the upper segment is updated based on counting animation cycles. One "cycle" for the Blink
   *  effect is one on/off sequence. We'll change the upper segment every 5 blinks.
   */
  // increment the blink counter every time seg[1] or seg[3] complete an animation cycle
  if(ws2812fx.isCycle(1) || ws2812fx.isCycle(3)) {
    blink_counter++;
  }
  if(blink_counter >= 5) {                  // every 5 blinks...
    if(ws2812fx.isActiveSegment(1)) {       // if seg[1] active, switch to seg[3]
      ws2812fx.swapActiveSegment(1, 3);
    } else {                                // else, switch to seg[1]
      ws2812fx.swapActiveSegment(3, 1);
    }
    blink_counter = 0;
  }
}