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.

55 line
1.1KB

  1. static const char *TAG = "leds";
  2. #include <esp_log.h>
  3. #include <cstring>
  4. #include "leds.hpp"
  5. #include "utils.hpp"
  6. LEDStrip::LEDStrip(int _length) :
  7. length(_length), pattern(NULL),
  8. pixels(new Color[_length]),
  9. state(NULL)
  10. {}
  11. void LEDStrip::update_state() {
  12. if (state) delete state;
  13. if (pattern) state = pattern->start(length);
  14. else state = NULL;
  15. }
  16. void LEDStrip::setLength(int _length) {
  17. clear();
  18. length_changing(_length);
  19. delete[] pixels;
  20. length = _length;
  21. pixels = new Color[_length];
  22. update_state();
  23. }
  24. void LEDStrip::step() {
  25. if (pattern) pattern->step(pixels, length, state);
  26. }
  27. void LEDStrip::clear() {
  28. memset(pixels, 0, length * sizeof(*pixels));
  29. show();
  30. }
  31. static std::string termcolor(Color c) {
  32. return "\x1b[48;2;" +
  33. std::to_string((int)c.r) + ";" +
  34. std::to_string((int)c.g) + ";" +
  35. std::to_string((int)c.b) + "m";
  36. }
  37. void TerminalLEDs::show() {
  38. std::string line;
  39. for (int i = 0; i < length; i++) {
  40. line += termcolor(pixels[i]) + " ";
  41. }
  42. line += "\x1b[0m";
  43. ESP_LOGI(TAG, "%s", line.c_str());
  44. }