Guest User

Untitled

a guest
Dec 6th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. #include <FastLED.h>
  2. //#define NUM_LEDS 356
  3. #define DATA_PIN 2
  4. #define CMD_NEW_DATA 1
  5. #define BAUD_RATE 115200
  6.  
  7. // Params for width and height
  8. const uint8_t kMatrixWidth = 14;
  9. const uint8_t kMatrixHeight = 26;
  10.  
  11. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  12. CRGB leds[ NUM_LEDS ];
  13. #define LAST_VISIBLE_LED 355
  14. uint16_t XY (uint16_t x, uint16_t y) {
  15.   // any out of bounds address maps to the first hidden pixel
  16.   if ( (x >= kMatrixWidth) || (y >= kMatrixHeight) ) {
  17.     return (LAST_VISIBLE_LED + 1);
  18.   }
  19.  
  20.   const uint16_t XYTable[] = {
  21.      0,  51,  52, 103, 356, 357, 358, 361, 362, 363, 252, 303, 304, 355,
  22.      1,  50,  53, 102, 104, 153, 359, 360, 202, 251, 253, 302, 305, 354,
  23.      2,  49,  54, 101, 105, 152, 154, 201, 203, 250, 254, 301, 306, 353,
  24.      3,  48,  55, 100, 106, 151, 155, 200, 204, 249, 255, 300, 307, 352,
  25.      4,  47,  56,  99, 107, 150, 156, 199, 205, 248, 256, 299, 308, 351,
  26.      5,  46,  57,  98, 108, 149, 157, 198, 206, 247, 257, 298, 309, 350,
  27.      6,  45,  58,  97, 109, 148, 158, 197, 207, 246, 258, 297, 310, 349,
  28.      7,  44,  59,  96, 110, 147, 159, 196, 208, 245, 259, 296, 311, 348,
  29.      8,  43,  60,  95, 111, 146, 160, 195, 209, 244, 260, 295, 312, 347,
  30.      9,  42,  61,  94, 112, 145, 161, 194, 210, 243, 261, 294, 313, 346,
  31.     10,  41,  62,  93, 113, 144, 162, 193, 211, 242, 262, 293, 314, 345,
  32.     11,  40,  63,  92, 114, 143, 163, 192, 212, 241, 263, 292, 315, 344,
  33.     12,  39,  64,  91, 115, 142, 164, 191, 213, 240, 264, 291, 316, 343,
  34.     13,  38,  65,  90, 116, 141, 165, 190, 214, 239, 265, 290, 317, 342,
  35.     14,  37,  66,  89, 117, 140, 166, 189, 215, 238, 266, 289, 318, 341,
  36.     15,  36,  67,  88, 118, 139, 167, 188, 216, 237, 267, 288, 319, 340,
  37.     16,  35,  68,  87, 119, 138, 168, 187, 217, 236, 268, 287, 320, 339,
  38.     17,  34,  69,  86, 120, 137, 169, 186, 218, 235, 269, 286, 321, 338,
  39.     18,  33,  70,  85, 121, 136, 170, 185, 219, 234, 270, 285, 322, 337,
  40.     19,  32,  71,  84, 122, 135, 171, 184, 220, 233, 271, 284, 323, 336,
  41.     20,  31,  72,  83, 123, 134, 172, 183, 221, 232, 272, 283, 324, 335,
  42.     21,  30,  73,  82, 124, 133, 173, 182, 222, 231, 273, 282, 325, 334,
  43.     22,  29,  74,  81, 125, 132, 174, 181, 223, 230, 274, 281, 326, 333,
  44.     23,  28,  75,  80, 126, 131, 175, 180, 224, 229, 275, 280, 327, 332,
  45.     24,  27,  76,  79, 127, 130, 176, 179, 225, 228, 276, 279, 328, 331,
  46.     25,  26,  77,  78, 128, 129, 177, 178, 226, 227, 277, 278, 329, 330
  47.   };
  48.  
  49.   uint16_t i = (y * kMatrixWidth) + x;
  50.   uint16_t j = XYTable[i];
  51.   return j;
  52. }
  53.  
  54. void setup() {
  55.   FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  56.   Serial.begin(BAUD_RATE);
  57. }
  58. int serialGlediator () {
  59.   while (!Serial.available()) {}
  60.   return Serial.read();
  61. }
  62. void loop() {
  63.   byte r,g,b;
  64.   int i;
  65.   while (serialGlediator () != CMD_NEW_DATA) {}
  66.  
  67.   /*
  68.   //This works but it handles all strips the same
  69.   Serial.readBytes((char*)leds, NUM_LEDS*3);
  70.   FastLED.show();
  71.  
  72.  
  73.   //This also works but also handles all the strip the same
  74.   for (i=0; i < NUM_LEDS; i++) {
  75.     //b = serialGlediator();
  76.     //r = serialGlediator();
  77.     //g = serialGlediator();
  78.  
  79.     Serial.readBytes( (char*)(&leds[i]), 3);
  80.     //leds[i].setRGB(Color(r,g,b));
  81.     //leds[i] = CHSV( r,g,b);
  82.     //leds.setPixel(i, Color(r,g,b));
  83.   }  
  84.   FastLED.show();
  85.  
  86.   */
  87.  
  88.   //This doesnt work exactly but need to get led from serial and map to x and y??
  89.   for( byte y = 0; y < kMatrixHeight; y++) {
  90.     for( byte x = 0; x < kMatrixWidth; x++) {
  91.       //Serial.print(XY(x,y));Serial.print("\n");    
  92.       //leds[ XY(x, y)]  = CHSV( 10, 10, 10);
  93.       Serial.readBytes( (char*)(&leds[ XY(x, y)]), 3);
  94.     }
  95.   }
  96.   FastLED.show();
  97.  
  98. }
  99.  
  100. /* Helper functions */
  101. // Create a 24 bit color value from R,G,B
  102. unsigned int Color(byte r, byte g, byte b)
  103. {
  104.   return (((unsigned int)b << 16) | ((unsigned int)r << 8) | (unsigned int)g);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment