#include <FastLED.h>
#include <ButtonGestures.h>


#define   BUTTON_PIN    23
ButtonGestures  button(BUTTON_PIN, LOW, INPUT_PULLUP);
 

#define LED_PIN     4
#define NUM_LEDS    100
#define BRIGHTNESS  64
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100


#define NUM_COLUMNS 25
#define NUM_LEDS_PER_COLUMN 4
  

CRGBPalette16 currentPalette( RainbowColors_p);
CRGBPalette16 targetPalette( RainbowColors_p );

CRGBPalette16 redPalette( CRGB::Red);

TBlendType    currentBlending;


//
// These functions will be registered and called for the various gestures:
//
void function1(const uint8_t /*pin*/, const uint8_t /*state*/) {
 targetPalette = RainbowColors_p;
    Serial.println(" single press and release");
}

void function2(const uint8_t /*pin*/, const uint8_t /*state*/) {
    Serial.println("Double Click");
    targetPalette = redPalette;
}
   
 void setup() {
  Serial.begin(115200);
 

    button.set_callback(SHORT1, function1);     // normal single click and release
    button.set_callback(SHORT2, function2);     // double click and release

    delay( 3000 ); // power-up safety delay

    FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.setBrightness( BRIGHTNESS );

    currentPalette = RainbowColors_p;
    targetPalette  = RainbowColors_p;

    currentBlending = LINEARBLEND;
    currentBlending = NOBLEND;
}


void loop()
{
 
  button.check_button();
  
  uint8_t maxChanges = 24; 
  nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);

  static uint8_t startIndex = 0;
  startIndex = startIndex + 1; /* motion speed */

  FillLEDsFromPaletteColors( startIndex);

  FastLED.show();
  FastLED.delay(1000 / UPDATES_PER_SECOND);

}


void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
    uint8_t brightness = 255;
    int xOffset = 0;
  // This outer loop will go over each strip, one at a time
  for(int x = 0; x < NUM_COLUMNS; x++) {
    // This inner loop will go over each led in the current strip, one at a time
    for(int i = 0; i < NUM_LEDS_PER_COLUMN; i++) {
        leds[xOffset + i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
    }
    xOffset += NUM_LEDS_PER_COLUMN;
    colorIndex+=3;

  }
 
}