feat(tick): add lv_delay

This commit is contained in:
Gabor Kiss-Vamosi
2023-12-15 16:43:55 +01:00
parent 6bdadad1f3
commit 3e769a8ecf
2 changed files with 45 additions and 5 deletions

View File

@@ -80,11 +80,34 @@ uint32_t lv_tick_elaps(uint32_t prev_tick)
return prev_tick; return prev_tick;
} }
void lv_delay_ms(uint32_t ms)
{
if(state.delay_cb) {
state.delay_cb(ms);
}
else {
uint32_t t = lv_tick_get();
while(lv_tick_elaps(t) < ms) {
/*Do something to no call `lv_tick_elaps` too often as it might interfere with interrupts*/
volatile uint32_t i;
volatile uint32_t x = ms;
for(i = 0; i < 100; i++) {
x = x * 3;
}
}
}
}
void lv_tick_set_cb(lv_tick_get_cb_t cb) void lv_tick_set_cb(lv_tick_get_cb_t cb)
{ {
state.tick_get_cb = cb; state.tick_get_cb = cb;
} }
void lv_delay_set_cb(lv_delay_cb_t cb)
{
state.delay_cb = cb;
}
/********************** /**********************
* STATIC FUNCTIONS * STATIC FUNCTIONS
**********************/ **********************/

View File

@@ -30,10 +30,13 @@ extern "C" {
**********************/ **********************/
typedef uint32_t (*lv_tick_get_cb_t)(void); typedef uint32_t (*lv_tick_get_cb_t)(void);
typedef void (*lv_delay_cb_t)(uint32_t ms);
typedef struct { typedef struct {
uint32_t sys_time; uint32_t sys_time;
volatile uint8_t sys_irq_flag; volatile uint8_t sys_irq_flag;
lv_tick_get_cb_t tick_get_cb; lv_tick_get_cb_t tick_get_cb;
lv_delay_cb_t delay_cb;
} lv_tick_state_t; } lv_tick_state_t;
/********************** /**********************
@@ -42,29 +45,43 @@ typedef struct {
/** /**
* You have to call this function periodically * You have to call this function periodically
* @param tick_period the call period of this function in milliseconds * @param tick_period the call period of this function in milliseconds
*/ */
LV_ATTRIBUTE_TICK_INC void lv_tick_inc(uint32_t tick_period); LV_ATTRIBUTE_TICK_INC void lv_tick_inc(uint32_t tick_period);
/** /**
* Get the elapsed milliseconds since start up * Get the elapsed milliseconds since start up
* @return the elapsed milliseconds * @return the elapsed milliseconds
*/ */
uint32_t lv_tick_get(void); uint32_t lv_tick_get(void);
/** /**
* Get the elapsed milliseconds since a previous time stamp * Get the elapsed milliseconds since a previous time stamp
* @param prev_tick a previous time stamp (return value of lv_tick_get() ) * @param prev_tick a previous time stamp (return value of lv_tick_get() )
* @return the elapsed milliseconds since 'prev_tick' * @return the elapsed milliseconds since 'prev_tick'
*/ */
uint32_t lv_tick_elaps(uint32_t prev_tick); uint32_t lv_tick_elaps(uint32_t prev_tick);
/**
* Delay for the given milliseconds.
* By default it's a blocking delay, but with `lv_delay_set_cb()`
* a custom delay function can be set too
* @param ms the number of milliseconds to delay
*/
void lv_delay_ms(uint32_t ms);
/** /**
* Set the custom callback for 'lv_tick_get' * Set the custom callback for 'lv_tick_get'
* @param cb call this callback on 'lv_tick_get' * @param cb call this callback on 'lv_tick_get'
*/ */
void lv_tick_set_cb(lv_tick_get_cb_t cb); void lv_tick_set_cb(lv_tick_get_cb_t cb);
/**
* Set a custom callback for 'lv_dalay_ms'
* @param cb call this callback in 'lv_dalay_ms'
*/
void lv_delay_set_cb(lv_delay_cb_t cb);
/********************** /**********************
* MACROS * MACROS
**********************/ **********************/