#include "C:\Cizelu\Documents\MLAB\projekty\cizelu\Maturitni_prace\Program\PIC\main.h"

//univerzalni LED diody
#define  LED1     PIN_D0
#define  LED2     PIN_D1

//piezo pipak
#DEFINE SOUND_HI   PIN_B1
#DEFINE SOUND_LO   PIN_B2

//naraznik
#define  BUMPL    !input(PIN_D2)
#define  BUMPR    !input(PIN_D3)

//nouzove senzory
#define  LINEL    !input(PIN_A1)
#define  LINER    !input(PIN_A2)
int   line_l;
int   line_r;

// motory


//PODPROGRAMY
//SENZORY
void cti_bocni_senzory()
{
   set_adc_channel(LINEL);    //cti levy nouzovy senzor
   delay_us(10);
   line_l=read_adc();
   set_adc_channel(LINER);    //cti pravy nouzovy senzor
   delay_us(10);
   line_r=read_adc();
}

//PIPAK
void beep(unsigned int16 period, unsigned int16 length)
{
   unsigned int16 bp;   //promenna pro nastaveni delky
   
   for(bp=length;bp>0;bp--)
   {
     output_high(SOUND_HI);output_low(SOUND_LO);
     delay_us(period);
     output_high(SOUND_LO);output_low(SOUND_HI);
     delay_us(period);
   }
}

//DIAGNOSTIKA A SLEDOVANI CARY
void diagnostika()   //spusti diagnostiku
{
   output_low(LED2);
   while(true)
   {
      cti_bocni_senzory();
   }
}

void stopuj()     //zacne jezdit po care
{
   output_low(LED1);
   while(true)
   {
      cti_bocni_senzory();
   }
}

// HLAVNI SMYCKA
void main()
{
   // NASTAVENI > provede se pouze pri zapnuti
   setup_adc_ports(ALL_ANALOG);          // vsechny ADC porty > analogove vstupy
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16,255,1);  //casovac pro PWM
   setup_ccp1(CCP_PWM);                // povoli PWM na pinu RC2
   setup_ccp2(CCP_PWM);                // povolĂ­ PWM na pinu RC1
   setup_comparator(NC_NC_NC_NC);
   while(true)
   {
      if(BUMPL==1)
      {
         diagnostika();
      }
      if(BUMPR==1)
      {
         stopuj();
      }
   
   }

}