#include "main.h"

#define REV "$Rev$"

unsigned int32 section[4];
unsigned int32 time;   // pocitadlo preteceni casovace

#Define INORDER 1

#int_RTCC
void  RTCC_isr(void)    // preruseni od pretekleho casovace 
{
  time++;
}

#define LCD_ENABLE_PIN  PIN_E0
#define LCD_RS_PIN      PIN_E1
#define LCD_RW_PIN      PIN_E2
#define LCD_DATA4       PIN_D4
#define LCD_DATA5       PIN_D5
#define LCD_DATA6       PIN_D6
#define LCD_DATA7       PIN_D7

#include <lcd.c>

#define BUTTON1   PIN_D0
#define BUTTON2   PIN_D1
#define BUTTON3   PIN_D2
#define BUTTON4   PIN_D3

#define START1 PIN_B0
#define STOP1  PIN_B1
#define START2 PIN_B2
#define STOP2  PIN_B3
#define START3 PIN_B4
#define STOP3  PIN_B5
#define START4 PIN_B6
#define STOP4  PIN_B7

#define BEEPER    PIN_C0

unsigned int32 run;

// Includes all USB code and interrupts, as well as the CDC API
#include <usb_cdc.h>

void beep(int16 period)
{
  while(period){
    output_toggle(BEEPER);
    delay_us(800);
    period--;
  }  
}

void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   port_b_pullups(TRUE);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

   lcd_init();
   usb_init();

   lcd_gotoxy(1,1);
   printf(lcd_putc,"\f STOPWATCH01A");
   lcd_gotoxy(1,2);
   printf(lcd_putc," Kaklik 2010");
   delay_ms(1000);

   while (TRUE)
   {

   printf(lcd_putc,"\fWaiting for PC... ");
   while(!usb_cdc_connected());  // pockej nez se pripoji seriovy port PC
   printf(lcd_putc,"\f PC connected...");
   beep(10);
   
   run=0;

   while(usb_cdc_connected())  // pockej nez se pripoji seriovy port PC
   {   
      run++;
      section[0] = 0;
      section[1] = 0;
      section[2] = 0;
      section[3] = 0;
      
     printf(lcd_putc,"\f\nSTART       TEST");
     
     while(input(BUTTON1))   // ceka na zmacknuti startovaciho tlacitka
     if(!input(BUTTON4))
     {
      printf(lcd_putc,"\f\n        ESC");
      while(input(BUTTON3))
      {   
         lcd_gotoxy(1,1);
         printf(lcd_putc,"%1u %1u %1u %1u %1u %1u %1u %1u",input(START1),input(STOP1),input(START2), input(STOP2),input(START3),input(STOP3),input(START4),input(STOP4));
         delay_ms(100);
      }
      printf(lcd_putc,"\f\nSTART       TEST");
     }
     printf(lcd_putc,"\f\n   STOP"); //

// mereni casu ve ctyrech usecich
     while((!input(START1)) && (input(BUTTON2)));     // cekame na tlacitko stop nebo na preruseni paprsku
     time = 0;
     set_timer0(0);
     while((!input(STOP1)) && (input(BUTTON2)))
     section[0] = (time << 16) + get_timer0();

     while((!input(START2)) && (input(BUTTON2)));     // cekame na tlacitko stop nebo na preruseni paprsku
     time = 0;
     set_timer0(0);
     while((!input(STOP2)) && (input(BUTTON2)))
     section[1] = (time << 16) + get_timer0();

     while((!input(START3)) && (input(BUTTON2)));     // cekame na tlacitko stop nebo na preruseni paprsku
     time = 0;
     set_timer0(0);
     while((!input(STOP3)) && (input(BUTTON2)))
     section[2] = (time << 16) + get_timer0();

     while((!input(START4)) && (input(BUTTON2)));     // cekame na tlacitko stop nebo na preruseni paprsku
     time = 0;
     set_timer0(0);
     while((!input(STOP4)) && (input(BUTTON2)))
     section[3] = (time << 16) + get_timer0();

     // odesli namerene hodnoty
     printf(usb_cdc_putc, "\n\r %7lu %7.1f %7.1f %7.1f %7.1f", run, section[0]/15.625, section[1]/15.625, section[2]/15.625, section[3]/15.625); //konstanta je kvuli prevodu do rozzumnych jednotek [ms]
   }
 }
}