#include "app_utils.h"

static uint32_t millis_cnt = 0;

uint32_t millis(void)
{
    return millis_cnt;
}

void millis_run_cb(void *arg)
{
    millis_cnt++;
}

void us_delay(uint32_t us)
{
    struct tls_timer_cfg timer_config = 
	{
		.is_repeat = false,
		.unit = TLS_TIMER_UNIT_US,
		.timeout = 0xFFFFFFFF,
	};

	uint8_t timer_id = tls_timer_create(&timer_config);

    if(WM_TIMER_ID_INVALID == timer_id) return;

	tls_timer_start(timer_id);

    // Perform a blocking delay
    while(tls_timer_read(timer_id) < us);

    tls_timer_destroy(timer_id);
}

void ms_delay(uint32_t ms)
{
    struct tls_timer_cfg timer_config = 
	{
		.is_repeat = false,
		.unit = TLS_TIMER_UNIT_MS,
		.timeout = 0xFFFFFFFF,
	};

	uint8_t timer_id = tls_timer_create(&timer_config);

    if(WM_TIMER_ID_INVALID == timer_id) return;

	tls_timer_start(timer_id);

    // Perform a blocking delay
    while(tls_timer_read(timer_id) < ms);

    tls_timer_destroy(timer_id);
}