Raspberry Pi Pico W にBluetoothのキーボードとマウスをつないでみた
ArduinoのRaspberry Pi Pico WとのBluetooth通信はPCやスマホをセントラルにしたシリアル通信ではとても簡単にできます。→ Arduino IDEでRaspberry Pi Pico W のBluetoothを試してみたBluetoothのキーボードやマウスがpicoに直接つなげられたらもっと手軽なコントローラーになりそうなので試してみました。機材です。廉価版で揃えています(^^)。・MCU :Raspberry Pi Pico W (arduino-pico)・fosa 7インチBluetoothキーボード・ダイソー Bluetoothマウス E-MOU-7 ソフトはArduino IDEでEarle F. Philhowerのarduino-picoボードマネージャを使っています。arduino-picoにはBluetoothでつなぐHID(Human Interface Device)であるキーボードやマウス用に"BluetoothHIDMaster"ライブラリが用意されています。このライブラリを使ったサンプルプログラムで"KeyboardPiano example"というのがついていましたので,このプログラムからキーボードとマウスの部分を抜き出してチェックプログラムにしてみました(^^)。チェックプログラムでpicoにキーボードからBluetoothで入力し,そのデータをPCのシリアルモニタに表示したところです。押されたキーのUP/DOWNと文字が分かります。マウスのチェックプログラムを動かしたところです。picoには押されたボタンのUP/DOWN情報とX/Y座標などが送られています。部分的に抜き出しただけのプログラムですが,メモしておきます。それぞれキーボードやマウスはまず先に接続待ちモードにしておき,それからpicoをスタートさせて少し待つと接続できました(^^)。キーボード用です。 /***** Bluetooth_keyboard check Program *****/// This program is an excertpt from the "KeyboardPiano example"// Released to the public domain in 2024 by Earle F. Philhower, III#include <BluetoothHIDMaster.h>// We need the inverse map, borrow from the Keyboard library#include <HID_Keyboard.h>extern const uint8_t KeyboardLayout_en_US[128];BluetoothHIDMaster hid;HIDKeyStream keystream;// We get make/break for every key which lets us hold notes while a key is depressedvoid kb(void *cbdata, int key) { bool state = (bool)cbdata; // The HIDKeyStream object converts a key and state into ASCII. HID key IDs do not map 1:1 to ASCII! // Write the key and make/break state, then read 1 ASCII char back out. keystream.write((uint8_t)key); keystream.write((uint8_t)state); Serial.printf("Keyboard: %02x %s = '%c'\n", key, state ? "DOWN" : "UP", state ? keystream.read() : '-');}// Consumer keys are the special media keys on most modern keyboards (mute/etc.)void ckb(void *cbdata, int key) { bool state = (bool)cbdata; Serial.printf("Consumer: %02x %s\n", key, state ? "DOWN" : "UP");}void setup() { Serial.begin(); // Serial Monitor delay(3000); Serial.printf("Starting HID master, put your device in pairing mode now.\n"); // Setup the HID key to ASCII conversion keystream.begin(); // We can use the cbData as a flag to see if we're making or breaking a key hid.onKeyDown(kb, (void *)true); hid.onKeyUp(kb, (void *)false); // Consumer keys are the special function ones hid.onConsumerKeyDown(ckb, (void *)true); hid.onConsumerKeyUp(ckb, (void *)false); hid.begin(true); hid.connectBLE();}void loop() { if (BOOTSEL) { while (BOOTSEL) { delay(1); } hid.disconnect(); hid.clearPairing(); Serial.printf("Restarting HID master, put your device in pairing mode now.\n"); hid.connectBLE(); }} マウス用です。/***** Bluetooth_Mouth check Program for "E-MOU-7" (Bluetooth Ver.5) *****/// This program is an excertpt from the "KeyboardPiano example"// Released to the public domain in 2024 by Earle F. Philhower, III#include <BluetoothHIDMaster.h>BluetoothHIDMaster hid;// Mouse callbacks. Could keep track of global mouse position, update a cursor, etc.void mm(void *cbdata, int dx, int dy, int dw) { (void) cbdata; Serial.printf("Mouse: X:%d Y:%d Wheel:%d\n", dx, dy, dw);}// Buttons are sent separately from movementvoid mb(void *cbdata, int butt, bool down) { (void) cbdata; Serial.printf("Mouse: Button %d %s\n", butt, down ? "DOWN" : "UP");}void setup() { Serial.begin(); // Serial Monitor delay(3000); Serial.printf("Starting HID master, put your device in pairing mode now.\n"); // Mouse buttons and movement reported separately hid.onMouseMove(mm); // X, Y and Wheel(1,0,-1) hid.onMouseButton(mb); // 5 buttons (0-4) hid.begin(true); hid.connectBLE();}void loop() { if (BOOTSEL) { while (BOOTSEL) { delay(1); } hid.disconnect(); hid.clearPairing(); Serial.printf("Restarting HID master, put your device in pairing mode now.\n"); hid.connectBLE(); }} 以上のように,Bluetoothのキーボードとマウスは動作を読み取れるようになりました。これを使って簡単なコントローラは作れそうですね(^^)。ただキーボードやマウスのようなHIDとよばれる物にはそれなりのライブラリがありますので何とかなりそうですが,,,私にとってBluetoothはなかなか難解です。I2Cは7bit固定アドレスの相手があって、その中で各固定内部アドレスを持つレジスタの読み書きでほぼ大丈夫なのですが,,Bluetoothではそもそも解説にも新しい言葉が多くて難儀します(^^;;;;;。相手のデバイスのサービスが分かって,アドレスが分かって,サービスのキャラクタリスティックとその属性が分かったら読み書きすれば良いではないかと素人は思うのですが,どうもBluetoothを担当するチップへのコマンドがややこしくさせている気がします。そのあたりは優しく抽象化したライブラリなりがあれば助かりますね。理論的な構造の長ったらしいコマンドや定数名はプロには分かり易いのかもしれませんけど,蝶々やトンボの正式なラテン語系の学術名でないと話が通じないみたいな世界は素人オヤジにはどうにもしんどいですね(^^;;;;;;。ま,できることからコツコツと,です(^^)。