Advertisement
Guest User

Untitled

a guest
Aug 24th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. #define FORCE_SOFTWARE_SPI
  2. //#define FORCE_SOFTWARE_PINS
  3. #include "FastLED.h"
  4. #define NUM_LEDS 30
  5. #define PIN 4
  6. #define serialRate 115200
  7.  
  8. // Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data
  9. uint8_t prefix[] = {
  10.   'A', 'd', 'a'}
  11. , hi, lo, chk, i;
  12.  
  13. // initialise LED-array
  14. CRGB diody[NUM_LEDS];
  15.  
  16. void setup()
  17. {
  18.   FastLED.addLeds<WS2812B, PIN, GRB>(diody, NUM_LEDS);
  19.   //  FastLED.addLeds<WS2812B, PIN, BGR>(diody, NUM_LEDS);
  20.   // initial RGB flash
  21.   LEDS.showColor(CRGB(255, 0, 0));
  22.   delay(1000);
  23.   LEDS.showColor(CRGB(0, 255, 0));
  24.   delay(1000);
  25.   LEDS.showColor(CRGB(0, 0, 255));
  26.   delay(1000);
  27.   LEDS.showColor(CRGB(0, 0, 0));
  28.  
  29.   Serial.begin(serialRate);
  30.   Serial.print("Ada\n"); // Send "Magic Word" string to host
  31.  
  32. }
  33.  
  34. void loop() {
  35.  
  36.   LEDS.showColor(CRGB(0, 0, 0));
  37.   // wait for first byte of Magic Word
  38.   for(i = 0; i < sizeof prefix; ++i) {
  39. waitLoop:
  40.     while (!Serial.available()) ;
  41.     ;
  42.     // Check next byte in Magic Word
  43.     if(prefix[i] == Serial.read()) continue;
  44.     // otherwise, start over
  45.     i = 0;
  46.     goto waitLoop;
  47.   }
  48.  
  49.   // Hi, Lo, Checksum
  50.  
  51.   while (!Serial.available()) ;
  52.   ;
  53.   hi=Serial.read();
  54.   while (!Serial.available()) ;
  55.   ;
  56.   lo=Serial.read();
  57.   while (!Serial.available()) ;
  58.   ;
  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(diody, 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.     diody[i].r = r;
  79.     diody[i].g = g;
  80.     diody[i].b = b;
  81.   }
  82.   // shows new values
  83.   FastLED.show();
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement