#include ".\main.h"

#define LED1 PIN_C6  //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
#define LED2 PIN_C5  //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
#define LED3 PIN_C4  // pulse detect
#define LED4 PIN_D3  //CHANGE PIN_XX TO YOUR LED PIN NUMBER

#define S1 PIN_C2  // raw up 
#define S2 PIN_C3  // raw down   
#define S3 PIN_D0  // fine up
#define S4 PIN_D1  // fine down 
#define S5 PIN_B1  // time setup
#define S6 PIN_B2  // treshold setup

#define BEEP PIN_D2  //piezo beeper

#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>


unsigned int32 pulse_count=0;
unsigned int16 overflow_count=0;
unsigned int16 time_overflow_count=0;


#int_RTCC
void  RTCC_isr(void) 
{
   time_overflow_count++;
}

#int_TIMER1
void  TIMER1_isr(void) 
{
   overflow_count++;
   output_toggle(LED3);
   output_toggle(BEEP);
}

void sound_beep( unsigned int lenght, int16 frequency)
{
unsigned int i;
   
   for(i=0;i<=lenght;i++)
   {
      output_toggle(BEEP);
      delay_us(1/frequency);
   }
}

#define  TRESHOLD_setup 1
#define  INTERVAL_setup 0


void main()
{
unsigned int16 integration_time=60;
unsigned int16 time;
unsigned int16 last_timer; // promena pro praskani
unsigned int1  button_press,setup_mode=INTERVAL_setup;  // semafor pro cteni tlacitek
unsigned int16 measurement_number=0;
unsigned int8  treshold=6;

   setup_adc_ports(sAN0|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_32);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   setup_oscillator(OSC_8MHZ);

   set_pwm1_duty(0);
   set_pwm2_duty(0);

   output_high(LED1);
   output_high(LED2);
   output_high(LED3);
   output_high(LED4);
   output_low(BEEP);

   setup_comparator(CP2_A0_VREF|CP2_OUT_ON_A5);  // sets two comparators(A1 and VR and A2 as the output)
   setup_vref(VREF_HIGH|treshold);   //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V
   enable_interrupts(INT_COMP);  //enables the comparator interrupt
   enable_interrupts(INT_RTCC);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(GLOBAL);
   lcd_init();

   lcd_putc("\fGM counter V1.0 \n   2013 MLAB");
   printf("Geiger-Muller Counter V1.0 \r\n");
   printf("(c) 2013 MLAB and UST.cz \r\n");
   Delay_ms(1000);
   
   lcd_putc("\f");
   printf("\r\n");
   
   set_timer1(0);

   lcd_gotoxy(1,1);     // vypsani hodnoty count, abz byla na LCD hned od zapnuti 
   printf(lcd_putc,"Count:%lu     ",pulse_count);

   while(true)
   {
      pulse_count = get_timer1() + (0xffff * overflow_count); // compute pulse count
      time = integration_time + 1 - (time_overflow_count * 0.0327); // compute time interval
      
      if(get_timer1() != last_timer)  // timer count increment detection
      {
         output_toggle(BEEP);
         delay_ms(1);
         output_toggle(BEEP);
         output_toggle(LED3);
         last_timer=get_timer1();
         lcd_gotoxy(1,1);     // prepsani dat na displeje 
         printf(lcd_putc,"Count:%lu     ",pulse_count);
      }
      
      lcd_gotoxy(1,2);
      printf(lcd_putc,"T:%3lu I:%3lu U:%2u ",time,integration_time,treshold);
//      printf(lcd_putc,"I:%4lu ", integration_time);

      if(time == 0)     // po uplynuti mereneho intervalu  vypis na seriovku  
      {
         set_timer0(0);
         time_overflow_count=0;
         sound_beep(1000,700);
         printf("$GMC1.0 %lu %lu %lu  \r\n", measurement_number, integration_time, pulse_count);
         set_timer1(0);
         overflow_count=0;
         measurement_number++;
      }

      if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta.
      {
         if(!input(S1))
         {
            delay_ms(20);
            if(!input(S1))
            {
               button_press=true;
               sound_beep(100,700);
               switch (setup_mode)
               {
                  case INTERVAL_setup:
                     if(integration_time < 0xfffa ) integration_time+=5;
                  break;
                  
                  case TRESHOLD_setup:
                     if(treshold < 0x0a ) treshold +=5;
                     setup_vref(VREF_HIGH|treshold);   //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V
                  break;
               }
            }
         }

         if(!input(S2))
         {
            delay_ms(20);
            if(!input(S2))
            {
               button_press=true;
               sound_beep(100,600);
               switch (setup_mode)
               {
                  case INTERVAL_setup:
                     if(integration_time > 0x0005 ) integration_time-=5;
                     set_timer0(0);  // vynulovani pocitadel, kdyz uzivatel sahne na tlacitka
                     time_overflow_count=0;               
                     set_timer1(0);
                     overflow_count=0;
                  break;
                  case TRESHOLD_setup:
                     if(treshold > 0x05 ) treshold +=5;
                     setup_vref(VREF_HIGH|treshold);   //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V
                  break;
               }
            }
         }

         if(!input(S3))
         {
            delay_ms(20);
            if(!input(S3))
            {
               button_press=true;
               sound_beep(100,500);
               switch (setup_mode)
               {
                  case INTERVAL_setup:
                     if(integration_time < 0xffff ) integration_time++;
                  break;

                  case TRESHOLD_setup:
                     if(treshold < 0x0F ) treshold ++;
                     setup_vref(VREF_HIGH|treshold);   //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V
                  break;
               }
            }
         }

         if(!input(S4))
         {
            delay_ms(20);
            if(!input(S4))
            {
               button_press=true;
               sound_beep(100,400);
               switch (setup_mode)
               {
                  case INTERVAL_setup:
                  
                     if(integration_time > 0x0001 ) integration_time--;

                     set_timer0(0);  // vynulovani pocitadel, kdyz uzivatel sahne na tlacitka
                     time_overflow_count=0;               
                     set_timer1(0);
                     overflow_count=0;
                  break;
                  case TRESHOLD_setup:
                     if(treshold > 0x01 ) treshold--;
                     setup_vref(VREF_HIGH|treshold);   //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V
                  break;
               }
            }
         }
         
         if(!input(S5))    // prepnuti na  nastavovani casu
         {
            delay_ms(20);
            if(!input(S5))
            {
              button_press=true;
              sound_beep(100,800);
              setup_mode=INTERVAL_setup;
              lcd_gotoxy(16,1);     // prepsani dat na displeje 
              printf(lcd_putc,"I");
            }
         }
         
         if(!input(S6))    // prepnuti na nastavovani napeti
         {
            delay_ms(20);
            if(!input(S6))
            {
              button_press=true;
              sound_beep(100,800);
              setup_mode=TRESHOLD_setup;
              lcd_gotoxy(16,1);     // prepsani dat na displeje 
              printf(lcd_putc,"U");
            }
         }
      }
      
      if ( input(S1) && input(S2) && input(S3) && input(S4) && input(S5) && input(S6))  // detekce pustenych tlacitek 
      { 
         button_press=false;
      }
   }
}