shdowmyst

Untitled

Sep 7th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. //////////
  2. // 21:9 LG Ultrawide 29MA73 Adalight 112 LED controlled by Arduino Nano v3 and Ambibox software, using ws2812b led strips. 17 led vertically, 40 horizontally on the top, 38 on bottom.
  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. #include <FastLED.h>
  8.  
  9. ///// User definitions /////
  10.  
  11. #define LED_PIN     11
  12. #define NUM_LEDS    35
  13. #define BRIGHTNESS  96
  14. #define LED_TYPE    WS2811
  15. #define COLOR_ORDER GRB
  16.  
  17. // Baudrate, higher rate allows faster refresh rate and more LEDs (defined in /etc/boblight.conf)
  18. #define serialRate 115200
  19.  
  20. // Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data
  21. uint8_t prefix[] = {'A', 'd', 'a'}, hi, lo, chk, i;
  22.  
  23. // initialise LED-array
  24. CRGB leds[NUM_LEDS];
  25.  
  26. void setup()
  27. {
  28.  
  29.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalSMD5050 );
  30.   FastLED.setBrightness(  BRIGHTNESS );
  31.   delay( 1000 ); // power-up safety delay
  32.  
  33.   Serial.begin(serialRate);
  34.   Serial.print("Ada\n"); // Send "Magic Word" string to host
  35.  
  36. }
  37.  
  38. void loop() {
  39.   // wait for first byte of Magic Word
  40.   for(i = 0; i < sizeof prefix; ++i) {
  41.     waitLoop: while (!Serial.available()) ;;
  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.   hi=Serial.read();
  53.   while (!Serial.available()) ;;
  54.   lo=Serial.read();
  55.   while (!Serial.available()) ;;
  56.   chk=Serial.read();
  57.  
  58.   // if checksum does not match go back to wait
  59.   if (chk != (hi ^ lo ^ 0x55))
  60.   {
  61.     i=0;
  62.     goto waitLoop;
  63.   }
  64.  
  65.   memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
  66.   // read the transmission data and set LED values
  67.   for (uint8_t i = 0; i < NUM_LEDS; i++) {
  68.     byte r, g, b;    
  69.     while(!Serial.available());
  70.     r = Serial.read();
  71.     while(!Serial.available());
  72.     g = Serial.read();
  73.     while(!Serial.available());
  74.     b = Serial.read();
  75.     leds[i].r = r;
  76.     leds[i].g = g;
  77.     leds[i].b = b;
  78.   }
  79.   // shows new values
  80.  FastLED.show();
  81. }
Advertisement
Add Comment
Please, Sign In to add comment