Guest User

ws2812

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