WS2812B Liblary this use SPI

Committer:
Suzutomo
Date:
Sun Jun 30 08:26:25 2019 +0000
Revision:
3:c0a82b9775e6
Parent:
1:c7bb475f0022
Child:
4:02e88df0ae2d
function Send() Add

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Suzutomo 0:6a2dcf5cd545 1 //--------------------------------------------------------
Suzutomo 0:6a2dcf5cd545 2 // SPI を使って WS2812B を点灯するためのクラス
Suzutomo 0:6a2dcf5cd545 3 // サポートするボード: Nucleo-F401RE, Nucleo-F446RE
Suzutomo 0:6a2dcf5cd545 4 //
Suzutomo 0:6a2dcf5cd545 5 // 2016/11/21, Copyright (c) 2016 MIKAMI, Naoki
Suzutomo 0:6a2dcf5cd545 6 //--------------------------------------------------------
Suzutomo 0:6a2dcf5cd545 7
Suzutomo 0:6a2dcf5cd545 8 #include "WS2812B.h"
Suzutomo 0:6a2dcf5cd545 9 #include "PeripheralPins.h" // for pinmap_peripheral()
Suzutomo 0:6a2dcf5cd545 10
Suzutomo 3:c0a82b9775e6 11 WS2812B::WS2812B(PinName pin, int num, bool inv)
Suzutomo 0:6a2dcf5cd545 12 : spi_(pin, NC, NC),
Suzutomo 0:6a2dcf5cd545 13 mySpi_((SPI_TypeDef *)pinmap_peripheral(pin, PinMap_SPI_MOSI))
Suzutomo 0:6a2dcf5cd545 14 {
Suzutomo 0:6a2dcf5cd545 15 spi_.format(8, 0);
Suzutomo 0:6a2dcf5cd545 16 // クロックを 23 MHz 以下で最大の値に設定
Suzutomo 0:6a2dcf5cd545 17 // F401RE: 21.0 MHz
Suzutomo 0:6a2dcf5cd545 18 // F446RE: 22.5 MHz
Suzutomo 0:6a2dcf5cd545 19 #if defined(STM32F446xx)
Suzutomo 1:c7bb475f0022 20 spi_.frequency(22500000);
Suzutomo 0:6a2dcf5cd545 21 #elif defined(STM32F401xE)
Suzutomo 0:6a2dcf5cd545 22 spi_.frequency(21000000);
Suzutomo 0:6a2dcf5cd545 23 #else
Suzutomo 0:6a2dcf5cd545 24 #error This code is not move this board.
Suzutomo 0:6a2dcf5cd545 25 #endif
Suzutomo 0:6a2dcf5cd545 26 if (!inv) fp = &WS2812B::SendByteNorm;
Suzutomo 0:6a2dcf5cd545 27 else fp = &WS2812B::SendByteInv;
Suzutomo 3:c0a82b9775e6 28
Suzutomo 3:c0a82b9775e6 29 colors = (uint32_t *)calloc(num,sizeof(uint32_t));
Suzutomo 3:c0a82b9775e6 30 if (colors == NULL) printf("can not reserve memory\n");
Suzutomo 3:c0a82b9775e6 31 bufferSize = num;
Suzutomo 3:c0a82b9775e6 32 bright = 1.0;
Suzutomo 3:c0a82b9775e6 33 //printf("buffer : %d\r\n",bufferSize);
Suzutomo 3:c0a82b9775e6 34 }
Suzutomo 3:c0a82b9775e6 35
Suzutomo 3:c0a82b9775e6 36 uint32_t BrightAdjust(uint32_t x,double brightness)
Suzutomo 3:c0a82b9775e6 37 {
Suzutomo 3:c0a82b9775e6 38 uint8_t r = ((x >> 16) & 0xFF) * brightness;
Suzutomo 3:c0a82b9775e6 39 uint8_t g = ((x >> 8) & 0xFF) * brightness;
Suzutomo 3:c0a82b9775e6 40 uint8_t b = ((x >> 0) & 0xFF) * brightness;
Suzutomo 3:c0a82b9775e6 41 x = (r << 16) | (g << 8) | b;
Suzutomo 3:c0a82b9775e6 42 return x;
Suzutomo 3:c0a82b9775e6 43 }
Suzutomo 3:c0a82b9775e6 44
Suzutomo 3:c0a82b9775e6 45 void WS2812B::Write(int index,uint32_t x,double brightness)
Suzutomo 3:c0a82b9775e6 46 {
Suzutomo 3:c0a82b9775e6 47 if (index >= 0 && index < bufferSize) colors[index] = BrightAdjust(x,brightness);
Suzutomo 0:6a2dcf5cd545 48 }
Suzutomo 0:6a2dcf5cd545 49
Suzutomo 3:c0a82b9775e6 50 void WS2812B::Write(uint32_t x,double brightness)
Suzutomo 0:6a2dcf5cd545 51 {
Suzutomo 3:c0a82b9775e6 52 for (int i = 0; i < bufferSize; i++) colors[i] = BrightAdjust(x,brightness);
Suzutomo 0:6a2dcf5cd545 53 }
Suzutomo 0:6a2dcf5cd545 54
Suzutomo 3:c0a82b9775e6 55 void WS2812B::Send()
Suzutomo 0:6a2dcf5cd545 56 {
Suzutomo 3:c0a82b9775e6 57 uint32_t *colors_m;
Suzutomo 3:c0a82b9775e6 58 colors_m = (uint32_t *)calloc(bufferSize,sizeof(uint32_t));
Suzutomo 3:c0a82b9775e6 59 for (int i = 0; i < bufferSize; i++) {
Suzutomo 3:c0a82b9775e6 60 uint32_t x = colors[i];
Suzutomo 3:c0a82b9775e6 61 x = BrightAdjust(x,bright);
Suzutomo 3:c0a82b9775e6 62 //printf("%6x\r\n",x);
Suzutomo 3:c0a82b9775e6 63 colors_m[i] = 0;
Suzutomo 3:c0a82b9775e6 64 colors_m[i] |= ((x >> 8) & 0xFF00);
Suzutomo 3:c0a82b9775e6 65 colors_m[i] |= ((x << 8) & 0xFF0000);
Suzutomo 3:c0a82b9775e6 66 colors_m[i] |= (x & 0xFF);
Suzutomo 3:c0a82b9775e6 67 }
Suzutomo 3:c0a82b9775e6 68 static const uint32_t bit23 = 0x800000;
Suzutomo 3:c0a82b9775e6 69 for (int i = 0; i < bufferSize; i++) {
Suzutomo 3:c0a82b9775e6 70 for (int n=0; n<24; n++) {
Suzutomo 3:c0a82b9775e6 71 if ((colors_m[i] & bit23) == bit23) T1HL();
Suzutomo 3:c0a82b9775e6 72 else T0HL();
Suzutomo 3:c0a82b9775e6 73 colors_m[i] <<= 1;
Suzutomo 3:c0a82b9775e6 74 }
Suzutomo 3:c0a82b9775e6 75 }
Suzutomo 3:c0a82b9775e6 76 Reset();
Suzutomo 3:c0a82b9775e6 77 free(colors_m);
Suzutomo 3:c0a82b9775e6 78 }
Suzutomo 0:6a2dcf5cd545 79
Suzutomo 0:6a2dcf5cd545 80 void WS2812B::Clear(int k)
Suzutomo 0:6a2dcf5cd545 81 {
Suzutomo 3:c0a82b9775e6 82 for (int n=0; n<k; n++) colors[n] = 0;
Suzutomo 3:c0a82b9775e6 83 Send();
Suzutomo 0:6a2dcf5cd545 84 Reset();
Suzutomo 0:6a2dcf5cd545 85 }
Suzutomo 0:6a2dcf5cd545 86
Suzutomo 0:6a2dcf5cd545 87 void WS2812B::Send3Bytes(uint8_t x0, uint8_t x1, uint8_t x2)
Suzutomo 0:6a2dcf5cd545 88 {
Suzutomo 0:6a2dcf5cd545 89 SendByte(x0);
Suzutomo 0:6a2dcf5cd545 90 SendByte(x1);
Suzutomo 0:6a2dcf5cd545 91 SendByte(x2);
Suzutomo 0:6a2dcf5cd545 92 }
Suzutomo 0:6a2dcf5cd545 93
Suzutomo 0:6a2dcf5cd545 94 void WS2812B::SendByteNorm(uint8_t x)
Suzutomo 0:6a2dcf5cd545 95 {
Suzutomo 0:6a2dcf5cd545 96 while ((mySpi_->SR & SPI_SR_TXE) != SPI_SR_TXE) {}
Suzutomo 0:6a2dcf5cd545 97 mySpi_->DR = x;
Suzutomo 0:6a2dcf5cd545 98 }
Suzutomo 0:6a2dcf5cd545 99
Suzutomo 0:6a2dcf5cd545 100 void WS2812B::SendByteInv(uint8_t x)
Suzutomo 0:6a2dcf5cd545 101 {
Suzutomo 0:6a2dcf5cd545 102 while ((mySpi_->SR & SPI_SR_TXE) != SPI_SR_TXE) {}
Suzutomo 0:6a2dcf5cd545 103 mySpi_->DR = ~x;
Suzutomo 0:6a2dcf5cd545 104 }
Suzutomo 0:6a2dcf5cd545 105
Suzutomo 3:c0a82b9775e6 106 void WS2812B::Brightness(double brightness)
Suzutomo 3:c0a82b9775e6 107 {
Suzutomo 3:c0a82b9775e6 108 bright = brightness;
Suzutomo 3:c0a82b9775e6 109 }
Suzutomo 3:c0a82b9775e6 110