Programming

Farewell Steve

Posted in ArKaos, Programming, real-time video, VJ on October 6th, 2011 by admin – Be the first to comment
Farewell Steve

Farewell Steve

Creating a Plasma effect for the Arduino and the Sparkfun LED tile

Posted in Programming, real-time video on December 26th, 2009 by admin – Be the first to comment

After having discovered the Arduino and before attacking the network extension I wanted to create something fun with the board and the 8 by 8 LED tile.

So I came with the idea of using one old school plasma effect of ArKaos VJ and porting it to the Arduino.

Here is the result:

Basically there are 2 textures pre-computed at startup. They are moved on a circular motion.

The 2 textures are moved and summed and you obtain an height value for each pixel. To get a nice effect you then go through a color table that is also pre-computed at startup.

To have a nice rotation effect you shift the color table at each generation of a new image.

At the end of the video you have another simple loop I did to test my setup.

Here is the plasma code for the Arduino:

//
// Plasma generation for the Arduino and a RGB Serial Backpack Matrix from SparkFun Electronics
// Marco Hinic, built on top of the code of Ryan Owens
//

//Define the SPI Pin Numbers
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss

void LED_Setup()
{
//SPI Bus setup
SPCR = (1<

//Set the pin modes for the RGB matrix
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);

//Make sure the RGB matrix is deactivated
digitalWrite(SLAVESELECT,HIGH);
}

//
// tables for plasma
//
// 8 by 8 -> 64 bytes
// 16 by 16 -> 256 bytes
// color table 256 bytes
//

#define PLASMA_W 8
#define PLASMA_H 8

#define TABLE_W 16
#define TABLE_H 16

unsigned char gPlasma[PLASMA_W*PLASMA_H];
unsigned char gTable1[TABLE_W*TABLE_H];
unsigned char gTable2[TABLE_W*TABLE_H];
unsigned char gColorTable[256];
float gCircle1, gCircle2, gCircle3, gCircle4, gCircle5, gCircle6, gCircle7, gCircle8;
int gRoll;

void Plasma_CalcTable1 ()
{
for (int i=0; i< TABLE_H; i++)
{
for (int j=0; j< TABLE_W; j++)
{
int index = (i*TABLE_W)+j;
gTable1[index] = (unsigned char) ((sqrt(16.0+(PLASMA_H-i)*(PLASMA_H-i)+(PLASMA_W-j)*(PLASMA_W-j))-4) *5 );
}
}
}

void Plasma_CalcTable2 ()
{
for (int i=0; i< TABLE_H; i++)
{
for (int j=0; j< TABLE_W; j++)
{
int index = (i*TABLE_W)+j;
float temp = sqrt(16.0+(PLASMA_H-i)*(PLASMA_H-i)+(PLASMA_W-j)*(PLASMA_W-j))-4;
gTable2[index] = (sin(temp/9.5)+1)*90;
}
}
}

void Plasma_SetColor (int index, unsigned char red, unsigned char green, unsigned char blue)
{
unsigned char new_color = (red & 0xE0) + ((green & 0xE0) >> 3) + ((blue & 0xC0) >> 6);
gColorTable [index] = new_color;
}

double gRed, gGreen, gBlue;

#define color(u,a) (cos((u)+(a))+1)*127

void BuildColorTable()
{
double u;
int i;
for (i=0; i<256; i++)
{
u=2*PI/256*i;
Plasma_SetColor(i,color(u,gRed),color(u,gGreen),color(u,gBlue));
}

gRed+=0.05;
gGreen-=0.05;
gBlue+=0.1;
}

