Skip to content

Instantly share code, notes, and snippets.

@kasperkamperman
Last active August 29, 2015 14:07
Show Gist options
  • Save kasperkamperman/59438e86914ac74ed01d to your computer and use it in GitHub Desktop.
Save kasperkamperman/59438e86914ac74ed01d to your computer and use it in GitHub Desktop.
interpolation with blend
/*
Interpolation with blend and three buffers.
*/
#include "FastLED.h"
#define NUM_LEDS 60
#define UPDATES_PER_SECOND 100
CRGB leds[NUM_LEDS];
CRGB lastFrame[NUM_LEDS]; // interpolation buffer
CRGB newFrame[NUM_LEDS]; // interpolation buffer
unsigned long t1; // for measuring purposes
unsigned long t2; // for measuring purposes
unsigned long t3 = 0; // for measuring purposes
uint8_t interpolationFraction = 0;
const uint8_t interpolationStepsize = 1; //2,4,8,16,32 for faster steps
uint8_t hue;
void setup() {
FastLED.addLeds<WS2812B, 2, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(128);
//FastLED.setDither(0);
// Teensy pins because this is also connected in FadeCandy
//pinMode(15, INPUT);
//pinMode(16, INPUT);
Serial.begin(57600);
memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
memset(newFrame, 0, NUM_LEDS * sizeof(struct CRGB));
memset(lastFrame, 0, NUM_LEDS * sizeof(struct CRGB));
// populate with whiter pixels, just to see the effect
for(int i = 0; i < NUM_LEDS; i++) {
newFrame[i] = CHSV(0,64,255);
}
}
void loop()
{ setTime();
if(interpolationFraction == 0)
{
//hue++;
//if(hue==255) hue = 0;
hue = random8();
// make the newFrame the lastFrame
// memcpy is a bit faster then updating the values in the for-loop below
memcpy8(lastFrame, newFrame, NUM_LEDS * sizeof(struct CRGB));
for(int i = 0; i < NUM_LEDS; i++) {
//lastFrame[i] = newFrame[i];
newFrame[i] = CHSV(hue,255,255);
//leds[i] = lastFrame[i];
}
memcpy8(leds, lastFrame, NUM_LEDS * sizeof(struct CRGB));
}
else
{
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = blend(lastFrame[i], newFrame[i], interpolationFraction);
}
}
printTime();
interpolationFraction =interpolationFraction + interpolationStepsize;
// we don't use 255 since that displays the new frame
// we display the completed newframe as lastframe when fraction = 0;
if(interpolationFraction>254) interpolationFraction = 0;
//FastLED.delay(1000 / UPDATES_PER_SECOND);
FastLED.show();
}
void setTime()
{ t1 = micros();
}
void printTime()
{
t2 = micros()-t1;
if(t2>t3) t3 = t2;
Serial.print(F("update time: "));
Serial.print(t3);
Serial.print(" ");
Serial.println(t2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment