logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

ws2812_pwm.c (17874B)


  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. #define WS2812_PWM_TICK_FREQUENCY (CPU_CLOCK / 2) /**< Clock frequency of PWM ticks, must be valid with respect to system clock! */
  83. #define WS2812_PWM_PERIOD (WS2812_PWM_TICK_FREQUENCY / WS2812_PWM_FREQUENCY) /**< Clock period in PWM ticks. */
  84. /**
  85. * @brief Number of bit-periods to hold the data line low at the end of a frame
  86. *
  87. * The reset period for each frame is defined in WS2812_TRST_US.
  88. * Calculate the number of zeroes to add at the end assuming 1.25 uS/bit:
  89. */
  90. #define WS2812_COLOR_BITS (WS2812_CHANNELS * 8)
  91. #define WS2812_RESET_BIT_N (1000 * WS2812_TRST_US / WS2812_TIMING)
  92. #define WS2812_COLOR_BIT_N (WS2812_LED_COUNT * WS2812_COLOR_BITS) /**< Number of data bits */
  93. #define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */
  94. /**
  95. * @brief High period for a zero, in ticks
  96. */
  97. #define WS2812_DUTYCYCLE_0 (WS2812_PWM_TICK_FREQUENCY / (1000000000 / WS2812_T0H))
  98. #if (WS2812_DUTYCYCLE_0 > 255)
  99. # error WS2812 PWM driver: High period for a 0 is more than a byte
  100. #endif
  101. /**
  102. * @brief High period for a one, in ticks
  103. */
  104. #define WS2812_DUTYCYCLE_1 (WS2812_PWM_TICK_FREQUENCY / (1000000000 / WS2812_T1H))
  105. #if (WS2812_DUTYCYCLE_1 > 255)
  106. # error WS2812 PWM driver: High period for a 1 is more than a byte
  107. #endif
  108. /* --- PRIVATE MACROS ------------------------------------------------------- */
  109. /**
  110. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given bit
  111. *
  112. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  113. * @param[in] byte: The byte number [0, 2]
  114. * @param[in] bit: The bit number [0, 7]
  115. *
  116. * @return The bit index
  117. */
  118. #define WS2812_BIT(led, byte, bit) (WS2812_COLOR_BITS * (led) + 8 * (byte) + (7 - (bit)))
  119. #if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB)
  120. /**
  121. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  122. *
  123. * @note The red byte is the middle byte in the color packet
  124. *
  125. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  126. * @param[in] bit: The bit number [0, 7]
  127. *
  128. * @return The bit index
  129. */
  130. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  131. /**
  132. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  133. *
  134. * @note The red byte is the first byte in the color packet
  135. *
  136. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  137. * @param[in] bit: The bit number [0, 7]
  138. *
  139. * @return The bit index
  140. */
  141. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  142. /**
  143. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  144. *
  145. * @note The red byte is the last byte in the color packet
  146. *
  147. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  148. * @param[in] bit: The bit index [0, 7]
  149. *
  150. * @return The bit index
  151. */
  152. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  153. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_RGB)
  154. /**
  155. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  156. *
  157. * @note The red byte is the middle byte in the color packet
  158. *
  159. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  160. * @param[in] bit: The bit number [0, 7]
  161. *
  162. * @return The bit index
  163. */
  164. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  165. /**
  166. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  167. *
  168. * @note The red byte is the first byte in the color packet
  169. *
  170. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  171. * @param[in] bit: The bit number [0, 7]
  172. *
  173. * @return The bit index
  174. */
  175. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  176. /**
  177. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  178. *
  179. * @note The red byte is the last byte in the color packet
  180. *
  181. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  182. * @param[in] bit: The bit index [0, 7]
  183. *
  184. * @return The bit index
  185. */
  186. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  187. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_BGR)
  188. /**
  189. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  190. *
  191. * @note The red byte is the middle byte in the color packet
  192. *
  193. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  194. * @param[in] bit: The bit number [0, 7]
  195. *
  196. * @return The bit index
  197. */
  198. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  199. /**
  200. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  201. *
  202. * @note The red byte is the first byte in the color packet
  203. *
  204. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  205. * @param[in] bit: The bit number [0, 7]
  206. *
  207. * @return The bit index
  208. */
  209. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  210. /**
  211. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  212. *
  213. * @note The red byte is the last byte in the color packet
  214. *
  215. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  216. * @param[in] bit: The bit index [0, 7]
  217. *
  218. * @return The bit index
  219. */
  220. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  221. #endif
  222. #ifdef WS2812_RGBW
  223. /**
  224. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given white bit
  225. *
  226. * @note The white byte is the last byte in the color packet
  227. *
  228. * @param[in] led: The led index [0, @ref WS2812_LED_N)
  229. * @param[in] bit: The bit index [0, 7]
  230. *
  231. * @return The bit index
  232. */
  233. # define WS2812_WHITE_BIT(led, bit) WS2812_BIT((led), 3, (bit))
  234. #endif
  235. /* --- PRIVATE VARIABLES ---------------------------------------------------- */
  236. // STM32F2XX, STM32F4XX and STM32F7XX do NOT zero pad DMA transfers of unequal data width. Buffer width must match TIMx CCR.
  237. // For all other STM32 DMA transfer will automatically zero pad. We only need to set the right peripheral width.
  238. #if defined(STM32F2XX) || defined(STM32F4XX) || defined(STM32F7XX)
  239. # if defined(WS2812_PWM_TIMER_32BIT)
  240. # define WS2812_PWM_DMA_MEMORY_WIDTH STM32_DMA_CR_MSIZE_WORD
  241. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_WORD
  242. typedef uint32_t ws2812_buffer_t;
  243. # else
  244. # define WS2812_PWM_DMA_MEMORY_WIDTH STM32_DMA_CR_MSIZE_HWORD
  245. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_HWORD
  246. typedef uint16_t ws2812_buffer_t;
  247. # endif
  248. #elif defined(AT32F415)
  249. # define WS2812_PWM_DMA_MEMORY_WIDTH AT32_DMA_CCTRL_MWIDTH_BYTE
  250. # if defined(WS2812_PWM_TIMER_32BIT)
  251. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH AT32_DMA_CCTRL_PWIDTH_WORD
  252. # else
  253. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH AT32_DMA_CCTRL_PWIDTH_HWORD
  254. # endif
  255. typedef uint8_t ws2812_buffer_t;
  256. #else
  257. # define WS2812_PWM_DMA_MEMORY_WIDTH STM32_DMA_CR_MSIZE_BYTE
  258. # if defined(WS2812_PWM_TIMER_32BIT)
  259. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_WORD
  260. # else
  261. # define WS2812_PWM_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_HWORD
  262. # endif
  263. typedef uint8_t ws2812_buffer_t;
  264. #endif
  265. static ws2812_buffer_t ws2812_frame_buffer[WS2812_BIT_N + 1]; /**< Buffer for a frame */
  266. /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */
  267. /*
  268. * Gedanke: Double-buffer type transactions: double buffer transfers using two memory pointers for
  269. * the memory (while the DMA is reading/writing from/to a buffer, the application can
  270. * write/read to/from the other buffer).
  271. */
  272. void ws2812_init(void) {
  273. // Initialize led frame buffer
  274. uint32_t i;
  275. for (i = 0; i < WS2812_COLOR_BIT_N; i++)
  276. ws2812_frame_buffer[i] = WS2812_DUTYCYCLE_0; // All color bits are zero duty cycle
  277. for (i = 0; i < WS2812_RESET_BIT_N; i++)
  278. ws2812_frame_buffer[i + WS2812_COLOR_BIT_N] = 0; // All reset bits are zero
  279. palSetLineMode(WS2812_DI_PIN, WS2812_OUTPUT_MODE);
  280. // PWM Configuration
  281. //#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
  282. static const PWMConfig ws2812_pwm_config = {
  283. .frequency = WS2812_PWM_TICK_FREQUENCY,
  284. .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
  285. .callback = NULL,
  286. .channels =
  287. {
  288. [0 ... 3] = {.mode = PWM_OUTPUT_DISABLED, .callback = NULL}, // Channels default to disabled
  289. [WS2812_PWM_CHANNEL - 1] = {.mode = WS2812_PWM_OUTPUT_MODE, .callback = NULL}, // Turn on the channel we care about
  290. },
  291. #if defined(AT32F415)
  292. .ctrl2 = 0,
  293. .iden = AT32_TMR_IDEN_OVFDEN, // DMA on update event for next period
  294. #else
  295. .cr2 = 0,
  296. .dier = TIM_DIER_UDE, // DMA on update event for next period
  297. #endif
  298. };
  299. //#pragma GCC diagnostic pop // Restore command-line warning options
  300. // Configure DMA
  301. // dmaInit(); // Joe added this
  302. #if defined(WB32F3G71xx) || defined(WB32FQ95xx)
  303. dmaStreamAlloc(WS2812_PWM_DMA_STREAM - WB32_DMA_STREAM(0), 10, NULL, NULL);
  304. dmaStreamSetSource(WS2812_PWM_DMA_STREAM, ws2812_frame_buffer);
  305. dmaStreamSetDestination(WS2812_PWM_DMA_STREAM, &(WS2812_PWM_DRIVER.tim->CCR[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register
  306. 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));
  307. #elif defined(AT32F415)
  308. dmaStreamAlloc(WS2812_PWM_DMA_STREAM - AT32_DMA_STREAM(0), 10, NULL, NULL);
  309. dmaStreamSetPeripheral(WS2812_PWM_DMA_STREAM, &(WS2812_PWM_DRIVER.tmr->CDT[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register
  310. dmaStreamSetMemory0(WS2812_PWM_DMA_STREAM, ws2812_frame_buffer);
  311. 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));
  312. #else
  313. dmaStreamAlloc(WS2812_PWM_DMA_STREAM - STM32_DMA_STREAM(0), 10, NULL, NULL);
  314. dmaStreamSetPeripheral(WS2812_PWM_DMA_STREAM, &(WS2812_PWM_DRIVER.tim->CCR[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register
  315. dmaStreamSetMemory0(WS2812_PWM_DMA_STREAM, ws2812_frame_buffer);
  316. 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));
  317. #endif
  318. dmaStreamSetTransactionSize(WS2812_PWM_DMA_STREAM, WS2812_BIT_N);
  319. // M2P: Memory 2 Periph; PL: Priority Level
  320. #if (STM32_DMA_SUPPORTS_DMAMUX == TRUE)
  321. // If the MCU has a DMAMUX we need to assign the correct resource
  322. dmaSetRequestSource(WS2812_PWM_DMA_STREAM, WS2812_PWM_DMAMUX_ID);
  323. #endif
  324. #if (AT32_DMA_SUPPORTS_DMAMUX == TRUE)
  325. // If the MCU has a DMAMUX we need to assign the correct resource
  326. dmaSetRequestSource(WS2812_PWM_DMA_STREAM, WS2812_PWM_DMAMUX_CHANNEL, WS2812_PWM_DMAMUX_ID);
  327. #endif
  328. // Start DMA
  329. dmaStreamEnable(WS2812_PWM_DMA_STREAM);
  330. // Configure PWM
  331. // NOTE: It's required that preload be enabled on the timer channel CCR register. This is currently enabled in the
  332. // 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,
  333. // disable counting, enable the channel, and then make whatever configuration changes we need.
  334. pwmStart(&WS2812_PWM_DRIVER, &ws2812_pwm_config);
  335. 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
  336. }
  337. void ws2812_write_led(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b) {
  338. // Write color to frame buffer
  339. for (uint8_t bit = 0; bit < 8; bit++) {
  340. ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  341. ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  342. ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  343. }
  344. }
  345. void ws2812_write_led_rgbw(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  346. // Write color to frame buffer
  347. for (uint8_t bit = 0; bit < 8; bit++) {
  348. ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  349. ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  350. ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  351. #ifdef WS2812_RGBW
  352. ws2812_frame_buffer[WS2812_WHITE_BIT(led_number, bit)] = ((w >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  353. #endif
  354. }
  355. }
  356. ws2812_led_t ws2812_leds[WS2812_LED_COUNT];
  357. void ws2812_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  358. ws2812_leds[index].r = red;
  359. ws2812_leds[index].g = green;
  360. ws2812_leds[index].b = blue;
  361. #if defined(WS2812_RGBW)
  362. ws2812_rgb_to_rgbw(&ws2812_leds[index]);
  363. #endif
  364. }
  365. void ws2812_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  366. for (int i = 0; i < WS2812_LED_COUNT; i++) {
  367. ws2812_set_color(i, red, green, blue);
  368. }
  369. }
  370. void ws2812_flush(void) {
  371. for (int i = 0; i < WS2812_LED_COUNT; i++) {
  372. #if defined(WS2812_RGBW)
  373. ws2812_write_led_rgbw(i, ws2812_leds[i].r, ws2812_leds[i].g, ws2812_leds[i].b, ws2812_leds[i].w);
  374. #else
  375. ws2812_write_led(i, ws2812_leds[i].r, ws2812_leds[i].g, ws2812_leds[i].b);
  376. #endif
  377. }
  378. }