mirror of
https://gitee.com/dlmu-cone/tronone-h7-scaffold
synced 2026-07-24 11:37:44 +08:00
ioc changed but not tested
This commit is contained in:
@@ -16,6 +16,9 @@
|
||||
|
||||
#include "ws2812.h"
|
||||
|
||||
#define WS2812_SPI_TIMEOUT 2U
|
||||
#define WS2812_RESET_BYTES 100U
|
||||
|
||||
#define WS2812_LowLevel 0xC0 // 0码
|
||||
#define WS2812_HighLevel 0xF0 // 1码
|
||||
|
||||
@@ -29,17 +32,22 @@ void WS2812_Ctrl(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
uint8_t txbuf[24];
|
||||
uint8_t res = 0;
|
||||
for (auto i = 0; i < 8; i++)
|
||||
for (uint8_t i = 0; i < 8U; i++)
|
||||
{
|
||||
txbuf[7 - i] = (((g >> i) & 0x01) ? WS2812_HighLevel : WS2812_LowLevel) >> 1;
|
||||
txbuf[15 - i] = (((r >> i) & 0x01) ? WS2812_HighLevel : WS2812_LowLevel) >> 1;
|
||||
txbuf[23 - i] = (((b >> i) & 0x01) ? WS2812_HighLevel : WS2812_LowLevel) >> 1;
|
||||
}
|
||||
HAL_SPI_Transmit(&WS2812_SPI_UNIT, &res, 0, 0xFFFF);
|
||||
while (WS2812_SPI_UNIT.State != HAL_SPI_STATE_READY);
|
||||
HAL_SPI_Transmit(&WS2812_SPI_UNIT, txbuf, 24, 0xFFFF);
|
||||
for (auto i = 0; i < 100; i++)
|
||||
if (HAL_SPI_Transmit(&WS2812_SPI_UNIT, txbuf, sizeof(txbuf), WS2812_SPI_TIMEOUT) != HAL_OK)
|
||||
{
|
||||
HAL_SPI_Transmit(&WS2812_SPI_UNIT, &res, 1, 0xFFFF);
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < WS2812_RESET_BYTES; i++)
|
||||
{
|
||||
if (HAL_SPI_Transmit(&WS2812_SPI_UNIT, &res, 1U, WS2812_SPI_TIMEOUT) != HAL_OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user