https://github.com/temoto/gpio-cdev-go
Linux 4.8+ GPIO interface (gpiochip char dev) for pure Go
https://github.com/temoto/gpio-cdev-go
gpio-library hardware linux status-active
Last synced: about 1 year ago
JSON representation
Linux 4.8+ GPIO interface (gpiochip char dev) for pure Go
- Host: GitHub
- URL: https://github.com/temoto/gpio-cdev-go
- Owner: temoto
- License: cc0-1.0
- Created: 2019-06-02T04:32:41.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T12:04:20.000Z (over 3 years ago)
- Last Synced: 2025-04-12T22:05:57.030Z (about 1 year ago)
- Topics: gpio-library, hardware, linux, status-active
- Language: Go
- Size: 61.5 KB
- Stars: 11
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# What
gpio-cdev-go is pure Go library to access Linux 4.8+ GPIO chardev interface. [](https://godoc.org/github.com/temoto/gpio-cdev-go)
Tested on:
- ARMv6,7 RaspberryPi 1, OrangePi Lite
- ARM64 RaspberryPi 3 B+
If you know how to make this work useful for more people, please take a minute to communicate:
- https://github.com/temoto/gpio-cdev-go/issues/new
- temotor@gmail.com
Ultimate success would be to merge this functionality into periph.io lib.
# Usage
Low level, bare ioctl API is provided by set of `Raw*` functions.
High-level wrapper (see api.go) is recommended way to use library.
```
// Open GPIO chip device.
// Default consumer tag will be used if one is not provided to line management functions.
chip, err := gpio.Open("/dev/gpiochip0", "default-consumer")
defer chip.Close()
// Set lines via either `SetBulk(values ...byte)`
// or create setter closure with `SetFunc(line) -> func(bool)`
// Either way you should call `Flush()` to commit changes to hardware.
pins, err := chip.OpenLines(
gpio.GPIOHANDLE_REQUEST_OUTPUT, "hd44780",
nRS, nRW, nE, nD4, nD5, nD6, nD7,
)
defer pins.Close()
pins.SetBulk(1, 0, 0, 1, 1, 1, 1)
err := pins.Flush()
set_pin_d4 := pins.SetFunc(nD4)
set_pin_d4(true)
err := pins.Flush()
// Reading current lines state
pins, err := chip.OpenLines(gpio.GPIOHANDLE_REQUEST_INPUT, "consumer", uint32(line))
defer pins.Close()
data, err := pins.Read()
lineActive := data.Values[0] == 1
// Waiting for edge. REQUEST_INPUT flag is implied.
lineEvent, err := chip.GetLineEvent(uint32(notifyLine), /*RequestFlag*/ 0,
gpio.GPIOEVENT_REQUEST_RISING_EDGE, "consumer")
defer lineEvent.Close()
currentValue, err := lineEvent.Read()
eventData, err := lineEvent.Wait(timeout) // 0 to block forever
ok := eventData.ID == GPIOEVENT_EVENT_RISING_EDGE
```
# Possible issues
- may leak `req.fd` descriptors, TODO test
# Testing
* get 2 free GPIO pins
* jumper them
* set environment variables and run tests
```
export GPIO_TEST_DEV="/dev/gpiochip0"
export GPIO_TEST_PIN="19"
export GPIO_TEST_PIN_LOOP="16"
go test ./...
```
# Flair
[](https://travis-ci.org/temoto/gpio-cdev-go)
[](https://codecov.io/gh/temoto/gpio-cdev-go)
[](https://goreportcard.com/report/github.com/temoto/gpio-cdev-go)