ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

60 行
1.3KB

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