/* Matrixanzeige (c) Oliver Mezger 15.02.2011
   Firmware fuer Prototyp 8x7 LED-Matrix
   Distributed under Creative Commons 3.0 -- Attib & Share Alike
   Synopsis:
    http://www.tvbgone.com/mfaire/ledcubekit/ledcube.c
	http://www.cse.ohio-state.edu/~stocka/LEDCube/
*/

#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>

struct imElt {
  unsigned char z[7];
  unsigned long int imageDuration;
}
const imageTab[] PROGMEM = {
 { 0b10010010, 0b10010010, 0b10010010, 0b11110010, 0b10010010, 0b10010010, 0b10010010, 10000 }, 
 { 0b11100111, 0b01001000, 0b01001000, 0b01001011, 0b01001001, 0b01001001, 0b01000110, 10000 }, 
 { 0b00000000, 0b00000000, 0b00101000, 0b00010000, 0b00101000, 0b00000000, 0b00000000, 2240 }, 
 { 0b00000000, 0b01111100, 0b01000100, 0b01000100, 0b01000100, 0b01111100, 0b00000000, 2240 }, 
 { 0b11111110, 0b10000010, 0b10000010, 0b10000010, 0b10000010, 0b10000010, 0b11111110, 2240 }, 
 { 0b10000010, 0b01000100, 0b00101000, 0b00010000, 0b00101000, 0b01000100, 0b10000010, 2240 }, 
 { 0b01000000, 0b00100010, 0b00101100, 0b00010000, 0b01101000, 0b10001000, 0b00000100, 2240 }, 
 { 0b00010000, 0b00010000, 0b00010000, 0b11111110, 0b00010000, 0b00010000, 0b00010000, 2240 }, 
 { 0b00111000, 0b01010100, 0b10010010, 0b11111110, 0b10010010, 0b01010100, 0b00111000, 2240 }, 
 { 0b00111000, 0b01000100, 0b10000010, 0b10000010, 0b10000010, 0b01000100, 0b00111000, 2240 }, 
 { 0b00010000, 0b00101000, 0b01000100, 0b10000010, 0b01000100, 0b00101000, 0b00010000, 2240 }, 
 { 0b00000000, 0b00010000, 0b00101000, 0b01000100, 0b00101000, 0b00010000, 0b00000000, 2240 }, 
 { 0b00000000, 0b00000000, 0b00010000, 0b00101000, 0b00010000, 0b00000000, 0b00000000, 2240 }, 
 { 0b00000000, 0b00000000, 0b00000000, 0b00010000, 0b00000000, 0b00000000, 0b00000000, 5000 }, 
 { 0b00000000, 0b11100111, 0b00011000, 0b00011000, 0b00000000, 0b01000010, 0b00111100, 15000 }, 
 { 0b01000010, 0b11100111, 0b01011010, 0b00011000, 0b00000000, 0b01000010, 0b00111100, 15000 }, 
 { 0b00000000, 0b01000010, 0b01011010, 0b00000000, 0b00111100, 0b01000010, 0b00111100, 15000 }, 
 { 0b01000010, 0b10100101, 0b01000010, 0b00011000, 0b00000000, 0b00100100, 0b00011000, 10000 }, 
 { 0, 0, 0, 0, 0, 0, 0, 0 },
};

void displayImage(int index){
  unsigned long int duration = pgm_read_dword( &imageTab[index].imageDuration );
  unsigned char zeile=1,p=0;
  for (int i=0; i<(duration/(10)); i++) {
	PORTB = pgm_read_byte( &imageTab[index].z[p] );
	PORTD = zeile;
	_delay_ms(1);
    zeile = zeile <<1;
	p++;
    if (zeile == 128){
	  zeile =1;
	  p=0;
	}
  }
}

int main(){
  DDRB = 0xff; // PB als Ausgang Spalten
  PORTB = 0xff;
  DDRD = 0xff;  // PD als Ausgang Zeilen
  while (1){ // Endlosschleife
    int i = 0;
    do {
      displayImage(i);
      i++;
    } while (pgm_read_byte(&imageTab[i].imageDuration) != 0);
  }
}