void Plasma_Setup ()
{
gCircle1 = 0;
gCircle2 = 0;
gCircle3 = 0;
gCircle4 = 0;
gCircle5 = 0;
gCircle6 = 0;
gCircle7 = 0;
gCircle8 = 0;
gRoll = 0;

for (int i=0; i gPlasma[i] = 0;

Plasma_CalcTable1 ();
Plasma_CalcTable2 ();

gRed=1.0/6.0*PI;
gGreen=3.0/6.0*PI;
gBlue=5.0/6.0*PI;

BuildColorTable ();
}

void setup()
{
LED_Setup ();
Plasma_Setup ();
}

void ComputePlasma (int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int roll)
{
int i, j;

for (i=0; i< PLASMA_H; i++)
{
for (j=0; j< PLASMA_W; j++)
{
int index = (i*PLASMA_W)+j;

unsigned int new_height = gTable1[TABLE_W*(i+y1)+j+x1];
new_height = new_height + roll;
new_height = new_height + gTable2[TABLE_W*(i+y2)+j+x2];
new_height = new_height + gTable2[TABLE_W*(i+y3)+j+x3]; // + gTable2[TABLE_W*(i+y4)+j+x4];

new_height = new_height & 0xFF;

new_height = gColorTable[new_height];

gPlasma[index] = new_height;
}
}
}

char SendOneChar (volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1< {
};
return SPDR; // return the received byte
}

void SendToLEDs ()
{
int i;
//Activate the RGB Matrix
digitalWrite(SLAVESELECT, LOW);
//Send the color buffer to the RGB Matrix
for(i=0; i {
SendOneChar((char)gPlasma[i]);
}
//Deactivate the RGB matrix.
digitalWrite(SLAVESELECT, HIGH);
}

void loop()
{
int x1,y1,x2,y2,x3,y3,x4,y4;

float ratio = 2;

gCircle1 = gCircle1 + (ratio * 0.085/6);
gCircle2 = gCircle2 – (ratio * 0.1/6);
gCircle3 = gCircle3 + (ratio * 0.3/6);
gCircle4 = gCircle4 – (ratio * 0.2/6);
gCircle5 = gCircle5 + (ratio * 0.4/6);
gCircle6 = gCircle6 – (ratio * 0.15/6);
gCircle7 = gCircle7 + (ratio * 0.35/6);
gCircle8 = gCircle8 – (ratio * 0.05/6);

x2=(PLASMA_W/2)+(PLASMA_W/2)*sin(gCircle1);
y2=(PLASMA_H/2)+(PLASMA_H/2)*cos(gCircle2);

x1=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle3);
y1=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle4);

x3=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle5);
y3=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle6);

x4=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle7);
y4=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle8);

ComputePlasma(x1,y1,x2,y2,x3,y3,x4,y4,gRoll);

gRoll = gRoll + 1;

SendToLEDs();

delay (20);
}

First contact with the Arduino

Posted in Programming, real-time video on December 19th, 2009 by admin – Be the first to comment

I bought a small Arduino board that has a small CPU to prototype some ideas about driving LEDs.

Here is the first test, it’s was quick to have something blinking!

I want to use that as a prototyping platform to develop a protocol to drive LEDs in a simpler way.

Today you drive LED or with DMX or with video converters. DMX is cool and inexpensive but difficult to setup and video converters are very expansive.

There should be a way to connect LEDs to a computer and having them recognized automatically by a VJ software…

If you are curious to see on how simple the code is, here it is:

// Writing to the RGB Serial Backpack Matrix from SparkFun Electronics
// Marco Hinic, built on top of the code of Ryan Owens
//

//Define the SPI Pin Numbers
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss

//Define the variables we’ll need later in the program
char color_buffer [64];

void setup()
{
//SPI Bus setup
SPCR = (1<

//Set the pin modes for the RGB matrix
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);

//Make sure the RGB matrix is deactivated
digitalWrite(SLAVESELECT,HIGH);

}

void SendToLEEDs ()
{
//Activate the RGB Matrix
digitalWrite(SLAVESELECT, LOW);
//Send the color buffer to the RGB Matrix
for(int LED=0; LED<64; LED++){
spi_transfer(color_buffer[LED]);
}
//Deactivate the RGB matrix.
digitalWrite(SLAVESELECT, HIGH);
}

void loop()
{

char color = 0;
while(1)
{
for (int i=0; i<64; i++)
{
color_buffer[i] = color + i;
}

color = color+1;

SendToLEEDs ();

delay (100);

}
}

//Use this command to send a single color value to the RGB matrix.
//NOTE: You must send 64 color values to the RGB matrix before it displays an image!
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1< {
};
return SPDR; // return the received byte
}

An application to create time lapse: Lapseler

Posted in Programming on October 18th, 2009 by admin – Be the first to comment

Lapseler

Lapseler

After a few months of tweaking the code I released today Lapseler.

Lapselser is an application that can take pictures from any QuickTime supported devices and compile them in a movie.

