//Meteorologicka cast
#define VERSION "0.1"
#define AUTOR "Jan Chroust"
#define DATE "15.4.2013"

#include <main.h>
#include <math.h>
#include <string.h>

#include "SHT25.h"
#include "MPL3115.h"
#include "HMC5883L.h"

unsigned int16 timer0_overflow_count;
float anemo=0;

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

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

}

#int_TIMER0 //pro preteceni èítaèe pùlzù od anemometru (RA4)
void  TIMER0_isr(void) 
{
 timer0_overflow_count++;
}


float azimut (void) //vypocet azimutu smeru vetru
{
signed int16 X,Y,Z;
X= mag_readX();  
Y= mag_readY();
Z= mag_readZ();

float a, b;
a=(float)Y/X;
b=atan(a);
b = (b/3.14)*180;
b=abs(b);

if(X==0) //osetreni proti deleni 0
   { 
       if(Y>0)
         { 
         b=90;
         }
      else
         {
         b=270;
         }
   }  
   else
      {
      if(X>0)
       {
       if(Y>=0)
       {
         b=180+b;
       
       }
       else
       {
         b=180-b;
       
       }
       }
      else
       {
       if(Y>=0)
       {
         b=360-b;
      
       }
       else
       {
         b=b;
       
       }
       }
         
      }   

return b;
   
}


void uvitani(void)    // uvodni zprava
{
   printf("\r\n\r\n# Meteorologicka stanice %s (C) 2013 www.mlab.cz \r\n",VERSION); 
   printf("\r\n# Autor: %s Posledni uprava: %s \r\n",AUTOR, DATE);// Welcome message
   printf("# ver poradi ");
   printf("altimet_t[°C] altimet_a[m] altimet_p[Pa] ");
   printf("sht_t[°C] sht_h[%%]  Anemo[m/s]check\r\n\r\n");
}

void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard


float altimet_t;  //teplota z MPL3115
float altimet_p;  //tlak z MPL3115
float altimet_a;  //vyska z MPL3115
float sht25_t;    //teplota z SHT25
float sht25_h;    //relativni vlhkost z SHT25
float smer_v;     //smer vetru
 
unsigned int8 sht_config;

//nastaveni SHT25
SHT25_soft_reset();
sht_config = SHT25_RH12_T14 | SHT25_HEATER_OFF; //vypnuti topeni v SHT25
SHT25_setup(sht_config);

//nastavení pøerušení pro anemometr
enable_interrupts(INT_TIMER1);   
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);


//uvitani();
set_mag(); //nastaveni magnetometru pro smer vetru

// vynulovani promenych pro anemometr
set_timer0(0);
set_timer1(0);
timer0_overflow_count=0;
anemo=0;
   
   

while(TRUE)
   {
  
   char dataOUT[60];

   mpl3115_setP(); //nastaveni pro tlak a teplotu
   delay_ms (500);
   altimet_t=mpl3115_T();
   altimet_p=mpl3115_P();
   
   mpl3115_setA(); //nastaveni pro vysku a teplotu
   delay_ms (500);
   altimet_a=mpl3115_A();
   
   sht25_t=SHT25_get_temp(); //mereni hodnot z SHT25
   sht25_h=SHT25_get_hum();
  
   smer_v=azimut(); //vrati azimut aktualniho smeru vetru
  
 sprintf(dataOUT, " %6.2f %7.2f %9.2f %6.2f %6.2f %5.2f %6.2f", altimet_t, altimet_a, altimet_p, sht25_t, sht25_h, smer_v, anemo); 
 
  printf(dataOUT); //zaslani hodnot na seriovou linku
  int i=0;
  for(i=0;i<40;i++)
  {
  delay_ms(1000);
  }
  }

}