Sky_Jones

I love lamp

Dec 25th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.80 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 6
  4. #define BRIGHTNESS  255
  5. #define LED_TYPE    WS2811
  6. #define COLOR_ORDER RGB
  7.  
  8. const uint8_t kMatrixWidth  = 10;
  9. const uint8_t kMatrixHeight = 4;
  10. const bool    kMatrixSerpentineLayout = true;
  11.  
  12.  
  13. // This example combines two features of FastLED to produce a remarkable range of
  14. // effects from a relatively small amount of code.  This example combines FastLED's
  15. // color palette lookup functions with FastLED's Perlin/simplex noise generator, and
  16. // the combination is extremely powerful.
  17. //
  18. // You might want to look at the "ColorPalette" and "Noise" examples separately
  19. // if this example code seems daunting.
  20. //
  21. //
  22. // The basic setup here is that for each frame, we generate a new array of
  23. // 'noise' data, and then map it onto the LED matrix through a color palette.
  24. //
  25. // Periodically, the color palette is changed, and new noise-generation parameters
  26. // are chosen at the same time.  In this example, specific noise-generation
  27. // values have been selected to match the given color palettes; some are faster,
  28. // or slower, or larger, or smaller than others, but there's no reason these
  29. // parameters can't be freely mixed-and-matched.
  30. //
  31. // In addition, this example includes some fast automatic 'data smoothing' at
  32. // lower noise speeds to help produce smoother animations in those cases.
  33. //
  34. // The FastLED built-in color palettes (Forest, Clouds, Lava, Ocean, Party) are
  35. // used, as well as some 'hand-defined' ones, and some proceedurally generated
  36. // palettes.
  37.  
  38.  
  39. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  40. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  41.  
  42. // The leds
  43. CRGB leds[kMatrixWidth * kMatrixHeight];
  44.  
  45. // The 16 bit version of our coordinates
  46. static uint16_t x;
  47. static uint16_t y;
  48. static uint16_t z;
  49.  
  50. // We're using the x/y dimensions to map to the x/y pixels on the matrix.  We'll
  51. // use the z-axis for "time".  speed determines how fast time moves forward.  Try
  52. // 1 for a very slow moving effect, or 60 for something that ends up looking like
  53. // water.
  54. uint16_t speed = 1; // speed is set dynamically once we've started up
  55.  
  56. // Scale determines how far apart the pixels in our noise matrix are.  Try
  57. // changing these values around to see how it affects the motion of the display.  The
  58. // higher the value of scale, the more "zoomed out" the noise iwll be.  A value
  59. // of 1 will be so zoomed in, you'll mostly see solid colors.
  60. uint16_t scale = 30; // scale is set dynamically once we've started up
  61.  
  62. // This is the array that we keep our computed noise values in
  63. uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
  64.  
  65. CRGBPalette16 currentPalette( PartyColors_p );
  66. uint8_t       colorLoop = 1;
  67.  
  68. void setup() {
  69.   delay(3000);
  70.   LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
  71.   LEDS.setBrightness(BRIGHTNESS);
  72.  
  73.   // Initialize our coordinates to some random values
  74.   x = random16();
  75.   y = random16();
  76.   z = random16();
  77. }
  78.  
  79.  
  80.  
  81. // Fill the x/y array of 8-bit noise values using the inoise8 function.
  82. void fillnoise8() {
  83.   // If we're runing at a low "speed", some 8-bit artifacts become visible
  84.   // from frame-to-frame.  In order to reduce this, we can do some fast data-smoothing.
  85.   // The amount of data smoothing we're doing depends on "speed".
  86.   uint8_t dataSmoothing = 0;
  87.   if( speed < 50) {
  88.     dataSmoothing = 200 - (speed * 4);
  89.   }
  90.  
  91.   for(int i = 0; i < MAX_DIMENSION; i++) {
  92.     int ioffset = scale * i;
  93.     for(int j = 0; j < MAX_DIMENSION; j++) {
  94.       int joffset = scale * j;
  95.      
  96.       uint8_t data = inoise8(x + ioffset,y + joffset,z);
  97.  
  98.       // The range of the inoise8 function is roughly 16-238.
  99.       // These two operations expand those values out to roughly 0..255
  100.       // You can comment them out if you want the raw noise data.
  101.       data = qsub8(data,16);
  102.       data = qadd8(data,scale8(data,39));
  103.  
  104.       if( dataSmoothing ) {
  105.         uint8_t olddata = noise[i][j];
  106.         uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
  107.         data = newdata;
  108.       }
  109.      
  110.       noise[i][j] = data;
  111.     }
  112.   }
  113.  
  114.   z += speed;
  115.  
  116.   // apply slow drift to X and Y, just for visual variation.
  117.   x += speed / 8;
  118.   y -= speed / 16;
  119. }
  120.  
  121. void mapNoiseToLEDsUsingPalette()
  122. {
  123.   static uint8_t ihue=0;
  124.  
  125.   for(int i = 0; i < kMatrixWidth; i++) {
  126.     for(int j = 0; j < kMatrixHeight; j++) {
  127.       // We use the value at the (i,j) coordinate in the noise
  128.       // array for our brightness, and the flipped value from (j,i)
  129.       // for our pixel's index into the color palette.
  130.  
  131.       uint8_t index = noise[j][i];
  132.       uint8_t bri =   noise[i][j];
  133.  
  134.       // if this palette is a 'loop', add a slowly-changing base value
  135.       if( colorLoop) {
  136.         index += ihue;
  137.       }
  138.  
  139.       // brighten up, as the color palette itself often contains the
  140.       // light/dark dynamic range desired
  141.       if( bri > 127 ) {
  142.         bri = 255;
  143.       } else {
  144.         bri = dim8_raw( bri * 2);
  145.       }
  146.  
  147.       CRGB color = ColorFromPalette( currentPalette, index, bri);
  148.       leds[XY(i,j)] = color;
  149.     }
  150.   }
  151.  
  152.   ihue+=1;
  153. }
  154.  
  155. void loop() {
  156.   // Periodically choose a new palette, speed, and scale
  157.   ChangePaletteAndSettingsPeriodically();
  158.  
  159.   // generate noise data
  160.   fillnoise8();
  161.  
  162.   // convert the noise data to colors in the LED array
  163.   // using the current palette
  164.   mapNoiseToLEDsUsingPalette();
  165.  
  166.   LEDS.show();
  167.   // delay(10);
  168. }
  169.  
  170.  
  171.  
  172. // There are several different palettes of colors demonstrated here.
  173. //
  174. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  175. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  176. //
  177. // Additionally, you can manually define your own color palettes, or you can write
  178. // code that creates color palettes on the fly.
  179.  
  180. // 1 = 5 sec per palette
  181. // 2 = 10 sec per palette
  182. // etc
  183. #define HOLD_PALETTES_X_TIMES_AS_LONG 60
  184.  
  185. void ChangePaletteAndSettingsPeriodically()
  186. {
  187.   uint8_t secondHand = ((millis() / 1000) / HOLD_PALETTES_X_TIMES_AS_LONG) % 60;
  188.   static uint8_t lastSecond = 99;
  189.  
  190.   if( lastSecond != secondHand) {
  191.     lastSecond = secondHand;
  192.     if( secondHand ==  0)  { PalCandy();          speed = 1; scale = 30; colorLoop = 1; }
  193.     if( secondHand ==  3)  { PalRed2();           speed = 1; scale = 100; colorLoop = 1; }
  194.     if( secondHand ==  6)  { PalOrange2();        speed = 1; scale = 100; colorLoop = 1; }
  195.     if( secondHand ==  9)  { PalYellow2();        speed = 1; scale = 100; colorLoop = 1; }
  196.     if( secondHand == 12)  { PalGreen2();         speed = 1; scale = 100; colorLoop = 1; }
  197.     if( secondHand == 15)  { PalAqua2();          speed = 1; scale = 100; colorLoop = 1; }
  198.     if( secondHand == 18)  { PalBlue2();          speed = 1; scale = 100; colorLoop = 1; }
  199.     if( secondHand == 21)  { PalPurple2();        speed = 1; scale = 100; colorLoop = 1; }
  200.     if( secondHand == 24)  { PalPink2();          speed = 1; scale = 100; colorLoop = 1; }
  201.     if( secondHand == 27)  { PalWhite();          speed = 1; scale = 100; colorLoop = 1; }
  202.     if( secondHand == 30)  { PalRainbow();        speed = 1; scale = 1; colorLoop = 1; }
  203.     if( secondHand == 33)  { PalROY();           speed = 1; scale = 100; colorLoop = 1; }
  204.     if( secondHand == 36)  { PalOYG();           speed = 1; scale = 100; colorLoop = 1; }
  205.     if( secondHand == 39)  { PalYGA();           speed = 1; scale = 100; colorLoop = 1; }
  206.     if( secondHand == 42)  { PalGAB();           speed = 1; scale = 100; colorLoop = 1; }
  207.     if( secondHand == 45)  { PalABP();           speed = 1; scale = 100; colorLoop = 1; }
  208.     if( secondHand == 48)  { PalBPP();           speed = 1; scale = 100; colorLoop = 1; }
  209.     if( secondHand == 51)  { PalPPR();           speed = 1; scale = 100; colorLoop = 1; }
  210.     if( secondHand == 54)  { PalPRO();           speed = 1; scale = 100; colorLoop = 1; }
  211.     if( secondHand == 57)  { PalRainbow2();      speed = 1; scale = 100; colorLoop = 1; }
  212.   }
  213. }
  214.  
  215. void PalCandy()
  216. {
  217.   currentPalette = CRGBPalette16(
  218.     CHSV( 96, 255, 255),  CHSV( 96, 255, 255),  CHSV( 96, 255, 255),  CHSV( 0, 255, 255),
  219.     CHSV( 0, 255, 255),  CHSV( 0, 255, 255),  CHSV( 0, 0, 255),  CHSV( 0, 0, 255),
  220.     CHSV( 0, 255, 255),  CHSV( 0, 255, 255),  CHSV( 0, 0, 255),  CHSV( 0, 0, 255),
  221.     CHSV( 0, 255, 255),  CHSV( 0, 255, 255),  CHSV( 0, 0, 255),  CHSV( 0, 0, 255) );
  222. }
  223.  
  224. void PalROY()
  225. {
  226.   currentPalette = CRGBPalette16(
  227.     CHSV( 0, 255, 255),  CHSV( 8, 255, 255),  CHSV( 16, 255, 255),  CHSV( 24, 255, 255),
  228.     CHSV( 32, 255, 255),  CHSV( 40, 255, 255),  CHSV( 48, 255, 255),  CHSV( 56, 255, 255),
  229.     CHSV( 64, 255, 255),  CHSV( 56, 255, 255),  CHSV( 48, 255, 255),  CHSV( 40, 255, 255),
  230.     CHSV( 32, 255, 255),  CHSV( 24, 255, 255),  CHSV( 16, 255, 255),  CHSV( 8, 255, 255) );
  231. }
  232. void PalOYG()
  233. {
  234.   currentPalette = CRGBPalette16(
  235.     CHSV( 32, 255, 255),  CHSV( 40, 255, 255),  CHSV( 48, 255, 255),  CHSV( 56, 255, 255),
  236.     CHSV( 64, 255, 255),  CHSV( 72, 255, 255),  CHSV( 80, 255, 255),  CHSV( 88, 255, 255),
  237.     CHSV( 96, 255, 255),  CHSV( 88, 255, 255),  CHSV( 80, 255, 255),  CHSV( 72, 255, 255),
  238.     CHSV( 64, 255, 255),  CHSV( 56, 255, 255),  CHSV( 48, 255, 255),  CHSV( 40, 255, 255) );
  239. }
  240. void PalYGA()
  241. {
  242.   currentPalette = CRGBPalette16(
  243.     CHSV( 64, 255, 255),  CHSV( 72, 255, 255),  CHSV( 80, 255, 255),  CHSV( 88, 255, 255),
  244.     CHSV( 96, 255, 255),  CHSV( 104, 255, 255),  CHSV( 112, 255, 255),  CHSV( 120, 255, 255),
  245.     CHSV( 128, 255, 255),  CHSV( 120, 255, 255),  CHSV( 112, 255, 255),  CHSV( 104, 255, 255),
  246.     CHSV( 96, 255, 255),  CHSV( 88, 255, 255),  CHSV( 80, 255, 255),  CHSV( 72, 255, 255) );
  247. }
  248. void PalGAB()
  249. {
  250.   currentPalette = CRGBPalette16(
  251.     CHSV( 96, 255, 255),  CHSV( 104, 255, 255),  CHSV( 112, 255, 255),  CHSV( 120, 255, 255),
  252.     CHSV( 128, 255, 255),  CHSV( 136, 255, 255),  CHSV( 144, 255, 255),  CHSV( 152, 255, 255),
  253.     CHSV( 160, 255, 255),  CHSV( 152, 255, 255),  CHSV( 144, 255, 255),  CHSV( 136, 255, 255),
  254.     CHSV( 128, 255, 255),  CHSV( 120, 255, 255),  CHSV( 112, 255, 255),  CHSV( 104, 255, 255) );
  255. }
  256. void PalABP()
  257. {
  258.   currentPalette = CRGBPalette16(
  259.     CHSV( 128, 255, 255),  CHSV( 136, 255, 255),  CHSV( 144, 255, 255),  CHSV( 152, 255, 255),
  260.     CHSV( 160, 255, 255),  CHSV( 168, 255, 255),  CHSV( 176, 255, 255),  CHSV( 184, 255, 255),
  261.     CHSV( 192, 255, 255),  CHSV( 184, 255, 255),  CHSV( 176, 255, 255),  CHSV( 168, 255, 255),
  262.     CHSV( 160, 255, 255),  CHSV( 152, 255, 255),  CHSV( 144, 255, 255),  CHSV( 136, 255, 255) );
  263. }
  264. void PalBPP()
  265. {
  266.   currentPalette = CRGBPalette16(
  267.     CHSV( 160, 255, 255),  CHSV( 168, 255, 255),  CHSV( 176, 255, 255),  CHSV( 184, 255, 255),
  268.     CHSV( 192, 255, 255),  CHSV( 200, 255, 255),  CHSV( 208, 255, 255),  CHSV( 216, 255, 255),
  269.     CHSV( 224, 255, 255),  CHSV( 216, 255, 255),  CHSV( 208, 255, 255),  CHSV( 200, 255, 255),
  270.     CHSV( 192, 255, 255),  CHSV( 184, 255, 255),  CHSV( 176, 255, 255),  CHSV( 168, 255, 255) );
  271. }
  272. void PalPPR()
  273. {
  274.   currentPalette = CRGBPalette16(
  275.     CHSV( 192, 255, 255),  CHSV( 200, 255, 255),  CHSV( 208, 255, 255),  CHSV( 216, 255, 255),
  276.     CHSV( 224, 255, 255),  CHSV( 232, 255, 255),  CHSV( 240, 255, 255),  CHSV( 248, 255, 255),
  277.     CHSV( 0, 255, 255),  CHSV( 248, 255, 255),  CHSV( 240, 255, 255),  CHSV( 232, 255, 255),
  278.     CHSV( 224, 255, 255),  CHSV( 216, 255, 255),  CHSV( 208, 255, 255),  CHSV( 200, 255, 255) );
  279. }
  280. void PalPRO()
  281. {
  282.   currentPalette = CRGBPalette16(
  283.     CHSV( 224, 255, 255),  CHSV( 232, 255, 255),  CHSV( 240, 255, 255),  CHSV( 248, 255, 255),
  284.     CHSV( 0, 255, 255),  CHSV( 8, 255, 255),  CHSV( 16, 255, 255),  CHSV( 24, 255, 255),
  285.     CHSV( 32, 255, 255),  CHSV( 24, 255, 255),  CHSV( 16, 255, 255),  CHSV( 8, 255, 255),
  286.     CHSV( 0, 255, 255),  CHSV( 248, 255, 255),  CHSV( 240, 255, 255),  CHSV( 232, 255, 255) );
  287. }
  288. void PalRainbow()
  289. {
  290.   currentPalette = CRGBPalette16(
  291.     CHSV( 0, 255, 255),  CHSV( 16, 255, 255),  CHSV( 32, 255, 255),  CHSV( 48, 255, 255),
  292.     CHSV( 64, 255, 255),  CHSV( 80, 255, 255),  CHSV( 96, 255, 255),  CHSV( 116, 255, 255),
  293.     CHSV( 128, 255, 255),  CHSV( 144, 255, 255),  CHSV( 152, 255, 255),  CHSV( 160, 255, 255),
  294.     CHSV( 180, 255, 255),  CHSV( 192, 255, 255),  CHSV( 208, 255, 255),  CHSV( 224, 255, 255) );
  295. }
  296. void PalWhite()
  297. {
  298.   currentPalette = CRGBPalette16(
  299.     CHSV( 0, 0, 255),  CHSV( 0, 0, 224),  CHSV( 0, 0, 192),  CHSV( 0, 0, 160),
  300.     CHSV( 0, 0, 128),  CHSV( 0, 0, 96),  CHSV( 0, 0, 64),  CHSV( 0, 0, 32),
  301.     CHSV( 0, 0, 0),  CHSV( 0, 0, 32),  CHSV( 0, 0, 64),  CHSV( 0, 0, 96),
  302.     CHSV( 0, 0, 128),  CHSV( 0, 0, 160),  CHSV( 0, 0, 192),  CHSV( 0, 0, 224) );
  303. }
  304.  
  305. void PalRed2()
  306. {
  307.   currentPalette = CRGBPalette16(
  308.     CHSV( 0, 255, 255),  CHSV( 0, 255, 224),  CHSV( 0, 255, 192),  CHSV( 0, 255, 160),
  309.     CHSV( 0, 255, 128),  CHSV( 0, 255, 96),  CHSV( 0, 255, 64),  CHSV( 0, 255, 32),
  310.     CHSV( 0, 255, 0),  CHSV( 0, 255, 32),  CHSV( 0, 255, 64),  CHSV( 0, 255, 96),
  311.     CHSV( 0, 255, 128),  CHSV( 0, 255, 160),  CHSV( 0, 255, 192),  CHSV( 0, 255, 224) );
  312. }
  313. void PalOrange2()
  314. {
  315.   currentPalette = CRGBPalette16(
  316.     CHSV( 16, 255, 255),  CHSV( 16, 255, 224),  CHSV( 16, 255, 192),  CHSV( 16, 255, 160),
  317.     CHSV( 16, 255, 128),  CHSV( 16, 255, 96),  CHSV( 16, 255, 64),  CHSV( 16, 255, 32),
  318.     CHSV( 16, 255, 0),  CHSV( 16, 255, 32),  CHSV( 16, 255, 64),  CHSV( 16, 255, 96),
  319.     CHSV( 16, 255, 128),  CHSV( 16, 255, 160),  CHSV( 16, 255, 192),  CHSV( 16, 255, 224) );
  320. }
  321. void PalYellow2()
  322. {
  323.   currentPalette = CRGBPalette16(
  324.     CHSV( 64, 255, 255),  CHSV( 64, 255, 224),  CHSV( 64, 255, 192),  CHSV( 64, 255, 160),
  325.     CHSV( 64, 255, 128),  CHSV( 64, 255, 96),  CHSV( 64, 255, 64),  CHSV( 64, 255, 32),
  326.     CHSV( 64, 255, 0),  CHSV( 64, 255, 32),  CHSV( 64, 255, 64),  CHSV( 64, 255, 96),
  327.     CHSV( 64, 255, 128),  CHSV( 64, 255, 160),  CHSV( 64, 255, 192),  CHSV( 64, 255, 224) );
  328. }
  329. void PalGreen2()
  330. {
  331.   currentPalette = CRGBPalette16(
  332.     CHSV( 96, 255, 255),  CHSV( 96, 255, 224),  CHSV( 96, 255, 192),  CHSV( 96, 255, 160),
  333.     CHSV( 96, 255, 128),  CHSV( 96, 255, 96),  CHSV( 96, 255, 64),  CHSV( 96, 255, 32),
  334.     CHSV( 96, 255, 0),  CHSV( 96, 255, 32),  CHSV( 96, 255, 64),  CHSV( 96, 255, 96),
  335.     CHSV( 96, 255, 128),  CHSV( 96, 255, 160),  CHSV( 96, 255, 192),  CHSV( 96, 255, 224) );
  336. }
  337. void PalAqua2()
  338. {
  339.   currentPalette = CRGBPalette16(
  340.     CHSV( 128, 255, 255),  CHSV( 128, 255, 224),  CHSV( 128, 255, 192),  CHSV( 128, 255, 160),
  341.     CHSV( 128, 255, 128),  CHSV( 128, 255, 96),  CHSV( 128, 255, 64),  CHSV( 128, 255, 32),
  342.     CHSV( 128, 255, 0),  CHSV( 128, 255, 32),  CHSV( 128, 255, 64),  CHSV( 128, 255, 96),
  343.     CHSV( 128, 255, 128),  CHSV( 128, 255, 160),  CHSV( 128, 255, 192),  CHSV( 128, 255, 224) );
  344. }
  345. void PalBlue2()
  346. {
  347.   currentPalette = CRGBPalette16(
  348.     CHSV( 160, 255, 255),  CHSV( 160, 255, 224),  CHSV( 160, 255, 192),  CHSV( 160, 255, 160),
  349.     CHSV( 160, 255, 128),  CHSV( 160, 255, 96),  CHSV( 160, 255, 64),  CHSV( 160, 255, 32),
  350.     CHSV( 160, 255, 0),  CHSV( 160, 255, 32),  CHSV( 160, 255, 64),  CHSV( 160, 255, 96),
  351.     CHSV( 160, 255, 128),  CHSV( 160, 255, 160),  CHSV( 160, 255, 192),  CHSV( 160, 255, 224) );
  352. }
  353. void PalPurple2()
  354. {
  355.   currentPalette = CRGBPalette16(
  356.     CHSV( 192, 255, 255),  CHSV( 192, 255, 224),  CHSV( 192, 255, 192),  CHSV( 192, 255, 160),
  357.     CHSV( 192, 255, 128),  CHSV( 192, 255, 96),  CHSV( 192, 255, 64),  CHSV( 192, 255, 32),
  358.     CHSV( 192, 255, 0),  CHSV( 192, 255, 32),  CHSV( 192, 255, 64),  CHSV( 192, 255, 96),
  359.     CHSV( 192, 255, 128),  CHSV( 192, 255, 160),  CHSV( 192, 255, 192),  CHSV( 192, 255, 224) );
  360. }
  361. void PalPink2()
  362. {
  363.   currentPalette = CRGBPalette16(
  364.     CHSV( 224, 255, 255),  CHSV( 224, 255, 224),  CHSV( 224, 255, 192),  CHSV( 224, 255, 160),
  365.     CHSV( 224, 255, 128),  CHSV( 224, 255, 96),  CHSV( 224, 255, 64),  CHSV( 224, 255, 32),
  366.     CHSV( 224, 255, 0),  CHSV( 224, 255, 32),  CHSV( 224, 255, 64),  CHSV( 224, 255, 96),
  367.     CHSV( 224, 255, 128),  CHSV( 224, 255, 160),  CHSV( 224, 255, 192),  CHSV( 224, 255, 224) );
  368. }
  369. void PalRainbow2()
  370. {
  371.   currentPalette = CRGBPalette16(
  372.     CHSV( 0, 255, 255),  CHSV( 16, 255, 255),  CHSV( 32, 255, 255),  CHSV( 48, 255, 255),
  373.     CHSV( 64, 255, 255),  CHSV( 80, 255, 255),  CHSV( 96, 255, 255),  CHSV( 116, 255, 255),
  374.     CHSV( 128, 255, 255),  CHSV( 144, 255, 255),  CHSV( 152, 255, 255),  CHSV( 160, 255, 255),
  375.     CHSV( 180, 255, 255),  CHSV( 192, 255, 255),  CHSV( 208, 255, 255),  CHSV( 224, 255, 255) );
  376. }
  377.  
  378.  
  379.  
  380. //
  381. // Mark's xy coordinate mapping code.  See the XYMatrix for more information on it.
  382. //
  383. uint16_t XY( uint8_t x, uint8_t y)
  384. {
  385.   uint16_t i;
  386.   if( kMatrixSerpentineLayout == false) {
  387.     i = (y * kMatrixWidth) + x;
  388.   }
  389.   if( kMatrixSerpentineLayout == true) {
  390.     if( y & 0x01) {
  391.       // Odd rows run backwards
  392.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  393.       i = (y * kMatrixWidth) + reverseX;
  394.     } else {
  395.       // Even rows run forwards
  396.       i = (y * kMatrixWidth) + x;
  397.     }
  398.   }
  399.   return i;
  400. }
Advertisement
Add Comment
Please, Sign In to add comment