Advertisement
Guest User

Untitled

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