ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

106 líneas
2.2KB

  1. #pragma once
  2. #include <cstdint>
  3. #include <string>
  4. struct Color {
  5. uint8_t r, g, b;
  6. } __attribute__ ((packed));
  7. typedef uint16_t Fixed;
  8. constexpr int factor = 65536;
  9. constexpr int shift = 16;
  10. struct Gradient {
  11. Color at(Fixed x) const;
  12. static Gradient* make(int n) {
  13. Gradient *gradient = (Gradient*)malloc(
  14. sizeof(Gradient) + n * sizeof(*colors)
  15. );
  16. gradient->n_colors = n;
  17. return gradient;
  18. }
  19. unsigned int n_colors;
  20. Color colors[];
  21. };
  22. class Pattern {
  23. public:
  24. virtual ~Pattern() {};
  25. struct State {};
  26. virtual State* start(int) const = 0;
  27. virtual void step(Color[], int, State*) const = 0;
  28. };
  29. class GradientPattern : public Pattern {
  30. public:
  31. GradientPattern(
  32. Gradient *_gradient,
  33. int _cycle_length=20, int _cycle_time_ms=-1,
  34. bool _reverse=false, bool _march=false
  35. ) : cycle_length(_cycle_length),
  36. cycle_time_ms( _cycle_time_ms < 0 ?
  37. _cycle_length * 500 : _cycle_time_ms
  38. ),
  39. reverse(_reverse), march(_march), gradient(_gradient)
  40. {}
  41. ~GradientPattern() { free(gradient); }
  42. State* start(int) const { return new State(); }
  43. void step(Color[], int, State*) const;
  44. private:
  45. struct State : Pattern::State {
  46. State() : last_us(0), offset(0) {}
  47. int64_t last_us;
  48. Fixed offset;
  49. };
  50. int cycle_length;
  51. int cycle_time_ms;
  52. bool reverse;
  53. bool march;
  54. Gradient *gradient;
  55. };
  56. class LEDStrip {
  57. public:
  58. LEDStrip(int length = 0);
  59. void step();
  60. virtual void show() = 0;
  61. virtual void length_changing(int) {};
  62. void setPattern(const Pattern *_pattern) { pattern = _pattern; update_state(); }
  63. const Pattern* getPattern() const { return pattern; }
  64. void setLength(int length);
  65. int getLength() const { return length; }
  66. protected:
  67. int length;
  68. const Pattern *pattern;
  69. Color *pixels;
  70. private:
  71. void update_state();
  72. Pattern::State *state;
  73. };
  74. class TerminalLEDs : public LEDStrip {
  75. public:
  76. TerminalLEDs(int length = 0) : LEDStrip(length) {}
  77. void show();
  78. };