Advertisement
Guest User

Adalight_WS2812B

a guest
Nov 4th, 2014
113,896
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "FastLED.h"
  2.  
  3. // How many leds in your strip?
  4. #define NUM_LEDS 240
  5.  
  6. // For led chips like Neopixels, which have a data line, ground, and power, you just
  7. // need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
  8. // ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
  9. #define DATA_PIN 6
  10. #define CLOCK_PIN 13
  11.  
  12. #define COLOR_ORDER RGB
  13.  
  14. // Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data
  15. uint8_t prefix[] = {'A', 'd', 'a'}, hi, lo, chk, i;
  16.  
  17. // Baudrate, higher rate allows faster refresh rate and more LEDs (defined in /etc/boblight.conf)
  18. #define serialRate 460800
  19.  
  20. // Define the array of leds
  21. CRGB leds[NUM_LEDS];
  22.  
  23. void setup() {
  24. // Uncomment/edit one of the following lines for your leds arrangement.
  25. // FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
  26. // FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
  27. // FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
  28. // FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
  29. // FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  30. FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  31. // FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  32. // FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
  33. // FastLED.addLeds<UCS1903B, DATA_PIN, RGB>(leds, NUM_LEDS);
  34. // FastLED.addLeds<GW6205, DATA_PIN, RGB>(leds, NUM_LEDS);
  35. // FastLED.addLeds<GW6205_400, DATA_PIN, RGB>(leds, NUM_LEDS);
  36.  
  37. // FastLED.addLeds<WS2801, RGB>(leds, NUM_LEDS);
  38. // FastLED.addLeds<SM16716, RGB>(leds, NUM_LEDS);
  39. // FastLED.addLeds<LPD8806, RGB>(leds, NUM_LEDS);
  40.  
  41. // FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  42. // FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  43. // FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  44.  
  45. // initial RGB flash
  46. LEDS.showColor(CRGB(255, 0, 0));
  47. delay(500);
  48. LEDS.showColor(CRGB(0, 255, 0));
  49. delay(500);
  50. LEDS.showColor(CRGB(0, 0, 255));
  51. delay(500);
  52. LEDS.showColor(CRGB(0, 0, 0));
  53.  
  54. Serial.begin(serialRate);
  55. Serial.print("Ada\n"); // Send "Magic Word" string to host
  56.  
  57. }
  58.  
  59. void loop() {
  60. // wait for first byte of Magic Word
  61. for(i = 0; i < sizeof prefix; ++i) {
  62. waitLoop: while (!Serial.available()) ;;
  63. // Check next byte in Magic Word
  64. if(prefix[i] == Serial.read()) continue;
  65. // otherwise, start over
  66. i = 0;
  67. goto waitLoop;
  68. }
  69.  
  70. // Hi, Lo, Checksum
  71.  
  72. while (!Serial.available()) ;;
  73. hi=Serial.read();
  74. while (!Serial.available()) ;;
  75. lo=Serial.read();
  76. while (!Serial.available()) ;;
  77. chk=Serial.read();
  78.  
  79. // if checksum does not match go back to wait
  80. if (chk != (hi ^ lo ^ 0x55))
  81. {
  82. i=0;
  83. goto waitLoop;
  84. }
  85.  
  86. memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
  87. // read the transmission data and set LED values
  88. for (uint8_t i = 0; i < NUM_LEDS; i++) {
  89. byte r, g, b;
  90. while(!Serial.available());
  91. r = Serial.read();
  92. while(!Serial.available());
  93. g = Serial.read();
  94. while(!Serial.available());
  95. b = Serial.read();
  96. leds[i].r = r;
  97. leds[i].g = g;
  98. leds[i].b = b;
  99. }
  100. // shows new values
  101. FastLED.show();
  102. }
Advertisement
RAW Paste Data Copied
Advertisement