micro:bit x Web Bluetooth APIの最終章。
最後はLED
micro:bitのBLEを色々いじってきました。
ちょっとキリがないので、今回のLED制御で一旦シメたいと思います。
micro:bit側は、BLE系のサービスを開始するだけ
相変わらず、Bluetooth系のサービスをすべて初期化するコードです。
今回も変える必要がないので、このままの実装で進めます。
bluetooth.startAccelerometerService() bluetooth.startButtonService() bluetooth.startIOPinService() bluetooth.startLEDService() bluetooth.startTemperatureService() bluetooth.startMagnetometerService()
IOピンサービスっていうのもあります。たまには思い出してあげてください。
HTML側のプログラミング
例によって、このプロファイル仕様書を参考にしています。
Bluetooth Developer Studio - Profile Report
LED系のサービスは少し特殊で、今表示しているLEDの状態を取得したり、任意の状態を書き込んで、好きな用に光らせることもできます(ReadもWriteもできる)。また、文字列を流し込んでスクロール表示させたり、スクロール速度も変えられます。
今回は、5x5のチェックボックスをページ上に表示して、チェックした位置に対応するLEDを光らせるというプログラミングにしました。
Octet 0 represents the first row of LEDs i.e. the top row when the micro:bit is viewed with the edge connector at the bottom and USB connector at the top.
Octet 1 represents the second row and so on.
In each octet, bit 4 corresponds to the first LED in the row, bit 3 the second and so on.
Bit values represent the state of the related LED: off (0) or on (1).
So we have:
Octet 0, LED Row 1: bit4 bit3 bit2 bit1 bit0
Octet 1, LED Row 2: bit4 bit3 bit2 bit1 bit0
Octet 2, LED Row 3: bit4 bit3 bit2 bit1 bit0
Octet 3, LED Row 4: bit4 bit3 bit2 bit1 bit0
Octet 4, LED Row 5: bit4 bit3 bit2 bit1 bit0
こんな感じで、LEDの一列をUint8で表す、Uint8Arrayデータをmicro:bitに流し込めばOKです。
いやー、BLEでなんでもできちゃうんですね・・・。
micro:bit、奥が深いわ・・・。