// https://github.com/FastLED/FastLED/pull/1659

// Shows the original fill_gradient() on the left matrix,
// and the proposed fix in #1659 on the right matrix.

#include <FastLED.h>
#include "fill_gradient2.h"

#define NUM_LEDS 2000

CRGB leds[NUM_LEDS];
CRGB leds2[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, 25, GRB>(leds, 0, NUM_LEDS);
  FastLED.addLeds<WS2812B, 22, GRB>(leds2, 0, NUM_LEDS);
  Serial.begin(115200);
}


void loop() {
  // Cover a large range of both forwards and backwards hue shifts
  uint8_t starthue = beatsin8(1);
  uint8_t endhue = beatsin8(3);
  CRGB end_rgb = CHSV(endhue, 255, 255);

  if (starthue < endhue) {
    fill_gradient(leds, NUM_LEDS, CHSV(starthue, 255, 255), CHSV(endhue, 255, 255), FORWARD_HUES);
    fill_gradient2(leds2, NUM_LEDS, CHSV(starthue, 255, 255), CHSV(endhue, 255, 255), FORWARD_HUES);
  } else {
    fill_gradient(leds, NUM_LEDS, CHSV(starthue, 255, 255), CHSV(endhue, 255, 255), BACKWARD_HUES);
    fill_gradient2(leds2, NUM_LEDS, CHSV(starthue, 255, 255), CHSV(endhue, 255, 255), BACKWARD_HUES);
  }
  FastLED.show();
  delay(20);

  int diffs = 0;
  for (uint8_t led_no = 0; led_no < 32; led_no++) {
    if (leds[led_no] != leds2[led_no]) {    CRGB end_rgb = CHSV(endhue, 255, 255);

      diffs++;
      Serial.print("LED# ");
      Serial.print(led_no);
      Serial.print("\t original: ");
      Serial.print((uint32_t) leds[led_no], HEX);
      Serial.print("\t #1649: ");
      Serial.println((uint32_t) leds2[led_no], HEX);
    }
  }
  if (leds2[NUM_LEDS-1] != end_rgb) {
    diffs++;
  }
  if (diffs) {
    Serial.print("Diffs: ");
    Serial.print(diffs);
    Serial.print("\tstarthue: ");
    Serial.print(starthue);
    Serial.print("\tendhue: ");
    Serial.println(endhue);
    Serial.print("last RGB in leds[]:  ");
    Serial.println((uint32_t) leds[NUM_LEDS - 1], HEX);
    Serial.print("last RGB in leds2[]: ");
    Serial.println((uint32_t) leds2[NUM_LEDS - 1], HEX);
    Serial.print("Desired end_rgb:     ");
    Serial.println((uint32_t) end_rgb, HEX);
    Serial.println();
    delay(1000);
  }
}