Advertisement
Guest User

Adalight for APA102

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