-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Event Device Support (
evdev
) (#1943)
* Add event device plugin This implements an "event device" listener plugin, that enables the user to configure the phoniebox to respond to events from an "event device" (device under /dev/input). This incluces eg button presses from an USB controller or keyboard. * Adds documentation for the event device plugin Includes a detailed how-to as well as example config * Allow empty button config This is an actual usecase in case someone wants to setup a device and figure out the button ids by looking at the logs * Update README.rst * Improve README - Remove duplicated section as suggested - Add Zero Delay Arcade USB Encoder usecase * Give more meaningfull name to listener thread variable * Link to old documentation * Remove README and fix typo The documentation is now part of the markdown documentation * Remove unecessary plain=1 * Remove indents in example code There was some left over indentation from the old rtf format in the python code examples. * Add example how to access evdev in docker * Example for new Evdev config structure This is tighly modeled after gpio. In contrast to GPIO, this is a list of devices containing each one or more input/output devices. Eg a Joystick has keys but potentially also rumble or leds. Currently only very simply input devices (buttons) are supported. This structer should enable to extend this easily. * Move evdev config to separate file As suggested this should be a separate file, similar to the gpioz config * Adapt evdev code to new config Now the config for evdev is akin to GPIO. The big difference is that multiple devices with each multiple input/output devices are supported. Note that the backend is still the old event device listener backend. Thus to support more features, the backend would need to be quite drastically overhauled. * Update documentation for new evdev config format * Fix wrong reference * Fix bug to correctly check supported input device type * Add more tests for evdev init * Fix wrong attribute name The attribute 'device_request' of EvDevKeyListener is now called 'device_name_request' * Validate input config better Fails if there is no device_name and setts proper default for 'exact' * Add pyzmq installation for github action This is required for some tests. Following the instructions from issue #2050 Specifically: #2050 (comment) * Revert "Add pyzmq installation for github action" This reverts commit 75161b5. As this was not working * Mock `jukebox.publishing` in tests to avoid zmq Currently it is hard to install zmq (#2050) and for CI pytest `zmq` is not available. As the test do not really require `jukebox.publishing` running, mocking it in the test avoids it being imported. Thus the tests do not require `zmq` to be installed. --------- Co-authored-by: pabera <[email protected]> Co-authored-by: Vito Zanotelli <[email protected]> Co-authored-by: s-martin <[email protected]>
- Loading branch information
1 parent
e1e8416
commit 07208e6
Showing
7 changed files
with
602 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Event devices | ||
|
||
## Background | ||
Event devices are generic input devices that are exposed in `/dev/input`. | ||
This includes USB peripherals (Keyboards, Controllers, Joysticks or Mouse) as well as potentially bluetooth devices. | ||
|
||
A specific usecase for this could be, if a Zero Delay Arcade USB Encoder is used to wire arcade buttons instead of using GPIO pins. | ||
|
||
These device interface support various kinds of input events, such as press, movements and potentially also outputs (eg. rumble, led lights, ...). Currently only the usage of button presses as input is supported. | ||
|
||
This functionality was previously implemented under the name of [USB buttons](https://github.com/MiczFlor/RPi-Jukebox-RFID/blob/develop/components/controls/buttons_usb_encoder/README.md). | ||
|
||
The devices and their button mappings need to be mapped in the configuration file. | ||
|
||
## Configuration | ||
|
||
To configure event devices, first add the plugin as an entry to the module list of your main configuration file ``shared/settings/jukebox.yaml``: | ||
|
||
``` yaml | ||
modules: | ||
named: | ||
event_devices: controls.event_devices | ||
``` | ||
And add the following section with the plugin specific configuration: | ||
``` yaml | ||
evdev: | ||
enabled: true | ||
config_file: ../../shared/settings/evdev.yaml | ||
``` | ||
The actual configuration itself is stored in a separate file. In this case in ``../../shared/settings/evdev.yaml``. | ||
The configuration is structured akin to the configuration of the [GPIO devices](./gpio.md). | ||
In contrast to `gpio`, multiple devices (eg arcade controllser, keyboards, joysticks, mice, ...) are supported, each with their own `input_devices` (=buttons). `output_devices` or actions other than `on_press` are currently not yet supported. | ||
|
||
``` yaml | ||
devices: # list of devices to listen for | ||
{device nickname}: # config for a specific device | ||
device_name: {device_name} # name of the device | ||
exact: False/True # optional to require exact match. Otherwise it is sufficient that a part of the name matches | ||
input_devices: # list of buttons to listen for for this device | ||
{button nickname}: | ||
type: Button | ||
kwargs: | ||
key_code: {key-code}: # evdev event id | ||
actions: | ||
on_press: # Currently only the on_press action is supported | ||
{rpc_command_definition} # eg `alias: toggle` | ||
``` | ||
The `{device nickname}` is only for your own orientation and can be choosen freely. | ||
For each device you need to figure out the `{device_name}` and the `{event_id}` corresponding to key strokes, as indicated in the sections below. | ||
|
||
### Identifying the `{device_name}` | ||
|
||
The `{device_name}` can be identified using the following Python snippet: | ||
|
||
``` Python | ||
import evdev | ||
devices = [evdev.InputDevice(path) for path in evdev.list_devices()] | ||
for device in devices: | ||
print(device.path, device.name, device.phys) | ||
``` | ||
|
||
The output could be in the style of: | ||
|
||
``` | ||
/dev/input/event1 Dell Dell USB Keyboard usb-0000:00:12.1-2/input0 | ||
/dev/input/event0 Dell USB Optical Mouse usb-0000:00:12.0-2/input0 | ||
``` | ||
|
||
In this example, the `{device_name}` could be `DELL USB Optical Mouse`. | ||
Note that if you use the option `exact: False`, it would be sufficient to add a substring such as `USB Keyboard`. | ||
|
||
### Identifying the `{key-code}` | ||
|
||
The key code for a button press can be determined using the following code snippet: | ||
|
||
``` Python | ||
import evdev | ||
device = evdev.InputDevice('/dev/input/event0') | ||
device.capabilities(verbose=True)[('EV_KEY', evdev.ecodes.EV_KEY)] | ||
``` | ||
|
||
With the `InputDevice` corresponding to the path from the output of the section `{device_name}` (eg. in the example `/dev/input/event0` | ||
would correspond to `Dell Dell USB Keyboard`). | ||
|
||
If the naming is not clear, it is also possible to empirically check for the key code by listening for events: | ||
|
||
``` Python | ||
from evdev import InputDevice, categorize, ecodes | ||
dev = InputDevice('/dev/input/event1') | ||
print(dev) | ||
for event in dev.read_loop(): | ||
if event.type == ecodes.EV_KEY: | ||
print(categorize(event)) | ||
``` | ||
The output could be of the form: | ||
``` | ||
device /dev/input/event1, name "DragonRise Inc. Generic USB Joystick ", phys "usb-3f980000.usb-1.2/input0" | ||
key event at 1672569673.124168, 297 (BTN_BASE4), down | ||
key event at 1672569673.385170, 297 (BTN_BASE4), up | ||
``` | ||
|
||
In this example output, the `{key-code}` would be `297` | ||
|
||
Alternatively, the device could also be setup without a mapping. | ||
Afterwards, when pressing keys, the key codes can be found in the log files. Press various buttons on your device, | ||
while watching the logs with `tail -f shared/logs/app.log`. | ||
Look for entries like `No callback registered for button ...`. | ||
|
||
### Specifying the `{rpc_command_definition}` | ||
|
||
The RPC command follows the regular RPC command rules as defined in the [following documentation](./rpc-commands.md). | ||
|
||
|
||
## Full example config | ||
|
||
A complete configuration example for a USB Joystick controller can be found in the [examples](../../resources/default-settings/evdev.example.yaml). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
devices: # A list of evdev devices each containing one or multiple input/output devices | ||
joystick: # A nickname for a device | ||
device_name: DragonRise Inc. Generic USB # Device name | ||
exact: false # If true, the device name must match exactly, otherwise it is sufficient to contain the name | ||
input_devices: | ||
TogglePlayback: | ||
type: Button | ||
kwargs: | ||
key_code: 299 | ||
actions: | ||
on_press: | ||
alias: toggle | ||
NextSong: | ||
type: Button | ||
kwargs: | ||
key_code: 298 | ||
actions: | ||
on_press: | ||
alias: next_song | ||
PrevSong: | ||
type: Button | ||
kwargs: | ||
key_code: 297 | ||
actions: | ||
on_press: | ||
alias: prev_song | ||
VolumeUp: | ||
type: Button | ||
kwargs: | ||
key_code: 296 | ||
actions: | ||
on_press: | ||
alias: change_volume | ||
args: 5 | ||
VolumeDown: | ||
type: Button | ||
kwargs: | ||
key_code: 295 | ||
actions: | ||
on_press: | ||
alias: change_volume | ||
args: -5 | ||
VolumeReset: | ||
type: Button | ||
kwargs: | ||
key_code: 291 | ||
actions: | ||
on_press: | ||
package: volume | ||
plugin: ctrl | ||
method: set_volume | ||
args: [18] | ||
Shutdown: | ||
type: Button | ||
kwargs: | ||
key_code: 292 | ||
actions: | ||
on_press: | ||
alias: shutdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.