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.

95 líneas
2.7KB

  1. static const char *TAG = "leds";
  2. #include <esp_log.h>
  3. #include "leds.hpp"
  4. #include "utils.hpp"
  5. static inline uint8_t mix(int l, int r, Fixed x) {
  6. return ((l << shift) + (x * (r - l))) >> shift;
  7. }
  8. Color Gradient::at(Fixed x) const {
  9. int i_left = (x * n_colors) >> shift;
  10. int i_right = i_left < n_colors - 1 ? i_left + 1 : 0;
  11. Fixed ratio = (x * n_colors) - (i_left << shift);
  12. //ESP_LOGI(TAG, "at(%d) = %d between %d and %d", x, ratio, i_left, i_right);
  13. Color cl = colors[i_left];
  14. Color cr = colors[i_right];
  15. Color color = {
  16. mix(cl.r, cr.r, ratio),
  17. mix(cl.g, cr.g, ratio),
  18. mix(cl.b, cr.b, ratio)
  19. };
  20. /*ESP_LOGI(TAG, "(%d, %d, %d) %f (%d, %d, %d) -> (%d, %d, %d)",
  21. cl.r, cl.g, cl.b,
  22. ratio / (float)factor,
  23. cr.r, cr.g, cr.b,
  24. color.r, color.g, color.b
  25. );*/
  26. return color;
  27. }
  28. void Pattern::step(Color pixels[], int len, int64_t &last_us, Fixed &offset) const {
  29. int64_t now_us = time_us();
  30. int64_t duration_us = last_us == 0 ? 0 : now_us - last_us;
  31. last_us = now_us;
  32. //ESP_LOGI(TAG, "duration %d", duration_us);
  33. /*
  34. int period_us = 1000000 / 60;
  35. int n_skipped = (duration_us - period_us / 2) / period_us;
  36. if (n_skipped) {
  37. ESP_LOGW(TAG, "Skipped %d frames", n_skipped);
  38. }
  39. */
  40. // Don't make a major animation jump if it's been terribly long
  41. if (duration_us > 100000) duration_us = 100000;
  42. int offset_delta = (duration_us << shift) / (cycle_time_ms * 1000);
  43. if (reverse) offset += offset_delta; else offset -= offset_delta;
  44. Fixed off = march ?
  45. (((int)offset * cycle_length) & ~((1 << shift) - 1)) / cycle_length :
  46. offset;
  47. //ESP_LOGI(TAG, "cycle time %d, delta %d, offset %d, off %d", cycle_time_ms, offset_delta, offset, off);
  48. for (int i = 0; i < len; ++i) {
  49. pixels[i] = gradient.at(off + ((i << shift) / cycle_length));
  50. }
  51. }
  52. LEDStrip::LEDStrip(int _length) :
  53. length(_length), pattern(NULL),
  54. pixels(new Color[_length]),
  55. last_us(0), offset(0)
  56. {}
  57. void LEDStrip::setLength(int _length) {
  58. length_changing(_length);
  59. delete[] pixels;
  60. length = _length;
  61. pixels = new Color[_length];
  62. }
  63. void LEDStrip::step() {
  64. if (pattern) pattern->step(pixels, length, last_us, offset);
  65. }
  66. static std::string termcolor(Color c) {
  67. return "\x1b[48;2;" +
  68. std::to_string((int)c.r) + ";" +
  69. std::to_string((int)c.g) + ";" +
  70. std::to_string((int)c.b) + "m";
  71. }
  72. void TerminalLEDs::show() {
  73. std::string line;
  74. for (int i = 0; i < length; i++) {
  75. line += termcolor(pixels[i]) + " ";
  76. }
  77. line += "\x1b[0m";
  78. ESP_LOGI(TAG, "%s", line.c_str());
  79. }