Teensy 3 OctoWS2811 code for Boblight

Status
Not open for further replies.

W11cE

Member
My friend wanted to build Boblight for his TV and we decided to use Teensy 3.1 with OctoWS2811 shield for that. Could not find anyone who has already done this, so I decided to share the scetch i made for this.

There is only this part you need to modify in addition to the normal settings:
Code:
// Led strip lengths, (0 if there is no strip):
#define STRIP_1  86
#define STRIP_2  47
#define STRIP_3  84
#define STRIP_4  47
#define STRIP_5  0
#define STRIP_6  0
#define STRIP_7  0
#define STRIP_8  0

// ledsPerStrip has to be >= the longest led strip
const int ledsPerStrip = 90;
We made it so that first LED strip is bottom, second is left, third is top and fourth is right. When you have these lenghts set correctly, the code will chain installed LEDs automatically between 0 and LEDCOUNT.

Complete scetch:
Code:
/* t4a_boblight
 * (C) 2014 Hans Luijten, www.tweaking4all.com
 *
 * t4a_boblight is free software and can be distributed and/or modified
 * freely as long as the copyright notice remains in place.
 * Nobody is allowed to charge you for this code.
 * Use of this code is entirely at your own risk.
 */

#include <OctoWS2811.h>

/*Required Connections    http://www.pjrc.com/store/octo28_adaptor.html
  --------------------
    pin 2:  LED Strip #1    OctoWS2811 drives 8 LED Strips.
    pin 14: LED strip #2    All 8 are the same length.
    pin 7:  LED strip #3
    pin 8:  LED strip #4    A 100 ohm resistor should used
    pin 6:  LED strip #5    between each Teensy pin and the
    pin 20: LED strip #6    wire to the LED strip, to minimize
    pin 21: LED strip #7    high frequency ringining & noise.
    pin 5:  LED strip #8
    pin 15 & 16 - Connect together, but do not use
    pin 4 - Do not use
    pin 3 - Do not use as PWM.  Normal use is ok.
*/

// Led strip lengths, (0 if there is no strip):
#define STRIP_1  86
#define STRIP_2  47
#define STRIP_3  84
#define STRIP_4  47
#define STRIP_5  0
#define STRIP_6  0
#define STRIP_7  0
#define STRIP_8  0

// ledsPerStrip has to be >= the longest led strip
const int ledsPerStrip = 90;

DMAMEM int displayMemory[ledsPerStrip*6];
int drawingMemory[ledsPerStrip*6];

/*
Create OctoWS2811 object. You can only create a single object, but it must be created with these parameters:

    -ledsPerStrip: The number of LEDs connected to each pin, or maximum number if different on each pin.
    -displayMemory: The memory used for display data. Use an array of "int" 6 times ledsPerStrip.
    -drawingMemory: The memory used for drawing operations. Use either an array of "int" 6 times ledsPerStrip,
	 or NULL to perform all drawing directly to the display memory.
    -config: Configure the WS2811 speed and LED color order.
	 Options are WS2811_RGB, WS2811_RBG, WS2811_GRB, WS2811_GBR, WS2811_800kHz, WS2811_400kHz. 
*/
const int config = WS2811_GRB | WS2811_800kHz;
OctoWS2811 strip(ledsPerStrip, displayMemory, drawingMemory, config);

// DEFINITIONS

#define STARTCOLOR 0xFFFFFF  	// LED colors at start
#define BLACK      0x000000  	// LED color BLACK

#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 SHOWDELAY  200       	// Delay in micro seconds before showing
#define BAUDRATE   460800    	// Serial port speed, 460800 tested with Arduino Uno R3
#define BRIGHTNESS 100        	// Max. brightness in %

const char prefix[] = {0x41, 0x64, 0x61, 0x00, 0x18, 0x4D};  // Start prefix

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()
{
  strip.begin();            // Init LED strand, set all black, then all to startcolor
  
  setAllLEDs(BLACK, 0);
  setAllLEDs(STARTCOLOR, 5);
  
  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
        
        strip.setPixel( t3convert(currentLED), bConvert(buffer[0], BRIGHTNESS), bConvert(buffer[1], BRIGHTNESS), bConvert(buffer[2], BRIGHTNESS));
        currentLED++;
        
      }
  
      if( currentLED >= LEDCOUNT )        // Reached the last LED? Display it!
      {
          strip.show();                  // Make colors visible
          delayMicroseconds(SHOWDELAY);  // Wait a few micro seconds
          
          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


// Sets the color of all LEDs in the strand to 'color'
// If 'wait'>0 then it will show a swipe from start to end
void setAllLEDs(uint32_t color, int wait)
{
  for ( int Counter=0; Counter < LEDCOUNT; Counter++ )      // For each LED
  {
	strip.setPixel(Counter, color); 

    if( wait > 0 )                        // if a wait time was set then
    {
      strip.show();                     // Show the LED color
      delay(wait);                      // and wait before we do the next LED
    } // if wait
    
  } // for Counter
  
  strip.show();                         // Show all LEDs
} // setAllLEDs

uint8_t bConvert(uint8_t input, uint8_t brightness){
	return (input*brightness)/100;
}

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 + (ledsPerStrip-STRIP_1);
	if(i >= STRIP_1 + STRIP_2 && i < STRIP_1 + STRIP_2 + STRIP_3)                        return i + (ledsPerStrip-STRIP_1) + (ledsPerStrip-STRIP_2);
	if(i >= STRIP_1 + STRIP_2 + STRIP_3 && i < STRIP_1 + STRIP_2 + STRIP_3 + STRIP_4)    return i + (ledsPerStrip-STRIP_1) + (ledsPerStrip-STRIP_2) + (ledsPerStrip-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 + (ledsPerStrip-STRIP_1) + (ledsPerStrip-STRIP_2) + (ledsPerStrip-STRIP_3) + (ledsPerStrip-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 + (ledsPerStrip-STRIP_1) + (ledsPerStrip-STRIP_2) + (ledsPerStrip-STRIP_3) + (ledsPerStrip-STRIP_4) + (ledsPerStrip-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 + (ledsPerStrip-STRIP_1) + (ledsPerStrip-STRIP_2) + (ledsPerStrip-STRIP_3) + (ledsPerStrip-STRIP_4) + (ledsPerStrip-STRIP_5) + (ledsPerStrip-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 + (ledsPerStrip-STRIP_1) + (ledsPerStrip-STRIP_2) + (ledsPerStrip-STRIP_3) + (ledsPerStrip-STRIP_4) + (ledsPerStrip-STRIP_5) + (ledsPerStrip-STRIP_6) + (ledsPerStrip-STRIP_7);	}
}
 
I asked for him to take some pics and here those are:
IMG_1357.JPGIMG_1359.JPGIMG_1364.JPG

The colors look quite bad on those pictures, in reality colors match really well the video. If you have some specific video to test i can ask him to try that.
 
Status
Not open for further replies.
Back
Top