Skip to content

Instantly share code, notes, and snippets.

@badgraemer badgraemer/XYmap.h
Created Jan 24, 2019

Embed
What would you like to do?
// Helper functions for a two-dimensional XY matrix of pixels.
// Special credit to Mark Kriegsman
//
// 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
// as 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.
//
// 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 = 34;
const uint8_t kMatrixHeight = 12;
// 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 200
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
};
*/
const uint8_t ShadesTable[] = {
12,13,14,201,202,203,204,64,65,72,205,73,96,97,206,207,208,209,210,141,142,143,211,212,213,214,215,216,217,159,160,161,218,219,
11,16,15,220,221,222,63,66,71,223,224,74,95,98,225,226,227,228,140,145,144,229,230,231,232,233,234,235,158,163,162,236,237,238,
10,17,18,239,240,62,67,70,241,242,243,75,94,99,244,245,246,139,146,149,247,248,249,250,251,252,253,157,164,170,254,255,256,257,
9,20,19,258,61,68,69,259,260,261,262,76,93,100,263,264,138,147,148,150,151,152,153,154,265,266,156,165,169,171,172,173,174,175,
8,21,36,37,60,59,267,268,269,270,271,77,92,101,272,137,136,135,134,133,132,131,130,273,274,155,166,167,168,179,178,177,176,275,
7,22,35,38,39,58,276,277,278,279,280,78,91,102,281,282,283,284,285,115,116,129,286,287,288,289,290,291,292,180,181,182,293,294,
6,23,34,295,40,57,56,296,297,298,299,79,90,103,300,301,302,303,114,117,128,304,305,306,307,308,309,310,185,184,183,311,312,313,
5,24,33,314,41,42,55,315,316,317,318,80,89,104,319,320,321,113,118,127,322,323,324,325,326,327,328,186,187,188,329,330,331,332,
4,25,32,333,334,43,44,54,335,336,337,81,88,105,338,339,340,112,119,126,341,342,343,344,345,346,347,191,190,189,348,349,350,351,
3,26,31,352,353,354,45,46,53,355,356,82,87,106,357,358,111,120,125,359,360,361,362,363,364,365,192,193,194,366,367,368,369,370,
2,27,30,371,372,373,374,47,52,51,375,83,86,107,376,110,121,124,377,378,379,380,381,382,383,197,196,195,384,385,386,387,388,389,
1,28,29,390,391,392,393,48,49,50,394,84,85,108,395,109,122,123,396,397,398,399,400,401,402,198,199,200,403,404,405,406,407,408
};
uint8_t i = (y * kMatrixWidth) + x;
uint8_t j = ShadesTable[i];
return j;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.