chardevgpio

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2024 License: MIT Imports: 8 Imported by: 3

README

chardevgpio

Release GoDoc Build Status codecov Go Report Card

chardevgio is a pure Go library for access the Linux GPIO character device user API.

Usage

import gpio "github.com/vinymeuh/chardevgpio"

In following examples, error handling will be ommited.

Chip

A Chip object must be open before to request lines or lines informations from a GPIO chip.

chip, _ := gpio.NewChip("/dev/gpiochip0")
...
defer chip.Close()

Closing a chip does not invalidate any previously requested lines that can still be used.

LineInfo

Lines information can be requested from the chip at any moment as long as it is open.

li, _ := chip.LineInfo(0)
line0Name := li.Name()
HandleRequest

An HandleRequest is mandatory to setup a request an input line or an output line from the chip. The request should at minimum define the offsets of requested lines and the communication direction.

lineIn_0, _    := chip.NewHandleRequest([]int{0}, gpio.HandleRequestInput)
lineOut_8_9, _ := chip.NewHandleRequest([]int{8, 9}, gpio.HandleRequestOutput)

Consumer name or default values can be set on the HandleRequest:

lineIn_0.WithConsumer("myapp")
lineOut_8_9.WithDefaults([]int{1, 1})

Requesting lines to the chip is done passing a reference to the HandleRequest to Chip.RequestLines():

c.RequestLines(lineIn_0)
c.RequestLines(lineOut_8_9) 

If no errors, the returned HandleRequest can then be used to read from or write to the lines:

val0 := lineIn_0.Read()
lineOut_8_9.Write(0, 0)

Note that HandleRequest.Read() returns 3 values:

  • first one is the read value for the first line managed by the HandleRequest. It is useful when working on a request with only one line.
  • second one is an array containing read values for all lines managed by the HandleRequest
  • last one is the error if any
LineWatcher

Event on an input line can be trapped using a LineWatcher:

watcher, _ := gpio.NewLineWatcher()

Add to it lines and events to be watched:

watcher.Add(c, 0, gpio.RisingEdge, "wait rising edge on line 0")
watcher.Add(c, 1, gpio.FallingEdge, "wait falling edge on line 1")
watcher.Add(c, 2, gpio.BothEdges, "wait state change on line 2")

The watcher can then run indefinitely and call a handler function for each event trapped:

func myFuncHandler(evd Event) { ... }
watcher.WaitForEver(myFuncHandler)

For simpler cases, the watcher can be used to block waiting for the first event occurrence:

event, _ := watcher.Wait()

Tests

During development, the library is tested using the Linux kernel module gpio-mockup on an x86_64 environment.

> ./setup-test.sh   # to be run once, prompts for sudo password
> make test

For real world tests on a Raspberry, see command line utilities provided under cmd directory.

Documentation

Overview

Package chardevgpio is a library to the Linux GPIO Character device API.

Index

Constants

View Source
const (
	HandleRequestInput        HandleRequestFlag = 1 << 0
	HandleRequestOutput                         = 1 << 1
	HandleRequestActiveLow                      = 1 << 2
	HandleRequestOpenDrain                      = 1 << 3
	HandleRequestOpenSource                     = 1 << 4
	HandleRequestBiasPullUp                     = 1 << 5
	HandleRequestBiasPullDown                   = 1 << 6
	HandleRequestBiasDisable                    = 1 << 7
)

HandleRequest request flags.

View Source
const (
	RisingEdge  EventRequestFlags = 1 << 0
	FallingEdge                   = 1 << 1
	BothEdges                     = (1 << 0) | (1 << 1)
)

EventLine request flags.

Variables

View Source
var ErrOperationNotPermitted = errors.New("operation not permitted")

ErrOperationNotPermitted is returned when trying to read on an output line or to write on a input line.

Functions

This section is empty.

Types

type Chip

type Chip struct {
	ChipInfo
	// contains filtered or unexported fields
}

Chip is a GPIO chip controlling a set of lines.

func NewChip added in v1.0.0

func NewChip(path string) (Chip, error)

NewChip returns a Chip for a GPIO character device from its path.

func (Chip) Close

func (c Chip) Close() error

Close releases resources helded by the chip.

func (Chip) Label added in v1.0.0

func (c Chip) Label() string

Label returns the label of the chip.

func (Chip) LineInfo

func (c Chip) LineInfo(offset int) (LineInfo, error)

LineInfo returns informations about the requested line.

func (Chip) Lines added in v1.0.0

func (c Chip) Lines() int

Lines returns the number of lines managed by the chip.

func (Chip) Name added in v1.0.0

func (c Chip) Name() string

Name returns the name of the chip.

func (Chip) RequestLines added in v1.0.0

func (c Chip) RequestLines(request *HandleRequest) error

RequestLines takes a prepared HandleRequest and returns it ready to work.

type ChipInfo added in v1.0.0

type ChipInfo struct {
	// contains filtered or unexported fields
}

ChipInfo contains informations about a GPIO chip.

type Event added in v1.0.0

type Event struct {
	Timestamp uint64
	ID        uint32
}

Event represents a occurred event.

func (Event) IsFalling added in v1.0.0

func (e Event) IsFalling() bool

IsFalling returns true for event on a falling edge.

func (Event) IsRising added in v1.0.0

func (e Event) IsRising() bool

IsRising returns true for event on a rising edge.

