2
0
mirror of https://github.com/pikvm/pikvm synced 2024-11-05 00:00:28 +00:00
pikvm/pages/gpio.md

215 lines
12 KiB
Markdown
Raw Normal View History

2020-09-10 11:49:39 +00:00
# GPIO
[GPIO (general-purpose input/output)](https://en.wikipedia.org/wiki/General-purpose_input/output) is a series of digital interfaces that can be used to connect relays, LEDs, sensors, and other components.
2020-09-10 17:20:39 +00:00
2020-09-10 17:28:02 +00:00
:exclamation: Note: Using GPIO on a Pi-KVM was designed as a feature for advanced users, so please familiarize yourself with the topic to make sure you understand how to use use it before setting it up. Otherwise you might damage your Raspberry Pi or components.
2020-09-10 11:49:39 +00:00
When talking about Pi-KVM and GPIO it refers not solely to the [physical interface of the Raspberry Pi](https://www.raspberrypi.org/documentation/usage/gpio), but also to various plugins (for example, for [USB relays](http://vusb.wikidot.com/project:driver-less-usb-relays-hid-interface)) that can also be used transparently by emulating an abstract GPIO API.
2020-09-10 12:49:27 +00:00
# Configuration
2020-09-11 08:39:59 +00:00
Setting up GPIO is considerably complex. The interface is divided into several layers for flexibility. Any configuration is performed using a file `/etc/kvmd/override.yaml` which uses the [YAML syntax](https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html). We will look at each part of the configuration individually with an example for each. Sections should be combined under shared keys.
2020-09-11 08:40:48 +00:00
2020-09-11 08:39:59 +00:00
Wrong:
```yaml
kvmd:
gpio:
drivers: ...
kvmd:
gpio:
scheme: ...
```
Correct:
```yaml
kvmd:
gpio:
drivers: ...
scheme: ...
```
2020-09-10 12:49:27 +00:00
2020-09-10 13:36:03 +00:00
### Drivers
2020-09-10 17:20:39 +00:00
The first part of the configuration refers to the hardware layer, which defines which IO channels are used (standard GPIO pins of the Raspberry Pi, an USB relay, and so on). If you just want to use GPIO with the default settings you can skip to the next section [Scheme](#Scheme).
2020-09-10 13:36:03 +00:00
2020-09-10 17:20:39 +00:00
Each hardware input/output requires a individual driver configuration entry. Each driver has a type (which refers to the plugin that handles the communication between Pi-KVM and the hardware) and a unique name. This allows you to either can add multiple drivers of the same type with different settings or connect multiple USB HID relays.
2021-01-14 16:26:56 +00:00
:exclamation: Each driver requires a unique name. Names surrounded by double underscore are system reserved and should not be used.
The only exception to this is the default GPIO driver with the name `__gpio__`, representing the physical GPIO interface of the Raspberry Pi. The configuration section for `__gpio__` is only required in your `/etc/kvmd/override.yaml` if you want to change the default settings. It can be omitted if you are fine with the defaults.
2020-09-10 13:36:03 +00:00
```yaml
kvmd:
gpio:
drivers:
# This example shows how the default __gpio__ driver settings can be changed. It can be omitted if you are fine with the defaults.
2021-01-14 16:26:56 +00:00
__gpio__: # Names surrounded by double underscore are system reserved
2020-09-10 17:20:39 +00:00
type: gpio # Refers to the plugin name handling the communication
2020-09-10 13:36:03 +00:00
2020-09-18 00:20:00 +00:00
# You can define another gpio driver for some reason
my_gpio:
2020-09-10 17:20:39 +00:00
type: gpio # Refers to the plugin name handling the communication
2020-09-10 12:49:27 +00:00
# Example for a USB HID relay connected to Pi-KVM
2020-09-10 13:36:03 +00:00
relay:
2020-09-10 17:20:39 +00:00
type: hidrelay # Eefers to the plugin name handling the communication
device: /dev/hidraw0 # The path to the linux device
2020-09-10 13:36:03 +00:00
```
### Scheme
2020-09-10 17:20:39 +00:00
The second part defines how the various driver channels are configured. Each channel has a unique name, a mode (`input` or `output`), a pin number, and a reference to the driver configured in the previous part.
:exclamation: Names that starts and ends with two underscores (like `__magic__`) are reserved.
2020-09-10 13:36:03 +00:00
2020-09-18 00:20:00 +00:00
Two interaction modes are available for outputs: `pulse` and `switch`. In pulse mode, the output quickly switches its state to logical 1 and back (just like pressing a button). In switch mode, it saves (toggles) the state that the user set. When Pi-KVM is started/rebooted (any time the KVMD daemon is started or stopped) all output channels are reset to 0. This can be changed using the `initial` parameter. For example, `initial=true` for logic 1 on startup.
2020-09-10 13:36:03 +00:00
2020-09-10 17:20:39 +00:00
If you don't specify a driver for the channel in the scheme the default driver, `__gpio__` will be used.
2020-09-10 13:36:03 +00:00
2020-09-10 17:24:42 +00:00
| Parameter | Type | Allowed values | Default | Description |
|-----------------------------------|-----------|--------------------------|---------|-----------------------|
| `led1`, `button1`, `relay1`, etc. | `string` | `a-Z`, numbers, `_`, `-` | | A section for the named channel |
| `pin` | `integer` | `X >= 0` | | Refers to a GPIO pin or driver's pin/port |
2020-09-10 17:28:02 +00:00
| `mode` | `enum` | `input` or `output` | | Defines if a channel is used for input or output, may be limited by driver plugin |
2020-09-18 00:36:13 +00:00
| **Input only** | | | | |
| `debounce` | `float` | `x >= 0` | `0.1` | [Debounce](https://www.arduino.cc/en/Tutorial/Debounce) time in seconds. `0` for disable debounce |
| **Output only** | | | | |
2020-11-10 02:32:46 +00:00
| `switch` | `bool` | `true` or `false` | `true` | Enables or disables the switch mode on the channel (enabled by default). |
2020-09-18 00:20:00 +00:00
| `initial` | `nullable bool` | `true`, `false` or `null` | `false` | Defines the initial state of the switch upon boot, `null` for don't make changes (the last one does not supported by generic GPIO) |
2020-11-10 02:36:49 +00:00
| `inverted` | `bool` | `true` or `false` | `false` | Inverts the active logical level |
2020-09-10 17:24:42 +00:00
| `pulse` | | | | A section header to define switch pulse configuration |
| `delay` | `float` | `X >= 0` | `0.1` | Defines the pulse time in seconds, `0` for disable pulsing |
| `min_delay` | `float` | `X >= 0.1` | `0.1` |
| `max_delay` | `float` | `X >= 0.1` | `0.1` |
__Example configuration__
2020-09-10 13:36:03 +00:00
```yaml
kvmd:
gpio:
scheme:
# A certain device sends signals to the RPi and we want the Pi-KVM to display this as an led
led1:
pin: 19 # GPIO pin number on the RPi
mode: input
2020-09-10 13:36:03 +00:00
led2:
pin: 16 # GPIO pin number on the RPi
mode: input
2020-09-10 13:36:03 +00:00
# Two outputs of RPi's GPIO
button1:
pin: 26 # GPIO pin number on the RPi
2020-09-10 13:36:03 +00:00
mode: output
switch: false # Disable switching, only pulse available
button2:
pin: 20 # GPIO pin number on the RPi
2020-09-10 13:36:03 +00:00
mode: output
switch: false # Disable switching, only pulse available
2020-09-10 13:36:03 +00:00
relay1: # Channel 1 of the relay /dev/hidraw0
pin: 0 # Numerating starts from 0
mode: output # Relays can't be inputs
initial: null # Don't reset the state to 0 when initializing and terminating KVMD
relay2: # Channel 2
pin: 1
mode: output # Relays can't be inputs
2020-09-10 13:36:03 +00:00
initial: null
pulse:
delay: 2 # Default pulse value
2020-09-10 17:20:39 +00:00
max_delay: 2 # The pulse interval can be between min_delay=0.1 (by default) and max_delay=2
2020-09-10 13:36:03 +00:00
```
### View
2020-09-10 17:20:39 +00:00
This is the last part of the required configuration. It defines how the previous driver and channel configuration is rendered on the Web interface. Here's an example for the example configuration above:
2020-09-10 13:36:03 +00:00
```yaml
kvmd:
gpio:
view:
header:
title: Switches # the menu title
2020-09-10 13:36:03 +00:00
table: # The menu items are rendered in the form of a table of text labels and controls
- ["#Generic GPIO leds"] # Text starting with the sharp symbol will be a label
- [] # creates a horizontal separator and starts a new table
2020-09-10 13:36:03 +00:00
- ["#Test 1:", led1, button1] # Text label, one input, one button with text "Click"
- ["#Test 2:", led2, button2]
- [] # creates a horizontal separator and starts a new table
2020-09-10 13:36:03 +00:00
- ["#HID Relays /dev/hidraw0"]
- [] # creates a horizontal separator and starts a new table
2020-09-10 17:24:42 +00:00
- ["#Relay #1:", "relay1|Boop 0.1"] # Text label and button with alternative text
- ["#Relay #2:", "relay2|Boop 2.0"] # Text label and button with alternative text
2020-09-10 13:36:03 +00:00
```
This will be rendered as:
<img src="../img/gpio_menu.png" alt="drawing" />
Some rules and customization options:
2020-09-10 13:36:03 +00:00
- Text starting with the `#` symbol will be a label.
2020-09-10 17:20:39 +00:00
- To place a channel in a cell, use the name you defined in the scheme.
- Inputs are displayed as round LEDs.
- Outputs are displayed as a switch AND a button.
2020-09-10 13:36:03 +00:00
- If the switch mode is disabled, only a button will be displayed. If pulse is disabled, only a switch will be shown.
2020-09-10 17:20:39 +00:00
- To change the LED's color specify it after the channel name like `"led1|red"`. Available: `green`, `yellow` and `red`.
- To change title of the button, write some its name like `"relay1|My cool relay"`.
2020-10-03 08:24:25 +00:00
- Buttons and switches can request confirmation on acting. To do this write its name like `"relay1|confirm|My cool relay"`. The third argument with a title is required in this case.
2020-09-11 08:39:59 +00:00
2021-04-10 09:17:19 +00:00
# Hardware modules and pseudo-drivers
2020-09-11 08:39:59 +00:00
### Raspberry's GPIO
2020-09-18 00:20:00 +00:00
The driver `gpio` provides access to regular GPIO pins with input and output modes. It uses `/dev/gpiochip0` and the libgpiod library to communicate with the hardware. Does not support saving state between KVMD restarts (meaning `initial=null`).
2020-09-11 08:39:59 +00:00
You can use the [interactive scheme](https://pinout.xyz/) when selecting the pins to use. Please note that when selecting a pin for a channel, you need to use a logical number instead of a physical number. That is, if you want to use a physical pin with the number 40, the channel must have the number 21 corresponding to the logical GPIO21.
2020-09-20 01:06:23 +00:00
Channels should not use duplicate pins. You can also not use already used pins. To see which pins are currently used, run the command `gpioinfo`.
2020-09-11 08:39:59 +00:00
### USB HID Relay
The driver `hidrelay` provides access to cheap managed [USB HID relays](http://vusb.wikidot.com/project:driver-less-usb-relays-hid-interface) that can be found on AliExpress. This driver does not support input mode, only output. To use it, you need to specify the path to the device file (like `/dev/hidraw0`) using the `device` parameter.
Additionally, we recommend to configure access rights and static device name using [UDEV rules](https://wiki.archlinux.org/index.php/udev). For example, create `/etc/udev/rules.d/99-kvmd-extra.rules`:
```
SUBSYSTEM=="usb", ATTR{idVendor}=="16c0", ATTR{idProduct}=="05df", MODE="666"
```
2020-09-20 01:06:23 +00:00
2020-09-20 01:08:35 +00:00
Channels should not use duplicate physical numbers. The driver supports saving state between KVMD restarts (meaning `initial=null`).
2020-11-11 19:43:31 +00:00
### ezCoo KVM switch
2020-11-11 19:45:18 +00:00
You can use GPIO to control KVM port switching. This usually requires the use of relays and buttons, but for the [ezCoo switch](https://github.com/pikvm/pikvm/blob/master/pages/ezcoo.md) there is a special `ezcoo` driver that simulates GPIO by sending commands to the switch via serial port. So you can make a menu in Pi-KVM to control the multiport switch.
2021-04-10 09:17:19 +00:00
### IPMI
The driver `ipmi` provides the ability to send IPMI commands (on, off, reset) and show the power status of the remote host. In fact, this is not a hardware driver, but something like a pseudo-GPIO. Each "pin" is actually responsible for a specific IPMI operation of `ipmitool`:
| Pin | Type | Command |
|-----|----------|---------|
| `0` | `input` | `ipmitool ... power status`, can be used to draw the LED in the menu |
| `1` | `output` | `ipmitool ... power on`, sends the `on` command (and only this), so like all other outputs it should be a button |
| `2` | `output` | `ipmitool ... power off` |
| `3` | `output` | `ipmitool ... power cycle` |
| `4` | `output` | `ipmitool ... power reset` |
| `5` | `output` | `ipmitool ... power diag` |
| `6` | `output` | `ipmitool ... power soft` |
You are supposed to define one driver per host:
```yaml
kvmd:
gpio:
drivers:
my_server:
type: ipmi
host: myserver.local
user: admin
passwd: admin
scheme:
my_server_status:
driver: my_server
pin: 0
mode: input
my_server_on:
driver: my_server
pin: 1
mode: output
switch: off
my_server_off:
driver: my_server
pin: 2
mode: output
switch: off
view:
table:
- [my_server_status, "my_server_on|On", "my_server_off|Off"]
```