Documentation ¶
Overview ¶
Package gpiod is a library for accessing GPIO pins/lines on Linux platforms using the GPIO character device.
This is a Go equivalent of libgpiod.
Supports:
- Line direction (input/output)
- Line write (active/inactive)
- Line read (active/inactive)
- Line bias (pull-up/pull-down/disabled)
- Line drive (push-pull/open-drain/open-source)
- Line level (active-high/active-low)
- Line edge detection (rising/falling/both)
- Line labels
- Collections of lines for near simultaneous reads and writes on multiple lines
Example of use:
v := 0 l, err := gpiod.RequestLine("gpiochip0", 4, gpiod.AsOutput(v)) if err != nil { panic(err) } for { <-time.After(time.Second) v ^= 1 l.SetValue(v) }
Index ¶
- Constants
- Variables
- func Chips() []string
- func IsChip(name string) error
- type ABIVersionOption
- type AsIsOption
- type Chip
- func (c *Chip) Close() error
- func (c *Chip) LineInfo(offset int) (info LineInfo, err error)
- func (c *Chip) Lines() int
- func (c *Chip) RequestLine(offset int, options ...LineReqOption) (*Line, error)
- func (c *Chip) RequestLines(offsets []int, options ...LineReqOption) (*Lines, error)
- func (c *Chip) UapiAbiVersion() int
- func (c *Chip) UnwatchLineInfo(offset int) error
- func (c *Chip) WatchLineInfo(offset int, lich InfoChangeHandler) (info LineInfo, err error)
- type ChipOption
- type ChipOptions
- type ConsumerOption
- type DebounceOption
- type DefaultedOption
- type ErrUapiIncompatibility
- type EventBufferSizeOption
- type EventHandler
- type InfoChangeHandler
- type InputOption
- type LevelOption
- type Line
- func (l *Line) Chip() string
- func (l *Line) Close() error
- func (l *Line) Info() (info LineInfo, err error)
- func (l *Line) Offset() int
- func (l *Line) Reconfigure(options ...LineConfigOption) error
- func (l *Line) SetValue(value int) error
- func (l *Line) UapiAbiVersion() int
- func (l *Line) Value() (int, error)
- type LineBias
- type LineConfig
- type LineConfigOption
- type LineDirection
- type LineDrive
- type LineEdge
- type LineEvent
- type LineEventClock
- type LineEventType
- type LineInfo
- type LineInfoChangeEvent
- type LineInfoChangeType
- type LineReqOption
- type Lines
- func (l *Lines) Chip() string
- func (l *Lines) Close() error
- func (l *Lines) Info() ([]*LineInfo, error)
- func (l *Lines) Offsets() []int
- func (l *Lines) Reconfigure(options ...LineConfigOption) error
- func (l *Lines) SetValues(values []int) error
- func (l *Lines) UapiAbiVersion() int
- func (l *Lines) Values(values []int) error
- type LinesOption
- type OutputOption
- type SubsetLineConfigOption
Constants ¶
const AsActiveHigh = LevelOption(false)
AsActiveHigh indicates that a line be considered active when the line level is high.
This is the default active level.
const AsActiveLow = LevelOption(true)
AsActiveLow indicates that a line be considered active when the line level is low.
const AsInput = InputOption(0)
AsInput indicates that a line be requested as an input.
This option overrides and clears any previous Output, OpenDrain, or OpenSource options.
const AsIs = AsIsOption(0)
AsIs indicates that a line be requested as neither an input or output.
That is its direction is left as is. This option overrides and clears any previous Input or Output options.
const AsOpenDrain = LineDriveOpenDrain
AsOpenDrain indicates that a line be driven low but left floating for high.
This option sets the Output option and overrides and clears any previous Input, RisingEdge, FallingEdge, BothEdges, OpenSource, or Debounce options.
const AsOpenSource = LineDriveOpenSource
AsOpenSource indicates that a line be driven high but left floating for low.
This option sets the Output option and overrides and clears any previous Input, RisingEdge, FallingEdge, BothEdges, OpenDrain, or Debounce options.
const AsPushPull = LineDrivePushPull
AsPushPull indicates that a line be driven both low and high.
This option sets the Output option and overrides and clears any previous Input, RisingEdge, FallingEdge, BothEdges, OpenDrain, OpenSource or Debounce options.
const Defaulted = DefaultedOption(0)
Defaulted resets all configuration options to default values.
This option provides the means to simply reset all configuration options to their default values. This is rarely necessary but is made available for completeness.
When applied within WithLines() it resets the configuration of the lines to the default for the request, effectively clearing all previous WithLines() options for the specified offsets. If no offsets are specified then the configurarion for all offsets is reset to the request default.
When applied outside WithLines() it resets the default configuration for the request itself to default values but leaves any configuration set within WithLines() unchanged.
const WithBiasAsIs = LineBiasUnknown
WithBiasAsIs indicates that a line have its internal bias left unchanged.
This option corresponds to the default bias configuration and its only useful application is to clear any previous bias option in a chain of LineOptions, before that configuration is applied.
Requires Linux v5.5 or later.
const WithBiasDisabled = LineBiasDisabled
WithBiasDisabled indicates that a line have its internal bias disabled.
This option overrides and clears any previous bias options.
Requires Linux v5.5 or later.
const WithBothEdges = LineEdgeBoth
WithBothEdges indicates that a line will generate events when its active state transitions from low to high and from high to low.
Events are forwarded to the provided handler function.
This option sets the Input option and overrides and clears any previous Output, OpenDrain, or OpenSource options.
const WithFallingEdge = LineEdgeFalling
WithFallingEdge indicates that a line will generate events when its active state transitions from high to low.
Events are forwarded to the provided handler function.
This option sets the Input option and overrides and clears any previous Output, OpenDrain, or OpenSource options.
const WithMonotonicEventClock = LineEventClockMonotonic
WithMonotonicEventClock specifies that the edge event timestamps are sourced from CLOCK_MONOTONIC.
This option corresponds to the default event clock configuration and its only useful application is to clear any previous event clock option in a chain of LineOptions, before that configuration is applied.
const WithPullDown = LineBiasPullDown
WithPullDown indicates that a line have its internal pull-down enabled.
This option overrides and clears any previous bias options.
Requires Linux v5.5 or later.
const WithPullUp = LineBiasPullUp
WithPullUp indicates that a line have its internal pull-up enabled.
This option overrides and clears any previous bias options.
Requires Linux v5.5 or later.
const WithRealtimeEventClock = LineEventClockRealtime
WithRealtimeEventClock specifies that the edge event timestamps are sourced from CLOCK_REALTIME.
Requires Linux v5.11 or later.
const WithRisingEdge = LineEdgeRising
WithRisingEdge indicates that a line will generate events when its active state transitions from low to high.
Events are forwarded to the provided handler function.
This option sets the Input option and overrides and clears any previous Output, OpenDrain, or OpenSource options.
const WithoutEdges = LineEdgeNone
WithoutEdges indicates that a line will not generate events due to active state transitions.
This is the default for line requests, but allows the removal of edge detection by reconfigure.
This option sets the Input option and overrides and clears any previous Output, OpenDrain, or OpenSource options.
The WithoutEdges option requires Linux v5.10 or later.
Variables ¶
var ( // ErrClosed indicates the chip or line has already been closed. ErrClosed = errors.New("already closed") // ErrConfigOverflow indicates the provided configuration is too complicated // to be mapped to the kernel uAPI. // // Reduce the number of line options or split the request into multiple // requests for smaller sets of lines. ErrConfigOverflow = errors.New("configuration too complex to map to kernel uAPI") // ErrInvalidOffset indicates a line offset is invalid. ErrInvalidOffset = errors.New("invalid offset") // ErrNotCharacterDevice indicates the device is not a character device. ErrNotCharacterDevice = errors.New("not a character device") // ErrPermissionDenied indicates caller does not have required permissions // for the operation. ErrPermissionDenied = errors.New("permission denied") )
Functions ¶
Types ¶
type ABIVersionOption ¶
type ABIVersionOption int
ABIVersionOption selects the version of the GPIO ioctl commands to use.
The default is to use the latest version supported by the kernel.
func WithABIVersion ¶
func WithABIVersion(version int) ABIVersionOption
WithABIVersion indicates the version of the GPIO ioctls to use.
The default is to use the latest version supported by the kernel.
ABI version 2 requires Linux v5.10 or later.
type Chip ¶
type Chip struct { // The system name for this chip. Name string // A more individual label for the chip. Label string // contains filtered or unexported fields }
Chip represents a single GPIO chip that controls a set of lines.
func NewChip ¶
func NewChip(name string, options ...ChipOption) (*Chip, error)
NewChip opens a GPIO character device.
func (*Chip) Close ¶
Close releases the Chip.
It does not release any lines which may be requested - they must be closed independently.
func (*Chip) LineInfo ¶
LineInfo returns the publicly available information on the line.
This is always available and does not require requesting the line.
func (*Chip) RequestLine ¶
func (c *Chip) RequestLine(offset int, options ...LineReqOption) (*Line, error)
RequestLine requests control of a single line on the chip.
If granted, control is maintained until the Line is closed.
func (*Chip) RequestLines ¶
func (c *Chip) RequestLines(offsets []int, options ...LineReqOption) (*Lines, error)
RequestLines requests control of a collection of lines on the chip.
If granted, control is maintained until the Lines are closed.
func (*Chip) UapiAbiVersion ¶
UapiAbiVersion returns the version of the GPIO uAPI the chip is using.
func (*Chip) UnwatchLineInfo ¶
UnwatchLineInfo disables watching changes to line info.
Requires Linux v5.7 or later.
func (*Chip) WatchLineInfo ¶
func (c *Chip) WatchLineInfo(offset int, lich InfoChangeHandler) (info LineInfo, err error)
WatchLineInfo enables watching changes to line info for the specified lines.
The changes are reported via the chip InfoChangeHandler. Repeated calls replace the InfoChangeHandler.
Requires Linux v5.7 or later.
type ChipOption ¶
type ChipOption interface {
// contains filtered or unexported methods
}
ChipOption defines the interface required to provide a Chip option.
type ChipOptions ¶
type ChipOptions struct {
// contains filtered or unexported fields
}
ChipOptions contains the options for a Chip.
type ConsumerOption ¶
type ConsumerOption string
ConsumerOption defines the consumer label for a line.
func WithConsumer ¶
func WithConsumer(consumer string) ConsumerOption
WithConsumer provides the consumer label for the line.
When applied to a chip it provides the default consumer label for all lines requested by the chip.
type DebounceOption ¶
DebounceOption indicates that a line will be debounced.
The DebounceOption requires Linux v5.10 or later.
func WithDebounce ¶
func WithDebounce(period time.Duration) DebounceOption
WithDebounce indicates that a line will be debounced with the specified debounce period.
This option sets the Input option and overrides and clears any previous Output, OpenDrain, or OpenSource options.
Requires Linux v5.10 or later.
type DefaultedOption ¶
type DefaultedOption int
DefaultedOption resets the configuration to default values.
type ErrUapiIncompatibility ¶
ErrUapiIncompatibility indicates the feature is not supported by the given kernel uAPI version.
func (ErrUapiIncompatibility) Error ¶
func (e ErrUapiIncompatibility) Error() string
type EventBufferSizeOption ¶
type EventBufferSizeOption int
EventBufferSizeOption provides a suggested minimum number of events the kernel will buffer for the line request.
The EventBufferSizeOption requires Linux v5.10 or later.
func WithEventBufferSize ¶
func WithEventBufferSize(size int) EventBufferSizeOption
WithEventBufferSize suggests a minimum number of events the kernel will buffer for the line request.
Note that the value is only a suggestion, and the kernel may set higher values or place a cap on the buffer size.
A zero value (the default) indicates that the kernel should use its default buffer size (the number of requested lines * 16).
Requires Linux v5.10 or later.
type EventHandler ¶
type EventHandler func(LineEvent)
EventHandler is a receiver for line events.
func WithEventHandler ¶
func WithEventHandler(e EventHandler) EventHandler
WithEventHandler indicates that a line will generate events when its active state transitions from high to low.
Events are forwarded to the provided handler function.
To maintain event ordering, the event handler is called serially for each event from the requested lines. To minimize the possiblity of overflowing the queue of events in the kernel, the event handler should handle or hand-off the event and return as soon as possible.
Note that calling Close on the requested line from within the event handler will result in deadlock, as the Close waits for the event handler to return. Therefore the Close must be called from a different goroutine.
type InfoChangeHandler ¶
type InfoChangeHandler func(LineInfoChangeEvent)
InfoChangeHandler is a receiver for line info change events.
type InputOption ¶
type InputOption int
InputOption indicates the line direction should be set to an input.
type LevelOption ¶
type LevelOption bool
LevelOption determines the line level that is considered active.
type Line ¶
type Line struct {
// contains filtered or unexported fields
}
Line represents a single requested line.
func RequestLine ¶
func RequestLine(chip string, offset int, options ...LineReqOption) (*Line, error)
RequestLine requests control of a single line on a chip.
If granted, control is maintained until the Line is closed.
func (*Line) Chip ¶
func (l *Line) Chip() string
Chip returns the name of the chip from which the line was requested.
func (*Line) Close ¶
func (l *Line) Close() error
Close releases all resources held by the requested line.
Note that this includes waiting for any running event handler to return. As a consequence the Close must not be called from the context of the event handler - the Close should be called from a different goroutine.
func (*Line) Reconfigure ¶
func (l *Line) Reconfigure(options ...LineConfigOption) error
Reconfigure updates the configuration of the requested line(s).
Configuration for options other than those passed in remain unchanged.
Not valid for lines with edge detection enabled.
Requires Linux v5.5 or later.
func (*Line) SetValue ¶
SetValue sets the current active state of the line.
Only valid for output lines.
func (*Line) UapiAbiVersion ¶
func (l *Line) UapiAbiVersion() int
UapiAbiVersion returns the version of the GPIO uAPI the line is using.
type LineBias ¶
type LineBias int
LineBias indicates the bias applied to a line.
const ( // LineBiasUnknown indicates the line bias is unknown. LineBiasUnknown LineBias = iota // LineBiasDisabled indicates the line bias is disabled. LineBiasDisabled // LineBiasPullUp indicates the line has pull up enabled. LineBiasPullUp // LineBiasPullDown indicates the line has pull down enabled. LineBiasPullDown )
type LineConfig ¶
type LineConfig struct { // A flag indicating if the line is active low. ActiveLow bool // The line direction. Direction LineDirection // The line drive. Drive LineDrive // The line bias. Bias LineBias // The line edge detection. EdgeDetection LineEdge // A flag indicating if the line is debounced. Debounced bool // The line debounce period. DebouncePeriod time.Duration // The source clock for events on the line. EventClock LineEventClock }
LineConfig contains the configuration parameters for the line.
type LineConfigOption ¶
type LineConfigOption interface {
// contains filtered or unexported methods
}
LineConfigOption defines the interface required to update an option for Line and Lines.
type LineDirection ¶
type LineDirection int
LineDirection indicates the direction of a line.
const ( // LineDirectionUnknown indicate the line direction is unknown. LineDirectionUnknown LineDirection = iota // LineDirectionInput indicates the line is an input. LineDirectionInput // LineDirectionOutput indicates the line is an output. LineDirectionOutput )
type LineEdge ¶
type LineEdge int
LineEdge indicates the edges detected by the line.
const ( // LineEdgeNone indicates the line edge detection is disabled. LineEdgeNone LineEdge = iota // LineEdgeRising indicates the line has rising edge detection enabled. LineEdgeRising // LineEdgeFalling indicates the line has falling edge detection enabled. LineEdgeFalling // LineEdgeBoth indicates the line has both rising and falling edge // detection enabled. LineEdgeBoth = LineEdgeRising | LineEdgeFalling )
type LineEvent ¶
type LineEvent struct { // The line offset within the GPIO chip. Offset int // Timestamp indicates the time the event was detected. // // The timestamp is intended for accurately measuring intervals between // events. It is not guaranteed to be based on a particular clock. It has // been based on CLOCK_REALTIME, but from Linux v5.7 it is based on // CLOCK_MONOTONIC. Timestamp time.Duration // The type of state change event this structure represents. Type LineEventType // The seqno for this event in all events on all lines in this line request. // // Requires uAPI v2. Seqno uint32 // The seqno for this event in all events in this line. // // Requires uAPI v2. LineSeqno uint32 }
LineEvent represents a change in the state of a line.
type LineEventClock ¶
type LineEventClock int
LineEventClock indicates the source clock used to timestamp edge events.
const ( // LineEventClockMonotonic indicates the source clock is CLOCK_MONOTONIC. LineEventClockMonotonic LineEventClock = iota // LineEventClockRealtime indicates the source clock is CLOCK_REALTIME. LineEventClockRealtime )
type LineEventType ¶
type LineEventType int
LineEventType indicates the type of change to the line active state.
Note that for active low lines a low line level results in a high active state.
const ( // LineEventRisingEdge indicates an inactive to active event. LineEventRisingEdge LineEventType // LineEventFallingEdge indicates an active to inactive event. LineEventFallingEdge )
type LineInfo ¶
type LineInfo struct { // The line offset within the chip. Offset int // The system name for the line. Name string // A string identifying the requester of the line, if requested. Consumer string // The line is in use. Used bool // The configuration parameters for the line. Config LineConfig }
LineInfo contains a summary of publicly available information about the line.
type LineInfoChangeEvent ¶
type LineInfoChangeEvent struct { // Info is the updated line info. Info LineInfo // Timestamp indicates the time the event was detected. // // The timestamp is intended for accurately measuring intervals between // events. It is not guaranteed to be based on a particular clock, but from // Linux v5.7 it is based on CLOCK_MONOTONIC. Timestamp time.Duration // The type of info change event this structure represents. Type LineInfoChangeType }
LineInfoChangeEvent represents a change in the info a line.
type LineInfoChangeType ¶
type LineInfoChangeType int
LineInfoChangeType indicates the type of change to the line info.
const ( // LineRequested indicates the line has been requested. LineRequested LineInfoChangeType // LineReleased indicates the line has been released. LineReleased // LineReconfigured indicates the line configuration has changed. LineReconfigured )
type LineReqOption ¶
type LineReqOption interface {
// contains filtered or unexported methods
}
LineReqOption defines the interface required to provide an option for Line and Lines as part of a line request.
type Lines ¶
type Lines struct {
// contains filtered or unexported fields
}
Lines represents a collection of requested lines.
func RequestLines ¶
func RequestLines(chip string, offsets []int, options ...LineReqOption) (*Lines, error)
RequestLines requests control of a collection of lines on a chip.
If granted, control is maintained until the Lines are closed.
func (*Lines) Chip ¶
func (l *Lines) Chip() string
Chip returns the name of the chip from which the line was requested.
func (*Lines) Close ¶
func (l *Lines) Close() error
Close releases all resources held by the requested line.
Note that this includes waiting for any running event handler to return. As a consequence the Close must not be called from the context of the event handler - the Close should be called from a different goroutine.
func (*Lines) Reconfigure ¶
func (l *Lines) Reconfigure(options ...LineConfigOption) error
Reconfigure updates the configuration of the requested line(s).
Configuration for options other than those passed in remain unchanged.
Not valid for lines with edge detection enabled.
Requires Linux v5.5 or later.
func (*Lines) SetValues ¶
SetValues sets the current active state of the collection of lines.
Only valid for output lines.
All lines in the set are set at once. If insufficient values are provided then the remaining lines are set to inactive. If too many values are provided then the surplus values are ignored.
func (*Lines) UapiAbiVersion ¶
func (l *Lines) UapiAbiVersion() int
UapiAbiVersion returns the version of the GPIO uAPI the line is using.
type LinesOption ¶
type LinesOption struct {
// contains filtered or unexported fields
}
LinesOption specifies line options that are to be applied to a subset of the lines in a request.
func WithLines ¶
func WithLines(offsets []int, options ...SubsetLineConfigOption) LinesOption
WithLines specifies line options to be applied to a subset of the lines in a request.
The offsets should be a strict subset of the offsets provided to RequestLines(). Any offsets outside that set are ignored.
type OutputOption ¶
type OutputOption []int
OutputOption indicates the line direction should be set to an output.
func AsOutput ¶
func AsOutput(values ...int) OutputOption
AsOutput indicates that a line or lines be requested as an output.
The initial active state for the line(s) can optionally be provided. If fewer values are provided than lines then the remaining lines default to inactive.
This option overrides and clears any previous Input, RisingEdge, FallingEdge, BothEdges, or Debounce options.
type SubsetLineConfigOption ¶
type SubsetLineConfigOption interface {
// contains filtered or unexported methods
}
SubsetLineConfigOption defines the interface required to update an option for a subset of requested lines.