Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #define LED_PIN 6
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812
- #define NUM_LEDS 512
- #define BRIGHTNESS 64
- // Helper functions for an two-dimensional XY matrix of pixels.
- // 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;
- // No error checking is performed on the ranges of x and y.
- //
- // XYsafe(x,y) takes x and y coordinates and returns an LED index number,
- // for use like this: leds[ XY(x,y) ] == CRGB::Red;
- // Error checking IS performed on the ranges of x and y, and an
- // index of "-1" is returned. Special instructions below
- // explain how to use this without having to do your own error
- // checking every time you use this function.
- // This is a slightly more advanced technique, and
- // it REQUIRES SPECIAL ADDITIONAL setup, described below.
- // Params for width and height
- const uint8_t kMatrixWidth = 16;
- const uint8_t kMatrixHeight = 16;
- // Param for different pixel layouts
- const bool kMatrixSerpentineLayout = true;
- const bool kMatrixVertical = false;
- // Set 'kMatrixSerpentineLayout' to false if your pixels are
- // laid out all running the same way, like this:
- //
- // 0 > 1 > 2 > 3 > 4
- // |
- // .----<----<----<----'
- // |
- // 5 > 6 > 7 > 8 > 9
- // |
- // .----<----<----<----'
- // |
- // 10 > 11 > 12 > 13 > 14
- // |
- // .----<----<----<----'
- // |
- // 15 > 16 > 17 > 18 > 19
- //
- // Set 'kMatrixSerpentineLayout' to true if your pixels are
- // laid out back-and-forth, like this:
- //
- // 0 > 1 > 2 > 3 > 4
- // |
- // |
- // 9 < 8 < 7 < 6 < 5
- // |
- // |
- // 10 > 11 > 12 > 13 > 14
- // |
- // |
- // 19 < 18 < 17 < 16 < 15
- //
- // Bonus vocabulary word: anything that goes one way
- // in one row, and then backwards in the next row, and so on
- // is call "boustrophedon", meaning "as the ox plows."
- // This function will return the right 'led index number' for
- // a given set of X and Y coordinates on your matrix.
- // IT DOES NOT CHECK THE COORDINATE BOUNDARIES.
- // That's up to you. Don't pass it bogus values.
- //
- // Use the "XY" function like this:
- //
- // for( uint8_t x = 0; x < kMatrixWidth; x++) {
- // for( uint8_t y = 0; y < kMatrixHeight; y++) {
- //
- // // Here's the x, y to 'led index' in action:
- // leds[ XY( x, y) ] = CHSV( random8(), 255, 255);
- //
- // }
- // }
- //
- //
- uint16_t XY( uint8_t x, uint8_t y)
- {
- uint16_t i;
- if( kMatrixSerpentineLayout == false) {
- if (kMatrixVertical == false) {
- i = (y * kMatrixWidth) + x;
- } else {
- i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
- }
- }
- if( kMatrixSerpentineLayout == true) {
- if (kMatrixVertical == false) {
- if( y & 0x01) {
- // Odd rows run backwards
- uint8_t reverseX = (kMatrixWidth - 1) - x;
- i = (y * kMatrixWidth) + reverseX;
- } else {
- // Even rows run forwards
- i = (y * kMatrixWidth) + x;
- }
- } else { // vertical positioning
- if ( x & 0x01) {
- i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
- } else {
- i = kMatrixHeight * (kMatrixWidth - x) - (y+1);
- }
- }
- }
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment