/************************************************************
* Show images on LEDs - POV (Persistence of Vision)         *  
*                                                           *  
* 20051231                                                  *  
* sklarm@electric-clothing.com                              *  
*                                                           *  
************************************************************/
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>

#define TIMER0_PRESCALER_1 0x01
#define TIMER0_PRESCALER_8 0x02
#define TIMER0_PRESCALER_64 0x03
#define TIMER0_PRESCALER_256 0x04
#define TIMER0_PRESCALER_1024 0x05

#define IMGWIDTH 11

SIGNAL(SIG_OVERFLOW0);

/* 
 *  Text file based automated coordinated
 *  RFID
 */
static int graphic_pa[] = {0x7f,0x1,0x9,0x1,0x9,0x15,0x13,0x8,0x0};
static int graphic_pb[] = {0x7f,0x9,0x3,0x3,0x1,0x1,0x1,0x0,0x0};
static int graphic_pc[] = {0x41,0x41,0x41,0x7f,0x41,0x3,0x3,0x0,0x0};
static int graphic_pd[] = {0x7f,0x41,0x41,0x41,0x41,0x41,0x3e,0x0,0x0};

/*
 * pixel movement counter
 */
int i=0;

int main(void)
{

        /* 
         * Enable PORT{A,B,C,D} 0-7 as outputs
         */
        DDRA=0xFF;
        DDRB=0xFF;
        DDRC=0xFF;
        DDRD=0xFF;

        /* 
         * prescaler setup for timer0
         */
        TCCR0=TIMER0_PRESCALER_256;

        /* 
         * start timer with nothing
         */
        TCNT0=0x00;

        /*
         * set lowest BIT of TIMSK to enable overflow interrupt
         */
        timer_enable_int(1<<TOIE0);

        /*
         * Set (global) Enable Interrupts
         */
        sei();

        while(1);

}


/* 
 * Timer0 Interrupt Service Routine
 * - 8bit
 * - rollover occurs at 255
 * - TCCR0 - Timer Counter Control Register
 * - 256 divide ( CS00 = 0 / CS01 = 0 / CS02 = 1 )
 * - 1MHz (clock) / 256 prescalar = 3906.25
 * - 1 / 3906.25 (period) = .256ms = 256us
 * - requires reload of (256 - 234) 
 * - reload 22
 * - 22us
 * - frequency 5405Hz
 * - Flash letters at 5405Hz
 */
SIGNAL(SIG_OVERFLOW0) {

        /* 
         * reload timer 
         * prescalar 256 
         */
        TCNT0=0xEA;

        /* 
         * restart message 
         */
        if ( i >= IMGWIDTH ) {
                i=0;
        }

        /*
         * display graphic codes on PORTS A,B
         */
        PORTA = graphic_pa[i];
        PORTB = graphic_pb[i];
        PORTC = graphic_pc[i];
        PORTD = graphic_pd[i];
        i++;
}