LoginSignup
15
14

More than 3 years have passed since last update.

【Python】Pygame で Nintendo Switch の Joy-Con(L) の操作を検出

Last updated at Posted at 2017-12-05

ロボット開発のデバッグにコントローラーを使いたくて、何か良いの無いかと探しておりました。

はじめは、iBUFFALO USBゲームパッド SFC風 とか良いかなと考えていたのですが、
Joy-ConがBluetooth接続で簡単に使用できると聞いて、Joy-Conへ方向転換。
PythonのPygameを用いて簡単に検出できたので、手順を以下にメモ。

① Joy-ConをBluetooth接続モードへ移行
01.png

②-1 Windows側でBluetooth機器を検出
2017-12-05 (16).png
②-2 Windows側でBluetooth機器を検出
2017-12-05 (17).png
②-3 Windows側でBluetooth機器を検出
2017-12-05 (18).png
②-4 Windows側でBluetooth機器を検出
2017-12-05 (19).png
②-5 Windows側でBluetooth機器を検出
2017-12-05 (20).png

実際に検出してみたところの動画は以下。
https://www.youtube.com/watch?v=UiDuYm2suMk
【Python】Nintendo Switch の JOY-CON を Pygame で Joy-Con の操作を検出<br>

スティックは、ハットスイッチとして検出するようで、以下のような割り付けになっておりました(横向き持ちの場合)
 スティック左倒し:ハットスイッチ X軸 -1
 スティック右倒し:ハットスイッチ X軸 1
 スティック上倒し:ハットスイッチ Y軸 1
 スティック下倒し:ハットスイッチ Y軸 -1
 SL:ボタン 4
 SR:ボタン 5
 L :ボタン 14
 ZL:ボタン 15
 十字キー 左:ボタン 2
 十字キー 右:ボタン 1
 十字キー 上:ボタン 3
 十字キー 下:ボタン 0
 - :ボタン 8
 キャプチャーボタン :ボタン 13

 モーションIRカメラ、NFC、加速度センサー、ジャイロセンサーとかスティックの微妙な倒し具合 は検出できない模様。
HD振動とか指示出来たら、色々楽しめそうですが、今のところ操作出来るか不明。

ソースコードは以下。


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygame
from pygame.locals import *
import time

def main() :
    pygame.joystick.init()
    joystick0 = pygame.joystick.Joystick(0)
    joystick0.init()

    print 'joystick start'

    pygame.init()

    while True:
         # コントローラーの操作を取得
        eventlist = pygame.event.get()

        # イベント処理
        for e in eventlist:
            if e.type == QUIT:
                return

            if e.type == pygame.locals.JOYAXISMOTION:
                x, y = joystick0.get_axis(0), joystick0.get_axis(1)
                print 'axis x:' + str(x) + ' axis y:' + str(y)
            elif e.type == pygame.locals.JOYHATMOTION:
                x, y = joystick0.get_hat(0)
                print 'hat x:' + str(x) + ' hat y:' + str(y)
            elif e.type == pygame.locals.JOYBUTTONDOWN:
                print 'button:' + str(e.button)

        time.sleep(0.1)

if __name__ == '__main__':
    try:
        main()
    except pygame.error:
        print 'joystickが見つかりませんでした。'

以上。

15
14
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
15
14