树莓派使用Python连接任天堂Wii遥控器
前段时间一直在寻找通过蓝牙将Wii遥控器的数据发送到树莓派的方法。想像中可以通过安卓手机和自定义的APP来完成,但常常会在APP的蓝牙连接过程出现问题。
通过大量的搜索,我找到了一个名为“CWiid”的Python库。它支持树莓派通过蓝牙从任天堂Wii控制器读取数据,并可以在自己的Python脚本中使用这些数据。所以决定试一试,我使用了最新的Raspbian镜像和一个USB蓝牙适配器。
按钮映射
在开始实际操作之前,我们需要现看看Wii遥控器的按钮映射。当按下Wii遥控器上的一个按钮时,它就会通过蓝牙连接发送数据。发送的数据是一串数字,它是与每个按钮功能相关联的惟一代码。这些代码以十进制和二进制的形式显示见上图和下表。你将看到,每个按钮在二进制数中都只设置了一位“1”。
十进制 二进制 按钮 ================================= 1 00000000000001 2 2 00000000000010 1 4 00000000000100 B 8 00000000001000 A 16 00000000010000 MINUS 128 00000010000000 HOME 256 00000100000000 DOWN 512 00001000000000 UP 1024 00010000000000 RIGHT 2048 00100000000000 LEFT 4096 01000000000000 PLUS =================================
所以按A键会发送8,按LEFT键将发送2048。同时按下A和MINUS就会得到8 + 16 = 24。
步骤1 -更新Raspbian
本文下载了用于最新Raspbian映像,并新创建了一张SD卡。为了确保系统保持最新,运行以下两个命令:
sudo apt-get update
和
sudo apt-get upgrade
步骤2 -安装蓝牙驱动程序
为了与蓝牙设备(如Wii遥控器)通信,你需要安装驱动程序,输入以下命令:
sudo apt-get install --no-install-recommends bluetooth
“–no-install-recommends”的作用是确保只安装基本的蓝牙驱动程序,而不安装附加软件。
步骤3 -安装CWiid模块
下一步,需要CWiid Python库,键入命令:
sudo apt-get install python-cwiid
步骤4 -测试连接
插入USB蓝牙适配器,待其启动完成后,测试树莓派是否可以看到任天堂Wii遥控器。输入以下命令:
sudo service bluetooth status
应该得到这样的回应:
[ ok ] bluetooth is running.
如果没有看到这条消息,重新启动树莓派再尝试一下。
同时按下Wii控制器上的1和2个按钮。蓝色led应该会闪烁。现在输入:
hcitool scan
如果一切顺利,你应该看到一个回应,显示你的树莓派找到你的遥控器了:
Scanning ... 00:19:1C:B6:BB:41 Nintendo RVL-CNT-01
步骤5 – Python脚本示例
下面是示例脚本。它包含如何连接到远程并读取11个按钮状态。
#!/usr/bin/python #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k| #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # # wii_remote_1.py # Connect a Nintendo Wii Remote via Bluetooth # and read the button states in Python. # # ----------------------- # Import required Python libraries # ----------------------- import cwiid import time button_delay = 0.1 print 'Press 1 + 2 on your Wii Remote now ...' time.sleep(1) # Connect to the Wii Remote. If it times out # then quit. try: wii=cwiid.Wiimote() except RuntimeError: print "Error opening wiimote connection" quit() print 'Wii Remote connected...\n' print 'Press some buttons!\n' print 'Press PLUS and MINUS together to disconnect and quit.\n' wii.rpt_mode = cwiid.RPT_BTN while True: buttons = wii.state['buttons'] # If Plus and Minus buttons pressed # together then rumble and quit. if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0): print '\nClosing connection ...' wii.rumble = 1 time.sleep(1) wii.rumble = 0 exit(wii) # Check if other buttons are pressed by # doing a bitwise AND of the buttons number # and the predefined constant for that button. if (buttons & cwiid.BTN_LEFT): print 'Left pressed' time.sleep(button_delay) if(buttons & cwiid.BTN_RIGHT): print 'Right pressed' time.sleep(button_delay) if (buttons & cwiid.BTN_UP): print 'Up pressed' time.sleep(button_delay) if (buttons & cwiid.BTN_DOWN): print 'Down pressed' time.sleep(button_delay) if (buttons & cwiid.BTN_1): print 'Button 1 pressed' time.sleep(button_delay) if (buttons & cwiid.BTN_2): print 'Button 2 pressed' time.sleep(button_delay) if (buttons & cwiid.BTN_A): print 'Button A pressed' time.sleep(button_delay) if (buttons & cwiid.BTN_B): print 'Button B pressed' time.sleep(button_delay) if (buttons & cwiid.BTN_HOME): print 'Home Button pressed' time.sleep(button_delay) if (buttons & cwiid.BTN_MINUS): print 'Minus Button pressed' time.sleep(button_delay) if (buttons & cwiid.BTN_PLUS): print 'Plus Button pressed' time.sleep(button_delay)
将代码保存为“wii_remote_1.py”文件,然后运行它 :
python wii_remote_1.py
按下Wii控制器上的按钮,一些文本会被显示到屏幕上,这个文本会告诉我们按下了哪个按钮。同时按加号和减号就会退出脚本。至此树莓派使用Python连接任天堂Wii遥控器就完成了。关于游戏手柄连接Arduino的文章,请查阅:如何通过蓝牙将PS3手柄连接到Arduino