#include "dataConversions.h"

uint32_t changeEndian(uint32_t value)
{
	uint32_t output = 0;
	char *tmp1 = (char *) &output;
	char *tmp2 = (char *) &value;
	tmp1[0] = tmp2[3];
	tmp1[1] = tmp2[2];
	tmp1[2] = tmp2[1];
	tmp1[3] = tmp2[0];
	
	return output;
}

uint64_t changeEndianLong(uint64_t value)
{
	uint64_t output = 0;
	char *tmp1 = (char *) &output;
	char *tmp2 = (char *) &value;	
	tmp1[0] = tmp2[7];
	tmp1[1] = tmp2[6];
	tmp1[2] = tmp2[5];
	tmp1[3] = tmp2[4];
	tmp1[4] = tmp2[3];
	tmp1[5] = tmp2[2];
	tmp1[6] = tmp2[1];
	tmp1[7] = tmp2[0];
	
	return output;
}

uint8_t * store(uint8_t *dst, void * src, int numBytes, int littleEndian)
{
	if (littleEndian)
	while (numBytes--)
	dst[numBytes] = ((uint8_t*)src)[numBytes];
	else
	while (numBytes--)
	*dst++ = ((uint8_t*)src)[numBytes];
	return dst;
}

uint16_t Get16bits(void)
{
	uint16_t value = (uint16_t)0;
	
	value = (udi_cdc_getc() << 8) | (udi_cdc_getc());
	
	return value;
}

uint64_t Get64bits(void)
{
	uint64_t value  = (uint64_t)Get32bits();
	value = value << 32;
	uint64_t value1 = (uint64_t)Get32bits();
    
	return  (value| value1);
}

uint32_t Get32bits(void)
{
	uint32_t value = (uint32_t)0;
	
	value = ( udi_cdc_getc() << 24) | (udi_cdc_getc() << 16) | (udi_cdc_getc() << 8) | (udi_cdc_getc());
	
	return value;
}

uint8_t Get8bits(void)
{
	return(udi_cdc_getc());
}
//******************************************************************************
//! @fn uint32_t float2Uint32 (float input)
//!
//! @description
//! Converts float to unsigned 32bits integer
//!
//! @param    input     float number
//!
//! @return   input number as uint32
//!
//! @calls    None
//!
//! @req
//!
//! @example 
//! 
//! @code
//! 
//! @endcode
//******************************************************************************
uint32_t float2Uint32 (float input)
{
	union
	{
		float d;
		uint32_t t;
	}
	test_union;
	
	test_union.d = input;
	return (test_union.t);
}