#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>

char cBlocks [ 16 ];
char cSerial = 0;
char cSerialFlag = 0;
char sSerialString [ 16 ];
char cSerialIndex = 0;

ISR ( USART0_RX_vect )
{
   cSerial = UDR0;
   cSerialFlag = 1;
}

void serialInput()
{
   switch ( cSerial )
   {
      case '{': 
      {
         cSerialIndex = 0;
      }
      break;
      case '}': processSerial();break;
      default:
      {
         sSerialString [ cSerialIndex ] = cSerial;
         cSerialIndex ++;
         sSerialString [ cSerialIndex ] = 0;
      }
   }
   cSerialFlag = 0;
}

void processSerial()
{
   if ( sSerialString [ 0 ] == 'B' )
   {
      switch ( sSerialString [ 1 ] )
      {
         case '1': cBlocks [ 1 ] = sSerialString [ 2 ] - 48;
         case '2': cBlocks [ 2 ] = sSerialString [ 2 ] - 48;
         case '3': cBlocks [ 3 ] = sSerialString [ 2 ] - 48;
         case '4': cBlocks [ 4 ] = sSerialString [ 2 ] - 48;
         case '5': cBlocks [ 5 ] = sSerialString [ 2 ] - 48;
         case '6': cBlocks [ 6 ] = sSerialString [ 2 ] - 48;
         case '7': cBlocks [ 7 ] = sSerialString [ 2 ] - 48;
         case '8': cBlocks [ 8 ] = sSerialString [ 2 ] - 48;
         case '9': cBlocks [ 9 ] = sSerialString [ 2 ] - 48;
         case 'A': cBlocks [ 10 ] = sSerialString [ 2 ] - 48;
         case 'B': cBlocks [ 11 ] = sSerialString [ 2 ] - 48;
         case 'C': cBlocks [ 12 ] = sSerialString [ 2 ] - 48;
     }

     cSerialIndex = 0;
     sSerialString [ 0 ] = 0;
   }
}

