WS2812B Liblary this use SPI

Committer:
Suzutomo
Date:
Sat Dec 21 15:33:10 2019 +0000
Revision:
6:21a721b31a20
Parent:
5:62b6ac21c199
change irq

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 "mbed.h"
Suzutomo 0:6a2dcf5cd545 9
Suzutomo 0:6a2dcf5cd545 10 #ifndef STM32F4xx_WS2812B_HPP
Suzutomo 0:6a2dcf5cd545 11 #define STM32F4xx_WS2812B_HPP
Suzutomo 0:6a2dcf5cd545 12
Suzutomo 6:21a721b31a20 13 enum RGB {B,G,R};
Suzutomo 6:21a721b31a20 14
Suzutomo 0:6a2dcf5cd545 15 class WS2812B
Suzutomo 0:6a2dcf5cd545 16 {
Suzutomo 0:6a2dcf5cd545 17 public:
Suzutomo 0:6a2dcf5cd545 18 // コンストラクタ
Suzutomo 0:6a2dcf5cd545 19 // inv = true: インバータを介して WS2812B に接続する場合
Suzutomo 0:6a2dcf5cd545 20 // = false: 直接 WS2812B に接続する場合
Suzutomo 3:c0a82b9775e6 21 WS2812B(PinName pin, int num, bool inv = false);
Suzutomo 0:6a2dcf5cd545 22
Suzutomo 0:6a2dcf5cd545 23 virtual ~WS2812B() {}
Suzutomo 0:6a2dcf5cd545 24
Suzutomo 3:c0a82b9775e6 25 void Send(); // LED へ書き込む
Suzutomo 3:c0a82b9775e6 26 void Write(uint32_t x,double brightness = 1.0); // 一つの LED へ書き込む
Suzutomo 3:c0a82b9775e6 27 void Write(int index,uint32_t x,double brightness = 1.0); // k 個の LED へ書き込む
Suzutomo 0:6a2dcf5cd545 28 void Reset() { wait_us(50); }
Suzutomo 0:6a2dcf5cd545 29 void Clear(int k); // k 個の LED を消灯
Suzutomo 3:c0a82b9775e6 30 void Brightness(double brightness);
Suzutomo 6:21a721b31a20 31 uint32_t BrightAdjust(uint32_t x,double brightness);
Suzutomo 6:21a721b31a20 32 void Normalize(bool a = true);
Suzutomo 0:6a2dcf5cd545 33
Suzutomo 0:6a2dcf5cd545 34 private:
Suzutomo 3:c0a82b9775e6 35 uint32_t *colors;
Suzutomo 3:c0a82b9775e6 36 int bufferSize;
Suzutomo 3:c0a82b9775e6 37 double bright;
Suzutomo 6:21a721b31a20 38 bool normalize;
Suzutomo 3:c0a82b9775e6 39
Suzutomo 0:6a2dcf5cd545 40 SPI spi_;
Suzutomo 0:6a2dcf5cd545 41 SPI_TypeDef *mySpi_;
Suzutomo 0:6a2dcf5cd545 42
Suzutomo 0:6a2dcf5cd545 43 void (WS2812B::*fp)(uint8_t);
Suzutomo 0:6a2dcf5cd545 44 void SendByte(uint8_t x) { (this->*fp)(x); }
Suzutomo 4:02e88df0ae2d 45 void Send3Bytes(uint16_t x0, uint16_t x1, uint16_t x2);
Suzutomo 5:62b6ac21c199 46 void T0HL() { Send3Bytes(0b11111111, 0b0, 0b0); } // 0 を送る
Suzutomo 5:62b6ac21c199 47 void T1HL() { Send3Bytes(0b11111111, 0b11111111, 0b0); } // 1 を送る
Suzutomo 0:6a2dcf5cd545 48 void SendByteNorm(uint8_t x); // データをそのまま送る
Suzutomo 0:6a2dcf5cd545 49 void SendByteInv(uint8_t x); // データを反転して送る
Suzutomo 6:21a721b31a20 50
Suzutomo 6:21a721b31a20 51 uint8_t getRGB(uint32_t color,RGB rgb);
Suzutomo 6:21a721b31a20 52 uint32_t NormalizeAdjust(uint32_t x);
Suzutomo 0:6a2dcf5cd545 53
Suzutomo 0:6a2dcf5cd545 54 // コピー・コンストラクタと代入演算子は使用禁止
Suzutomo 0:6a2dcf5cd545 55 WS2812B(const WS2812B&);
Suzutomo 0:6a2dcf5cd545 56 WS2812B& operator=(const WS2812B&);
Suzutomo 0:6a2dcf5cd545 57 };
Suzutomo 0:6a2dcf5cd545 58 #endif