diff --git a/src/tick/lv_tick.c b/src/tick/lv_tick.c index b815f334a..2e4626b78 100644 --- a/src/tick/lv_tick.c +++ b/src/tick/lv_tick.c @@ -80,11 +80,34 @@ uint32_t lv_tick_elaps(uint32_t 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) { state.tick_get_cb = cb; } +void lv_delay_set_cb(lv_delay_cb_t cb) +{ + state.delay_cb = cb; +} + /********************** * STATIC FUNCTIONS **********************/ diff --git a/src/tick/lv_tick.h b/src/tick/lv_tick.h index 10b468f23..da8620371 100644 --- a/src/tick/lv_tick.h +++ b/src/tick/lv_tick.h @@ -30,10 +30,13 @@ extern "C" { **********************/ typedef uint32_t (*lv_tick_get_cb_t)(void); +typedef void (*lv_delay_cb_t)(uint32_t ms); + typedef struct { uint32_t sys_time; volatile uint8_t sys_irq_flag; lv_tick_get_cb_t tick_get_cb; + lv_delay_cb_t delay_cb; } lv_tick_state_t; /********************** @@ -42,29 +45,43 @@ typedef struct { /** * 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); /** * Get the elapsed milliseconds since start up - * @return the elapsed milliseconds + * @return the elapsed milliseconds */ uint32_t lv_tick_get(void); /** * Get the elapsed milliseconds since a previous time stamp - * @param prev_tick a previous time stamp (return value of lv_tick_get() ) - * @return the elapsed milliseconds since 'prev_tick' + * @param prev_tick a previous time stamp (return value of lv_tick_get() ) + * @return the elapsed milliseconds since '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' - * @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); +/** + * 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 **********************/