LoginSignup
3
5

More than 3 years have passed since last update.

BluetoothゲームコントローラーをIoTボタンとして使う。

Posted at

出退勤で打刻するためにAmazon Dash Buttonを使っていたのですが、電池交換に失敗して壊してしまったので、代替として、MOCUTE-039という手のひらサイズのBluetoothゲームコントローラーを使うことにしました。ちなみにDash Buttonは2個持っていたのですが、どちらも電池交換に失敗して壊しました。つらい。

連携先にRaspberry Pi 3を使います。私が使っているのはRaspberry Pi 3 Model B Rev 1.2で、OSはRaspbian Busterです。

Bluetoothペアリング

事前作業

ペアリングにはbluetoothctlを使いますが、bluetoothctl でスキャンするとどのMACアドレスがMOCUTE-039なのかわからないため、適当なスマホ端末でペアリングしてMACアドレスを調べてから作業を実施しました。

[NEW] Device 16:01:xx... 16-01-xxx... # デバイス名がわからないのであらかじめMACアドレスを調べておく

ペアリング作業

$ sudo apt install bluetooth

$ sudo hciconfig hci0 down
$ sudo hciconfig hci0 up

$ sudo bluetoothctl
[bluetooth]# power on
[bluetooth]# pair 16:01:xx...
Attempting to pair with 16:01:xx...
# 略
Pairing successful
[bluetooth]# trust 16:01:xx...
Changing 16:01:xx... trust succeeded
[bluetooth]# connect 16:01:xx...
Attempting to connect to 16:01:xx...
# 略
Connection successful
[MOCUTE-039_A32-B6FA]# quit

キー押下のイベントがどこに記録されているか探す

/dev/input/event*のどれかにキー押下のイベントが記録されているので、hexdump /dev/input/event1,2,3しながらキーを押下して探します。私の環境ではevent2でした。

python-evdevのインストール

押したキーによって実行するスクリプトを変えたいので、python-evdevをインストールします。
Introduction — Python-evdev

Python3.7.3を使ったのでpip3でインストールしました。Python2.7でも動くと思います。

$ sudo pip3 install evdev

evdevのQuickStartで動作確認する

Quick Start — Python-evdev のコードをそのままコピーして動作確認します。

#!/usr/bin/python3

import evdev

devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
for device in devices:
        print(device.path, device.name, device.phys)

device = evdev.InputDevice('/dev/input/event2')
print(device)
for event in device.read_loop():
        if event.type == evdev.ecodes.EV_KEY:
                print(evdev.util.categorize(event))
## 実行結果
/dev/input/event3 MOCUTE-039_A32-B6FA b8:27:...
/dev/input/event2 MOCUTE-039_A32-B6FA Consumer Control b8:27:...
/dev/input/event1 MOCUTE-039_A32-B6FA Keyboard b8:27:...
/dev/input/event0 MOCUTE-039_A32-B6FA Mouse b8:27:...
device /dev/input/event2, name "MOCUTE-039_A32-B6FA Consumer Control", phys "b8:27:..."
key event at 1595599952.585498, 139 (KEY_MENU), down
key event at 1595599952.792990, 139 (KEY_MENU), up
key event at 1595599971.126813, 158 (KEY_BACK), down
key event at 1595599971.284303, 158 (KEY_BACK), up

どのイベントがどのファイルに記録されるかわかるようになっています。キーイベントも取得できました。

キーごとに実行したいスクリプトを割り当てる

キー番号さえわかればあとはどうとでもなるため、実行したいスクリプトを割り当てます。今回はKEY_MENUのボタンがupしたときにシェルスクリプトが実行されるようにしました。
/home/pi/date.shはdateをechoしているだけです。

#!/usr/bin/python3

import evdev
import os

device = evdev.InputDevice('/dev/input/event2')
print(device)
for event in device.read_loop():
    if event.type == evdev.ecodes.EV_KEY:
        if event.code == 139 and event.value == 1:
             os.system('/home/pi/date.sh >> result.txt')
## 実行結果
$ cat result.txt
Fri 24 Jul 22:05:49 JST 2020
Fri 24 Jul 22:05:50 JST 2020

感想

Python-evdev便利でいいですね。Amazon Dash Buttonと違ってボタンが複数あるので、ボタンさえ覚えておけば複数処理ができて楽しそうです。

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5