PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Noise.ino 3.2KB

il y a 3 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <FastLED.h>
  2. //
  3. // Mark's xy coordinate mapping code. See the XYMatrix for more information on it.
  4. //
  5. // Params for width and height
  6. const uint8_t kMatrixWidth = 16;
  7. const uint8_t kMatrixHeight = 16;
  8. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  9. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  10. // Param for different pixel layouts
  11. const bool kMatrixSerpentineLayout = true;
  12. uint16_t XY( uint8_t x, uint8_t y)
  13. {
  14. uint16_t i;
  15. if( kMatrixSerpentineLayout == false) {
  16. i = (y * kMatrixWidth) + x;
  17. }
  18. if( kMatrixSerpentineLayout == true) {
  19. if( y & 0x01) {
  20. // Odd rows run backwards
  21. uint8_t reverseX = (kMatrixWidth - 1) - x;
  22. i = (y * kMatrixWidth) + reverseX;
  23. } else {
  24. // Even rows run forwards
  25. i = (y * kMatrixWidth) + x;
  26. }
  27. }
  28. return i;
  29. }
  30. // The leds
  31. CRGB leds[kMatrixWidth * kMatrixHeight];
  32. // The 32bit version of our coordinates
  33. static uint16_t x;
  34. static uint16_t y;
  35. static uint16_t z;
  36. // We're using the x/y dimensions to map to the x/y pixels on the matrix. We'll
  37. // use the z-axis for "time". speed determines how fast time moves forward. Try
  38. // 1 for a very slow moving effect, or 60 for something that ends up looking like
  39. // water.
  40. // uint16_t speed = 1; // almost looks like a painting, moves very slowly
  41. uint16_t speed = 20; // a nice starting speed, mixes well with a scale of 100
  42. // uint16_t speed = 33;
  43. // uint16_t speed = 100; // wicked fast!
  44. // Scale determines how far apart the pixels in our noise matrix are. Try
  45. // changing these values around to see how it affects the motion of the display. The
  46. // higher the value of scale, the more "zoomed out" the noise iwll be. A value
  47. // of 1 will be so zoomed in, you'll mostly see solid colors.
  48. // uint16_t scale = 1; // mostly just solid colors
  49. // uint16_t scale = 4011; // very zoomed out and shimmery
  50. uint16_t scale = 311;
  51. // This is the array that we keep our computed noise values in
  52. uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
  53. void setup() {
  54. // uncomment the following lines if you want to see FPS count information
  55. // Serial.begin(38400);
  56. // Serial.println("resetting!");
  57. delay(3000);
  58. LEDS.addLeds<WS2811,5,RGB>(leds,NUM_LEDS);
  59. LEDS.setBrightness(96);
  60. // Initialize our coordinates to some random values
  61. x = random16();
  62. y = random16();
  63. z = random16();
  64. }
  65. // Fill the x/y array of 8-bit noise values using the inoise8 function.
  66. void fillnoise8() {
  67. for(int i = 0; i < MAX_DIMENSION; i++) {
  68. int ioffset = scale * i;
  69. for(int j = 0; j < MAX_DIMENSION; j++) {
  70. int joffset = scale * j;
  71. noise[i][j] = inoise8(x + ioffset,y + joffset,z);
  72. }
  73. }
  74. z += speed;
  75. }
  76. void loop() {
  77. static uint8_t ihue=0;
  78. fillnoise8();
  79. for(int i = 0; i < kMatrixWidth; i++) {
  80. for(int j = 0; j < kMatrixHeight; j++) {
  81. // We use the value at the (i,j) coordinate in the noise
  82. // array for our brightness, and the flipped value from (j,i)
  83. // for our pixel's hue.
  84. leds[XY(i,j)] = CHSV(noise[j][i],255,noise[i][j]);
  85. // You can also explore other ways to constrain the hue used, like below
  86. // leds[XY(i,j)] = CHSV(ihue + (noise[j][i]>>2),255,noise[i][j]);
  87. }
  88. }
  89. ihue+=1;
  90. LEDS.show();
  91. // delay(10);
  92. }