/**** Automatic weather station 01A ****/
#define VERSION "0.1"
#define ID "$Id: main.c 3083 2013-06-18 10:50:31Z kaklik $"
#include "main.h"
#include ".\common\dbloader.h"
#include <string.h>

#CASE    // Case sensitive compiler

#define  SEND_DELAY     50       // Time between two characters on RS232

char  VER[4]=VERSION;   // Buffer for concatenate of a version string

#define ONE_WIRE_PIN       PIN_B1    // DS18B20 sensor connection
#include "..\ds1820.c"

#define sht_data_pin   PIN_D0    // SHT11 sensor connection 
#define sht_clk_pin    PIN_D1
#include "..\SHT.c"

#use i2c(master, sda=PIN_D2, scl=PIN_D3)
#include "..\SHT25.h"

#define CSN_SPI PIN_C2        // preassure sensor connection
#include "..\MPL115A1.c"

unsigned int16 timer0_overflow_count;
float anemo;

int1  barometer_present;

#int_TIMER1
void  TIMER1_isr(void) 
{
// wind speed calculation 32.768 kHz crystal on timer1 oscilator expected.
  anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(32768.0/0xFFFF); // pulses per second calculation
  anemo = anemo / 10.5;  // frequency divided by anemomether constant.

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

#int_TIMER0             // anemometr pulses counting timer owerflow
void  TIMER0_isr(void) 
{
 timer0_overflow_count++;
}

/*#int_default
void default_isr()
{
   printf("Unexplained interrupt\r\n");
}
*/
void welcome(void)               // Welcome message
{
   char  REV[50]=ID;       // Buffer for concatenate of a version string

   if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0;
   printf("\r\n\r\n# AWS01A %s (C) 2013 www.mlab.cz \r\n",VER);   // Welcome message
   printf("#%s\r\n",&REV[4]);
//   printf("# ver seq ");
//   printf("#temp[mK] hum_temp[mK] hum[%%] ");
//   printf("bar_temp[mK] pressure[hPa]  Anemo[m/s]check\r\n\r\n");
}

void main()
{
unsigned int16 seq=0;

   setup_oscillator(OSC_8MHZ);       // pri prouziti bootloaderu neni treba nastavovat
   setup_wdt(WDT_2304MS);
   restart_wdt();  //---WDT
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2);
   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
   setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
   output_high(CSN_SPI);

   welcome();   // welcome print and device indentification
   
   enable_interrupts(INT_TIMER1);   // interrupts used for anemometer readings
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

   restart_wdt();  //---WDT

   // barometer init
   barometer_present = MPL_init();  // get correction coefficients from the sensor 

   sht_init();

   SHT25_soft_reset();
   
   // anemometer init
   set_timer0(0);
   set_timer1(0);
   timer0_overflow_count=0;
   anemo=0;
   
   restart_wdt();  //---WDT

   while (TRUE)
   {
   char output[8];   // Output buffer
   int8 j;           // String pointer
   int8 check=0;     // Checksum is calculated between '$' and '*'
   float SHT_temp1=0,SHT_hum1=0;
   float SHT_temp2=0,SHT_hum2=0;
   float local_temp; 
   float barometer_temperature, barometer_pressure;

     delay_ms(1000);
         { // printf

     local_temp = ds1820_read()+27315;
     sht_rd(SHT_temp1,SHT_hum1);
     SHT_temp1 = (SHT_temp1 + 273.15)*100;
     
     SHT_temp2 = SHT25_get_temp();
     SHT_hum2 = SHT25_get_hum();
     SHT_temp2 = (SHT_temp2 + 273.15)*100;

     if (barometer_present == TRUE)
     {
       barometer_temperature =  (MPL_get_temperature() + 273.15)*100;
       barometer_pressure = MPL_get_pressure() * 10.0; // conversion to hectopascals
     }
     else
     {
       barometer_temperature =  0;
       barometer_pressure = 0;
     }
     
         delay_us(SEND_DELAY);
         putc('$');
         delay_us(SEND_DELAY);
         sprintf(output,"AWS%s \0",VER);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%Lu \0", seq);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%5.0f \0", local_temp );
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%5.0f \0", SHT_temp1);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%3.1f \0", SHT_hum1);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%5.0f \0", SHT_temp2);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%3.1f \0", SHT_hum2);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%5.0f \0", barometer_temperature);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%5.1f \0", barometer_pressure);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%3.1f \0", anemo);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"*%X\r\n\0", check);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j++]); }
         delay_us(SEND_DELAY);
      }

//---WDT
      restart_wdt();
            seq++;        // Increment the number of measurement
   }
}