Rev 8 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8 | Rev 9 | ||
---|---|---|---|
Line 4... | Line 4... | ||
4 | * Created on: 17 Aug 2019 |
4 | * Created on: 17 Aug 2019 |
5 | * Author: Mike |
5 | * Author: Mike |
6 | */ |
6 | */ |
7 | 7 | ||
8 | #include "main.h" |
8 | #include "main.h" |
9 | #include "leds.h" |
9 | #include "libWS2812/leds.h" |
- | 10 | #include "libWS2812/spiInterface.h" |
|
10 | #include "sendLeds.h" |
11 | #include "sendLeds.h" |
11 | 12 | ||
12 | - | ||
13 | #include <stdint.h> |
13 | #include <stdint.h> |
14 | 14 | ||
15 | /* This is xoroshiro128+ 1.0, our best and fastest small-state generator |
15 | /* This is xoroshiro128+ 1.0, our best and fastest small-state generator |
16 | for floating-point numbers. We suggest to use its upper bits for |
16 | for floating-point numbers. We suggest to use its upper bits for |
17 | floating-point generation, as it is slightly faster than |
17 | floating-point generation, as it is slightly faster than |
18 | xoroshiro128**. It passes all tests we are aware of except for the four |
18 | xoroshiro128**. It passes all tests we are aware of except for the four |
19 | lower bits, which might fail linearity tests (and just those), so if |
19 | lower bits, which might fail linearity tests (and just those), so if |
20 | low linear complexity is not considered an issue (as it is usually the |
20 | low linear complexity is not considered an issue (as it is usually the |
21 | case) it can be used to generate 64-bit outputs, too; moreover, this |
21 | case) it can be used to generate 64-bit outputs, too; moreover, this |
22 | generator has a very mild Hamming-weight dependency making our test |
22 | generator has a very mild Hamming-weight dependency making our test |
23 | (http://prng.di.unimi.it/hwd.php) fail after 5 TB of output; we believe |
23 | (http://prng.di.unimi.it/hwd.php) fail after 5 TB of output; we believe |
24 | this slight bias cannot affect any application. If you are concerned, |
24 | this slight bias cannot affect any application. If you are concerned, |
25 | use xoroshiro128++, xoroshiro128** or xoshiro256+. |
25 | use xoroshiro128++, xoroshiro128** or xoshiro256+. |
26 | 26 | ||
27 | We suggest to use a sign test to extract a random Boolean value, and |
27 | We suggest to use a sign test to extract a random Boolean value, and |
28 | right shifts to extract subsets of bits. |
28 | right shifts to extract subsets of bits. |
29 | 29 | ||
30 | The state must be seeded so that it is not everywhere zero. If you have |
30 | The state must be seeded so that it is not everywhere zero. If you have |
31 | a 64-bit seed, we suggest to seed a splitmix64 generator and use its |
31 | a 64-bit seed, we suggest to seed a splitmix64 generator and use its |
32 | output to fill s. |
32 | output to fill s. |
33 | - | ||
34 | NOTE: the parameters (a=24, b=16, b=37) of this version give slightly |
- | |
35 | better results in our test than the 2016 version (a=55, b=14, c=36). |
- | |
36 | */ |
- | |
37 | 33 | ||
38 | static inline uint64_t rotl(const uint64_t x, int k) { |
34 | NOTE: the parameters (a=24, b=16, b=37) of this version give slightly |
39 | return (x << k) | (x >> (64 - k)); |
35 | better results in our test than the 2016 version (a=55, b=14, c=36). |
40 | } |
36 | */ |
41 | 37 | ||
- | 38 | static inline uint64_t |
|
- | 39 | rotl (const uint64_t x, int k) |
|
- | 40 | { |
|
- | 41 | return (x << k) | (x >> (64 - k)); |
|
- | 42 | } |
|
42 | 43 | ||
43 | static uint64_t s[2] = { 102,33 }; |
44 | static uint64_t s[2] = |
- | 45 | { 102, 33 }; |
|
44 | 46 | ||
- | 47 | uint64_t |
|
45 | uint64_t next(void) { |
48 | next (void) |
- | 49 | { |
|
46 | const uint64_t s0 = s[0]; |
50 | const uint64_t s0 = s[0]; |
47 | uint64_t s1 = s[1]; |
51 | uint64_t s1 = s[1]; |
48 | const uint64_t result = s0 + s1; |
52 | const uint64_t result = s0 + s1; |
49 | 53 | ||
50 | s1 ^= s0; |
54 | s1 ^= s0; |
51 | s[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); // a, b |
55 | s[0] = rotl (s0, 24) ^ s1 ^ (s1 << 16); // a, b |
52 | s[1] = rotl(s1, 37); // c |
56 | s[1] = rotl (s1, 37); // c |
53 | 57 | ||
54 | return result; |
58 | return result; |
55 | } |
59 | } |
56 | 60 | ||
57 | - | ||
- | 61 | #if defined WS2812 |
|
58 | frgbw_t led0 = { 128, 0, 0, 0, 0 }; |
62 | frgbw_t led0 = { 128, 0, 0, 0 }; |
59 | frgbw_t led1 = { 128, 0, 0, 0, 2 }; |
63 | frgbw_t led1 = { 128, 0, 0, 2 }; |
60 | frgbw_t ledZ = { 0,0,0,0 }; |
64 | frgbw_t ledZ = { 0,0,0 }; |
61 | 65 | #else |
|
- | 66 | frgbw_t led0 = |
|
- | 67 | { 128, 0, 0, 0 }; |
|
- | 68 | frgbw_t led1 = |
|
- | 69 | { 128, 0, 0, 2 }; |
|
- | 70 | frgbw_t ledZ = |
|
- | 71 | { 0, 0, 0, }; |
|
- | 72 | #endif |
|
62 | int counter = 1; |
73 | int counter = 1; |
63 | 74 | ||
- | 75 | void |
|
64 | void sendLeds() |
76 | sendLeds () |
65 | { |
77 | { |
66 | 78 | ||
67 | initCode(); |
79 | initCode (); |
68 | codeReset(); |
80 | codeReset (); |
69 | 81 | ||
70 | int target = 128; |
82 | int target = 128; |
71 | counter--; |
83 | counter--; |
72 | 84 | ||
73 | if(counter == 0) |
85 | if (counter == 0) |
74 | { |
86 | { |
75 | counter = (next() & 0xF0)+16; |
87 | counter = (next () & 0xF0) + 16; |
76 | - | ||
77 | led0.red = next() & 0xFF; |
- | |
78 | led0.green =next() & 0xFF; |
- | |
79 | led0.blue = next() & 0xFF; |
- | |
80 | led0.white = next() & 0xFF; |
- | |
81 | - | ||
82 | } |
- | |
83 | if(led0.fader < target) |
- | |
84 | led0.fader++; |
- | |
85 | if(led0.fader > target) |
- | |
86 | led0.fader--; |
- | |
87 | 88 | ||
88 | if((counter & 8) == 0) |
89 | led0.red = next () & 0xFF; |
- | 90 | led0.green = next () & 0xFF; |
|
- | 91 | led0.blue = next () & 0xFF; |
|
- | 92 | #if defined WS2812 |
|
- | 93 | led0.white = 0; |
|
- | 94 | // led0.white = next() & 0xFF; |
|
- | 95 | #endif |
|
- | 96 | ||
89 | target = (next() & 0xE0) + 0x20; |
97 | led1.red = next () & 0xFF; |
- | 98 | led1.green = next () & 0xFF; |
|
- | 99 | led1.blue = next () & 0xFF; |
|
- | 100 | #if defined WS2812 |
|
- | 101 | led1.white = 0; |
|
- | 102 | //led1.white = next() & 0xFF; |
|
- | 103 | #endif |
|
90 | 104 | ||
91 | codeFRGBW(led0); |
- | |
92 | 105 | ||
- | 106 | } |
|
- | 107 | if (led1.fader < target) |
|
- | 108 | led1.fader++; |
|
- | 109 | if (led1.fader > target) |
|
- | 110 | led1.fader--; |
|
- | 111 | ||
- | 112 | if ((counter & 8) == 0) |
|
- | 113 | target = (next () & 0x70) + 0x00; |
|
- | 114 | ||
- | 115 | codeFRGBW (led0); |
|
- | 116 | codeFRGBW (led1); |
|
93 | // send terminal |
117 | // send terminal |
94 | 118 | ||
95 | codeStop(); |
119 | codeStop (); |
96 | sendCode(); // send coded pattern |
120 | sendCode (); // send coded pattern |
97 | } |
121 | } |
98 | 122 | ||
99 | - | ||
100 | - |