Skip to content

Instantly share code, notes, and snippets.

@jontutcher
Created December 12, 2016 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jontutcher/71d500970a95073b069f5bfdd32f6d09 to your computer and use it in GitHub Desktop.
Save jontutcher/71d500970a95073b069f5bfdd32f6d09 to your computer and use it in GitHub Desktop.
// Jon T <jon@jtutcher.co.uk>
#include "pattern.h"
#include "leds.h"
const uint32_t speed = 50;
const int numberOfColours = 7;
const int16_t maxHeightAbsolute = axis_max[2];
const int16_t minHeightAbsolute = axis_min[2];
const int16_t treeHeightAbsolute = maxHeightAbsolute - minHeightAbsolute;
// the set of colours we will use (in order)
const int colours[numberOfColours] = {0x000000, 0x0000FF, 0xFF8000, 0xFF0020, 0xCC00FF, 0xFFFFFF, 0xFFFFFF};
// relative positions down the tree that we'll draw each colour to (0 means bottom of tree)
const float drawHeightBoundaries[numberOfColours] = {0, 0, 0.2, 0.4, 0.6, 0.8, 1};
// the number of 'frames' per animation - to control speed
const int framesPerTransition[numberOfColours] = {20, 80, 70, 60, 40, 30, 100};
int last_offset = 0;
int state = 0; // state can be between 0 and colours.length
int frame = 0;
void setup() {}
bool needsRedraw() {
int offset = (millis() / speed);
if (offset == last_offset)
return false;
last_offset = offset;
return true;
}
int16_t scaleHeight(float relativeHeight) {
return (relativeHeight*1.0*treeHeightAbsolute);
}
float getRelativeHeight(int16_t absoluteHeight) {
return (absoluteHeight-minHeightAbsolute)/(treeHeightAbsolute*1.0);
}
void incrementState() {
state = (state+1)%(numberOfColours);
}
void incrementFrame() {
if(frame<framesPerTransition[state]) {
frame++;
} else {
frame = 0;
incrementState();
}
}
void sweep(int i) {
float heightScale = (1 - drawHeightBoundaries[state]);
float thresholdHeight = 1 - (((frame*1.0)/(framesPerTransition[state]*1.0))*heightScale);
if(getRelativeHeight(leds[i].z)>thresholdHeight) {
setPixel(i, colours[state]);
}
}
void setLeds() {
for (int i = 0; i < num_leds; i++) {
sweep(i);
}
}
void loop() {
if(needsRedraw()) {
incrementFrame();
setLeds();
show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment