WS2812_ATY.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @file WS2812_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief functions of WS2812 for C platform (Dev-style)
  20. *
  21. * @version
  22. * - 1_01_251112 > ATY
  23. * -# Refactor to Dev-style; remove legacy global interfaces
  24. ********************************************************************************
  25. */
  26. #ifndef __WS2812_ATY_C
  27. #define __WS2812_ATY_C
  28. #include "WS2812_ATY.h"
  29. static void _ws2812_send_byte(struct WS2812_ATY_Dev* dev, uint8_t byte)
  30. {
  31. for (int i = 7; i >= 0; --i) {
  32. uint8_t bit = (byte >> i) & 0x01;
  33. dev->sendBit(bit);
  34. }
  35. }
  36. uint8_t WS2812_InitDev(struct WS2812_ATY_Dev* dev)
  37. {
  38. if (!dev || !dev->sendBit) return 1;
  39. if (dev->debugEnable == 1 && dev->LOG) dev->LOG("\r\nWS2812 init done.");
  40. return 0;
  41. }
  42. void WS2812_SendColorDev(struct WS2812_ATY_Dev* dev, uint8_t R, uint8_t G, uint8_t B)
  43. {
  44. __ATY_LOCK(dev);
  45. _ws2812_send_byte(dev, G);
  46. _ws2812_send_byte(dev, R);
  47. _ws2812_send_byte(dev, B);
  48. if (dev->reset_us) dev->reset_us(60);
  49. if (dev->debugEnable == 1 && dev->LOG) dev->LOG("\r\nWS2812 send color.");
  50. __ATY_UNLOCK(dev);
  51. }
  52. void WS2812_SendBufferDev(struct WS2812_ATY_Dev* dev, const uint8_t* rgb, uint16_t count)
  53. {
  54. __ATY_LOCK(dev);
  55. if (!dev || !dev->sendBit || !rgb) { __ATY_UNLOCK(dev); return; }
  56. for (uint16_t i = 0; i < count; ++i) {
  57. uint8_t R = rgb[i*3 + 0];
  58. uint8_t G = rgb[i*3 + 1];
  59. uint8_t B = rgb[i*3 + 2];
  60. _ws2812_send_byte(dev, G);
  61. _ws2812_send_byte(dev, R);
  62. _ws2812_send_byte(dev, B);
  63. }
  64. if (dev->reset_us) dev->reset_us(60);
  65. if (dev->debugEnable == 1 && dev->LOG) dev->LOG("\r\nWS2812 send buffer.");
  66. __ATY_UNLOCK(dev);
  67. }
  68. #endif /* __WS2812_ATY_C */
  69. /******************************** End Of File *********************************/