不韋 呂 / UIT_WS2812B

Dependents:   Demo_WS2812B_SPI

Revision:
0:160ed7a225a4
Child:
1:2d0f84d78ca2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ws2812B.hpp	Sat Sep 24 05:52:29 2016 +0000
@@ -0,0 +1,51 @@
+//----------------------------------------------------
+//  SPI を使って WS2812B を点灯するためのクラス(ヘッダ)
+//      サポートしているボード
+//          Nucleo-F401RE, Nucleo-F446RE
+//  2016/09/24, Copyright (c) 2016 MIKAMI, Naoki
+//----------------------------------------------------
+
+#include "mbed.h"
+
+#if !defined(STM32F401xE) && !defined(STM32F446xx)
+#error Select STM32F401 or STM32F446.
+#endif
+
+#ifndef STM32F4xx_WS2812B_HPP
+#define STM32F4xx_WS2812B_HPP
+
+namespace Mikami
+{
+    class WS2812B
+    {
+    public:
+        // コンストラクタ
+        //      inv = true:  インバータを介して WS2812B に接続する場合
+        //          = false: 直接 WS2812B に接続する場合
+        WS2812B(PinName pin, bool inv = false);
+
+        void Write(uint32_t x); // 一つの LED へ書き込む
+        void Reset();           // 一連の書き込みの最後に実行する
+        void Clear(int k);      // k 個の LED を消灯
+
+    private:
+        SPI_TypeDef *mySpi_;
+
+        struct spi123{PinName pin; SPI_TypeDef *spi; };
+#ifdef STM32F401xE
+        static const spi123 f401[];
+#else
+        static const spi123 f446[];
+#endif  // STM32F401xE
+
+        void (WS2812B::*fp)(uint8_t);
+        void SendByte(uint8_t x) { (this->*fp)(x); }
+        void Send3Bytes(uint8_t x0, uint8_t x1, uint8_t x2);
+        void T0HL() { Send3Bytes(0xFE, 0x00, 0x00); }   // 0 を送る
+        void T1HL() { Send3Bytes(0xFF, 0xFF, 0x00); }   // 1 を送る
+        void Dummy() { SendByte(0x00); }
+        void SendByteNrm(uint8_t x);    // データをそのまま送る
+        void SendByteInv(uint8_t x);    // データを反転して送る
+    };
+}
+#endif  // STM32F4xx_WS2812B_HPP