Documentation ¶
Overview ¶
Package gpio provides GPIO access on the Raspberry Pi (rev 2 and later).
Supports simple operations such as: - Pin mode/direction (input/output) - Pin write (high/low) - Pin read (high/low) - Pull up/down/off
The package intentionally does not support:
- the obsoleted rev 1 PCB (no longer worth the effort)
- active low (to prevent confusion this package reflects only the actual hardware levels)
Example of use:
gpio.Open() defer gpio.Close() pin := gpio.NewPin(gpio.J8p7) pin.Low() pin.Output() for { pin.Toggle() time.Sleep(time.Second) }
The library uses the raw BCM2835 pin numbers, not the ports as they are mapped on the J8 output pins for the Raspberry Pi. A mapping from J8 to BCM is provided for those wanting to use the J8 numbering.
See the spec for full details of the BCM2835 controller: http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
Index ¶
- Constants
- Variables
- func Close() error
- func Open() (err error)
- type Chipset
- type Edge
- type Level
- type Mode
- type Pin
- func (pin *Pin) High()
- func (pin *Pin) Input()
- func (pin *Pin) Low()
- func (pin *Pin) Mode() Mode
- func (pin *Pin) Output()
- func (pin *Pin) Pin() int
- func (pin *Pin) PullDown()
- func (pin *Pin) PullNone()
- func (pin *Pin) PullUp()
- func (pin *Pin) Read() (level Level)
- func (pin *Pin) SetMode(mode Mode)
- func (pin *Pin) SetPull(pull Pull)
- func (pin *Pin) Shadow() Level
- func (pin *Pin) Toggle()
- func (p *Pin) Unwatch()
- func (p *Pin) Watch(edge Edge, handler func(*Pin)) error
- func (pin *Pin) Write(level Level)
- type Pull
- type Watcher
Constants ¶
const ( J8p27 = iota J8p28 J8p3 J8p5 J8p7 J8p29 J8p31 J8p26 J8p24 J8p21 J8p19 J8p23 J8p32 J8p33 J8p8 J8p10 J8p36 J8p11 J8p12 J8p35 J8p38 J8p40 J8p15 J8p16 J8p18 J8p22 J8p37 J8p13 MaxGPIOPin )
Convenience mapping from J8 pinouts to BCM pinouts.
const ( GPIO2 = J8p3 GPIO3 = J8p5 GPIO4 = J8p7 GPIO5 = J8p29 GPIO6 = J8p31 GPIO7 = J8p26 GPIO8 = J8p24 GPIO9 = J8p21 GPIO10 = J8p19 GPIO11 = J8p23 GPIO12 = J8p32 GPIO13 = J8p33 GPIO14 = J8p8 GPIO15 = J8p10 GPIO16 = J8p36 GPIO17 = J8p11 GPIO18 = J8p12 GPIO19 = J8p35 GPIO20 = J8p38 GPIO21 = J8p40 GPIO22 = J8p15 GPIO23 = J8p16 GPIO24 = J8p18 GPIO25 = J8p22 GPIO26 = J8p37 GPIO27 = J8p13 )
GPIO aliases to J8 pins
const ( // MaxGPIOInterrupt is the maximum pin number. MaxGPIOInterrupt = MaxGPIOPin )
Variables ¶
var ( // ErrTimeout indicates the operation could not be performed within the // expected time. ErrTimeout = errors.New("timeout") // ErrBusy indicates the operation is already active on the pin. ErrBusy = errors.New("pin already in use") )
var ( // ErrAlreadyOpen indicates the mem is already open. ErrAlreadyOpen = errors.New("already open") )
Functions ¶
Types ¶
type Chipset ¶ added in v0.5.0
type Chipset int
Chipset identifies the GPIO chip.
const ( // BCM2835 indicates the chipset is BCM2825 or compatible. BCM2835 Chipset // BCM2711 indicates the chipset is BCM2711. BCM2711 )
type Edge ¶
type Edge string
Edge represents the change in Pin level that triggers an interrupt.
const ( // EdgeNone indicates no level transitions will trigger an interrupt EdgeNone Edge = "none" // EdgeRising indicates an interrupt is triggered when the pin transitions from low to high. EdgeRising Edge = "rising" // EdgeFalling indicates an interrupt is triggered when the pin transitions from high to low. EdgeFalling Edge = "falling" // EdgeBoth indicates an interrupt is triggered when the pin changes level. EdgeBoth Edge = "both" )
type Pin ¶
type Pin struct {
// contains filtered or unexported fields
}
Pin represents a single GPIO pin.
func (*Pin) PullDown ¶
func (pin *Pin) PullDown()
PullDown sets the pull state of the Pin to PullDown.
func (*Pin) PullNone ¶
func (pin *Pin) PullNone()
PullNone disables pullup/down on pin, leaving it floating.
func (*Pin) SetPull ¶
SetPull sets the pull up/down mode for a Pin. Unlike the mode, the pull value cannot be read back from hardware and so must be remembered by the caller.
func (*Pin) Shadow ¶
Shadow returns the value of the last write to an output pin or the last read on an input pin.
type Watcher ¶
type Watcher struct { // Guards the following, and sysfs interactions. sync.Mutex // contains filtered or unexported fields }
Watcher monitors the pins for level transitions that trigger interrupts.
func NewWatcher ¶
func NewWatcher() *Watcher
NewWatcher creates a goroutine that watches Pins for transitions that trigger interrupts.
func (*Watcher) RegisterPin ¶
RegisterPin creates a watch on the given pin.
The pin can only be registered once. Subsequent registers, without an Unregister, will return an error.
func (*Watcher) UnregisterPin ¶
UnregisterPin removes any watch on the Pin.