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.

54 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 n, void *f, void *d, HardwareSerial &s, uint8_t c) :
  14. numled(n), config(c),
  15. frameBuffer((uint8_t *)f), drawBuffer((uint8_t *)d),
  16. uart( &s == &Serial1 ? &KINETISK_UART0 : nullptr)
  17. {
  18. }
  19. void begin();
  20. void setPixel(uint32_t num, int color) {
  21. if (num >= numled) return;
  22. num *= 3;
  23. frameBuffer[num+0] = color;
  24. frameBuffer[num+1] = color >> 8;
  25. frameBuffer[num+2] = color >> 16;
  26. }
  27. void setPixel(uint32_t num, uint8_t red, uint8_t green, uint8_t blue) {
  28. if (num >= numled) return;
  29. num *= 3;
  30. frameBuffer[num+0] = blue;
  31. frameBuffer[num+1] = green;
  32. frameBuffer[num+2] = red;
  33. }
  34. void show();
  35. bool busy();
  36. uint16_t numPixels() {
  37. return numled;
  38. }
  39. private:
  40. const uint16_t numled;
  41. const uint8_t config;
  42. uint8_t *frameBuffer;
  43. uint8_t *drawBuffer;
  44. //HardwareSerial &serial;
  45. KINETISK_UART_t *uart;
  46. DMAChannel *dma = nullptr;
  47. };
  48. #endif