Because the compression library is from FFmpeg it can generate QuickTime files but also flash videos.

The motivation to work on this application is both that I am interested into time lapse since I saw Koyaanisqatsi many years ago and I am learning several interfaces of Mac OS X / cocoa right now.

Lapseler is available on my web site:
fraktus.com/lapseler/
Leave me a note if you make some use of it.

The app is completely free and has no limitation, so you can use it to make cool videos for whatever purpose.

Easily converting an image into C++ / Obj C code..

Posted in Programming on September 30th, 2009 by admin – Be the first to comment

If you need a way to convert simply a small image into C++ / Obj C code that can be compiled by GCC or any other modern compiler there are a few utilities out there. In my case I was not happy with the results, so I wrote a few lines of code to do this.

What I needed is a simple code that fill an array of char that can then be used by OpenGL or custom pixel processing code. It’s so easy to load an image into an NSBitmapImageRep. Once you have this there are a few accessor like pixelsWide, bitmapData … that lets you play with the pixels and it’s then very easy to generate some text that will be C++ / Obj C compliant.

Here is a small 16 by 16 image converted into code with my utility:

int gImageWidth = 16;
int gImageHeight = 16;
int gImageBits = 24;
unsigned char gImagePixels[] = {
// line 0
0xf2,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,
0xf1,0xf1,0xf1,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,
0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xef,0xf0,0xf0,0xef,0xef,0xef,
0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xee,0xee,0xee,

// line 1
0xe5,0xe5,0xe5,0xe5,0xe5,0xe5,0xe4,0xe4,0xe4,0xe4,0xe4,0xe4,
0xe4,0xe4,0xe4,0xe4,0xe4,0xe4,0xe4,0xe3,0xe4,0xe3,0xe3,0xe3,
0xe3,0xe3,0xe3,0xe3,0xe3,0xe3,0xe3,0xe3,0xe3,0xe2,0xe2,0xe2,
0xe2,0xe2,0xe2,0xe2,0xe2,0xe2,0xe2,0xe2,0xe2,0xe1,0xe1,0xe1,

// line 2
0xd7,0xd7,0xd7,0xd7,0xd7,0xd7,0xd7,0xd7,0xd7,0xd6,0xd6,0xd6,
0xd6,0xd6,0xd6,0xd6,0xd6,0xd6,0xd6,0xd6,0xd6,0xd5,0xd5,0xd5,
0xd5,0xd5,0xd5,0xd5,0xd5,0xd5,0xd4,0xd5,0xd5,0xd4,0xd4,0xd4,
0xd4,0xd4,0xd4,0xd4,0xd4,0xd4,0xd3,0xd3,0xd4,0xd3,0xd3,0xd3,

And here is the code of the small command line utility:

#import Cocoa/Cocoa.h

int main (int argc, const char * argv[])
{
if (argc >=3)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString * src_file = [[NSString alloc] initWithFormat: @”%s”, argv[1]];
NSData * raw = [NSData dataWithContentsOfFile: src_file];

if (raw)
{
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData: raw];
if (rep)
{
FILE * out = fopen (argv[2], “w”);
if (out)
{
int width = [rep pixelsWide];
int height = [rep pixelsHigh];
int bits = [rep bitsPerPixel];
int rowbytes = [rep bytesPerRow];
unsigned char * src = [rep bitmapData];

fprintf (out, “int gImageWidth = %ld;\n”, width);
fprintf (out, “int gImageHeight = %ld;\n”, height);
fprintf (out, “int gImageBits = %ld;\n”, bits);
fprintf (out, “unsigned char gImagePixels[] = {\n”);
for (int v=0; v {
unsigned char * pixels = src;
fprintf (out, “// line %ld\n”, v);
for (int h=0; h {
fprintf (out, “0x%x,0x%x,0x%x,”, pixels[0], pixels[1], pixels[2]);
pixels +=3;
if (((h+1) % 4) == 0)
fprintf (out, “\n”);
}
fprintf (out, “\n”);

src += rowbytes;
}
fprintf (out, “};”);
fclose (out);
}
}
}
[pool release];
}
else
{
printf (“syntax is image2code src dst”);
}

return 0;
}

Just compile it as a command line utility with XCode and link against the cocoa framework and you are ready to convert simply image into C++ / Obj C code