#include "utils.h"
#include "math.h"
uint64_t timeMs = 0;
void _TimeUpdate()//insert in systemTick to calculate time;
{
    timeMs+=1;
}

uint64_t getMillis()
{
    return timeMs;
}

void delayUs(uint32_t n)
{
    for(uint32_t j = 0; j < n; j++)
    {
    	asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
    	asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
    	asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
    	asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
    	asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");

    }
}

void delayMs(uint32_t n)
{
    volatile unsigned long long currentTime = getMillis();
    while(1)
    {
        if(getMillis()-currentTime > n)
            return;
        delayUs(1000);
    }

}

uint32_t getVersion(){return (MAIN_VERSION<<16) | (MINOR_VERSION<<8)| FIX_VERSION;}
uint8_t getMainVersion(){return MAIN_VERSION;}
uint8_t getMinorVersion(){return MINOR_VERSION;}
uint8_t getFixVersion(){return  FIX_VERSION;}

int32_t getWholeValueFromFloat(uint32_t value)
{
    int32_t _return;
    _return=(int32_t)(value&0x007FFFFF) | 0x00800000;
    uint8_t exp = (uint8_t)((value>>23)&0xFF) - 23;
    if(exp-127>0)
    {
        _return=_return<<(exp-127);
    }
    else {
        _return=_return>>(127-exp);
    }
    return _return|(value&0x80000000);
}


