ws2812_pwm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #include "ws2812.h"
  2. #include "gpio.h"
  3. #include "chibios_config.h"
  4. // ======== DEPRECATED DEFINES - DO NOT USE ========
  5. #ifdef WS2812_DMA_STREAM
  6. # define WS2812_PWM_DMA_STREAM WS2812_DMA_STREAM
  7. #endif
  8. #ifdef WS2812_DMA_CHANNEL
  9. # define WS2812_PWM_DMA_CHANNEL WS2812_DMA_CHANNEL
  10. #endif
  11. #ifdef WS2812_DMAMUX_ID
  12. # define WS2812_PWM_DMAMUX_ID WS2812_DMAMUX_ID
  13. #endif
  14. // ========
  15. /* Adapted from https://github.com/joewa/WS2812-LED-Driver_ChibiOS/ */
  16. #ifdef WS2812_RGBW
  17. # define WS2812_CHANNELS 4
  18. #else
  19. # define WS2812_CHANNELS 3
  20. #endif
  21. #ifndef WS2812_PWM_DRIVER
  22. # define WS2812_PWM_DRIVER PWMD2 // TIMx
  23. #endif
  24. #ifndef WS2812_PWM_CHANNEL
  25. # define WS2812_PWM_CHANNEL 2 // Channel
  26. #endif
  27. #ifndef WS2812_PWM_PAL_MODE
  28. # define WS2812_PWM_PAL_MODE 2 // DI Pin's alternate function value
  29. #endif
  30. #ifndef WS2812_PWM_DMA_STREAM
  31. # define WS2812_PWM_DMA_STREAM STM32_DMA1_STREAM2 // DMA Stream for TIMx_UP
  32. #endif
  33. #ifndef WS2812_PWM_DMA_CHANNEL
  34. # define WS2812_PWM_DMA_CHANNEL 2 // DMA Channel for TIMx_UP
  35. #endif
  36. #if (STM32_DMA_SUPPORTS_DMAMUX == TRUE) && !defined(WS2812_PWM_DMAMUX_ID)
  37. # error "please consult your MCU's datasheet and specify in your config.h: #define WS2812_PWM_DMAMUX_ID STM32_DMAMUX1_TIM?_UP"
  38. #endif
  39. #if (AT32_DMA_SUPPORTS_DMAMUX == TRUE) && !defined(WS2812_PWM_DMAMUX_CHANNEL) && !defined(WS2812_PWM_DMAMUX_ID)
  40. # error "please consult your MCU's datasheet and specify in your config.h: #define WS2812_PWM_DMAMUX_CHANNEL 1, #define WS2812_PWM_DMAMUX_ID AT32_DMAMUX_TMR?_OVERFLOW"
  41. #endif
  42. /* Summarize https://www.st.com/resource/en/application_note/an4013-stm32-crossseries-timer-overview-stmicroelectronics.pdf to
  43. * figure out if we are using a 32bit timer. This is needed to setup the DMA controller correctly.
  44. * Ignore STM32H7XX and STM32U5XX as they are not supported by ChibiOS.
  45. */
  46. #if !defined(STM32F1XX) && !defined(STM32L0XX) && !defined(STM32L1XX)
  47. # define WS2812_PWM_TIMER_32BIT_PWMD2 1
  48. #endif
  49. #if !defined(STM32F1XX)
  50. # define WS2812_PWM_TIMER_32BIT_PWMD5 1
  51. #endif
  52. #define WS2812_CONCAT1(a, b) a##b
  53. #define WS2812_CONCAT(a, b) WS2812_CONCAT1(a, b)
  54. #if WS2812_CONCAT(WS2812_PWM_TIMER_32BIT_, WS2812_PWM_DRIVER)
  55. # define WS2812_PWM_TIMER_32BIT
  56. #endif
  57. #ifndef WS2812_PWM_COMPLEMENTARY_OUTPUT
  58. # define WS2812_PWM_OUTPUT_MODE PWM_OUTPUT_ACTIVE_HIGH
  59. #else
  60. # define WS2812_PWM_OUTPUT_MODE PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH
  61. #endif
  62. // Push Pull or Open Drain Configuration
  63. // Default Push Pull
  64. #ifndef WS2812_EXTERNAL_PULLUP
  65. # if defined(USE_GPIOV1)
  66. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE_PUSHPULL
  67. # else
  68. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST | PAL_PUPDR_FLOATING
  69. # endif
  70. #else
  71. # if defined(USE_GPIOV1)
  72. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE_OPENDRAIN
  73. # else
  74. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN | PAL_OUTPUT_SPEED_HIGHEST | PAL_PUPDR_FLOATING
  75. # endif
  76. #endif
  77. // Default is 800000Hz, which has a period of 1.25us
  78. #ifndef WS2812_PWM_FREQUENCY
  79. # define WS2812_PWM_FREQUENCY (1000000000 / WS2812_TIMING)
  80. #endif
  81. /* --- PRIVATE CONSTANTS ---------------------------------------------------- */
  82. #ifndef WS2812_PWM_TICK_FREQUENCY
  83. # define WS2812_PWM_TICK_FREQUENCY (CPU_CLOCK / 2) /**< Clock frequency of PWM ticks, must be valid with respect to system clock! */
  84. #endif
  85. #define WS2812_PWM_PERIOD (WS2812_PWM_TICK_FREQUENCY / WS2812_PWM_FREQUENCY) /**< Clock period in PWM ticks. */
  86. /**
  87. * @brief Number of bit-periods to hold the data line low at the end of a frame
  88. *
  89. * The reset period for each frame is defined in WS2812_TRST_US.
  90. * Calculate the number of zeroes to add at the end assuming 1.25 uS/bit:
  91. */
  92. #define WS2812_COLOR_BITS (WS2812_CHANNELS * 8)
  93. #define WS2812_RESET_BIT_N (1000 * WS2812_TRST_US / WS2812_TIMING)
  94. #define WS2812_COLOR_BIT_N (WS2812_LED_COUNT * WS2812_COLOR_BITS) /**< Number of data bits */
  95. #define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */
  96. /**
  97. * @brief High period for a zero, in ticks
  98. */
  99. #define WS2812_DUTYCYCLE_0 (WS2812_PWM_TICK_FREQUENCY / (1000000000 / WS2812_T0H))
  100. #if (WS2812_DUTYCYCLE_0 > 255)
  101. # error WS2812 PWM driver: High period for a 0 is more than a byte
  102. #endif
  103. /**
  104. * @brief High period for a one, in ticks
  105. */
  106. #define WS2812_DUTYCYCLE_1 (WS2812_PWM_TICK_FREQUENCY / (1000000000 / WS2812_T1H))
  107. #if (WS2812_DUTYCYCLE_1 > 255)
  108. # error WS2812 PWM driver: High period for a 1 is more than a byte
  109. #endif
  110. /* --- PRIVATE MACROS ------------------------------------------------------- */
  111. /**
  112. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given bit
  113. *
  114. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  115. * @param[in] byte: The byte number [0, 2]
  116. * @param[in] bit: The bit number [0, 7]
  117. *
  118. * @return The bit index
  119. */
  120. #define WS2812_BIT(led, byte, bit) (WS2812_COLOR_BITS * (led) + 8 * (byte) + (7 - (bit)))
  121. #if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB)
  122. /**
  123. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  124. *
  125. * @note The red byte is the middle byte in the color packet
  126. *
  127. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  128. * @param[in] bit: The bit number [0, 7]
  129. *
  130. * @return The bit index
  131. */
  132. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  133. /**
  134. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  135. *
  136. * @note The red byte is the first byte in the color packet
  137. *
  138. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  139. * @param[in] bit: The bit number [0, 7]
  140. *
  141. * @return The bit index
  142. */
  143. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  144. /**
  145. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  146. *
  147. * @note The red byte is the last byte in the color packet
  148. *
  149. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  150. * @param[in] bit: The bit index [0, 7]
  151. *
  152. * @return The bit index
  153. */
  154. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  155. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_RGB)
  156. /**
  157. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  158. *
  159. * @note The red byte is the middle byte in the color packet
  160. *
  161. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  162. * @param[in] bit: The bit number [0, 7]
  163. *
  164. * @return The bit index
  165. */
  166. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  167. /**
  168. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  169. *
  170. * @note The red byte is the first byte in the color packet
  171. *
  172. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  173. * @param[in] bit: The bit number [0, 7]
  174. *
  175. * @return The bit index
  176. */
  177. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  178. /**
  179. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  180. *
  181. * @note The red byte is the last byte in the color packet
  182. *
  183. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  184. * @param[in] bit: The bit index [0, 7]
  185. *
  186. * @return The bit index
  187. */
  188. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  189. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_BGR)
  190. /**
  191. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  192. *
  193. * @note The red byte is the middle byte in the color packet
  194. *
  195. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  196. * @param[in] bit: The bit number [0, 7]
  197. *
  198. * @return The bit index
  199. */
  200. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  201. /**
  202. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  203. *
  204. * @note The red byte is the first byte in the color packet
  205. *
  206. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  207. * @param[in] bit: The bit number [0, 7]
  208. *
  209. * @return The bit index
  210. */
  211. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  212. /**
  213. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  214. *
  215. * @note The red byte is the last byte in the color packet
  216. *
  217. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  218. * @param[in] bit: The bit index [0, 7]
  219. *
  220. * @return The bit index
  221. */
  222. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  223. #endif
  224. #ifdef WS2812_RGBW
  225. /**
  226. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given white bit
  227. *
  228. * @note The white byte is the last byte in the color packet
  229. *
  230. * @param[in] led: The led index [0, @ref WS2812_LED_N)
  231. * @param[in] bit: The bit index [0, 7]
  232. *
  233. * @return The bit index
  234. */
  235. # define WS2812_WHITE_BIT(led, bit) WS2812_BIT((led), 3, (bit))
  236. #endif
  237. /* --- PRIVATE VARIABLES ---------------------------------------------------- */
  238. // STM32F2XX, STM32F4XX and STM32F7XX do NOT zero pad DMA transfers of unequal data width. Buffer width must match TIMx CCR.
  239. // For all other STM32 DMA transfer will automatically zero pad. We only need to set the right peripheral width.
  240. #if defined(STM32F2XX) || defined(STM32F4XX) || defined(STM32F7XX)
  241. # if defined(WS2812_PWM_TIMER_32BIT)
  242. # define WS2812_PWM_DMA_MEMORY_WIDTH STM32_DMA_CR_MSIZE_WORD
  243. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_WORD
  244. typedef uint32_t ws2812_buffer_t;
  245. # else
  246. # define WS2812_PWM_DMA_MEMORY_WIDTH STM32_DMA_CR_MSIZE_HWORD
  247. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_HWORD
  248. typedef uint16_t ws2812_buffer_t;
  249. # endif
  250. #elif defined(AT32F415)
  251. # define WS2812_PWM_DMA_MEMORY_WIDTH AT32_DMA_CCTRL_MWIDTH_BYTE
  252. # if defined(WS2812_PWM_TIMER_32BIT)
  253. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH AT32_DMA_CCTRL_PWIDTH_WORD
  254. # else
  255. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH AT32_DMA_CCTRL_PWIDTH_HWORD
  256. # endif
  257. typedef uint8_t ws2812_buffer_t;
  258. #else
  259. # define WS2812_PWM_DMA_MEMORY_WIDTH STM32_DMA_CR_MSIZE_BYTE
  260. # if defined(WS2812_PWM_TIMER_32BIT)
  261. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_WORD
  262. # else
  263. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_HWORD
  264. # endif
  265. typedef uint8_t ws2812_buffer_t;
  266. #endif
  267. static ws2812_buffer_t ws2812_frame_buffer[WS2812_BIT_N + 1]; /**< Buffer for a frame */
  268. /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */
  269. /*
  270. * Gedanke: Double-buffer type transactions: double buffer transfers using two memory pointers for
  271. * the memory (while the DMA is reading/writing from/to a buffer, the application can
  272. * write/read to/from the other buffer).
  273. */
  274. void ws2812_init(void) {
  275. // Initialize led frame buffer
  276. uint32_t i;
  277. for (i = 0; i < WS2812_COLOR_BIT_N; i++)
  278. ws2812_frame_buffer[i] = WS2812_DUTYCYCLE_0; // All color bits are zero duty cycle
  279. for (i = 0; i < WS2812_RESET_BIT_N; i++)
  280. ws2812_frame_buffer[i + WS2812_COLOR_BIT_N] = 0; // All reset bits are zero
  281. palSetLineMode(WS2812_DI_PIN, WS2812_OUTPUT_MODE);
  282. // PWM Configuration
  283. // #pragma GCC diagnostic ignored "-Woverride-init" // Turn off override-init warning for this struct. We use the overriding ability to set a "default" channel config
  284. static const PWMConfig ws2812_pwm_config = {
  285. .frequency = WS2812_PWM_TICK_FREQUENCY,
  286. .period = WS2812_PWM_PERIOD, // Mit dieser Periode wird UDE-Event erzeugt und ein neuer Wert (Länge WS2812_BIT_N) vom DMA ins CCR geschrieben
  287. .callback = NULL,
  288. .channels =
  289. {
  290. [0 ... 3] = {.mode = PWM_OUTPUT_DISABLED, .callback = NULL}, // Channels default to disabled
  291. [WS2812_PWM_CHANNEL - 1] = {.mode = WS2812_PWM_OUTPUT_MODE, .callback = NULL}, // Turn on the channel we care about
  292. },
  293. #if defined(AT32F415)
  294. .ctrl2 = 0,
  295. .iden = AT32_TMR_IDEN_OVFDEN, // DMA on update event for next period
  296. #else
  297. .cr2 = 0,
  298. .dier = TIM_DIER_UDE, // DMA on update event for next period
  299. #endif
  300. };
  301. // #pragma GCC diagnostic pop // Restore command-line warning options
  302. // Configure DMA
  303. // dmaInit(); // Joe added this
  304. #if defined(WB32F3G71xx) || defined(WB32FQ95xx)
  305. dmaStreamAlloc(WS2812_PWM_DMA_STREAM - WB32_DMA_STREAM(0), 10, NULL, NULL);
  306. dmaStreamSetSource(WS2812_PWM_DMA_STREAM, ws2812_frame_buffer);
  307. dmaStreamSetDestination(WS2812_PWM_DMA_STREAM, &(WS2812_PWM_DRIVER.tim->CCR[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register
  308. dmaStreamSetMode(WS2812_PWM_DMA_STREAM, WB32_DMA_CHCFG_HWHIF(WS2812_PWM_DMA_CHANNEL) | WB32_DMA_CHCFG_DIR_M2P | WB32_DMA_CHCFG_PSIZE_WORD | WB32_DMA_CHCFG_MSIZE_WORD | WB32_DMA_CHCFG_MINC | WB32_DMA_CHCFG_CIRC | WB32_DMA_CHCFG_TCIE | WB32_DMA_CHCFG_PL(3));
  309. #elif defined(AT32F415)
  310. dmaStreamAlloc(WS2812_PWM_DMA_STREAM - AT32_DMA_STREAM(0), 10, NULL, NULL);
  311. dmaStreamSetPeripheral(WS2812_PWM_DMA_STREAM, &(WS2812_PWM_DRIVER.tmr->CDT[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register
  312. dmaStreamSetMemory0(WS2812_PWM_DMA_STREAM, ws2812_frame_buffer);
  313. dmaStreamSetMode(WS2812_PWM_DMA_STREAM, AT32_DMA_CCTRL_DTD_M2P | WS2812_PWM_DMA_PERIPHERAL_WIDTH | WS2812_PWM_DMA_MEMORY_WIDTH | AT32_DMA_CCTRL_MINCM | AT32_DMA_CCTRL_LM | AT32_DMA_CCTRL_CHPL(3));
  314. #else
  315. dmaStreamAlloc(WS2812_PWM_DMA_STREAM - STM32_DMA_STREAM(0), 10, NULL, NULL);
  316. dmaStreamSetPeripheral(WS2812_PWM_DMA_STREAM, &(WS2812_PWM_DRIVER.tim->CCR[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register
  317. dmaStreamSetMemory0(WS2812_PWM_DMA_STREAM, ws2812_frame_buffer);
  318. dmaStreamSetMode(WS2812_PWM_DMA_STREAM, STM32_DMA_CR_CHSEL(WS2812_PWM_DMA_CHANNEL) | STM32_DMA_CR_DIR_M2P | WS2812_PWM_DMA_PERIPHERAL_WIDTH | WS2812_PWM_DMA_MEMORY_WIDTH | STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_PL(3));
  319. #endif
  320. dmaStreamSetTransactionSize(WS2812_PWM_DMA_STREAM, WS2812_BIT_N);
  321. // M2P: Memory 2 Periph; PL: Priority Level
  322. #if (STM32_DMA_SUPPORTS_DMAMUX == TRUE)
  323. // If the MCU has a DMAMUX we need to assign the correct resource
  324. dmaSetRequestSource(WS2812_PWM_DMA_STREAM, WS2812_PWM_DMAMUX_ID);
  325. #endif
  326. #if (AT32_DMA_SUPPORTS_DMAMUX == TRUE)
  327. // If the MCU has a DMAMUX we need to assign the correct resource
  328. dmaSetRequestSource(WS2812_PWM_DMA_STREAM, WS2812_PWM_DMAMUX_CHANNEL, WS2812_PWM_DMAMUX_ID);
  329. #endif
  330. // Start DMA
  331. dmaStreamEnable(WS2812_PWM_DMA_STREAM);
  332. // Configure PWM
  333. // NOTE: It's required that preload be enabled on the timer channel CCR register. This is currently enabled in the
  334. // ChibiOS driver code, so we don't have to do anything special to the timer. If we did, we'd have to start the timer,
  335. // disable counting, enable the channel, and then make whatever configuration changes we need.
  336. pwmStart(&WS2812_PWM_DRIVER, &ws2812_pwm_config);
  337. pwmEnableChannel(&WS2812_PWM_DRIVER, WS2812_PWM_CHANNEL - 1, 0); // Initial period is 0; output will be low until first duty cycle is DMA'd in
  338. }
  339. void ws2812_write_led(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b) {
  340. // Write color to frame buffer
  341. for (uint8_t bit = 0; bit < 8; bit++) {
  342. ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  343. ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  344. ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  345. }
  346. }
  347. void ws2812_write_led_rgbw(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  348. // Write color to frame buffer
  349. for (uint8_t bit = 0; bit < 8; bit++) {
  350. ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  351. ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  352. ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  353. #ifdef WS2812_RGBW
  354. ws2812_frame_buffer[WS2812_WHITE_BIT(led_number, bit)] = ((w >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  355. #endif
  356. }
  357. }
  358. ws2812_led_t ws2812_leds[WS2812_LED_COUNT];
  359. void ws2812_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  360. ws2812_leds[index].r = red;
  361. ws2812_leds[index].g = green;
  362. ws2812_leds[index].b = blue;
  363. #if defined(WS2812_RGBW)
  364. ws2812_rgb_to_rgbw(&ws2812_leds[index]);
  365. #endif
  366. }
  367. void ws2812_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  368. for (int i = 0; i < WS2812_LED_COUNT; i++) {
  369. ws2812_set_color(i, red, green, blue);
  370. }
  371. }
  372. void ws2812_flush(void) {
  373. for (int i = 0; i < WS2812_LED_COUNT; i++) {
  374. #if defined(WS2812_RGBW)
  375. ws2812_write_led_rgbw(i, ws2812_leds[i].r, ws2812_leds[i].g, ws2812_leds[i].b, ws2812_leds[i].w);
  376. #else
  377. ws2812_write_led(i, ws2812_leds[i].r, ws2812_leds[i].g, ws2812_leds[i].b);
  378. #endif
  379. }
  380. }