SHARE
TWEET

Ambilight Arduino SKetch

pacman_d Mar 19th, 2016 163 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Modified and commented by ai.rs
  2.  * t4a_boblight
  3.  * (C) 2014 Hans Luijten, www.tweaking4all.com
  4.  *
  5.  * t4a_boblight is free software and can be distributed and/or modified
  6.  * freely as long as the copyright notice remains in place.
  7.  * Nobody is allowed to charge you for this code.
  8.  * Use of this code is entirely at your own risk.
  9.  */
  10.  
  11. #include "Adafruit_NeoPixel.h"
  12.  
  13. // DEFINITIONS
  14. #define STARTCOLOR 0x333333  // LED colors at start
  15. #define BLACK 0x000000  // LED color BLACK
  16. #define DATAPIN    12   // Datapin choose what you want or that is available
  17. #define LEDCOUNT   240  // Number of LEDs used for boblight 178 B1 E4, 180 B3E6, 181 B4E1 (iz boblight conf -1 tj 180), 182 B5E0
  18. #define SHOWDELAY  200 // Delay in micro seconds before showing default 200
  19. #define BAUDRATE   500000 // Serial port speed, 460800 does not work – tested with Arduino Mega, Nano, Leonardo but 500000 is working with Nano
  20. #define BRIGHTNESS 70   // Max. brightness in %
  21.  
  22. //Hyperion sends prefix characters based on number of LEDs in config file
  23. // e.g. for 181 LEDs it will send 0xB4 and cheksum 0xE1
  24. // keep in mind if you are using boblight config to calculate prefix that Boblight counts diodes from 1 and Hyperion from 0
  25. // if you have problems try +1 or -1 diodes when generating prefix characters
  26. // values to save some time: 178 B1 E4, 180 B3E6, 181 B4E1, 182 B5E0
  27. //hyperion code
  28. //_ledBuffer[3] = ((ledValues.size() - 1) >> 8) & 0xFF; // LED count high byte
  29. //    _ledBuffer[4] = (ledValues.size() - 1) & 0xFF;        // LED count low byte
  30. //    _ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
  31.  
  32. const char prefix[] = {0x41, 0x64, 0x61, 0x00, 0xEF, 0xBA};  // Start prefix ADA
  33. char buffer[sizeof(prefix)]; // Temp buffer for receiving prefix data
  34.  
  35.  
  36. // Init LED strand, WS2811/WS2912 specific
  37.  
  38. // These might work for other configurations:
  39. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  40. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  41. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  42. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  43.  
  44. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, DATAPIN, NEO_GRB + NEO_KHZ800);
  45.  
  46. int state;                   // Define current state
  47. #define STATE_WAITING   1    // - Waiting for prefix
  48. #define STATE_DO_PREFIX 2    // - Processing prefix
  49. #define STATE_DO_DATA   3    // - Handling incoming LED colors
  50.  
  51. int readSerial;           // Read Serial data (1)
  52. int currentLED;           // Needed for assigning the color to the right LED
  53.  
  54. void setup()
  55. {
  56.   strip.begin();            // Init LED strand, set all black, then all to startcolor
  57.  
  58.   strip.setBrightness( (255 / 100) * BRIGHTNESS );
  59.  
  60.   setAllLEDs(BLACK, 0);
  61.   setAllLEDs(STARTCOLOR, 5);
  62.  
  63.   Serial.begin(BAUDRATE);   // Init serial speed
  64.  
  65.   state = STATE_WAITING;    // Initial state: Waiting for prefix
  66. }
  67.  
  68.  
  69. void loop()
  70. {
  71.   switch(state)
  72.   {
  73.     case STATE_WAITING:                  // *** Waiting for prefix ***
  74.       if( Serial.available()>0 )
  75.       {
  76.         readSerial = Serial.read();      // Read one character
  77.        
  78.         if ( readSerial == prefix[0] )   // if this character is 1st prefix char
  79.           { state = STATE_DO_PREFIX; }   // then set state to handle prefix
  80.       }
  81.       break;
  82.      
  83.      
  84.     case STATE_DO_PREFIX:                // *** Processing Prefix ***
  85.       if( Serial.available() > sizeof(prefix) - 2 )
  86.       {
  87.           Serial.readBytes(buffer, sizeof(prefix) - 1);
  88.          
  89.           for( int Counter = 0; Counter < sizeof(prefix) - 1; Counter++)
  90.           {
  91.             if( buffer[Counter] == prefix[Counter+1] )
  92.             {
  93.               state = STATE_DO_DATA;     // Received character is in prefix, continue
  94.               currentLED = 0;            // Set current LED to the first one
  95.             }
  96.             else
  97.             {
  98.               state = STATE_WAITING;     // Crap, one of the received chars is NOT in the prefix
  99.               break;                     // Exit, to go back to waiting for the prefix
  100.             } // end if buffer
  101.           } // end for Counter
  102.       } // end if Serial
  103.       break;
  104.      
  105.      
  106.     case STATE_DO_DATA:                  // *** Process incoming color data ***
  107.       if( Serial.available() > 2 )       // if we receive more than 2 chars
  108.       {
  109.         Serial.readBytes( buffer, 3 );   // Abuse buffer to temp store 3 charaters
  110.         strip.setPixelColor( currentLED++, buffer[0], buffer[1], buffer[2]);  // and assing to LEDs
  111.       }
  112.  
  113.       if( currentLED > LEDCOUNT )        // Reached the last LED? Display it!
  114.       {
  115.           strip.show();                  // Make colors visible
  116.           delayMicroseconds(SHOWDELAY);  // Wait a few micro seconds
  117.          
  118.           state = STATE_WAITING;         // Reset to waiting ...
  119.           currentLED = 0;                // and go to LED one
  120.          
  121.           break;                         // and exit ... and do it all over again
  122.       }
  123.       break;
  124.   } // switch(state)
  125.  
  126. } // loop
  127.  
  128.  
  129. // Sets the color of all LEDs in the strand to 'color'
  130. // If 'wait'>0 then it will show a swipe from start to end
  131. void setAllLEDs(uint32_t color, int wait)
  132. {
  133.   for ( int Counter=0; Counter < LEDCOUNT; Counter++ )      // For each LED
  134.   {
  135.     strip.setPixelColor( Counter, color );      // .. set the color
  136.  
  137.     if( wait > 0 )                        // if a wait time was set then
  138.     {
  139.       strip.show();                     // Show the LED color
  140.       delay(wait);                      // and wait before we do the next LED
  141.     } // if wait
  142.    
  143.   } // for Counter
  144.  
  145.   strip.show();                         // Show all LEDs
  146. } // setAllLEDs
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top