Thing-a-day, Day 7: Finished driver and demo software

I didn’t like how the thing was working out under parallel port control, so I bumped it over to an Arduino clone that I had. I’ll post directions next; there is a really simple way to do the circuit that should only involve about 5 chips besides the Arduino board, so don’t let my crazy wiring scare you off. Source code is available after the break.

Smiley face drawn in color LEDs

/* LED driver for Arduino
 *
 * By Matt Mets, completed in 2008
 *
 * This code is released into the public domain.  Attribution is appreciated.
 */
 
/***** Board setup ************************************************************************/
 
/* Note that the data and row pins are accessed using the PORTB and PORTBD macros, so they are
 * not defined here.
 */
int redClock = 11;
int greenClock = 12;
int blueClock = 13;
int rowEnable = 14;
 
/***** Type Defines ***********************************************************************/
typedef struct row_data_t {
  byte red;
  byte green;
  byte blue;
} row_data_t;
 
/***** Library functions ******************************************************************/
 
/* Store the data byte in a color bank register */
int writeColor(byte data, int clockPort)
{
  PORTD = data;
  digitalWrite(clockPort, LOW);
  digitalWrite(clockPort, HIGH);
 
  return 0;
}
 
/* Cause a row to be displayed by writing its address to the DEMUX, then strobing the enable
 * line high for a short time.
 */
int strobeRow(byte row)
{
  PORTB = row;
  digitalWrite(rowEnable, HIGH);
  delay(1);
  digitalWrite(rowEnable, LOW);
 
  return 0;
}
 
/* Draw an entire page, by drawing each row in succession. */
int writePage(struct row_data_t* page)
{
  int i;
  for(i = 0; i < 7; i++)
  {
    // write each color, then hold the row high for a small time to show it
    writeColor(page[i].red,   redClock);
    writeColor(page[i].green, greenClock);
    writeColor(page[i].blue,  blueClock);
    strobeRow(i);
  }
 
  return 0;
}
 
 
/***** Picture definitions ****************************************************************/
 
/* Each one of these represents a still image to be displayed on the board.  The format is
 * (R,G,B), repeated for each row (note that we can only display 7 rows for now).
 */
 
row_data_t redPage[] = {{255,0,0},{255,0,0},{255,0,0},{255,0,0},
                        {255,0,0},{255,0,0},{255,0,0},{255,0,0}};
 
row_data_t greenPage[] = {{0,255,0},{0,255,0},{0,255,0},{0,255,0},
                          {0,255,0},{0,255,0},{0,255,0},{0,255,0}};
 
row_data_t bluePage[] = {{0,0,255},{0,0,255},{0,0,255},{0,0,255},
                         {0,0,255},{0,0,255},{0,0,255},{0,0,255}};
 
row_data_t yellowPage[] = {{255,255,0},{255,255,0},{255,255,0},{255,255,0},
                           {255,255,0},{255,255,0},{255,255,0},{255,255,0}};
 
row_data_t cyanPage[] = {{0,255,255},{0,255,255},{0,255,255},{0,255,255},
                         {0,255,255},{0,255,255},{0,255,255},{0,255,255}};
 
row_data_t purplePage[] = {{255,0,255},{255,0,255},{255,0,255},{255,0,255},
                           {255,0,255},{255,0,255},{255,0,255},{255,0,255}};
 
row_data_t whitePage[] = {{255,255,255},{255,255,255},{255,255,255},{255,255,255},
                          {255,255,255},{255,255,255},{255,255,255},{255,255,255}};
 
row_data_t facePageA[] = {{B00111110,B00111110,0},
                         {127,127,0},
                         {B01101011,B01101011,B00010100},
                         {127,127,0},
                         {B01011101,B01011101,B00100010},
                         {B01100011,B01100011,B00011100},
                         {B00111110,B00111110,0}};
 
 
row_data_t facePageB[] = {{B00111110,B00111110,0},
                         {127,127,0},
                         {B01111111,B01101011,B00000000},
                         {127,127,0},
                         {B01011101,B01011101,B00100010},
                         {B01100011,B01100011,B00011100},
                         {B00111110,B00111110,0}};
 
 
row_data_t linesA[] = {{255,0,0},{255,255,0},{0,255,0},{0,255,255},{0,0,255},{255,0,255},{255,0,0}};
row_data_t linesB[] = {{255,255,0},{0,255,0},{0,255,255},{0,0,255},{255,0,255},{255,0,0},{255,255,0}};
row_data_t linesC[] = {{0,255,0},{0,255,255},{0,0,255},{255,0,255},{255,0,0},{255,255,0},{0,255,0}};
row_data_t linesD[] = {{0,255,255},{0,0,255},{255,0,255},{255,0,0},{255,255,0},{0,255,0},{0,255,255},{0,0,255}};
row_data_t linesE[] = {{0,0,255},{255,0,255},{255,0,0},{255,255,0},{0,255,0},{0,255,255},{0,0,255},{255,0,255}};
row_data_t linesF[] = {{255,0,255},{255,0,0},{255,255,0},{0,255,0},{0,255,255},{0,0,255},{255,0,255},{255,0,255}};
 
 
 
/***** Demo functions ********************************************************************/
 
/* Test the display by showing each color in succession */
void colorDemo()
{
  int i,j;
 
  for(i = 0; i < 7; i++) {
    for(j = 0; j < 50; j++) {
      switch(i)
      {
        case 0:
          writePage(redPage);
          break;
        case 1:
          writePage(greenPage);
          break;
        case 2:
          writePage(bluePage);
          break;
        case 3:
          writePage(cyanPage);
          break;
        case 4:
          writePage(yellowPage);
          break;
        case 5:
          writePage(purplePage);
          break;
        case 6:
          writePage(whitePage);
          break;
      }
    }
  }
}
 
void faceDemo()
{
  int i;
  for(i = 0; i < 100; i++)
    writePage(facePageA);
 
  for(i = 0; i < 10; i++)
    writePage(facePageB);
}
 
/* Draw some fun lines */
void linesDemo()
{
  int i,j;
 
  for(i = 0; i < 6; i++) {
    for(j = 0; j < 10; j++) {
      switch(i)
      {
        case 0:
          writePage(linesA);
          break;
        case 1:
          writePage(linesB);
          break;
        case 2:
          writePage(linesC);
          break;
        case 3:
          writePage(linesD);
          break;
        case 4:
          writePage(linesE);
          break;
        case 5:
          writePage(linesF);
          break;
      }
    }
  }
}
 
 
/* Initialize the IO ports */
void setup()
{
  DDRD = B11111111;              // data port
  DDRB = B00111111;              // row addresses, data clock
  pinMode(rowEnable, OUTPUT);
}
 
/* Main loop */
void loop()
{
  int i;
 
  // Shift throug each of the colors in succession, repeating 10 times.
  for(i = 0; i < 2; i++)
    colorDemo();
 
  for(i = 0; i < 5; i++)
    faceDemo();
 
  for(i = 0; i < 10; i++)
    linesDemo();
 
  for(i = 0; i < 5; i++)
    faceDemo();  
}
This entry was posted in tech, thingaday. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>