# PiBorg Gamepad Library
![Polling flowchart](Diagrams/gamepad-logo.svg)
The Gamepad library provides a simple way of getting inputs from joysticks, gamepads, and other game controllers.
Both buttons and axes / joysticks can be referenced by names as well as their raw index numbers. These can be easily customised and new controllers can easily be added as well.
It is designed and tested for the Raspberry Pi, but it should work with an Linux install and any device which shows up as a ```/dev/input/js?``` device.
Gamepad only uses imports which are part of the standard Python install and works with both Python 2 and 3.
Multiple controllers are also supported, just create more than one ```Gamepad``` instance with different joystick IDs.
See our other projects on GitHub [here](https://github.com/piborg) :)
# Installing the Library
```
cd ~
git clone https://github.com/piborg/Gamepad
```
That's it. The library does not need installing, it just needs to be downloaded.
# The Gamepad Library
The library provides three basic modes for getting updates from your controller:
1. Polling - we ask the library for the next controller update one at a time.
2. Asynchronous - the controller state is updated in the background.
3. Event - callbacks are made when the controller state changes.
See the examples further down for an explanation of how each mode is used.
The library itself is contained in just two scripts:
## ```Gamepad.py```
The main library, this script contains all of the code which reads input from the controller. It contains the ```Gamepad``` class which has all of the logic used to decode the joystick events and store the current state.
It works with both the raw axis / joystick and button numbers and easy to read names. It also contains the threading code needed to handle the background updating used in the asynchronous and event modes.
This script can be run directly using ```./Gamepad.py``` to check your controller mappings are correct or work out the mapping numbers for your own controller.
## ```Controllers.py```
This script contains the layouts for the different controllers. Each controller is represented by its own class inheriting from the main ```Gamepad``` class.
If the mapping is not right for you the layout for both the axis / joystick names and the button names by editing these classes. Adding your own controller is also simple, just make your own class based on the example at the bottom :)
Any button or axis without a name can still be used by the raw number if needed. This also means the ```Gamepad``` class can be used directly if you are only using the raw numbers.
This script is not run directly, instead it is read by ```Gamepad.py``` so that all of the devices are available when Gamepad is imported.
# Examples
## Polling mode - ```PollingExample.py```
The polling mode is probably the simplest and uses the Gamepad library to decode controller events one at a time.
![Polling flowchart](Diagrams/Polling.svg)
It works by repeatedly calling the ```getNextEvent()``` function to get the next update from the controller in the order they occurred. Each call returns three things:
1. The event type. This is either ```'BUTTON'``` for button presses or ```'AXIS'``` for axis / joystick movements.
2. The control which changed. This is a string with the name if available, or the index number if no name was available.
3. The new value for the control. For buttons this is ```True``` or ```False```, for axis / joystick movement it is a position between -1.0 and +1.0.
For example if the circle button on your controller was just pressed you would get ```'BUTTON', 'CIRCLE', True``` as the result.
Polling mode cannot be used at the same time as the asynchronous or event modes as they read the controller events for you.
## Asynchronous mode - ```AsyncExample.py```
Asynchronous mode works by reading the controller events in a background thread and updating the objects state to match the controller.
![Asynchronous flowchart](Diagrams/Asynchronous.svg)
It is started by calling the ```startBackgroundUpdates()``` function and should be stopped at the end of your script by calling the ```disconnect()``` function.
The current controller state can be queried using the following functions:
* ```isConnected()``` - check if the controller is still connected.
* ```isPressed(X)``` - check if a button is currently pressed.
* ```beenPressed(X)``` - see if a button was pressed since the last check. Only returns ```True``` once per press.
* ```beenReleased(X)``` - see if a button was released since the last check. Only returns ```True``` once per release.
* ```axis(X)``` - read the latest axis / joystick position. This is a float number between -1.0 and +1.0.
In all cases ```X``` can either be the string based name or the raw index number (e.g. ```'CIRCLE'``` or ```1```).
Asynchronous mode cannot be used at the same time as the polling mode as it reads the controller events for you, but it can be used with event mode.
## Event mode - ```EventExample.py```
Event mode works by reading the controller events in a background thread and calling functions in your script when changes occur.
![Event flowchart](Diagrams/Event.svg)
It is started by calling the ```startBackgroundUpdates()``` function and should be stopped at the end of your script by calling the ```disconnect()``` function. The ```isConnected()``` function will return ```False``` if the controller is disconnected while running.
Once started you can register your functions to controller events using these calls:
* ```addButtonPressedHandler(X, F)``` - called when a button is pressed, no values passed.
* ```addButtonReleasedHandler(X, F)``` - called when a button is released, no values passed.
* ```addButtonChangedHandler(X, F)``` - called when a button changes state, boolean passed with ```True``` for pressed or ```False``` for released.
* ```addAxisMovedHandler(X, F)``` - called when an axis / joystick is moved, a float number between -1.0 and +1.0 is passed.
In all cases ```X``` can either be the string based name or the raw index number (e.g. ```'CIRCLE'``` or ```1```). ```F``` is the function which gets called when the event occurs.
The same function can be registered with multiple events. You can also register multiple functions with the same event.
You can also remove an already registered event using these calls if needed:
* ```removeButtonPressedHandler(X, F)``` - removes a callback added by ```addButtonPressedHandler```.
* ```removeButtonReleasedHandler(X, F)``` - removes a callback added by ```addButtonReleasedHandler```.
* ```removeButtonChangedHandler(X, F)``` - removes a callback added by ```addButtonChangedHandler```.
* ```removeAxisMovedHandler(X, F)``` - removes a callback added by ```addAxisMovedHandler```.
* ```removeAllEventHandlers()``` - removes all added callbacks for this controller.
Event mode cannot be used at the same time as the polling mode as it reads the controller events for you, but it can be used with asynchronous mode.
## Asynchronous and event mode - ```AsyncAndEventExample.py```
This is not really a mode, but an example of how the asynchronous and event modes can be used at the same time. This is generally my preferred option as event mode is often easier to understand for button presses and asynchronous mode works well with axis / joystick movements.
![Asynchronous and event flowchart](Diagrams/Asynchronous-and-event.svg)
The example script here is really a hybrid between the ```AsyncExample.py``` and ```EventExample.py``` examples. Button callbacks are registered in the event style, then the loop only checks one button and the joystick positions.
In this style you are free to mix and match what you see as events and what you read the state of directly.
## Getting the available names - ```ListNames.py```
This example is just a helpful utility to print out all of the axis and button names for a controller type. You can c
没有合适的资源?快使用搜索试试~ 我知道了~
一个简单的库,用于在 Linux 中将游戏控制器和操纵杆输入输入 Python_代码_下载
共19个文件
py:10个
svg:5个
md:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 148 浏览量
2022-06-24
21:45:54
上传
评论
收藏 68KB ZIP 举报
温馨提示
提供了一种从操纵杆、游戏手柄和其他游戏控制器获取输入的简单方法。 按钮和轴/操纵杆都可以通过名称及其原始索引号来引用。这些可以很容易地定制,也可以很容易地添加新的控制器。 它是为 Raspberry Pi 设计和测试的,但它应该适用于 Linux 安装和任何显示为设备的/dev/input/js?设备。 Gamepad 仅使用作为标准 Python 安装的一部分的导入,并且适用于 Python 2 和 3。 效果展示: https://github.com/piborg/Gamepad/blob/master/Diagrams/gamepad-logo.svg 更多详情、使用方法,请下载后阅读README.md文件
资源推荐
资源详情
资源评论
收起资源包目录
Gamepad-master.zip (19个子文件)
Gamepad-master
Diagrams
gamepad-logo.svg 260KB
Event.svg 14KB
Asynchronous-and-event.svg 14KB
Polling.svg 5KB
Asynchronous.svg 10KB
CustomGamepadExample.py 2KB
Controllers.py 10KB
monsterJoy.py 6KB
.github
ISSUE_TEMPLATE
software-bug-report.md 760B
AsyncExample.py 2KB
EventExample.py 2KB
LICENSE 1KB
Gamepad.py 24KB
AsyncAndEventExample.py 2KB
PollingExample.py 2KB
.gitignore 1KB
README.md 10KB
rockyJoy.py 4KB
ListNames.py 981B
共 19 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功