From 492a474f5f0b8caafeb1a8859fd52d3d5497e8f5 Mon Sep 17 00:00:00 2001 From: Benign X <1341398182@qq.com> Date: Thu, 14 Dec 2023 21:22:54 +0800 Subject: [PATCH] feat(lv_math): add lv_rand_set_seed api (#5013) --- src/lv_init.c | 2 +- src/misc/lv_math.c | 5 +++++ src/misc/lv_math.h | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/lv_init.c b/src/lv_init.c index 3affdff0d..9ec104c17 100644 --- a/src/lv_init.c +++ b/src/lv_init.c @@ -87,7 +87,7 @@ static inline void lv_global_init(lv_global_t * global) global->style_last_custom_prop_id = (uint32_t)_LV_STYLE_LAST_BUILT_IN_PROP; global->area_trans_cache.angle_prev = INT32_MIN; global->event_last_register_id = _LV_EVENT_LAST; - global->math_rand_seed = 0x1234ABCD; + lv_rand_set_seed(0x1234ABCD); #if defined(LV_DRAW_SW_SHADOW_CACHE_SIZE) && LV_DRAW_SW_SHADOW_CACHE_SIZE > 0 global->sw_shadow_cache.cache_size = -1; diff --git a/src/misc/lv_math.c b/src/misc/lv_math.c index c3ead50c9..11f3f50ad 100644 --- a/src/misc/lv_math.c +++ b/src/misc/lv_math.c @@ -333,6 +333,11 @@ int32_t lv_map(int32_t x, int32_t min_in, int32_t max_in, int32_t min_out, int32 return ((x - min_in) * delta_out) / delta_in + min_out; } +void lv_rand_set_seed(uint32_t seed) +{ + rand_seed = seed; +} + uint32_t lv_rand(uint32_t min, uint32_t max) { /*Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"*/ diff --git a/src/misc/lv_math.h b/src/misc/lv_math.h index af2036e86..9d889d655 100644 --- a/src/misc/lv_math.h +++ b/src/misc/lv_math.h @@ -125,6 +125,12 @@ int64_t lv_pow(int64_t base, int8_t exp); */ int32_t lv_map(int32_t x, int32_t min_in, int32_t max_in, int32_t min_out, int32_t max_out); +/** + * Set the seed of the pseudo random number generator + * @param seed a number to initialize the random generator + */ +void lv_rand_set_seed(uint32_t seed); + /** * Get a pseudo random number in the given range * @param min the minimum value