June 12th : Light it Up
Here is a quick video of my little display showing of its true colours. Unfortunately the quality is pretty shocking, I can never seem to get a good video of my light displays (anyone who knows some tricks for filming bright LEDs let me know).
I use Bit Angle Modulation (BAM) for the colour fading, 8 bit modulation allows me to generate 16 million or so colours. I am sure there are plenty of different ways to implement BAM but this is my way and it works, I store the current animation frame in a 9x3 matrix and the interrupt does the rest.
//BIT ANGLE MODULATION IN AN INTERRUPT
unsigned char bitpos = 0x00;
unsigned char bitcount = 0x00;
unsigned int bitlength = 0x01;
void Timer0_A3_ISR(void){
if(bitcount == 0){
HIGH(OE);
load_matrix(); //LOAD THE ARRAY ONTO THE MATRIX
LOW(OE);
}
bitcount += 0x01;
if(bitcount >= bitlength){
bitpos += 0x01;
bitlength = bitlength<<0x01;
bitcount = 0x00;
}
if(bitpos >= 8){
bitpos = 0x00;
bitcount = 0x00;
bitlength = 0x01;
state |= update;
}
}
//LOAD THE DATA ONTO THE LED DRIVER BOARD
void load_matrix(void){
int x, y;
for(x = 0; x < 9; x++){
for(y = 0; y < 3; y++){
if((matrix[x][y].red)&(0x01<<bitpos)){
HIGH(SDIR);
}
if((matrix[x][y].green)&(0x01<<bitpos)){
HIGH(SDIG);
}
if((matrix[x][y].blue)&(0x01<<bitpos)){
HIGH(SDIB);
}
PULSE(CLK);
LOW(SDI_ALL);
}
}
PULSE(LE);
}
Because the launchpad is pretty strapped for memory and it is a bit sluggish when it comes to playing with floating point numbers, I needed a new way to generate a colour wheel. Usually I create a large array in memory, or use sine functions to generate the colours, this time I decided to go with a clever little conditional function.
My getColour function can generate 768 different colours, you hand the function an integer between 0 and 768 and it will spit out a colour from the colour wheel. I painted a picture below to try and explain myself a little better, the numbers in the image are the integer you hand to the getColour function... Obviously the function fades the colours a little better than I did in paint.
typedef struct{
unsigned char red;
unsigned char green;
unsigned char blue;
}colour;
colour getColour(int c){
colour result;
while(c >= 768){
c-=768;
}
if(c >= 0 && c < 256){
result.red = c;
result.green = 0;
result.blue = 255-c;
}else if(c >= 256 && c < 512){
c-=256;
result.red = 255-c;
result.green = c;
result.blue = 0;
}else if(c >= 512 && c < 768){
c-=512;
result.red = 0;
result.green = 255-c;
result.blue = c;
}
return result;
}
