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.

51 lines
1.2KB

  1. #ifndef WS2812Serial_h_
  2. #define WS2812Serial_h_
  3. #include <Arduino.h>
  4. #include "DMAChannel.h"
  5. #define WS2812_RGB 0 // The WS2811 datasheet documents this way
  6. #define WS2812_RBG 1
  7. #define WS2812_GRB 2 // Most LED strips are wired this way
  8. #define WS2812_GBR 3
  9. #define WS2812_BRG 4
  10. #define WS2812_BGR 5
  11. class WS2812Serial {
  12. public:
  13. constexpr WS2812Serial(uint16_t num, void *fb, void *db, uint8_t pin, uint8_t cfg) :
  14. numled(num), pin(pin), config(cfg),
  15. frameBuffer((uint8_t *)fb), drawBuffer((uint8_t *)db) {
  16. }
  17. bool begin();
  18. void setPixel(uint32_t num, int color) {
  19. if (num >= numled) return;
  20. num *= 3;
  21. drawBuffer[num+0] = color & 255;
  22. drawBuffer[num+1] = (color >> 8) & 255;
  23. drawBuffer[num+2] = (color >> 16) & 255;
  24. }
  25. void setPixel(uint32_t num, uint8_t red, uint8_t green, uint8_t blue) {
  26. if (num >= numled) return;
  27. num *= 3;
  28. drawBuffer[num+0] = blue;
  29. drawBuffer[num+1] = green;
  30. drawBuffer[num+2] = red;
  31. }
  32. void show();
  33. bool busy();
  34. uint16_t numPixels() {
  35. return numled;
  36. }
  37. private:
  38. const uint16_t numled;
  39. const uint8_t pin;
  40. const uint8_t config;
  41. uint8_t *frameBuffer;
  42. uint8_t *drawBuffer;
  43. DMAChannel *dma = nullptr;
  44. uint32_t prior_micros = 0;
  45. };
  46. #endif