Raspberry Pi Pico W で Bluetoothのジョイステイックを試作中
Raspberry Pi Pico WでBluetoothのジョイステイックのコントローラを試作中です。Bluetoothで接続できるHID(Human Interface Device)のコントローラとして,キーボード形式のコントローラは何とかなりそうな事が分かりましたが,ジョイスティック形式の方がアナログ値や独自のボタン操作には便利そうな気がして試しています。Raspberry Pi Pico W を2台使って,1台をコントローラ,もう1台をホストにしています。2台のPicoを同じPCのArduino IDE につないでそれぞれのCOM番号を確認しながらプログラミングします。"ツール"では時々Bluetoothの選択が外れていることがあり,コンパイルエラーになりますので,要チェックです(^^;;;;;コントローラ側からBluetooth経由でデータを送り,ホストで入力してシリアルモニタに打ち出しているところです。ジョイスティックはHIDのコントローラの一つとしてゲームにはよく使われているようですが,同時に多くの情報を送れるので用途によっては便利な気がします。ただ実際扱ってみると,いろいろと考える事もあります。そのなかには arduino-pico に限った仕様もあるかもしれませんが,,。arduino-picoのサンプルでは主にコントローラ側の内部で数値がテーブル変換?されている箇所があり,それが分かるまで結構混乱しました(^^;;;;。コントローラ側の数値フォーマットとホスト側でHID-Masterのレポート(パケット)として受け取る数値のフォーマットとはプログラム例では以下のようになっています。・X,Y,Z,Zrotate 10bit(0->1023) -> 16bit (-32767->32767) 8bit (-127 -> 127) -> 16bit(-32725 ->32725)・hat ボタン 16bit(0->360, -1) -> 8bit・ボタン 32bit(各ビットがボタンに相当)-> 32bit後はレポートをデフォルトの定期的な自動送信にするか,マニュアル送信にするか,,などありますが,ま,送るデータに合わせて色々考える事になりますね。スライダーを追加するなどレポートのフォーマットを変えたりするのは,,先になりそうですが(^^;;;以下にデータチェック用に使っているプログラムを貼っておきます。サンプルプログラムの切り貼りをちょっと変えただけです,,。コントローラ側のチェックプログラムです。/***** Bluetooth_Joystic check Program *****/// This program is an excertpt from the example code// Benjamin Aigner, 2022 <beni@asterics-foundation.org>// Public domain / CC 0 */#include <JoystickBLE.h>void joy( int x, int y, int z, int rz, int16_t hat, uint32_t buttons) { JoystickBLE.useManualSend(true); JoystickBLE.X(x); JoystickBLE.Y(y); JoystickBLE.Z(z); JoystickBLE.Zrotate(rz); JoystickBLE.button(buttons, true); delay(250); JoystickBLE.hat(hat); JoystickBLE.send_now(); JoystickBLE.button(buttons, false); delay(10); JoystickBLE.useManualSend(false);}void setup() {// Serial.begin(115200);// Serial.println("Use BOOTSEL to start the Joystick demo."); JoystickBLE.begin();}void loop() { if (BOOTSEL) {// joy( int x, int y, int z, int rz, int16_t hat, uint32_t buttons) joy(0,512,1023,0,0,1); // default 10bit mode (0= i < 1024) joy(512,1023,0,512,90,2); joy(1022,0,512,1023,180,3); JoystickBLE.use8bit(true); // 8bit mode (-127= i < 128) joy(-127,0,127,-127,-1,4); joy(0,127,-127,0,45,5); joy(127,-127,0,127,135,6); JoystickBLE.use8bit(false); }} マスター側のチェックプログラムです。/***** Bluetooth_Joystic check Program for HID_Master *****/// 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;// Joystick can get reports of 4 analog axes, 1 d-pad bitfield, and up to 32 buttons// Axes and hats that aren't reported by the joystick are read as 0void joy(void *cbdata, int x, int y, int z, int rz, uint8_t hat, uint32_t buttons) { (void) cbdata; const char *hats[16] = { "U", "UR", "R", "DR", "D", "DL", "L", "UL", "", "", "", "", "", "", "", "." }; Serial.printf("Joystick: (%4d, %4d) (%4d, %4d), Hat: %-2s, Buttons:", x, y, z, rz, hats[hat & 15]); for (int i = 0; i < 32; i++) { Serial.printf(" %c", (buttons & 1 << i) ? '*' : '.'); } Serial.println();}void setup() { Serial.begin(); // Serial Monitor delay(3000); Serial.printf("Starting HID master, put your device in pairing mode now.\n"); hid.onJoystick(joy); 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(); }}