BW16 改写官方SW2812基本库
本次主要解决BW16官方SW2812基本库不支持对RGBW类的led进行控制。
1、根据自己电脑路径找到下图两个文件:![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/e33363d8862f40fb89915b84166962d4.png)
2、主要改写的函数如下:
void WS2812B::sendPixel(uint8_t red ,uint8_t green ,uint8_t blue, uint8_t white, uint8_t mode) {
if (mode == 0)
{
uint8_t grb_in_array[3] = {green, red, blue};
//Set a bit pattern corresponding to RGB = 0,0,0 for WS2812B LED timings
uint16_t grb_out_array[6] = {0x0924, 0x0924, 0x0924, 0x0924, 0x0924, 0x924};
//Check the input colours bits and output it as a '1' or '0'.
for (int i = 0; i < 6; i++) {
//check the first four bits of each colours
if ((i % 2) == 0) {
grb_out_array[i] |= (grb_in_array[i/2] & 0x80) ? (0x0400) : (0);
grb_out_array[i] |= (grb_in_array[i/2] & 0x40) ? (0x0080) : (0);
grb_out_array[i] |= (grb_in_array[i/2] & 0x20) ? (0x0010) : (0);
grb_out_array[i] |= (grb_in_array[i/2] & 0x10) ? (0x0002) : (0);
//check the last four bits of each colours
} else {
grb_out_array[i] |= (grb_in_array[i/2] & 0x08) ? (0x0400) : (0);
grb_out_array[i] |= (grb_in_array[i/2] & 0x04) ? (0x0080) : (0);
grb_out_array[i] |= (grb_in_array[i/2] & 0x02) ? (0x0010) : (0);
grb_out_array[i] |= (grb_in_array[i/2] & 0x01) ? (0x0002) : (0);
}
spi_slave_write((spi_t *)pSpiMaster, grb_out_array[i]);
}
}
else if(mode == 1)
{
uint8_t grb_in_array[4] = { green, red, blue, white };
//Set a bit pattern corresponding to RGB = 0,0,0 for WS2812B LED timings
uint16_t grb_out_array[8] = { 0x0924, 0x0924, 0x0924, 0x0924, 0x0924, 0x924, 0x0924, 0x924 };
//Check the input colours bits and output it as a '1' or '0'.
for (int i = 0; i < 8; i++) {
//check the first four bits of each colours
if ((i % 2) == 0) {
grb_out_array[i] |= (grb_in_array[i / 2] & 0x80) ? (0x0400) : (0);
grb_out_array[i] |= (grb_in_array[i / 2] & 0x40) ? (0x0080) : (0);
grb_out_array[i] |= (grb_in_array[i / 2] & 0x20) ? (0x0010) : (0);
grb_out_array[i] |= (grb_in_array[i / 2] & 0x10) ? (0x0002) : (0);
//check the last four bits of each colours
}
else {
grb_out_array[i] |= (grb_in_array[i / 2] & 0x08) ? (0x0400) : (0);
grb_out_array[i] |= (grb_in_array[i / 2] & 0x04) ? (0x0080) : (0);
grb_out_array[i] |= (grb_in_array[i / 2] & 0x02) ? (0x0010) : (0);
grb_out_array[i] |= (grb_in_array[i / 2] & 0x01) ? (0x0002) : (0);
}
spi_slave_write((spi_t*)pSpiMaster, grb_out_array[i]);
}
}
}
以上代码中增加(uint8_t white, uint8_t mode)两个函数来进行设定参数,white=颜色值;mode=模式选择(0:RGB;1RGBW;)
总体改写情况如下:(修改文件已打包上传)
改写过的库文件下载:点击下载(真是路径如下图所示)
注:基本库的改写仅供参考学习,可以根据自己需求自己升华使用!