Advertisement
Guest User

Neopixel

a guest
Apr 10th, 2016
4,690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. //////////
  2. //
  3. // Arduino interface for the use of ws2812 operated LEDs
  4. // Uses Adalight protocol and is compatible with Boblight, Prismatik etc
  5. // "Magic Word" for synchronisation is 'Ada' followed by LED High, Low and Checksum
  6. //
  7. #include <FastLED.h>
  8.  
  9. ///// User definitions /////
  10.  
  11. // Define the number of LEDs
  12. #define NUM_LEDS 50
  13.  
  14. // Define SPI Pin
  15. #define PIN 2
  16.  
  17. // Baudrate, higher rate allows faster refresh rate and more LEDs (defined in /etc/boblight.conf)
  18. #define serialRate 115200
  19.  
  20. // Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data
  21. uint8_t prefix[] = {'A', 'd', 'a'}, hi, lo, chk, i;
  22.  
  23. // initialise LED-array
  24. CRGB leds[NUM_LEDS];
  25.  
  26. void setup()
  27. {
  28.  
  29. FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS);
  30.  
  31. // initial RGB flash
  32. LEDS.showColor(CRGB(255, 0, 0));
  33. delay(500);
  34. LEDS.showColor(CRGB(0, 255, 0));
  35. delay(500);
  36. LEDS.showColor(CRGB(0, 0, 255));
  37. delay(500);
  38. LEDS.showColor(CRGB(0, 0, 0));
  39.  
  40. Serial.begin(serialRate);
  41. Serial.print("Ada\n"); // Send "Magic Word" string to host
  42.  
  43. }
  44.  
  45. void loop() {
  46. // wait for first byte of Magic Word
  47. for(i = 0; i < sizeof prefix; ++i) {
  48. waitLoop: while (!Serial.available()) ;;
  49. // Check next byte in Magic Word
  50. if(prefix[i] == Serial.read()) continue;
  51. // otherwise, start over
  52. i = 0;
  53. goto waitLoop;
  54. }
  55.  
  56. // Hi, Lo, Checksum
  57.  
  58. while (!Serial.available()) ;;
  59. hi=Serial.read();
  60. while (!Serial.available()) ;;
  61. lo=Serial.read();
  62. while (!Serial.available()) ;;
  63. chk=Serial.read();
  64.  
  65. // if checksum does not match go back to wait
  66. if (chk != (hi ^ lo ^ 0x55))
  67. {
  68. i=0;
  69. goto waitLoop;
  70. }
  71.  
  72. memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
  73. // read the transmission data and set LED values
  74. for (uint8_t i = 0; i < NUM_LEDS; i++) {
  75. byte r, g, b;
  76. while(!Serial.available());
  77. r = Serial.read();
  78. while(!Serial.available());
  79. g = Serial.read();
  80. while(!Serial.available());
  81. b = Serial.read();
  82. leds[i].r = r;
  83. leds[i].g = g;
  84. leds[i].b = b;
  85. }
  86. // shows new values
  87. FastLED.show();
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement