/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_i2c.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "misc.h"
#include "stdio.h"
#include "func.h"

/* Global variables ----------------------------------------------------------*/
uint16_t test1;
float test2,test3;
/* Private functions ---------------------------------------------------------*/
void Delay(volatile uint32_t nCount){
 	  while(nCount--){}
}
void LED_toggle(volatile uint32_t DelayCount){
	Delay(DelayCount/3);
	LED_off; // or toggle GPIOC->ODR ^= GPIO_Pin_12;
	Delay(DelayCount);
	LED_on;
}
/* Main program function -----------------------------------------------------*/
int main(void)
{
	uint8_t i;
	float temperature;
	float humidity;
	uint16_t CO2;
	volatile char messtr[23];	//
	uint8_t MesVal[11]; // pro zpravu podle protokolu, ne jako ascii
	/* Initialization */
	InitGPIO();
	InitI2C1();
	InitTempSens(11);
	InitADC1();
	InitUSART1();
	InitUSART2();
	CalibTemp(DEFAULT,0); 	// prvotni kalibrace mereni teploty zalozena na merenich
	CalibCO2(DEFAULT,0); 	// prvotni kalibrace mereni CO2 zalozena na merenich
	/* Start by receiving module's ID or pressing the button */
	while((USART_ReceiveData(USART1) != MODULE_ID) && (USART_ReceiveData(USART2) != MODULE_ID) && !button){
		LED_toggle(300000);
	}
    while(1)
    {
    	sendreq = 0;
    	while(button); // cyklus muze byt prerusen drzenim tlacitka
    	temperature = 0;
    	humidity = 0;
    	CO2 = 0;
    	for(i=0 ; i<5 ; i++){
    		LED_toggle(15000000);
    		temperature += Temp1()/5;//GetTemp()/5;
    		humidity += GetHum()/5;
    		//CO2 += GetCO2()/5;	// vyuziva komunikaci po I2C
    		CO2 += GetCO2ADC()/5;	// ADC cteni analogoveho vystupu

    		test1 = CO2Val();
    		test2 = Temp1();
    		test3 = Temp2();
    	}
    	//prepocty hodnot na jednotlive ascii znaky
    	messtr[0]=0x30+MODULE_ID;
    	messtr[1]=0x20;//space
    	messtr[2]=0x20;//space
    	messtr[3]=temperature / 10 +0x30;//4
    	messtr[4]=temperature - (messtr[3]-0x30)*10 +0x30;//8
    	messtr[5]=0x2e;//.
    	messtr[6]=temperature*10 - 100*(messtr[3]-0x30) -10*(messtr[4]-0x30) +0x30;//2
    	messtr[7]=temperature*100 - 1000*(messtr[3]-0x30) -100*(messtr[4]-0x30) -10*(messtr[6]-0x30) +0x30;//5

    	messtr[8]=0x20;//space
    	messtr[9]=0x20;//space
    	messtr[10]=humidity / 10 +0x30;//4
    	messtr[11]=humidity - (messtr[10]-0x30)*10 +0x30;//8
    	messtr[12]=0x2e;//.
    	messtr[13]=humidity*10 - 100*(messtr[10]-0x30) -10*(messtr[11]-0x30) +0x30;//2
    	messtr[14]=humidity*100 - 1000*(messtr[10]-0x30) -100*(messtr[11]-0x30) -10*(messtr[13]-0x30) +0x30;//5

    	messtr[15]=0x20;//space
    	messtr[16]=0x20;//space
    	messtr[17]=CO2 / 10000 +0x30;//5
    	messtr[18]=CO2 / 1000 -10*(messtr[17]-0x30) +0x30;//4
    	messtr[19]=CO2 / 100 -100*(messtr[17]-0x30) -10*(messtr[18]-0x30) +0x30;//3
    	messtr[20]=CO2 / 10 -1000*(messtr[17]-0x30) -100*(messtr[18]-0x30) -10*(messtr[19]-0x30) +0x30;//2
    	messtr[21]=CO2 -10000*(messtr[17]-0x30) -1000*(messtr[18]-0x30) -100*(messtr[19]-0x30) -10*(messtr[20]-0x30)+0x30;//2
    	messtr[22]='\r';

        MesVal[0] = 11;									// delka zpravy v bytech
        MesVal[1] = 0;									// destinace
        MesVal[2] = MODULE_ID;									// tento modul
        MesVal[3] = 0x81;								// measurement response
    	MesVal[4] = (uint8_t)temperature;				// teplota jednotky °C
    	MesVal[5] = (float)(temperature-MesVal[0])*100;	// teplota setiny °C
    	MesVal[6] = (uint8_t)humidity;					// vlhkost jednotky %
    	MesVal[7] = (float)(humidity-MesVal[2])*100;	// teplota setiny %
    	MesVal[8] = (CO2 & 0xff00)>>8;					// MSB hodnoty CO2 ppm
    	MesVal[9] = (CO2 & 0x00ff);						// LSB hodnoty CO2 ppm
    	MesVal[10] = 00;								// ukoncovaci znak

    	while(sendreq != 1 && !button){}
        USART_puts(USART1, messtr);// FTDI
        USART_puts(USART2, messtr);// xBee
        /* pro posilani dat po bytech, ne jako ascii
    	USART_message(USART1, MesVal);	// zprava pres FTDI->USB
    	USART_message(USART2, MesVal);	// zprava pres xBee
         */
    }
}


