Androidのメモとか

ポキオの日記です。今日も遅延してない。

micro:bitのLEDをWeb Bluetooth APIで制御してみる

micro:bit x Web Bluetooth APIの最終章。

micro:bit web bluetooth api

最後はLED

micro:bitのBLEを色々いじってきました。

relativelayout.hatenablog.com

relativelayout.hatenablog.com

relativelayout.hatenablog.com

relativelayout.hatenablog.com

ちょっとキリがないので、今回のLED制御で一旦シメたいと思います。

micro:bit側は、BLE系のサービスを開始するだけ

相変わらず、Bluetooth系のサービスをすべて初期化するコードです。

micro:bit web bluetooth api

今回も変える必要がないので、このままの実装で進めます。

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です。

github.com

pokiiio.github.io

#microbit のLEDを #webbluetooth で制御してみる。

A post shared by pokio (@pokiiiiio) on

いやー、BLEでなんでもできちゃうんですね・・・。

micro:bit、奥が深いわ・・・。

「Androidのメモとか」は、Amazon.co.jpを宣伝しリンクすることによってサイトが紹介料を獲得できる手段を提供することを目的に設定されたアフィリエイト宣伝プログラムである、Amazonアソシエイト・プログラムの参加者です。

このブログは個人的なメモ書きであったり、考えを書く場所であります。執筆者の所属する団体や企業のコメントや意向とは無関係であります。また、このブログは必ずしも正しいことが書かれているとは限らず、誤字脱字や意図せず誤った情報を載せる場合がありえます。それが原因で読者が不利益を被ったとしても、執筆者はいかなる責任も負いません。ありがとうございます。