qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

ws2812_pwm.c (17876B)


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