type EventHandlerFunc added in v1.0.0

type EventHandlerFunc func(evd Event)

EventHandlerFunc is the type of the function called for each event retrieved by WaitForEver.

type EventLine added in v1.0.0

type EventLine struct {
	// contains filtered or unexported fields
}

EventLine represents a single line setup to receive GPIO events.

type EventRequestFlags added in v1.0.0

type EventRequestFlags uint32

EventRequestFlags defines the kind of event to wait on a line.

type HandleConfig added in v1.0.0

type HandleConfig struct {
	// contains filtered or unexported fields
}

HandleConfig is the structure to reconfigure an existing HandleRequest (not used currently, require Kernel 5.5 or later).

type HandleRequest added in v1.0.0

type HandleRequest struct {
	// contains filtered or unexported fields
}

HandleRequest represents at first a query to be sent to a chip to get control on a set of lines. After be returned by the chip, it must be used to send or received data to lines.

func NewHandleRequest added in v1.0.0

func NewHandleRequest(offsets []int, flags HandleRequestFlag) *HandleRequest

NewHandleRequest prepare a HandleRequest

func (HandleRequest) Close added in v1.0.0

func (hr HandleRequest) Close() error

Close releases resources helded by the HandleRequest.

func (*HandleRequest) Read added in v1.0.0

func (hr *HandleRequest) Read() (int, []int, error)

Reads return values read from the lines handled by the HandleRequest. The second return parameter contains all values returned as an array. The first one is the first element of this array, useful when dealing with 1 line HandleRequest.

func (*HandleRequest) WithConsumer added in v1.0.0

func (hr *HandleRequest) WithConsumer(consumer string) *HandleRequest

WithConsumer set the consumer for a prepared HandleRequest.

func (*HandleRequest) WithDefaults added in v1.0.0

func (hr *HandleRequest) WithDefaults(defaults []int) *HandleRequest

WithDefaults set the default values for a prepared HandleRequest. Note that setting default values on a InputLine is a nonsense but no error are returned.

func (*HandleRequest) Write added in v1.0.0

func (hr *HandleRequest) Write(value0 int, valueN ...int) error

Write writes values to the lines handled by the HandleRequest. If there is more values ​​supplied than lines managed by the HandleRequest, excess values ​​are silently ignored.

type HandleRequestFlag added in v1.0.0

type HandleRequestFlag uint32

HandleRequestFlag is the type of request flags.

type LineInfo

type LineInfo struct {
	// contains filtered or unexported fields
}

LineInfo contains informations about a GPIO line.

func (LineInfo) Consumer added in v1.0.0

func (li LineInfo) Consumer() string

Consumer returns the consumer of the line.

func (LineInfo) IsActiveHigh

func (li LineInfo) IsActiveHigh() bool

IsActiveHigh returns true if the line is configured as active high.

func (LineInfo) IsActiveLow

func (li LineInfo) IsActiveLow() bool

IsActiveLow returns true if the line is configured as active low.

func (LineInfo) IsBiasPullDown added in v1.0.0

func (li LineInfo) IsBiasPullDown() bool

IsBiasPullDown returns true if the line is configured as bias pull down.

func (LineInfo) IsBiasPullUp added in v1.0.0

func (li LineInfo) IsBiasPullUp() bool

IsBiasPullUp returns true if the line is configured as bias pull up.

func (LineInfo) IsInput

func (li LineInfo) IsInput() bool

IsInput returns true if the line is configured as an input.

func (LineInfo) IsKernel

func (li LineInfo) IsKernel() bool

IsKernel returns true if the line is configured as kernel.

func (LineInfo) IsOpenDrain

func (li LineInfo) IsOpenDrain() bool

IsOpenDrain returns true if the line is configured as open drain.

func (LineInfo) IsOpenSource

func (li LineInfo) IsOpenSource() bool

IsOpenSource returns true if the line is configured as open source.

func (LineInfo) IsOutput

func (li LineInfo) IsOutput() bool

IsOutput returns true if the line is configured as an output.

func (LineInfo) Name added in v1.0.0

func (li LineInfo) Name() string

Name returns the name of the line.

func (LineInfo) Offset added in v1.0.0

func (li LineInfo) Offset() int

Offset returns the offset number of the line.

type LineWatcher added in v1.0.0

type LineWatcher struct {
	// contains filtered or unexported fields
}

LineWatcher is a receiver of events for a set of event lines.

func NewLineWatcher added in v1.0.0

func NewLineWatcher() (LineWatcher, error)

NewLineWatcher initializes a new LineWatcher.

func (*LineWatcher) Add added in v1.0.0

func (lw *LineWatcher) Add(chip Chip, line int, flags EventRequestFlags, consumer string) error

Add adds a new line to watch to the LineWatcher.

func (*LineWatcher) Close added in v1.0.0

func (lw *LineWatcher) Close() error

Close releases resources helded by the LineWatcher.

func (*LineWatcher) Wait added in v1.0.0

func (lw *LineWatcher) Wait() (Event, error)

Wait waits for first occurrence of an event on one of the event lines.

func (*LineWatcher) WaitForEver added in v1.0.0

func (lw *LineWatcher) WaitForEver(handler EventHandlerFunc) error

WaitForEver waits indefinitely for events on the event lines. Note that for one event, more than one EventData can be retrieved on the event line.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL