PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

126 lines
2.8KB

  1. // Animated Fire Example - OctoWS2811 Library
  2. // http://www.pjrc.com/teensy/td_libs_OctoWS2811.html
  3. //
  4. // Based on the simple algorithm explained here:
  5. // http://caraesnaur.github.io/fire/
  6. //
  7. // This example code is in the public domain.
  8. #include <OctoWS2811.h>
  9. // The display size and color to use
  10. const unsigned int width = 60;
  11. const unsigned int height = 32;
  12. // These parameters control the fire appearance
  13. // (try controlling these with knobs / analogRead....)
  14. unsigned int heat = width / 5;
  15. unsigned int focus = 9;
  16. unsigned int cool = 26;
  17. // Arrays for fire animation
  18. unsigned char canvas[width*height];
  19. extern const unsigned int fireColor[100];
  20. // OctoWS2811 objects
  21. const int ledsPerPin = width * height / 8;
  22. DMAMEM int displayMemory[ledsPerPin*6];
  23. int drawingMemory[ledsPerPin*6];
  24. const int config = WS2811_GRB | WS2811_800kHz;
  25. OctoWS2811 leds(ledsPerPin, displayMemory, drawingMemory, config);
  26. // Run setup once
  27. void setup() {
  28. // turn on the display
  29. leds.begin();
  30. leds.show();
  31. }
  32. // A simple xy() function to turn display matrix coordinates
  33. // into the index numbers OctoWS2811 requires. If your LEDs
  34. // are arranged differently, edit this code...
  35. unsigned int xy(unsigned int x, unsigned int y) {
  36. if ((y & 1) == 0) {
  37. // even numbered rows (0, 2, 4...) are left to right
  38. return y * width + x;
  39. } else {
  40. // odd numbered rows (1, 3, 5...) are right to left
  41. return y * width + width - 1 - x;
  42. }
  43. }
  44. elapsedMillis msec;
  45. // Run repetitively
  46. void loop() {
  47. if (msec >= 45) {
  48. msec = 0;
  49. animateFire();
  50. }
  51. }
  52. void animateFire() {
  53. unsigned int i, c, n, x, y;
  54. // Step 1: move all data up one line
  55. memmove(canvas + width, canvas, width * (height - 1));
  56. memset(canvas, 0, width);
  57. // Step 2: draw random heatspots on bottom line
  58. i = heat;
  59. if (i > width-8) i = width-8;
  60. while (i > 0) {
  61. x = random(width - 2) + 1;
  62. if (canvas[x] == 0) {
  63. canvas[x] = 99;
  64. i--;
  65. }
  66. }
  67. // Step 3: interpolate
  68. for (y=0; y < height; y++) {
  69. for (x=0; x < width; x++) {
  70. c = canvas[y * width + x] * focus;
  71. n = focus;
  72. if (x > 0) {
  73. c = c + canvas[y * width + (x - 1)];
  74. n = n + 1;
  75. }
  76. if (x < width-1) {
  77. c = c + canvas[y * width + (x + 1)];
  78. n = n + 1;
  79. }
  80. if (y > 0) {
  81. c = c + canvas[(y -1) * width + x];
  82. n = n + 1;
  83. }
  84. if (y < height-1) {
  85. c = c + canvas[(y + 1) * width + x];
  86. n = n + 1;
  87. }
  88. c = (c + (n / 2)) / n;
  89. i = (random(1000) * cool) / 10000;
  90. if (c > i) {
  91. c = c - i;
  92. } else {
  93. c = 0;
  94. }
  95. canvas[y * width + x] = c;
  96. }
  97. }
  98. // Step 4: render canvas to LEDs
  99. for (y=0; y < height; y++) {
  100. for (x=0; x < width; x++) {
  101. c = canvas[((height - 1) - y) * width + x];
  102. leds.setPixel(xy(x, y), fireColor[c]);
  103. }
  104. }
  105. leds.show();
  106. }