ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.1KB

  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. class Pattern {
  11. public:
  12. virtual ~Pattern() = default;
  13. struct State {
  14. virtual ~State() = default;
  15. };
  16. virtual State* start(int) const = 0;
  17. virtual void step(Color[], int, State*) const = 0;
  18. };
  19. class LEDStrip {
  20. public:
  21. LEDStrip(int length = 0);
  22. ~LEDStrip() { if (state) delete state; }
  23. void step();
  24. void clear();
  25. virtual void show() = 0;
  26. virtual void length_changing(int) {};
  27. void setPattern(const Pattern *_pattern) { pattern = _pattern; update_state(); }
  28. const Pattern* getPattern() const { return pattern; }
  29. void setLength(int length);
  30. int getLength() const { return length; }
  31. protected:
  32. int length;
  33. const Pattern *pattern;
  34. Color *pixels;
  35. private:
  36. void update_state();
  37. Pattern::State *state;
  38. };
  39. class TerminalLEDs : public LEDStrip {
  40. public:
  41. TerminalLEDs(int length = 0) : LEDStrip(length) {}
  42. void show();
  43. };