/**** Automatic weather station 01A ****/
#define VERSION "0.1"
#define ID "$Id: main.c 2825 2013-03-10 14:57:58Z kaklik $"
#include "main.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_C7    // DS18B20 sensor connection
#include "..\ds1820.c"

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

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

unsigned int16 timer0_overflow_count=0;
float anemo=0;


#int_TIMER1
void  TIMER1_isr(void) 
{
 output_toggle(PIN_D1);
 anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(32768.0/0xFFFF);
 timer0_overflow_count=0;
 set_timer0(0);
}

#int_TIMER0
void  TIMER0_isr(void) 
{
 timer0_overflow_count++;
}



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("#\r\n");
//   printf("# commands: h, c, o, l, x, i, r, a, s, u\r\n");
//   printf("# h_eat, c_old, o_pen, l_ock, x_open, ");
//   printf("i_nfo, r_epeat, a_uto, s_single, u_pdate\r\n");
//   printf("#\r\n");
   printf("# ver seq  temp[K] hum_temp[K] hum[%%] ");
   printf("bar_temp[K] pressure[hPa] check\r\n\r\n");

//---WDT
   restart_wdt();
}

void main()
{
unsigned int16 seq=0;

   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_oscillator(OSC_8MHZ);
   setup_wdt(WDT_1152MS|WDT_DIV_16);
   setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
   output_high(CSN_SPI);


   delay_ms(100); 

   welcome();

   sht_init();
   MPL_init();  // get correction coefficients from the sensor 

//   enable_interrupts(INT_TIMER1);   // interrupts used for anemometer readings
//   enable_interrupts(INT_TIMER0);
//   enable_interrupts(GLOBAL);

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

     delay_ms(100);
         { // printf

     local_temp = ds1820_read()+273.15;
     sht_rd(SHT_temp,SHT_hum);
     SHT_temp += 273.15;
//     barometer_temperature =  MPL_get_temperature() + 273.15;
//     barometer_pressure = MPL_get_pressure() * 10.0; // conversion to hectopascals 
     
         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,"%f \0", local_temp );
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%f \0", SHT_temp);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%3.1f \0", SHT_hum);
         j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; }
         sprintf(output,"%f \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
   }
}
#include ".\common\dbloader.h"
//#include "dbloader.c" // Space reservation for the BootLoader