#include ".\infra.h"


#define MAX_SLOVO 10   // Maximalni pocet znaku ve slovu

int8 napis[MAX_SLOVO+1];
int8 otacky;

const int8 logo[MAX_SLOVO+1]={'[','[',92,'[','E','e','A','[',92,'[',0};   // [blesk] E4A [blesk]
const int8 rok[MAX_SLOVO+1]={'[','c','a','a','e',']','c','a','a','i',0}; // 2004-2008

#int_RDA
void  RDA_isr(void)
{
   int n;
   output_B(0x55);
   for(n=0;n<MAX_SLOVO;n++)
   {
     napis[n]=getc();
   }
   otacky=0;
}

void LED_print()
{
unsigned int slovo,pismeno,sloupec,j;

   const int8 pis[45*5]={0b00000001,0b11110110,0b11110110,0b11110110,0b00000001, // A
                        0b00000000,0b01110110,0b01110110,0b01110110,0b10001001,  // B
                        0b10000001,0b01111110,0b01111110,0b01111110,0b10111101,  // C
                        0b00000000,0b01111110,0b01111110,0b01111110,0b10000001,  // D
                        0b00000000,0b01110110,0b01110110,0b01111110,0b01111110,  // E
                        0b00000000,0b11110110,0b11110110,0b11111110,0b11111110,  // F
                        0b10000001,0b01111110,0b01110110,0b01110110,0b10000101,  // G
                        0b00000000,0b11110111,0b11110111,0b11110111,0b00000000,  // H
                        0b01111110,0b01111110,0b00000000,0b01111110,0b01111110,  // I
                        0b10111110,0b01111110,0b01111110,0b01111110,0b10000000,  // J
                        0b00000000,0b11110111,0b11110111,0b11101011,0b00011100,  // K
                        0b00000000,0b01111111,0b01111111,0b01111111,0b01111111,  // L
                        0b00000000,0b11111110,0b11110000,0b11111110,0b00000000,  // M
                        0b00000000,0b11101111,0b11011111,0b10111111,0b00000000,  // N
                        0b10000001,0b01111110,0b01111110,0b01111110,0b10000001,  // O
                        0b00000000,0b11110110,0b11110110,0b11110110,0b11111001,  // P
                        0b10000001,0b01111110,0b01111110,0b01111110,0b00000001,  // Q
                        0b00000000,0b11110110,0b11110110,0b11110110,0b00001001,  // R
                        0b10111001,0b01110110,0b01110110,0b01110110,0b10001101,  // S
                        0b11111110,0b11111110,0b00000000,0b11111110,0b11111110,  // T
                        0b10000000,0b01111111,0b01111111,0b01111111,0b10000000,  // U
                        0b11000000,0b10111111,0b01111111,0b10111111,0b11000000,  // V
                        0b10000000,0b01111111,0b10000000,0b01111111,0b10000000,  // W
                        0b00011100,0b11101011,0b11110111,0b11101011,0b00011100,  // X
                        0b11111000,0b11110111,0b00000111,0b11110111,0b11111000,  // Y
                        0b00011110,0b01101110,0b01110110,0b01111010,0b01111100,  // Z
                        0b11111111,0b11111111,0b11111111,0b11111111,0b11111111,  // mezera [  91
                        0b00010111,0b00110011,0b01010101,0b11100110,0b11110111,  // blesk  \  92
                        0b11110111,0b11110111,0b11110111,0b11110111,0b11110111,  // -      ]  93
                        0b11111111,0b11111111,0b01111111,0b11111111,0b11111111,  // .      ^  94
                        0b11111100,0b11110000,0b01000000,0b11110000,0b11111100,  // !      _  95
                        0b11111101,0b11111110,0b01001110,0b11110110,0b11111001,  // ?      `  96
                        0b00000000,0b01111110,0b01111110,0b01111110,0b00000000,  // 0      a
                        0b01111011,0b01111101,0b00000000,0b01111111,0b01111111,  // 1      b
                        0b00111101,0b01011110,0b01101110,0b01110110,0b01111001,  // 2      c
                        0b10111101,0b01111110,0b01110110,0b01110110,0b10001001,  // 3      d
                        0b11110000,0b11110111,0b11110111,0b00000001,0b11110111,  // 4      e
                        0b10111000,0b01110110,0b01110110,0b01110110,0b10001110,  // 5      f
                        0b10000001,0b01110110,0b01110110,0b01110110,0b10001101,  // 6      g
                        0b11111110,0b11111110,0b00001110,0b11110110,0b11111000,  // 7      h
                        0b10001001,0b01110110,0b01110110,0b01110110,0b10001001,  // 8      i
                        0b10111001,0b01110110,0b01110110,0b01110110,0b10000001,  // 9      j
                        0b11111111,0b01111110,0b10111101,0b11000011,0b11111111,  // )      k
                        0b11111111,0b11000011,0b10111101,0b01111110,0b11111111,  // (      l
                        0b11111111,0b11111111,0b11111111,0b11011011,0b11111111,  // :      m
                        };

   while(read_adc()<9);       // Cekani na magnet
   while(read_adc()>50);       // Cekani na opusteni magnetu
   delay_ms(7);      //cekani, na dostatecni odstup od magnetu, pred zacatkem psani textu

   for(pismeno=0;pismeno<MAX_SLOVO;pismeno++)
   {
      for(sloupec=0;sloupec<5;sloupec++)
      {
         output_B(pis[(napis[pismeno]-0x41)*5+sloupec]);
         delay_us(500);
      }
      output_B(0xFF);
      delay_us(500);
   }
}

void main()
{


   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_wdt(WDT_2304MS);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);

   set_adc_channel(0);     // Prevodnik pro Hall
   output_B(0xFF);         // Zhasneme

   otacky=0;
   napis[0]=0;

   while(TRUE)
   {
   int1 vyber;
   
      otacky++;
   
      if (napis[0]!=0 && otacky <= 30)
      {
         LED_print();
      }
      else
      {
         if (vyber == 0) strcpy(napis,logo);
         else strcpy(napis,rok);
         vyber++;
      }
   }
}