ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

80 рядки
1.9KB

  1. static const char *TAG = "screen_leds";
  2. #include <esp_log.h>
  3. #include "screen_leds.hpp"
  4. static inline void fill(uint16_t *buffer, uint16_t value, int w, int h, int p = -1) {
  5. if (p <= 0) p = w;
  6. uint16_t *line = buffer;
  7. while (--h >= 0) {
  8. uint16_t *pixel = line;
  9. for (int _w = w; _w > 0; --_w) {
  10. *pixel = value;
  11. ++pixel;
  12. }
  13. line += p;
  14. }
  15. }
  16. static inline void box(uint16_t *buffer, uint16_t value, int x, int y, int w, int h, int p = -1) {
  17. if (p <= 0) p = w;
  18. fill(buffer + x + (y * p), value, w, h, p);
  19. }
  20. static inline uint16_t rgb16(int r, int g, int b) {
  21. return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((uint8_t)b >> 3);
  22. }
  23. static inline uint16_t color_to_565(Color c) {
  24. return rgb16(c.r, c.g, c.b);
  25. }
  26. ScreenLEDs::ScreenLEDs(int length) : LEDStrip(length), display(135, 240) {}
  27. void ScreenLEDs::length_changing(int length) {
  28. fill(display.get_buffer(), COLOR_BLACK, display.get_width(), display.get_height());
  29. }
  30. void ScreenLEDs::show() {
  31. const int width = display.get_width();
  32. const int height = display.get_height();
  33. const int pitch = display.get_pitch();
  34. auto buffer = display.get_buffer();
  35. const size_t size = ((width + height) * 2) / (length + 4);
  36. int lx = 0, ly = 0;
  37. int dx = 0, dy = size;
  38. for (size_t l = 0; l < length; ++l) {
  39. if (ly + dy > height) {
  40. ly -= dy;
  41. dx = size;
  42. dy = 0;
  43. lx += dx;
  44. } else if (ly < 0) {
  45. ly -= dy;
  46. dx = -size;
  47. dy = 0;
  48. lx += dx;
  49. } else if (lx + dx > width) {
  50. lx -= dx;
  51. dx = 0;
  52. dy = -size;
  53. ly += dy;
  54. } else if (lx < 0) {
  55. lx -= dx;
  56. dx = 0;
  57. dy = size;
  58. ly += dy;
  59. }
  60. box(buffer, color_to_565(pixels[l]), lx, ly, size, size, pitch);
  61. lx += dx;
  62. ly += dy;
  63. }
  64. display.refresh();
  65. }