Xalopalyps

XBMC Boblight -> Arduino with WS2811 LED Strips

Mar 18th, 2013
1,789
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <FastSPI_LED.h>
  2.  
  3. //Set the number of leds in the strip.
  4. #define NUM_LEDS 25
  5.  
  6. // Sometimes chipsets wire in a backwards sort of way
  7. struct CRGB {
  8.   unsigned char b;
  9.   unsigned char r;
  10.   unsigned char g;
  11. };
  12. // struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
  13. struct CRGB *leds;
  14.  
  15. // specified under `rate` in the `[device]` section of /etc/boblight.conf
  16. #define serialRate 38400
  17.  
  18. // boblightd sends a prefix (defined in /etc/boblight.conf) before sending the pixel data
  19. uint8_t prefix[] = {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
  20.  
  21. #define PIN 4
  22.  
  23. void setup() {
  24.   FastSPI_LED.setLeds(NUM_LEDS);
  25.  
  26.   //Change this to match your led strip
  27.   FastSPI_LED.setChipset(CFastSPI_LED::SPI_WS2811);
  28.  
  29.   //Change datarate to match your led strip as well
  30.   FastSPI_LED.setDataRate(0);
  31.  
  32.   //If non-default SPI pins have been used change this.
  33.   FastSPI_LED.setPin(PIN);
  34.  
  35.   FastSPI_LED.init();
  36.   FastSPI_LED.start();
  37.   leds = (struct CRGB*)FastSPI_LED.getRGBData();
  38.  
  39.   clearLeds();
  40.  
  41.   Serial.begin(serialRate);
  42. }
  43.  
  44. void clearLeds(){
  45.   for(int tmpChannel=0; tmpChannel<NUM_LEDS; tmpChannel++){
  46.     leds[tmpChannel].r = 10;
  47.     leds[tmpChannel].b = 0;
  48.     leds[tmpChannel].g = 0;  
  49.   };
  50.   FastSPI_LED.show();
  51. }
  52.  
  53. void loop() {
  54.   // wait until we see the prefix
  55.   for(byte i = 0; i < sizeof prefix; ++i) {
  56.     waitLoop: while (!Serial.available()) ;;
  57.     // look for the next byte in the sequence if we see the one we want
  58.     if(prefix[i] == Serial.read()) continue;
  59.     // otherwise, start over
  60.     i = 0;
  61.     goto waitLoop;
  62.   }
  63.   // read the transmitted data
  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.    
  73.     leds[i].r = r;
  74.     leds[i].b = b;
  75.     leds[i].g = g;
  76.   }
  77.   FastSPI_LED.show();
  78. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×