/**
 * @file    decawave_app_misc.c
 * @brief   DecaWave application - misc functions
 * @version 6.0
 * @date 27/06/2017
 */
#include "deca_device_api.h"
#include "decawave_app.h"

/***************************************************************************************************
 * CONVERTER FUNCTIONS 
 **************************************************************************************************/
 
 /**@brief Function for converting microseconds to device time (64 bits)
 *
 * @param[in] microsecu  microseconds
 * @return device time
 */
uint64 convertmicrosectodevicetimeu (double microsecu)
{
    uint64 dt;
    long double dtime;

    dtime = (microsecu / (double) DWT_TIME_UNITS) / 1e6 ;

    dt =  (uint64) (dtime) ;

    return dt;
}

 /**@brief Function for converting microseconds to device time (32 bits)
 *
 * @param[in] microsecu  microseconds
 * @return device time
 */
uint32 convertmicrosectodevicetimeu32 (double microsecu)
{
    uint32 dt;
    long double dtime;

    dtime = (microsecu / (double) DWT_TIME_UNITS) / 1e6 ;

    dt =  (uint32) (dtime) ;

    return dt;
}

 /**@brief Function for converting device time to seconds
 *
 * @param[in] microsecu  device time
 * @return seconds
 */
double convertdevicetimetosec(int32 dt)
{
    double f = 0;

    f =  dt * DWT_TIME_UNITS ;  // seconds #define TIME_UNITS          (1.0/499.2e6/128.0) = 15.65e-12

    return f ;
}

 /**@brief Function for converting device time (8 bits) to seconds 
 *
 * @param[in] microsecu  device time (8 bits)
 * @return seconds
 */
double convertdevicetimetosec8(uint8* dt)
{
    double f = 0;

    uint32 lo = 0;
    int8 hi = 0;

    memcpy(&lo, dt, 4);
    hi = dt[4] ;

    f = ((hi * 65536.00 * 65536.00) + lo) * DWT_TIME_UNITS ;  // seconds #define TIME_UNITS          (1.0/499.2e6/128.0) = 15.65e-12

    return f ;
}
