diff options
author | XScorpion2 <rcalt2vt@gmail.com> | 2019-04-02 19:24:14 -0500 |
---|---|---|
committer | Drashna Jaelre <drashna@live.com> | 2019-04-09 09:46:45 -0700 |
commit | fd770232d94044966f6ebaa8a634242216d8ed60 (patch) | |
tree | f0a534b78d8e468980cc41afff819c69103f71c3 /lib/lib8tion/random8.h | |
parent | c846f82fa77da6a3a277bc4a2cafe177fa643f77 (diff) |
RGB Matrix Overhaul (#5372)
* RGB Matrix overhaul
Breakout of animations to separate files
Integration of optimized int based math lib
Overhaul of rgb_matrix.c and animations for performance
* Updating effect function api for future extensions
* Combined the keypresses || keyreleases define checks into a single define so I stop forgetting it where necessary
* Moving define RGB_MATRIX_KEYREACTIVE_ENABLED earlier in the include chain
Diffstat (limited to 'lib/lib8tion/random8.h')
-rw-r--r-- | lib/lib8tion/random8.h | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/lib/lib8tion/random8.h b/lib/lib8tion/random8.h new file mode 100644 index 0000000000..7ee67cbb36 --- /dev/null +++ b/lib/lib8tion/random8.h @@ -0,0 +1,94 @@ +#ifndef __INC_LIB8TION_RANDOM_H +#define __INC_LIB8TION_RANDOM_H +///@ingroup lib8tion + +///@defgroup Random Fast random number generators +/// Fast 8- and 16- bit unsigned random numbers. +/// Significantly faster than Arduino random(), but +/// also somewhat less random. You can add entropy. +///@{ + +// X(n+1) = (2053 * X(n)) + 13849) +#define FASTLED_RAND16_2053 ((uint16_t)(2053)) +#define FASTLED_RAND16_13849 ((uint16_t)(13849)) + +/// random number seed +extern uint16_t rand16seed;// = RAND16_SEED; + +/// Generate an 8-bit random number +LIB8STATIC uint8_t random8(void) +{ + rand16seed = (rand16seed * FASTLED_RAND16_2053) + FASTLED_RAND16_13849; + // return the sum of the high and low bytes, for better + // mixing and non-sequential correlation + return (uint8_t)(((uint8_t)(rand16seed & 0xFF)) + + ((uint8_t)(rand16seed >> 8))); +} + +/// Generate a 16 bit random number +LIB8STATIC uint16_t random16(void) +{ + rand16seed = (rand16seed * FASTLED_RAND16_2053) + FASTLED_RAND16_13849; + return rand16seed; +} + +/// Generate an 8-bit random number between 0 and lim +/// @param lim the upper bound for the result +LIB8STATIC uint8_t random8_max(uint8_t lim) +{ + uint8_t r = random8(); + r = (r*lim) >> 8; + return r; +} + +/// Generate an 8-bit random number in the given range +/// @param min the lower bound for the random number +/// @param lim the upper bound for the random number +LIB8STATIC uint8_t random8_min_max(uint8_t min, uint8_t lim) +{ + uint8_t delta = lim - min; + uint8_t r = random8_max(delta) + min; + return r; +} + +/// Generate an 16-bit random number between 0 and lim +/// @param lim the upper bound for the result +LIB8STATIC uint16_t random16_max(uint16_t lim) +{ + uint16_t r = random16(); + uint32_t p = (uint32_t)lim * (uint32_t)r; + r = p >> 16; + return r; +} + +/// Generate an 16-bit random number in the given range +/// @param min the lower bound for the random number +/// @param lim the upper bound for the random number +LIB8STATIC uint16_t random16_min_max( uint16_t min, uint16_t lim) +{ + uint16_t delta = lim - min; + uint16_t r = random16_max(delta) + min; + return r; +} + +/// Set the 16-bit seed used for the random number generator +LIB8STATIC void random16_set_seed(uint16_t seed) +{ + rand16seed = seed; +} + +/// Get the current seed value for the random number generator +LIB8STATIC uint16_t random16_get_seed(void) +{ + return rand16seed; +} + +/// Add entropy into the random number generator +LIB8STATIC void random16_add_entropy(uint16_t entropy) +{ + rand16seed += entropy; +} + +///@} + +#endif |