Skip to content

Instantly share code, notes, and snippets.

@kriegsman
Last active May 3, 2020 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriegsman/475e907a9624352c748c to your computer and use it in GitHub Desktop.
Save kriegsman/475e907a9624352c748c to your computer and use it in GitHub Desktop.
XY matrix functions for RGB Shades Kickstarter
#include <FastLED.h>
// Use Version 2.1 or later https://github.com/FastLED/FastLED/tree/FastLED2.1
// ZIP file https://github.com/FastLED/FastLED/archive/FastLED2.1.zip
// RGB Shades data output to LEDs on pin 5
#define LED_PIN 5
// RGB Shades color order
#define COLOR_ORDER GRB
#define CHIPSET WS2811
#define BRIGHTNESS 64
// Helper functions for an two-dimensional XY matrix of pixels.
//
// 2014-10-18 - Special version for RGB Shades Kickstarter
// https://www.kickstarter.com/projects/macetech/rgb-led-shades
// 2014-10-18 - code version 2c (local table, holes are r/w),
// by Mark Kriegsman
//
// This special 'XY' code lets you program the RGB Shades
// if it's was just a plain 16x5 matrix.
//
// Writing to and reading from the 'holes' in the layout is
// also allowed; holes retain their data, it's just not displayed.
//
// You can also test to see if you're on- or off- the layout
// like this
// if( XY(x,y) > LAST_VISIBLE_LED ) { ...off the layout...}
//
// X and Y bounds checking is also included, so it is safe
// to just do this without checking x or y in your code:
// leds[ XY(x,y) ] == CRGB::Red;
// All out of bounds coordinates map to the first hidden pixel.
//
// Simple 2-D demo code is included as well.
//
// XY(x,y) takes x and y coordinates and returns an LED index number,
// for use like this: leds[ XY(x,y) ] == CRGB::Red;
// Params for width and height
const uint8_t kMatrixWidth = 16;
const uint8_t kMatrixHeight = 5;
// Pixel layout
//
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// +------------------------------------------------
// 0 | . 0 1 2 3 4 5 6 7 8 9 10 11 12 13 .
// 1 | 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14
// 2 | 30 31 32 33 34 35 36 . . 37 38 39 40 41 42 43
// 3 | 57 56 55 54 53 52 51 . . 50 49 48 47 46 45 44
// 4 | . 58 59 60 61 62 . . . . 63 64 65 66 67 .
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds[ NUM_LEDS ];
// This function will return the right 'led index number' for
// a given set of X and Y coordinates on your RGB Shades.
// This code, plus the supporting 80-byte table is much smaller
// and much faster than trying to calculate the pixel ID with code.
#define LAST_VISIBLE_LED 67
uint8_t XY( uint8_t x, uint8_t y)
{
// any out of bounds address maps to the first hidden pixel
if( (x >= kMatrixWidth) || (y >= kMatrixHeight) ) {
return (LAST_VISIBLE_LED + 1);
}
const uint8_t ShadesTable[] = {
68, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 69,
29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14,
30, 31, 32, 33, 34, 35, 36, 70, 71, 37, 38, 39, 40, 41, 42, 43,
57, 56, 55, 54, 53, 52, 51, 72, 73, 50, 49, 48, 47, 46, 45, 44,
74, 58, 59, 60, 61, 62, 75, 76, 77, 78, 63, 64, 65, 66, 67, 79
};
uint8_t i = (y * kMatrixWidth) + x;
uint8_t j = ShadesTable[i];
return j;
}
// Demo that USES "XY" follows code below
void loop()
{
uint32_t ms = millis();
int32_t yHueDelta32 = ((int32_t)cos16( ms * (27/1) ) * (350 / kMatrixWidth));
int32_t xHueDelta32 = ((int32_t)cos16( ms * (39/1) ) * (310 / kMatrixHeight));
DrawOneFrame( ms / 65536, yHueDelta32 / 32768, xHueDelta32 / 32768);
if( ms < 5000 ) {
FastLED.setBrightness( scale8( BRIGHTNESS, (ms * 256) / 5000));
} else {
FastLED.setBrightness(BRIGHTNESS);
}
FastLED.show();
}
void DrawOneFrame( byte startHue8, int8_t yHueDelta8, int8_t xHueDelta8)
{
byte lineStartHue = startHue8;
for( byte y = 0; y < kMatrixHeight; y++) {
lineStartHue += yHueDelta8;
byte pixelHue = lineStartHue;
for( byte x = 0; x < kMatrixWidth; x++) {
pixelHue += xHueDelta8;
leds[ XY(x, y)] = CHSV( pixelHue, 255, 255);
}
}
}
void setup() {
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
FastLED.setBrightness( BRIGHTNESS );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment