Subversion Repositories LedShow

Rev

Rev 8 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * sendLeds.c
  3.  *
  4.  *  Created on: 17 Aug 2019
  5.  *      Author: Mike
  6.  */
  7.  
  8. #include "main.h"
  9. #include "libWS2812/leds.h"
  10. #include "libWS2812/spiInterface.h"
  11. #include "sendLeds.h"
  12.  
  13. #include <stdint.h>
  14.  
  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
  17.  floating-point generation, as it is slightly faster than
  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
  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
  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
  24.  this slight bias cannot affect any application. If you are concerned,
  25.  use xoroshiro128++, xoroshiro128** or xoshiro256+.
  26.  
  27.  We suggest to use a sign test to extract a random Boolean value, and
  28.  right shifts to extract subsets of bits.
  29.  
  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
  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.  
  38. static inline uint64_t
  39. rotl (const uint64_t x, int k)
  40. {
  41.   return (x << k) | (x >> (64 - k));
  42. }
  43.  
  44. static uint64_t s[2] =
  45.   { 102, 33 };
  46.  
  47. uint64_t
  48. next (void)
  49. {
  50.   const uint64_t s0 = s[0];
  51.   uint64_t s1 = s[1];
  52.   const uint64_t result = s0 + s1;
  53.  
  54.   s1 ^= s0;
  55.   s[0] = rotl (s0, 24) ^ s1 ^ (s1 << 16); // a, b
  56.   s[1] = rotl (s1, 37); // c
  57.  
  58.   return result;
  59. }
  60.  
  61. #if defined WS2812
  62. frgbw_t led0 = { 128, 0, 0, 0 };
  63. frgbw_t led1 = { 128, 0, 0, 2 };
  64. frgbw_t ledZ = { 0,0,0 };
  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
  73. int counter = 1;
  74.  
  75. void
  76. sendLeds ()
  77. {
  78.  
  79.   initCode ();
  80.   codeReset ();
  81.  
  82.   int target = 128;
  83.   counter--;
  84.  
  85.   if (counter == 0)
  86.     {
  87.       counter = (next () & 0xF0) + 16;
  88.  
  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.  
  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
  104.  
  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);
  117.   // send terminal
  118.  
  119.   codeStop ();
  120.   sendCode (); // send coded pattern
  121. }
  122.  
  123.