void setBlocks()
{
   // Block 1
   if ( cBlocks [ 1 ] == 0 )
   {
      PORTA &= ~( 1 << PA0 | 1 << PA1 );
   }
   else if ( cBlocks [ 1 ] == 1 )
   {
      PORTA |= ( 1 << PA0 );
      PORTA &= ~( 1 << PA1 );
   }
   else if ( cBlocks [ 1 ] == 2 )
   {
      PORTA |= ( 1 << PA1 );
      PORTA &= ~( 1 << PA0 );
   }

   // Block 2
   if ( cBlocks [ 2 ] == 0 )
   {
      PORTA &= ~( 1 << PA2 | 1 << PA3 );
   }
   else if ( cBlocks [ 2 ] == 1 )
   {
      PORTA |= ( 1 << PA2 );
      PORTA &= ~( 1 << PA3 );
   }
   else if ( cBlocks [ 2 ] == 2 )
   {
      PORTA |= ( 1 << PA3 );
      PORTA &= ~( 1 << PA2 );
   }

   // Block 3
   if ( cBlocks [ 3 ] == 0 )
   {
      PORTA &= ~( 1 << PA4 | 1 << PA5 );
   }
   else if ( cBlocks [ 3 ] == 1 )
   {
      PORTA |= ( 1 << PA4 );
      PORTA &= ~( 1 << PA5 );
   }
   else if ( cBlocks [ 3 ] == 2 )
   {
      PORTA |= ( 1 << PA5 );
      PORTA &= ~( 1 << PA4 );
   }

   // Block 4
   if ( cBlocks [ 4 ] == 0 )
   {
      PORTA &= ~( 1 << PA6 | 1 << PA7 );
   }
   else if ( cBlocks [ 4 ] == 1 )
   {
      PORTA |= ( 1 << PA6 );
      PORTA &= ~( 1 << PA7 );
   }
   else if ( cBlocks [ 4 ] == 2 )
   {
      PORTA |= ( 1 << PA7 );
      PORTA &= ~( 1 << PA6 );
   }

   // Block 5
   if ( cBlocks [ 5 ] == 0 )
   {
      PORTB &= ~( 1 << PB0 | 1 << PB1 );
   }
   else if ( cBlocks [ 5 ] == 1 )
   {
      PORTB |= ( 1 << PB0 );
      PORTB &= ~( 1 << PB1 );
   }
   else if ( cBlocks [ 5 ] == 2 )
   {
      PORTB |= ( 1 << PB1 );
      PORTB &= ~( 1 << PB0 );
   }

   // Block 6
   if ( cBlocks [ 6 ] == 0 )
   {
      PORTB &= ~( 1 << PB2 | 1 << PB3 );
   }
   else if ( cBlocks [ 6 ] == 1 )
   {
      PORTB |= ( 1 << PB2 );
      PORTB &= ~( 1 << PB3 );
   }
   else if ( cBlocks [ 6 ] == 2 )
   {
      PORTB |= ( 1 << PB3 );
      PORTB &= ~( 1 << PB2 );
   }

   // Block 7
   if ( cBlocks [ 7 ] == 0 )
   {
      PORTB &= ~( 1 << PB4 );
      PORTC &= ~( 1 << PC0 );
   }
   else if ( cBlocks [ 7 ] == 1 )
   {
      PORTB |= ( 1 << PB4 );
      PORTC &= ~( 1 << PC0 );
   }
   else if ( cBlocks [ 7 ] == 2 )
   {
      PORTC |= ( 1 << PC0 );
      PORTB &= ~( 1 << PB4 );
   }

   // Block 8
   if ( cBlocks [ 8 ] == 0 )
   {
      PORTC &= ~( 1 << PC1 | 1 << PC2 );
   }
   else if ( cBlocks [ 8 ] == 1 )
   {
      PORTC |= ( 1 << PC1 );
      PORTC &= ~( 1 << PC2 );
   }
   else if ( cBlocks [ 8 ] == 2 )
   {
      PORTC |= ( 1 << PC2 );
      PORTC &= ~( 1 << PC1 );
   }

   // Block 9
   if ( cBlocks [ 9 ] == 0 )
   {
      PORTC &= ~( 1 << PC3 | 1 << PC4 );
   }
   else if ( cBlocks [ 9 ] == 1 )
   {
      PORTC |= ( 1 << PC3 );
      PORTC &= ~( 1 << PC4 );
   }
   else if ( cBlocks [ 9 ] == 2 )
   {
      PORTC |= ( 1 << PC4 );
      PORTC &= ~( 1 << PC3 );
   }

   // Block 10
   if ( cBlocks [ 10 ] == 0 )
   {
      PORTC &= ~( 1 << PC5 | 1 << PC6 );
   }
   else if ( cBlocks [ 10 ] == 1 )
   {
      PORTC |= ( 1 << PC5 );
      PORTC &= ~( 1 << PC6 );
   }
   else if ( cBlocks [ 10 ] == 2 )
   {
      PORTC |= ( 1 << PC6 );
      PORTC &= ~( 1 << PC5 );
   }

   // Block 11
   if ( cBlocks [ 11 ] == 0 )
   {
      PORTD &= ~( 1 << PD4 | 1 << PD5 );
   }
   else if ( cBlocks [ 11 ] == 1 )
   {
      PORTD |= ( 1 << PD4 );
      PORTD &= ~( 1 << PD5 );
   }
   else if ( cBlocks [ 11 ] == 2 )
   {
      PORTD |= ( 1 << PD5 );
      PORTD &= ~( 1 << PD4 );
   }

   // Block 12
   if ( cBlocks [ 12 ] == 0 )
   {
      PORTD &= ~( 1 << PD6 | 1 << PD7 );
   }
   else if ( cBlocks [ 12 ] == 1 )
   {
      PORTD |= ( 1 << PD6 );
      PORTD &= ~( 1 << PD7 );
   }
   else if ( cBlocks [ 12 ] == 2 )
   {
      PORTD |= ( 1 << PD7 );
      PORTD &= ~( 1 << PD6 );
   }

}

int
main ()
{
   // UART setup
   UCSR0A = ( 1 << U2X0 );
   UCSR0B = ( 1 << RXCIE0 ) | ( 1 << RXEN0 );
   UCSR0C = ( 1 << UCSZ00 ) | ( 1 << UCSZ01 );
   UBRR0L = 12;		// 9600 baud

   // GPIO setup
   DDRA = 255;		// All pins outputs
   DDRB = ( 1 << PB0 ) | ( 1 << PB1 ) | ( 1 << PB2 ) | ( 1 << PB3) | ( 1 << PB4 );	// Pins 0-4 outputs (6 & 7 are programming pins)
   DDRC = 255;		// All pins outputs
   DDRD = ( 1 << PD1 ) | ( 1 << PD3 ) | ( 1 << PD4 ) | ( 1 << PD5 ) | ( 1 << PD6 ) | ( 1 << PD7 );	// UART0 transmit, pins 4 - 7

   cBlocks [ 1 ] = 0;
   cBlocks [ 2 ] = 0;
   cBlocks [ 3 ] = 0;
   cBlocks [ 4 ] = 0;
   cBlocks [ 5 ] = 0;
   cBlocks [ 6 ] = 0;
   cBlocks [ 7 ] = 0;
   cBlocks [ 8 ] = 0;
   cBlocks [ 9 ] = 0;
   cBlocks [ 10 ] = 0;
   cBlocks [ 11 ] = 0;
   cBlocks [ 12 ] = 0;
   setBlocks();

   sei();
   while ( 1 )
   {
      if ( cSerialFlag == 1 ) serialInput();
      setBlocks();
   }
}
