WS2812B Liblary this use SPI

Committer:
Suzutomo
Date:
Wed Jul 17 14:06:08 2019 +0000
Revision:
5:62b6ac21c199
Parent:
4:02e88df0ae2d
Child:
6:21a721b31a20
Perfect LED Light

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