Skip to content

Instantly share code, notes, and snippets.

@Paulchen-Panther
Created August 23, 2016 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Paulchen-Panther/07da7ada32977b3fe6dfef0000ccbb38 to your computer and use it in GitHub Desktop.
Save Paulchen-Panther/07da7ada32977b3fe6dfef0000ccbb38 to your computer and use it in GitHub Desktop.
Ambilight teensy
#define USE_OCTOWS2811
#include<OctoWS2811.h>
#include<FastLED.h>
// Led strip lengths, (0 if there is no strip):
#define STRIP_1 123
#define STRIP_2 69
#define STRIP_3 123
#define STRIP_4 69
#define STRIP_5 0
#define STRIP_6 0
#define STRIP_7 0
#define STRIP_8 0
// DEFINITIONS
#define STARTCOLOR 0x000000 // LED colors at start
#define NUM_STRIPS 8
#define LEDS_PER_STRIP 123
#define LEDCOUNT STRIP_1 + STRIP_2 + STRIP_3 + STRIP_4 + STRIP_5 + STRIP_6 + STRIP_7 + STRIP_8 // Number of LEDs used for boblight
#define BAUDRATE 115200 // Serial port speed
#define CALIBRATION_TEMPERATURE TypicalLEDStrip // Color correction
#define MAX_BRIGHTNESS 250 // 0-255
CRGB leds[NUM_STRIPS * LEDS_PER_STRIP];
const char prefix[] = {0x41, 0x64, 0x61, 0x01, 0x7F, 0x2B}; // Start prefix
// Bytes 0-2 are the magic word "Ada".
// Bytes 3 and 4 are the high byte and low byte of the 16-bit LED count.
// Boblight counts diodes from 1 and Hyperion from 0
// for Boblight from 1 to 25 for 25 LEDs, or 0x0019 hexadecimal,
// for Hyperion from 0 to 24 for 25 LEDs, or 0x0018 hexadecimal.
// The checksum equals the high byte XOR the low byte XOR 0x55
// In the 25 LED case again, this would be 0x4D. For 50 LEDs, it would be 0x64.
// 0x41 = 'A'
// 0x64 = 'd'
// 0x61 = 'a'
// 0x01 and 0x7F = Hex Value 0x017F = Decimal Value 383 LEDs
// 0x2B = 0x01 XOR 0x7F XOR 0x55
char buffer[sizeof(prefix)]; // Temp buffer for receiving prefix data
int state; // Define current state
#define STATE_WAITING 1 // - Waiting for prefix
#define STATE_DO_PREFIX 2 // - Processing prefix
#define STATE_DO_DATA 3 // - Handling incoming LED colors
int readSerial; // Read Serial data (1)
int currentLED; // Needed for assigning the color to the right LED
void setup()
{
LEDS.addLeds<OCTOWS2811>(leds, LEDS_PER_STRIP);
LEDS.setTemperature( CALIBRATION_TEMPERATURE );
LEDS.setBrightness( MAX_BRIGHTNESS );
setAllLEDs(STARTCOLOR); //Rot
Serial.begin(BAUDRATE); // Init serial speed
state = STATE_WAITING; // Initial state: Waiting for prefix
}
void loop()
{
switch(state)
{
case STATE_WAITING: // *** Waiting for prefix ***
if( Serial.available()>0 )
{
readSerial = Serial.read(); // Read one character
if ( readSerial == prefix[0] ) // if this character is 1st prefix char
{ state = STATE_DO_PREFIX; } // then set state to handle prefix
}
break;
case STATE_DO_PREFIX: // *** Processing Prefix ***
if( Serial.available() > sizeof(prefix) - 2 )
{
Serial.readBytes(buffer, sizeof(prefix) - 1);
for( int Counter = 0; Counter < sizeof(prefix) - 1; Counter++)
{
if( buffer[Counter] == prefix[Counter+1] )
{
state = STATE_DO_DATA; // Received character is in prefix, continue
currentLED = 0; // Set current LED to the first one
}
else
{
state = STATE_WAITING; // Crap, one of the received chars is NOT in the prefix
break; // Exit, to go back to waiting for the prefix
} // end if buffer
} // end for Counter
} // end if Serial
break;
case STATE_DO_DATA: // *** Process incoming color data ***
if( Serial.available() > 2 ) // if we receive more than 2 chars
{
Serial.readBytes( buffer, 3 ); // Abuse buffer to temp store 3 charaters
leds[t3convert(currentLED)].setRGB(buffer[0], buffer[1], buffer[2]);
currentLED++;
}
if( currentLED >= LEDCOUNT ) // Reached the last LED? Display it!
{
LEDS.show(); // Make colors visible
state = STATE_WAITING; // Reset to waiting ...
currentLED = 0; // and go to LED one
break; // and exit ... and do it all over again
}
break;
} // switch(state)
} // loop
void setAllLEDs(CRGB color)
{
for(int i = 0; i <= NUM_STRIPS * LEDS_PER_STRIP; i++) {
leds[i] = color;
}
LEDS.show(); // Show all LEDs
}
uint16_t t3convert(uint16_t i){
if(i >= 0 && i < STRIP_1) return i;
if(i >= STRIP_1 && i < STRIP_1 + STRIP_2) return i + (LEDS_PER_STRIP-STRIP_1);
if(i >= STRIP_1 + STRIP_2 && i < STRIP_1 + STRIP_2 + STRIP_3) return i + (LEDS_PER_STRIP-STRIP_1) + (LEDS_PER_STRIP-STRIP_2);
if(i >= STRIP_1 + STRIP_2 + STRIP_3 && i < STRIP_1 + STRIP_2 + STRIP_3 + STRIP_4) return i + (LEDS_PER_STRIP-STRIP_1) + (LEDS_PER_STRIP-STRIP_2) + (LEDS_PER_STRIP-STRIP_3);
if(i >= STRIP_1 + STRIP_2 + STRIP_3 + STRIP_4 && i < STRIP_1 + STRIP_2 + STRIP_3 + STRIP_4 + STRIP_5){
return i + (LEDS_PER_STRIP-STRIP_1) + (LEDS_PER_STRIP-STRIP_2) + (LEDS_PER_STRIP-STRIP_3) + (LEDS_PER_STRIP-STRIP_4); }
if(i >= STRIP_1 + STRIP_2 + STRIP_3 + STRIP_4 + STRIP_5 && i < STRIP_1 + STRIP_2 + STRIP_3 + STRIP_4 + STRIP_5 + STRIP_6){
return i + (LEDS_PER_STRIP-STRIP_1) + (LEDS_PER_STRIP-STRIP_2) + (LEDS_PER_STRIP-STRIP_3) + (LEDS_PER_STRIP-STRIP_4) + (LEDS_PER_STRIP-STRIP_5); }
if(i >= STRIP_1 + STRIP_2 + STRIP_3 + STRIP_4 + STRIP_5 + STRIP_6 && i < STRIP_1 + STRIP_2 + STRIP_3 + STRIP_4 + STRIP_5 + STRIP_6 + STRIP_7){
return i + (LEDS_PER_STRIP-STRIP_1) + (LEDS_PER_STRIP-STRIP_2) + (LEDS_PER_STRIP-STRIP_3) + (LEDS_PER_STRIP-STRIP_4) + (LEDS_PER_STRIP-STRIP_5) + (LEDS_PER_STRIP-STRIP_6); }
if(i >= STRIP_1 + STRIP_2 + STRIP_3 + STRIP_4 + STRIP_5 + STRIP_6 + STRIP_7 && i < STRIP_1 + STRIP_2 + STRIP_3 + STRIP_4 + STRIP_5 + STRIP_6 + STRIP_7 + STRIP_8){
return i + (LEDS_PER_STRIP-STRIP_1) + (LEDS_PER_STRIP-STRIP_2) + (LEDS_PER_STRIP-STRIP_3) + (LEDS_PER_STRIP-STRIP_4) + (LEDS_PER_STRIP-STRIP_5) + (LEDS_PER_STRIP-STRIP_6) + (LEDS_PER_STRIP-STRIP_7); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment