SparkFun RGB LED Backpack Blinking Lights

Sorry, no video here. Which is too bad because it looks pretty cool.

// Blinking LEDs

// Simple program to test SPI with the RGB Matrix and Backpack from Sparkfun.
//
// Revised 2009.12.24
//
// Credits:
//   Heather Dewey-Hagborg
//   Arduino Forum user Little-Scale
//   Daniel Hirschmann
//   Arduino Forum user chessplayer

// SPI register bit positions

#define RX_IN  0
#define TX_OUT 1
#define CS     2
#define MOSI   3
#define MISO   4
#define SCK    5

// Ardrino Duemilanove pin numbers for SPI

#define CHIPSELECT 10 // CS
#define DATAOUT    11 // MOSI
#define DATAIN     12 // MISO (not used)
#define SPICLOCK   13 // SCK

// SparkFun RGB LED backpack officially supported colors

typedef unsigned char ledcolorType;

#define colorBlack   0x00
#define colorRed     0xe0
#define colorGreen   0x1c
#define colorBlue    0x03
#define colorOrange  0xfc
#define colorMagneta 0xe3
#define colorTeal    0x1f
#define colorWhite   0xff

#define colorLen 8

static const ledcolorType colors[colorLen] =
{
  colorBlack,  colorRed,     colorGreen, colorBlue,
  colorOrange, colorMagneta, colorTeal,  colorWhite
};

// SparkFun RGB LED backpack frame geometry

#define rowLen 8
#define colLen 8

typedef struct
{
  ledcolorType pixels[rowLen][colLen];
} frameType;

static void SetFrameToColor(frameType& frame, const ledcolorType color)
{
  for (unsigned int col = 0; col < colLen; col++)
    for (unsigned int row = 0; row < rowLen; row++)
      frame.pixels[row][col] = color;
}

static void SetFrameToBlack(frameType& frame) {SetFrameToColor(frame, colorBlack);}

static unsigned char PickRandomColor(void) {return colors[random(colorLen)];}

static void ChangeOneRandomPixelToColor(frameType& frame, const ledcolorType color)
{
  frame.pixels[random(rowLen)][random(colLen)] = color;
}

static void SetBackpackSelectStatus(const unsigned int status)
{
  digitalWrite(CHIPSELECT, status); delayMicroseconds(500);
}

static void BackpackEnable(void)  {SetBackpackSelectStatus(LOW);}
static void BackpackDisable(void) {SetBackpackSelectStatus(HIGH);}

static unsigned char WriteSPIByte(unsigned char data)
{
  SPDR = data; while (!(SPSR & (1 << SPIF))); return SPDR;
}

static void SetBackpackDaisyChainLength(const unsigned int length)
{
  BackpackEnable();
  WriteSPIByte('%'); WriteSPIByte(length);  
  BackpackDisable();
}

static void WriteFrame(const frameType& frame)
{
  BackpackEnable();
  for (unsigned int row = 0; row < rowLen; row++)
    for (unsigned int col = 0; col < colLen; col++)
      WriteSPIByte(frame.pixels[row][col]);
  BackpackDisable();
}

// The last real frame displayed

static frameType realframe;

// One time setup

void setup()
{
  // Initialize the Arduino SPI pin directions
  
  pinMode(CHIPSELECT, OUTPUT);
  pinMode(DATAIN,     INPUT);      
  pinMode(DATAOUT,    OUTPUT);      
  pinMode(SPICLOCK,   OUTPUT);

  // Magical initialization of the SPI registers

  volatile byte clr;

  DDRB = (1 << CS) | (1 << MOSI) | (1 << SCK);
  DDRD = (1 << TX_OUT);
  SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1);
  SPSR = SPSR & 0xfe;  
  clr = SPSR; clr = SPDR;

  // Set the backpack to its default state

  BackpackDisable();
  SetBackpackDaisyChainLength(1);  // Be careful with this one

  // Initial display is all black pixels

  SetFrameToBlack(realframe); WriteFrame(realframe);
}

// Main loop

void loop()
{
  delay(100);

  ChangeOneRandomPixelToColor(realframe, PickRandomColor());
  WriteFrame(realframe);
}

How about some pictures? :sunglasses:

I'll try to put up a video after I get a youtube account.