#define VERSION "0.1"
#define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $"

#include "main.h"
#use i2c(SLAVE,Fast,sda=PIN_C4,scl=PIN_C3,force_hw,address=0xA2) // Motor 2

const int8 buf_len=8;

int8 buffer[buf_len];   // I2C buffer     

int8 address=0;

#include "..\common\dbloader.h"

unsigned int16 timer0_overflow_count;
unsigned int16 anemo=0;
unsigned int16 rain=0;

//we are using the rtctimer.c library, in which a counter is incremented
//every time the timer2 interrupt occurs (timer2 overflow).  the time math
//needs to know what rate the timer2 interrupt occurs.  this definition
//must match the rate the timer2 is configured for.
#define CLOCKS_PER_SECOND  1000

#INT_SSP
void ssp_interupt ()
{
BYTE incoming, state;

   state = i2c_isr_state();
   if(state < 0x80)                     //Master is sending data
   {
      incoming = i2c_read();
      if(state == 1)                     //First received byte is address
      {
         address = incoming;
         if (incoming == 2)
         {
            buffer[0]=make8(anemo,0);
            buffer[1]=make8(anemo,1);
            buffer[2]=make8(rain,0);
            buffer[3]=make8(rain,1);
         }
      }
      if(state == 2)                     //Second received byte is data
         buffer[address] = incoming;
   }
   if(state == 0x80)                     //Master is requesting data
   {
      if(address <= buf_len) i2c_write(buffer[address]);
      else i2c_write(ID[address - buf_len]);
   }
}


#int_TIMER0 //osetruje preteceni citace od anemometru (RA4)
void  TIMER0_isr(void) 
{
  timer0_overflow_count++;
}

#int_TIMER1
void  TIMER1_isr(void) 
{
// 32.768 kHz krystal pro timer1 oscilátor
  anemo = ((timer0_overflow_count * 0xFF) + get_timer0()); // pocet pulzu za 1s

  timer0_overflow_count=0; //nulovani
  set_timer0(0);
  set_timer1(0);
  output_toggle(PIN_E0);
}

#int_TIMER2
void  TIMER2_isr(void) 
{

}

#INT_EXT
void EXT_isr()  //interrup from rain sensor clip. 
{
  rain++;
//  if (input(PIN_B0)) ext_int_edge( H_TO_L );     osetreni pro pripad, ze by bylo treba cist obe hrany impulzu
//  if (!input(PIN_B0)) ext_int_edge( L_TO_H );
}


void welcome(void)    // uvodni zprava
{
   printf("\r\n\r\n# Meteorologicka stanice %s (C) 2013 www.mlab.cz \r\n",VERSION); 
   printf("\r\n %s \r\n",ID);// Welcome message
   printf("# ver poradi ");
   printf("check\r\n\r\n");
}

void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
//   setup_adc(ADC_CLOCK_DIV_2);
   setup_adc(ADC_OFF);
//   setup_spi(SPI_SS_DISABLED);  //must not be set if I2C are in use! 
   setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
//   setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_144MS);
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
//   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
//   setup_oscillator(OSC_8MHZ|OSC_INTRC);


   /* Setup timer 2
    * On a 4 Mhz clock, this will trigger a timer2 interrupt every 1.0 ms
    * For time.h to work properly, Timer2 must overflow every millisecond
    * OverflowTime = 4 * (1/OscFrequency) * Prescale * Period * Postscale 
    * For 4 Mhz: .001 seconds  = 4 * (1/4000000 seconds) * 4 * 250 * 1
    */
  #if getenv("CLOCK")==4000000)
   setup_timer_2(T2_DIV_BY_1,250,4);
  #elif getenv("CLOCK")==20000000)
   setup_timer_2(T2_DIV_BY_4,250,5);
  #else
   #error Configure TIMER2 so it interrupts at a rate defined by CLOCKS_PER_SECOND
  #endif
   

   enable_interrupts(INT_SSP); 
   enable_interrupts(INT_TIMER2);
   enable_interrupts(INT_TIMER1);    
   enable_interrupts(INT_TIMER0);
   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);


   set_timer0(0);
   set_timer1(0);
   timer0_overflow_count=0;

buffer[2]=0;
buffer[3]=0;
buffer[4]=0;
buffer[5]=0;

   welcome();

   while(true)
   {         
      printf("count: %X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]);
      printf("%Lu %Lu \n\r", anemo, rain); 
 
     delay_ms(1000);

   }
}