MCPcopy Index your code
hub / github.com/brian-armstrong/gpio

github.com/brian-armstrong/gpio @v1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.0 ↗ · + Follow
42 symbols 73 edges 5 files 16 documented · 38%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

GPIO

This is a Go library for general purpose pins on Linux devices which support /sys/class/gpio. This implementation conforms to the spec. An advantage of using /sys/class/gpio is that we can receive interrupt-like notifications from the kernel when an input changes, rather than polling an input periodically. See the notes on Watcher for more info.

I have only tested it so far on Raspberry Pi but it should also work on similar systems like the Beaglebone. If you test this library on another system please tell me so that I can confirm it -- I'll give you credit here for the confirmation.

Note that the GPIO numbers we want here as the CPU/kernel knows them, not as they may be marked on any external hardware headers.

Input

Call pin := gpio.NewInput(number) to create a new input with the given pin numbering. You can then access the value of this pin with pin.Read(), which returns 0 when the pin's value is logic low and 1 when high.

If you are only concerned with when the pin's value changes, consider using gpio.Watcher instead.

Output

Call pin := gpio.NewOutput(number, high), where high is a bool that describes the initial value of the pin -- set to false if you'd like the pin to initialize low, and true if you'd like it to initialize high.

Once you have a pin, you can change its value with pin.Low() and pin.High().

Watcher

The Watcher is a type which listens on the GPIO pins you specify and then notifies you when the values of those pins change. It uses a select() call so that it does not need to actively poll, which saves CPU time and gives you better latencies from your inputs.

Here is an example of how to use the Watcher.

watcher := gpio.NewWatcher()
watcher.AddPin(22)
watcher.AddPin(27)
defer watcher.Close()

go func() {
    for {
        pin, value := watcher.Watch()
        fmt.Printf("read %d from gpio %d\n", value, pin)
    }
}()

This example would print once each time the value read on either pin 22 or 27 changes. It also prints each pin once when starting.

License

3-clause BSD

Core symbols most depended-on inside this repo

Close
called by 4
io.go
exportGPIO
called by 2
sysfs.go
setDirection
called by 2
sysfs.go
openPin
called by 2
sysfs.go
writePin
called by 2
sysfs.go
Read
called by 2
io.go
removeFd
called by 2
watcher.go
setEdgeTrigger
called by 1
sysfs.go

Shape

Method 22
Function 12
Struct 4
TypeAlias 4

Languages

Go100%

Modules by API surface

watcher.go24 symbols
sysfs.go9 symbols
io.go7 symbols
select_linux.go1 symbols
select_darwin.go1 symbols

For agents

$ claude mcp add gpio \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact