### Install go-gpiocdev Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Installs the go-gpiocdev package for use in Go projects. For cross-compilation, it suggests fetching the package without building it immediately. ```shell go get github.com/warthog618/go-gpiocdev go get -d github.com/warthog618/go-gpiocdev ``` -------------------------------- ### Go GPIO v2 UAPI Usage Source: https://github.com/warthog618/go-gpiocdev/blob/master/uapi/README.md Demonstrates using the v2 UAPI for GPIO operations. This includes opening the device, getting chip and line info, requesting lines for output and input with events, reading events, getting/setting line values, and updating line configurations. ```go f, _ := os.OpenFile("/dev/gpiochip0", unix.O_CLOEXEC, unix.O_RDONLY) // get chip info ci, _ := uapi.GetChipInfo(f.Fd()) fmt.Print(ci) // get line info li, _ := uapi.GetLineInfo(f.Fd(), offset) fmt.Print(li) // request a line lr := uapi.LineRequest{ Lines: uint32(len(offsets)), Config: uapi.LineConfig{ Flags: uapi.LineFlagV2Output, }, // initialise Offsets, OutputValues and Consumer... } err := uapi.GetLine(f.Fd(), &lr) // request a line with events lr = uapi.LineRequest{ Lines: uint32(len(offsets)), Config: uapi.LineConfig{ Flags: uapi.LineFlagV2Input | uapi.LineFlagV2ActiveLow | uapi.LineFlagV2EdgeBoth, }, // initialise Offsets and Consumer... } err = uapi.GetLine(f.Fd(), &lr) if err != nil { // wait on lr.fd for events... // read event evt, _ := uapi.ReadLineEvent(uintptr(lr.Fd)) fmt.Print(evt) } // get values var values uapi.LineValues err = uapi.GetLineValuesV2(uintptr(lr.Fd), &values) // set values err = uapi.SetLineValuesV2(uintptr(lr.Fd), values) // update line config - change to outputs err = uapi.SetLineConfigV2(uintptr(lr.Fd), &uapi.LineConfig{ Flags: uapi.LineFlagV2Output, NumAttrs: 1, // initialise OutputValues... }) ``` -------------------------------- ### Go GPIO v1 UAPI Usage Source: https://github.com/warthog618/go-gpiocdev/blob/master/uapi/README.md Demonstrates using the v1 UAPI for GPIO operations. This includes opening the device, getting chip and line info, requesting lines for output and input with events, reading events, getting/setting line values, and updating line configurations. ```go f, _ := os.OpenFile("/dev/gpiochip0", unix.O_CLOEXEC, unix.O_RDONLY) // get chip info ci, _ := uapi.GetChipInfo(f.Fd()) fmt.Print(ci) // get line info li, _ := uapi.GetLineInfo(f.Fd(), offset) fmt.Print(li) // request a line hr := uapi.HandleRequest{ Lines: uint32(len(offsets)), Flags: uapi.HandleRequestOutput, // initialise Offsets, DefaultValues and Consumer... } err := uapi.GetLineHandle(f.Fd(), &hr) // request a line with events er := uapi.EventRequest{ Offset: uint32(offset), HandleFlags: uapi.HandleRequestActiveLow, EventFlags: uapi.EventRequestBothEdges, // initialise Consumer... } err = uapi.GetLineEvent(f.Fd(), &er) if err != nil { // wait on er.fd for events... // read event evt, _ := uapi.ReadEvent(uintptr(er.Fd)) fmt.Print(evt) } // get values var values uapi.HandleData err = uapi.GetLineValues(uintptr(er.Fd), &values) // set values values[0] = uint8(value) err = uapi.SetLineValues(uintptr(hr.Fd), values) // update line config - change to active low err = uapi.SetLineConfig(uintptr(hr.Fd), &uapi.HandleConfig{ Flags: uapi.HandleRequestInput, }) ``` -------------------------------- ### Get Line Info from Requested Line Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Retrieves information directly from a line object after it has been requested from the chip. ```go inf, _ := l.Info() ``` ```go infs, _ := ll.Info() ``` -------------------------------- ### Get Line Information Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Fetches information about a specific GPIO line from a chip. This includes line details but not its current value. ```go inf, _ := c.LineInfo(4) ``` ```go inf, _ := c.LineInfo(rpi.J8p7) // Using Raspberry Pi J8 mapping ``` -------------------------------- ### Get Chip Information (v1 & v2) Source: https://github.com/warthog618/go-gpiocdev/blob/master/uapi/README.md Retrieves information about the GPIO chip itself. This function is common to both v1 and v2 of the Linux GPIO UAPI. ```go package uapi // GetChipInfo returns information about the chip itself. func GetChipInfo(fd int) (*Info, error) ``` -------------------------------- ### Get Line Information (v1 & v2) Source: https://github.com/warthog618/go-gpiocdev/blob/master/uapi/README.md Retrieves information about a specific line on a GPIO chip. This function has different versions for v1 and v2 of the Linux GPIO UAPI. ```go package uapi // GetLineInfoV2 returns information about a particular line on the chip (v2). func GetLineInfoV2(fd int) (*LineInfo, error) ``` ```go package uapi // GetLineInfo returns information about a particular line on the chip (v1). func GetLineInfo(fd int) (*LineInfo, error) ``` -------------------------------- ### Get Line Values (v1 & v2) Source: https://github.com/warthog618/go-gpiocdev/blob/master/uapi/README.md Retrieves the current values of a set of GPIO lines. This functionality is available in both v1 and v2 of the Linux GPIO UAPI. ```go package uapi // GetLineValuesV2 returns the current value of a set of lines in an existing line request (v2). func GetLineValuesV2(lineFd int, req *LineValues) error ``` ```go package uapi // GetLineValues returns the current value of a set of lines in an existing handle or event request (v1). func GetLineValues(lineFd int, req *LineValues) error ``` -------------------------------- ### Initialize GPIO Chip Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Demonstrates how to initialize a GPIO chip using its device name. It also shows how to set a consumer label for the chip. ```go c, _ := gpiocdev.NewChip("gpiochip0") ``` ```go c, _ := gpiocdev.NewChip("gpiochip0", gpiocdev.WithConsumer("myapp")) ``` -------------------------------- ### Build and Run go-gpiocdev Tests Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Demonstrates how to compile and run tests for the go-gpiocdev library, including cross-compilation for ARM architectures. Tests require root privileges and specific kernel configurations. ```shell go test -c GOOS=linux GOARCH=arm GOARM=6 go test -c ``` -------------------------------- ### List Available GPIO Chips Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Retrieves a list of all currently available GPIO chips on the system. ```go cc := gpiocdev.Chips() ``` -------------------------------- ### Request GPIO Line and Toggle Value Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Demonstrates requesting an input GPIO line, reading its value, and then requesting an output line and setting its value based on the input. Error handling and resource release are omitted for brevity. ```Go import "github.com/warthog618/go-gpiocdev" ... in, _ := gpiocdev.RequestLine("gpiochip0", 2, gpiocdev.AsInput) val, _ := in.Value() out, _ := gpiocdev.RequestLine("gpiochip0", 3, gpiocdev.AsOutput(val)) ... ``` -------------------------------- ### Request Single GPIO Line Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Shows how to request a single GPIO line from a specific chip. The line can be requested in its existing state or with initial configuration options like setting it as an output with a specific initial value. ```Go import "github.com/warthog618/go-gpiocdev" ... l, _ := gpiocdev.RequestLine("gpiochip0", 4) // in its existing state ... l, _ := c.RequestLine(4) // from a Chip object ... l, _ := c.RequestLine(rpi.J8p7) // using Raspberry Pi J8 mapping ... l, _ := gpiocdev.RequestLine("gpiochip0", 4, gpiocdev.AsOutput(1)) // as an output line ``` -------------------------------- ### Run Benchmarks on Raspberry Pi Zero W Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Executes all benchmarks for the go-gpiocdev library on a Raspberry Pi Zero W, reporting performance metrics such as operations per second (ns/op). The results indicate performance characteristics for various GPIO operations. ```Shell $ ./go-gpiocdev.test -test.bench=.*\ngoos: linux\ngoarch: arm\npkg: github.com/warthog618/go-gpiocdev\nBenchmarkChipNewClose 248 4381075 ns/op\nBenchmarkLineInfo 24651 47278 ns/op\nBenchmarkLineReconfigure 20312 55273 ns/op\nBenchmarkLineValue 71774 14933 ns/op\nBenchmarkLinesValues 54920 24659 ns/op\nBenchmarkLineSetValue 73359 16501 ns/op\nBenchmarkLinesSetValues 53557 21056 ns/op\nBenchmarkInterruptLatency 105 10407929 ns/op\nPASS ``` -------------------------------- ### Set Multiple GPIO Line Values Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Shows how to set the values for a collection of GPIO lines simultaneously using the SetValues method, accepting a slice of integers representing the desired states. ```go ll.SetValues([]int{0, 1, 0, 1}) // Set a collection of lines ``` -------------------------------- ### Run Benchmarks on Raspberry Pi 4 Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Executes all benchmarks for the go-gpiocdev library on a Raspberry Pi 4, reporting performance metrics such as operations per second (ns/op). This benchmark suite helps in understanding the library's performance on more powerful hardware. ```Shell $ ./go-gpiocdev.test -test.bench=.*\ngoos: linux\ngoarch: arm\npkg: github.com/warthog618/go-gpiocdev\nBenchmarkChipNewClose-4 9727 118291 ns/op\nBenchmarkLineInfo-4 185316 6104 ns/op\nBenchmarkLineReconfigure-4 364795 3205 ns/op\nBenchmarkLineValue-4 1072785 1061 ns/op\nBenchmarkLinesValues-4 816200 1428 ns/op\nBenchmarkLineSetValue-4 1015972 1150 ns/op\nBenchmarkLinesSetValues-4 715154 1717 ns/op\nBenchmarkInterruptLatency-4 18439 61145 ns/op\nPASS ``` -------------------------------- ### Watch GPIO Line Edge Events Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Illustrates how to set up an edge watch on an input GPIO line to trigger a handler function on rising, falling, or both edges. The handler receives LineEvent details. ```go func handler(evt gpiocdev.LineEvent) { // handle edge event } l, _ = c.RequestLine(rpi.J8p7, gpiocdev.WithEventHandler(handler), gpiocdev.WithBothEdges) ``` -------------------------------- ### Complex GPIO Line Configuration Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Shows how to apply different configurations to subsets of requested GPIO lines using the WithLines option, such as setting specific lines to active low while others are standard output. ```go ll, _ = c.RequestLines([]int{0, 1, 2, 3}, gpiocdev.AsOutput(0, 0, 1, 1), gpiocdev.WithLines([]int{0, 3}, gpiocdev.AsActiveLow), gpiocdev.AsOpenDrain) ll.Reconfigure(gpiocdev.WithLines([]int{0}, gpiocdev.AsActiveHigh)) ll.Reconfigure(gpiocdev.WithLines([]int{3}, gpiocdev.Defaulted)) ``` -------------------------------- ### Release GPIO Lines Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Demonstrates the proper way to release requested GPIO lines and collections of lines to free up system resources. This should be called when the lines are no longer needed. ```Go ... l.Close() ll.Close() ... ``` -------------------------------- ### Request GPIO Lines (v1 & v2) Source: https://github.com/warthog618/go-gpiocdev/blob/master/uapi/README.md Requests a set of GPIO lines, returning a file handle for further ioctl commands. This operation is supported in both v1 and v2 of the GPIO UAPI. The lines remain reserved until the returned file descriptor is closed. ```go package uapi // GetLine requests a set of lines, and returns a file handle for ioctl commands. // The set may be any subset of the lines supported by the chip, including a single line. // This may be used for both input and output lines. // The lines remain reserved by the caller until the returned fd is closed. func GetLine(fd int, req *LineRequest) (*LineHandle, error) ``` ```go package uapi // GetLineHandle requests a set of lines, and returns a file handle for ioctl commands (v1). // The set may be any subset of the lines supported by the chip, including a single line. // This may be used for both input and output lines. // The lines remain reserved by the caller until the returned fd is closed. func GetLineHandle(fd int, req *LineRequest) (*LineHandle, error) ``` -------------------------------- ### Request Multiple GPIO Lines Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Illustrates requesting multiple GPIO lines from the same chip as a collection. This can be done using the gpiocdev.RequestLines function or the Chip.RequestLines method, specifying configuration options for each line. ```Go import "github.com/warthog618/go-gpiocdev" ... ll, _ := gpiocdev.RequestLines("gpiochip0", []int{0, 1, 2, 3}, gpiocdev.AsOutput(0, 0, 1, 1)) ... ll, _ := c.RequestLines([]int{0, 1, 2, 3}, gpiocdev.AsOutput(0, 0, 1, 1)) ``` -------------------------------- ### Configure Output Drive Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Configures the drive type for an output GPIO line, such as open-drain or open-source. The default is push-pull. ```go l,_ := c.RequestLine(4, gpiocdev.AsOpenDrain) // during request ``` ```go l.Reconfigure(gpiocdev.AsOpenSource) // once requested ``` -------------------------------- ### Request Individual Input Line with Edge Detection (v1) Source: https://github.com/warthog618/go-gpiocdev/blob/master/uapi/README.md Requests an individual input GPIO line with edge detection enabled, returning a file handle for ioctl commands and to receive edge events. This is specific to v1 of the Linux GPIO UAPI and can only be used on input lines. The line remains reserved until the file descriptor is closed. ```go package uapi // GetLineEvent requests an individual input line with edge detection enabled, and returns a file handle for ioctl commands and to return edge events. // Events can only be requested on input lines. // The line remains reserved by the caller until the returned fd is closed. func GetLineEvent(fd int, req *LineEventRequest) (*LineEventHandle, error) ``` -------------------------------- ### Watch Line Info Changes Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Sets up a watch to monitor changes in a line's configuration. It does not monitor the line's value (active/inactive). ```go func infoChangeHandler( evt gpiocdev.LineInfoChangeEvent) { // handle change in line info } inf, _ := c.WatchLineInfo(4, infoChangeHandler) ``` -------------------------------- ### Configure Line Direction Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Sets the direction of a GPIO line to either input or output. For output, an initial value can also be specified. ```go l, _ := c.RequestLine(4, gpiocdev.AsInput) // during request ``` ```go l.Reconfigure(gpiocdev.AsInput) // set direction to Input ``` ```go l.Reconfigure(gpiocdev.AsOutput(0)) // set direction to Output (and value to inactive) ``` -------------------------------- ### Set Line Configuration (v1 & v2) Source: https://github.com/warthog618/go-gpiocdev/blob/master/uapi/README.md Updates the configuration of GPIO lines within an existing request. This feature is available in both v1 and v2 of the Linux GPIO UAPI. ```go package uapi // SetLineConfigV2 updates the configuration of the lines in an existing line request (v2). func SetLineConfigV2(lineFd int, req *LineConfig) error ``` ```go package uapi // SetLineConfig updates the configuration of the lines in an existing handle request (v1). func SetLineConfig(lineFd int, req *LineConfig) error ``` -------------------------------- ### Configure Debounce Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Applies debouncing to an input GPIO line, either in hardware or by the kernel. Requires Linux 5.10 or later. ```go period := 10 * time.Millisecond l, _ = c.RequestLine(4, gpiocdev.WithDebounce(period))// during request ``` ```go l.Reconfigure(gpiocdev.WithDebounce(period)) // once requested ``` -------------------------------- ### Set GPIO Line Value Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Demonstrates how to set the value of a single GPIO line to active (1) or inactive (0) using the SetValue method. ```go l.SetValue(1) // Set line active l.SetValue(0) // Set line inactive ``` -------------------------------- ### Reconfigure GPIO Line Direction Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Demonstrates reconfiguring a GPIO line's direction to input or output. For output, it also shows setting initial active and inactive values. ```go l.Reconfigure(gpiocdev.AsInput) // set direction to Input ll.Reconfigure(gpiocdev.AsOutput(1, 0)) // set direction to Output (and values to active and inactive) ``` -------------------------------- ### Read GPIO Line Value Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Shows how to read the current state (active/inactive) of a requested GPIO line using the Value() method. For collections of lines, the Values() method reads the state of all lines simultaneously into a provided buffer. ```Go ... r, _ := l.Value() // Read state from line (active / inactive) ... rr := []int{0, 0, 0, 0} // buffer to read into... ll.Values(rr) // Read the state of a collection of lines ``` -------------------------------- ### Configure Line Bias Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Sets the bias state for a GPIO line, such as pull-up, pull-down, or disabled. Requires Linux 5.5 or later. ```go l, _ = c.RequestLine(4, gpiocdev.WithPullUp) // during request ``` ```go l.Reconfigure(gpiocdev.WithBiasDisabled) // once requested ``` -------------------------------- ### Configure Active Level Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Sets the active level for a GPIO line, either during request or reconfiguration. Lines are typically active high by default. ```go l, _ := c.RequestLine(4, gpiocdev.AsActiveLow) // during request ``` ```go l.Reconfigure(gpiocdev.AsActiveHigh) // once requested ``` -------------------------------- ### Set Line Values (v1 & v2) Source: https://github.com/warthog618/go-gpiocdev/blob/master/uapi/README.md Sets the current values for a set of GPIO lines. This operation is supported in both v1 and v2 of the Linux GPIO UAPI. ```go package uapi // SetLineValuesV2 sets the current value of a set of lines in an existing line request (v2). func SetLineValuesV2(lineFd int, req *LineValues) error ``` ```go package uapi // SetLineValues sets the current value of a set of lines in an existing handle request (v1). func SetLineValues(lineFd int, req *LineValues) error ``` -------------------------------- ### Remove GPIO Line Edge Watch Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Explains how to disable edge detection for a GPIO line by either closing the line or reconfiguring it without edge detection. ```go l.Close() l.Reconfigure(gpiocdev.WithoutEdges) ``` -------------------------------- ### Unwatch Line Info Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Cancels a previously set line information watch. Alternatively, closing the chip also cancels all watches. ```go c.UnwatchLineInfo(4) ``` -------------------------------- ### Watch Line Information Changes (v1 & v2) Source: https://github.com/warthog618/go-gpiocdev/blob/master/uapi/README.md Adds or removes a watch for changes to the information of a specific GPIO line on a chip. This functionality is present in both v1 and v2 of the Linux GPIO UAPI. ```go package uapi // WatchLineInfoV2 adds a watch for changes to the info of a particular line on the chip (v2). func WatchLineInfoV2(fd int, lineOffset uint32) error ``` ```go package uapi // WatchLineInfo adds a watch for changes to the info of a particular line on the chip (v1). func WatchLineInfo(fd int, lineOffset uint32) error ``` ```go package uapi // UnwatchLineInfo removes a watch for changes to the info of a particular line on the chip. func UnwatchLineInfo(fd int, lineOffset uint32) error ``` -------------------------------- ### Close GPIO Chip Source: https://github.com/warthog618/go-gpiocdev/blob/master/README.md Closes a GPIO chip to release its resources. This action does not affect any lines previously requested from the chip. ```go c.Close() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.