-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* libgpiodv2: introduce namespace for libgpiod v1 for differentation to v2 * libgpiodv2: add binding and proxy classes for libgpiod v2 * libgpiodv2: add libgpiodv2 driver and event observer * libgpiodv2: add libgpiod driver factory to select driver based on installed libgpiod version * libgpiodv2: add tests * libgpiodv2: add documentation on how to use libgpiodv2 driver * libgpiodv2: documentation improvement * libgpiodv2: name namespaces consistent * libgpiodv2: keep LibGpiodDriver name to not break client code, make namespaces shorter, improve libgpiod version detection * libgpiodv2: do not block operations when waiting for edge events * libgpiodv2: introduce factory for libgpiod proxy objects * libgpiodv2: locking reads is not necessary * libgpiodv2: add GetAvailableVersions * libgpiodv2: let GpioException derive from IOException * libgpiodv2: apply disposable pattern to all proxies, assign enum members explicitly * libgpiodv2: free all filedescriptors * libgpiodv2: make LibGpiodDriverFactory a singleton and use Lazy * libgpiodv2: use alternative init in LibGpiodDriverFactory * Add required suppressions They're rather strange and look like false positives * Disable LibGpiod2 for Helix until runners are updated * libgpiodv2: recursively search for libgpiod * Fix exclusions * libgpiodv2: improve XML doc --------- Co-authored-by: Patrick Grawehr <[email protected]>
- Loading branch information
Showing
67 changed files
with
4,724 additions
and
69 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,109 @@ | ||
# Using libgpiod to control GPIOs | ||
|
||
## Quick usage: blink LED example | ||
|
||
This example targets a RaspberryPi 3/4, see comments for more information: | ||
|
||
```c# | ||
// side note: on the Raspberry Pi the GPIO chip line offsets are the same numbers as the usual BCM GPIO numbering, which is convenient | ||
const int ledGpio = 15; | ||
|
||
// on the Pi3,4 you most likely want 0, on the Pi5 number 4, see 'gpioinfo' tool | ||
const int chipNumber = 0; | ||
// 'using' will dispose the controller when it falls out of scope, which will un-claim lines | ||
// alternatively be more explicit: 'new GpioController(chipNumber, new LibGpiodDriver())' | ||
using var gpioController = new GpioController(chipNumber); | ||
|
||
gpioController.OpenPin(ledGpio); | ||
|
||
for (int i = 0; i < 5; i++) | ||
{ | ||
controller.Write(ledGpio, PinValue.High); | ||
await Task.Delay(1000); | ||
controller.Write(ledGpio, PinValue.Low); | ||
await Task.Delay(1000); | ||
} | ||
``` | ||
|
||
## libgpiod versions | ||
|
||
**Note**: The documented version of libgpiod is not the same as the library so name, see the following table: | ||
|
||
| Documented version | Library so name | Comment | | ||
| ------------------ | ----------------- | -------------------------------------------------------- | | ||
| 1.0.2 | libgpiod.so.1.0.2 | last .so.1.x library | | ||
| 1.1 | libgpiod.so.2.0.0 | first occurrence of inconsistency, first .so.2.x library | | ||
| 1.6.4 | libgpiod.so.2.2.2 | last .so.2.x library | | ||
| 2.0 | libgpiod.so.3.0.0 | first .so.3.x library | | ||
| 2.1 | libgpiod.so.3.1.0 | latest .so.3.x library (currently) | | ||
|
||
## libgpiod version support | ||
|
||
Currently (12/23) dotnet-iot supports v0, v1 and v2 of libgpiod. | ||
|
||
The following table shows which driver supports which library version | ||
|
||
| LibGpiodDriverVersion | Libgpiod version (documented) | | ||
| --------------------- | ----------------------------- | | ||
| V1 | 0.x to 1.x | | ||
| V2 | 2.x | | ||
|
||
## Choose LibGpiodDriver Version | ||
|
||
If you want to explicitly select the version of the libgpiod driver, to target a specific library version, there are following options: | ||
|
||
1. constructor of LibGpiodDriver: | ||
|
||
```c# | ||
new LibGpiodDriver(chipNumber, LibGpiodDriverVersion.V1) | ||
``` | ||
|
||
2. Environment variable: | ||
|
||
```shell | ||
export DOTNET_IOT_LIBGPIOD_DRIVER_VERSION=V1 // or V2... | ||
``` | ||
|
||
When not explicitly specified, dotnet iot automatically tries to find a driver compatible to what library version is installed. | ||
|
||
## Install libgpiod | ||
|
||
If you want to control GPIOs using libgpiod, the library must be installed. | ||
|
||
Many package managers provide a libgpiod package, for example: | ||
|
||
```shell | ||
apt install libgpiod2 | ||
``` | ||
|
||
## Install libgpiod manually | ||
|
||
The installation should be the same on all Pi's, or boards whose distro uses the APT package manager. | ||
|
||
1. Install build dependencies | ||
|
||
```shell | ||
sudo apt update && sudo apt install -y autogen autoconf autoconf-archive libtool libtool-bin pkg-config build-essential | ||
``` | ||
|
||
2. Download the tarball and unpack it, see [releases](https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/refs/), e.g. | ||
```shell | ||
wget https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/snapshot/libgpiod-2.1.tar.gz | ||
tar -xzf libgpiod-2.1.tar.gz | ||
``` | ||
|
||
3. Compile and install (see [docs](https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/about/)) | ||
```shell | ||
cd libgpiod-2.1/ | ||
./autogen.sh | ||
make | ||
sudo make install | ||
sudo ldconfig | ||
``` | ||
|
||
This will install the library .so files to `/usr/lib/local` | ||
|
||
If you want to also build command line utilities `gpioinfo, gpiodetect` etc., specify `./autogen.sh --enable-tools=yes` |
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
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,27 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Device.Gpio.Drivers; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace System.Device.Gpio.Tests; | ||
|
||
[Trait("feature", "gpio")] | ||
[Trait("feature", "gpio-libgpiod2")] | ||
[Trait("SkipOnTestRun", "Windows_NT")] | ||
public class LibGpiodV2DriverTests : GpioControllerTestBase | ||
{ | ||
private const int ChipNumber = 0; | ||
|
||
public LibGpiodV2DriverTests(ITestOutputHelper testOutputHelper) | ||
: base(testOutputHelper) | ||
{ | ||
} | ||
|
||
protected override GpioDriver GetTestDriver() => new LibGpiodDriver(ChipNumber, LibGpiodDriverVersion.V2); | ||
|
||
protected override PinNumberingScheme GetTestNumberingScheme() => PinNumberingScheme.Logical; | ||
} |
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
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
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,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
// Disable these StyleCop rules for this file, as we are using native names here. | ||
#pragma warning disable SA1300 // Element should begin with upper-case letter | ||
|
||
using System.Runtime.InteropServices; | ||
|
||
internal partial class Interop | ||
{ | ||
[DllImport(LibcLibrary, SetLastError = true)] | ||
internal static extern int pipe(int[] pipefd); | ||
} |
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
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
11 changes: 11 additions & 0 deletions
11
src/System.Device.Gpio/Interop/Unix/libgpiod/V2/Binding/Enums/GpiodEdgeEventType.cs
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,11 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Device.Gpio.Libgpiod.V2; | ||
|
||
/// <seealso href="https://libgpiod.readthedocs.io/en/latest/group__edge__event.html"/> | ||
internal enum GpiodEdgeEventType | ||
{ | ||
RisingEdge = 1, | ||
FallingEdge = 2 | ||
} |
Oops, something went wrong.