Skip to content

Commit

Permalink
refactor: Rework Device GPIO docs into new 3.1 structure (#1291)
Browse files Browse the repository at this point in the history
* refactor: Rework Device GPIO docs into new 3.1 structure

closes #1285

Signed-off-by: Leonard Goodell <leonard.goodell@intel.com>
  • Loading branch information
Lenny Goodell authored Nov 13, 2023
1 parent adde19f commit e72b3cf
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 6 deletions.
5 changes: 0 additions & 5 deletions docs_src/microservices/device/services/device-gpio.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Device GPIO - API Reference
---

# Device GPIO - API Reference

Device GPIO inherits its REST APIs from the [Device Services SDK](../../ApiReference.md).
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: Device GPIO - Configuration
---

# Device GPIO - Configuration

This service defines the following **Driver** configuration settings in addition to the configuration to that provided by the Device SDK.
See [Device Service Configuration](../../Configuration.md) section for details on the common device service configuration.

| Name | Default Value | Description |
|---------------------|---------------|----------------------------------------------------------------------------------------------------|
| Driver.Interface | "sysfs" | GPIO interface to use. Valid values are `sysfs` and `chardev`. Note that `chardev` is experimental |
| Driver.ChipSelected | "0" | Chip to select when using `chardev` interface |

174 changes: 174 additions & 0 deletions docs_src/microservices/device/services/device-gpio/GettingStarted.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
---
title: Device GPIO - Getting Started
---

# Device GPIO - Getting Started

!!! note
Since GPIO `sysfs` interface is **deprecated after Linux version 4.8**, two ABI interfaces are provided: the `sysfs` version and the new `chardev` version. By default, the interface is set to `sysfs`. It can be changed inside the `Driver` section of the service's configuration. For the `chardev` interface, you need to specify a selected chip. This is also under `Driver` section. See the [Configuration](./Configuration.md) section for more details

## Running the Service

The simplest way to run all the required services is to use the [Compose Builder](https://github.com/edgexfoundry/edgex-compose/tree/{{edgexversion}}/compose-builder) tool from a terminal window

1. Clone [https://github.com/edgexfoundry/edgex-compose/tree/{{edgexversion}}](https://github.com/edgexfoundry/edgex-compose/tree/{{edgexversion}})

2. Change to the **compose-builder** folder

3. Run the services
```
make run no-secty ds-gpio
```
This runs, in non-secure mode, all the standard EdgeX services along with the Device GPIO service.

## Sample Device Profile and Devices

This service contains the following sample device profiles and devices:

| Device Profile | Device | Description |
|---------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------|
| [device.custom.gpio.yaml](https://github.com/edgexfoundry/device-gpio/blob/{{edgexversion}}/cmd/res/profiles/device.custom.gpio.yaml) | [device.custom.gpio.yaml](https://github.com/edgexfoundry/device-gpio/blob/{{edgexversion}}/cmd/res/devices/device.custom.gpio.yaml) | Example for GPIO with Power, LED and Switch |
| [device.led.gpio.yaml](https://github.com/edgexfoundry/device-gpio/blob/{{edgexversion}}/cmd/res/profiles/device.led.gpio.yaml) | [device.led.gpio.yaml](https://github.com/edgexfoundry/device-gpio/blob/{{edgexversion}}/cmd/res/devices/device.led.gpio.yaml) | Example for GPIO with just LED |

The device profiles are used to describe the actual GPIO hardware of a device and allow individual GPIOs to be given human-readable names/aliases.

## Example walk-thru

The following are step-by-step examples of using this device service. In these examples, we use Core Command RESTful API to interact with EdgeX rather than directly interact with GPIO device service.

!!! example - "Example - Query Core Command for device `GPIO-Device01` commands"
```shell
curl http://localhost:59882/api/v3/device/name/GPIO-Device01
```
```json
{
"apiVersion": "v2",
"statusCode": 200,
"deviceCoreCommand": {
"deviceName": "GPIO-Device01",
"profileName": "Custom-GPIO-Device",
"coreCommands": [
{
"name": "Power",
"get": true,
"set": true,
"path": "/api/v3/device/name/GPIO-Device01/Power",
"url": "http://edgex-core-command:59882",
"parameters": [
{
"resourceName": "Power",
"valueType": "Bool"
}
]
},
{
"name": "LED",
"set": true,
"path": "/api/v3/device/name/GPIO-Device01/LED",
"url": "http://edgex-core-command:59882",
"parameters": [
{
"resourceName": "LED",
"valueType": "Bool"
}
]
},
{
"name": "Switch",
"get": true,
"path": "/api/v3/device/name/GPIO-Device01/Switch",
"url": "http://edgex-core-command:59882",
"parameters": [
{
"resourceName": "Switch",
"valueType": "Bool"
}
]
}
]
}
}
```
Use the `curl` response to get the command URLs (with device and command ids) to issue commands to the GPIO device via the command service as shown above. You can also use a tool like `Postman` instead of `curl` to issue the same commands.

### Direction setting with sysfs
When using sysfs, the operations to access (read or write) the GPIO pins are:

1. Export the pin
2. Set the direction (either IN or OUT)
3. Read the pin input or write the pin value based on the direction
4. Unexport the pin

When using sysfs, setting the direction causes the value to be reset. Therefore, this implementation only sets the direction on opening the line to the GPIO. After that, it is assumed the same direction is used while the pin is in use and exported.

The direction is set by an optional attribute in the device profile called `defaultDirection`. It can be set to either "in" or "out". If it is not set, the default direction is assumed to be "out".

!!! example - "Example - GPIO resource in device profile"
``` yaml
-
name: "LED"
isHidden: false
description: "mocking LED"
attributes: { line: 27, defaultDirection: "out" }
properties:
valueType: "Bool"
readWrite: "W"
```

!!! note
The direction should not be confused with the device profile's read/write property. If you set the defaultDirection to `in` but then set the readWrite property to `RW` or `W`, any attempt to write to the pin will result in a "permission denied" error. For consistency, when your defaultDirection is `in` set readWrite to `R` only.

### Write value to GPIO
Assume a GPIO device (used for power enable) connected to gpio17 on current system of raspberry pi 4b. When a value is written to GPIO, this GPIO will give a high voltage.

!!! example - "Example - Set commands to set `Power` resource on `GPIO-Device01`"
```shell
# Set the 'Power' GPIO to high
$ curl -X PUT -d '{"Power":"true"}' http://localhost:59882/api/v3/device/name/GPIO-Device01/Power
{"apiVersion":"v2","statusCode":200}
$ cat /sys/class/gpio/gpio17/direction ; cat /sys/class/gpio/gpio17/value
out
1

# Set the 'Power' GPIO to low
$ curl -X PUT -d '{"Power":"false"}' http://localhost:59882/api/v3/device/name/GPIO-Device01/Power
{"apiVersion":"v2","statusCode":200}
$ cat /sys/class/gpio/gpio17/direction ; cat /sys/class/gpio/gpio17/value
out
0

Now if you test gpio17 of raspberry pi 4b , it is outputting high voltage.
```

### Read value from GPIO
Assume another GPIO device (used for button detection) connected to pin 22 on current system. When a value is read from GPIO, this GPIO will be exported and set direction to input.

!!! example - "Example - GET command for `Switch` resource on `GPIO-Device01`"
```shell
$ curl http://localhost:59882/api/v3/device/name/GPIO-Device01/Switch
```
```json
{
"apiVersion": "v2",
"statusCode": 200,
"event": {
"apiVersion": "v2",
"id": "a6104256-92a4-41a8-952a-396cd3dabe25",
"deviceName": "GPIO-Device01",
"profileName": "Custom-GPIO-Device",
"sourceName": "Switch",
"origin": 1634221479227566300,
"readings": [
{
"id": "240dc2ea-d69f-4229-94c4-3ad0507cf657",
"origin": 1634221479227566300,
"deviceName": "GPIO-Device01",
"resourceName": "Switch",
"profileName": "Custom-GPIO-Device",
"valueType": "Bool",
"value": "false"
}
]
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Device GPIO - Protocol Properties
---

# Device GPIO - Protocol Properties

This device service does not define any device specific protocol properties. Any protocol properties specified in the Device Definition are ignored.
The GPIO protocol specifics are specified in the `attributes` for each resource in the Device Profile. See [Example Resource](./GettingStarted.md#direction-setting-with-sysfs) in walk-thru section for more details.
13 changes: 13 additions & 0 deletions docs_src/microservices/device/services/device-gpio/Purpose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Device GPIO - Purpose
---

# Device GPIO - Purpose

The purpose of this device service is to connect GPIO devices to EdgeX. It uses sysfs ABI (default) or chardev ABI (experimental) to control connected GPIO devices.

This device service is contributed by [Jiangxing Intelligence](https://www.jiangxingai.com/)

!!! note
This device service ONLY works on Linux systems

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Device GPIO - Source Code
---

# Device GPIO - Source Code

The source code for Device GPIO can be found at [https://github.com/edgexfoundry/device-gpio/tree/{{edgexversion}}](https://github.com/edgexfoundry/device-gpio/tree/{{edgexversion}}).

8 changes: 7 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ nav:
- Services:
- Device BacNet: microservices/device/services/device-bacnet.md
- Device COAP: microservices/device/services/device-coap.md
- Device GPIO: microservices/device/services/device-gpio.md
- Device GPIO:
- Purpose: microservices/device/services/device-gpio/Purpose.md
- Getting Started: microservices/device/services/device-gpio/GettingStarted.md
- Configuration: microservices/device/services/device-gpio/Configuration.md
- Protocol Properties: microservices/device/services/device-gpio/ProtocolProperties.md
- API Reference: microservices/device/services/device-gpio/ApiReference.md
- Source Code: microservices/device/services/device-gpio/SourceCode.md
- Device ModBus: microservices/device/services/device-modbus.md
- Device MQTT:
- Purpose: microservices/device/services/device-mqtt/Purpose.md
Expand Down

0 comments on commit e72b3cf

Please sign in to comment.