Advertisement
Guest User

Adalight FASTLED ws2801

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