#ifndef _APPLICATION_H
#define _APPLICATION_H

#ifndef VERSION
#define VERSION "vdev"
#endif

#include <twr.h>
#include <bcl.h>
#include <stdio.h> //kvoli snprintf
#include <math.h> //skusam pracu s Pi ale nepotrebujem kniznicu


//prepocty hodnot z float na hexa zo senzorov, posielane na Sigfox
uint8_t temperature_to_hexa(float temp)
{
    //accuracy 0.5 C, range -40 to 88 C
    //rounding: 20.1>20, 20.251>2.5, 20.748>20.5, 20.750>21
    double des_cast = temp - floor(temp); //floor returns integer from its argument, if temp=15.248 des_cast is 0.248, if temp=15.51 des_cast is 0.51 
    
        if (des_cast < 0.25f)// || is logical or && is logical and
        {
            return (unsigned char)(80 + 2 * (floor(temp)));//pretypovanie na integer 1 B. 20.3 > 20   des_cast = 0.5 return 40 + 2 * 20 = 80 decimal. The same result with 19.8: 40 + 2 * 20 = 80 (spodna cast prikazu plati)
        } 
        else if ((des_cast >= 0.25) && (des_cast < 0.75))// && is logical and.  
        {
            return (unsigned char)(80 + 2 * (floor(temp) + 0.5f));//20.5 > 20   des_cast = 0.5 return 40 + 2 * 20 + 1 = 81 decimal
        }  
        else if (des_cast >= 0.75)//
        {
            return (unsigned char)(80 + 2 * (floor(temp)) + 1);//pretypovanie na integer 1 B.  20.75 > 21   des_cast = 0.75 return 40 + 2 * 20 + 1 = 82 decimal
        }      
        return 0;
}

uint8_t humidity_to_hexa(float hum)
{
        //accuracy 1%, range 0 to 100%
        return (unsigned char)(round(hum));
}

uint16_t illumination_to_hexa(float illum)
{   //range is from 0 to 100 000 lx (sunny day)
    //we use light 65 565 lux maximum. It is possible to store it to 2 Bytes
    //used bit shift to right
    //buffer[3] = value >> 8; from 2 bytes 1110 1100 1000 0000 will be saved 1110 1100 
    //buffer[4] = value; from 2 bytes 1110 1100 1000 0000 will be saved 1000 0000 
    //twr_log_debug("sdsdsdsds");
    //twr_log_debug((unsigned int)(round(illum)));
    return (unsigned short)(round(illum));
}

uint16_t pressure_to_hexa(float press)//POZOR na int vs uint
{
    //range 20 000 to 110 000 Pa
    //we use limit pressure from 44 435 Pa to 110 000 Pa. It is 65 545 values with accuracy 1 Pa. It is possible to store it to 2 Bytes (65 545 values)
    //00 00 > 44 435 Pa, ff ff > 110 000 Pa
    return (unsigned short)(round(press-44435));//44 435 Pa > 00 00, 110 000 Pa > ff ff
}

uint8_t battery_voltage_to_hexa(float vol)
{
    //range 0 - 6 V, accuracy 0.1 V, together 60 parts
    //0 V > 0 decimal, 6 V > 255
    //round to 1 decimal, multiplay by 10, from 0 - 60
    
    return (round(vol*10));//0 V > 0, 6 V > 60, 5.9 V > 59
}

uint16_t battery_charge_level_to_hexa(float charg)
{
    //battery charge is integer 0-100
    return (unsigned char)(charg);
}

/*
float co2concentration_na_hexa(float co2con)
{
    //vypocet pozri postup v IoT Help
    if (co2con<=2000)//od 0 po 2000 Luxov idem po 10 Luxoch 
    {
        return round(co2con/10);//uz moj vypocet
    }

    if (co2con <= 7400) 
    {
        return 181 + round(co2con/100);
    }
    return 0;//musi byt, inak prekladac hlasi warning
}
*/
#endif
