rpigpio

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 6 Imported by: 1

README

rpigpio GoDoc

Package for controlling GPIOs on the Raspberry Pi using plain GO.

About

This package provides access to the GPIO pins on any Raspberry Pi. The GPIOs are switch and controlled by directly writing to the corresponding registers.

The used Raspberry Pi hardware is automatically detected to accommodate different hardware addresses. For this to work, a curated list is included via the package github.com/DerLukas15/rpihardware.

Installation

go get github.com/DerLukas15/rpigpio

Usage

First initialize the package. This will determine the hardware. (You can also define your pins and initialize later. However the initialization has to be done before any methods are called.)

err := rpigpio.Initialize()

Now create the pin object. Use the GPIO numbers which you can find online and not the physical pin number.

pin, err := rpigpio.NewPin(17)
Typical uses
Output

Set the 'direction' of the pin:

err := pin.Mode(rpigpio.ModeOutput)

Set the output high:

err := pin.Set(1)

Set the output low:

err := pin.Set(0)

If the direction of the pin is an input, the Set method won't do anything.

Input

Set the 'direction' of the pin:

err := pin.Mode(rpigpio.ModeInput)

Get the current value: Now set the 'direction' of the pin:

value, err := pin.Get()

If the direction of the pin is an output, the 'Get' method will always return 0.

Extended uses
Alternate modes

To activate alternate modes for a pin use (e.g. for alternate mode 1)

err := pin.Mode(rpigpio.ModeAlternate1)
Pull-Up / Pull-Down

To activate the build in Pull-Up or Pull-Down resistors use:

For Pull-Up

err := pin.Pull(rpigpio.PullUp)

For Pull-Down

err := pin.Pull(rpigpio.PullDown)

To disable Pull-Up or Pull-Down

err := pin.Pull(rpigpio.PullOff)

The current status of the Pull-Up / Pull-Down resistors can not be read due to hardware restrictions.

Event Detects

There are different detect methods for events described below. To get the result of any event use:

hasEvent, err := pin.Event()

Reading an event will clear the event to be ready for the next one. To prevent the automatic event clearing set

rpigpio.NoEventClearing = true

Don't forget to clear the events by yourself

err := pin.ClearEvent()

If an event is still valid during an attempt to clear it, the clear won't do anything.

Rising Edge Detect

Enable rising edge detection with

err := pin.RisingEdgeDetect(true)

Any transition from 0 -> 1 will trigger the event.

Can be used together with 'Falling Edge Detect'

An asynchronous version of this method is available. This will sample outside of the system clock making it perfect for use with very short edges. Use pin.ARisingEdgeDetect(true)

Falling Edge Detect

Enable rising edge detection with

err := pin.FallingEdgeDetect(true)

Any transition from 1 -> 0 will trigger the event.

Can be used together with 'Rising Edge Detect'

An asynchronous version of this method is available. This will sample outside of the system clock making it perfect for use with very short edges. Use pin.FallingEdgeDetect(true)

High Detect

Will set the event once the pin is high:

err := pin.HighDetect(true)
Low Detect

Will set the event once the pin is low:

err := pin.LowDetect(true)

License

MIT, see LICENSE

Documentation

Overview

Package rpigpio grants access to the GPIOs on a Raspberry Pi completly written in GO. All actions write directly to the registers.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotInitialized = errors.New("Package not initialized or unsupported hardware. Check error during initialize.")
	ErrWrongPin       = errors.New("Pin is not defined. Max 53")
)

Errors

View Source
var Debug bool

Enable Debug output

View Source
var NoEventClearing bool

Functions

func Initialize

func Initialize() error

Initialize checks the hardware and opens the GPIO device for usage.

Types

type Mode

type Mode uint32

Mode is used to set the mode of a pin

const (
	ModeIn         Mode = 0b000
	ModeOut        Mode = 0b001
	ModeAlternate0 Mode = 0b100
	ModeAlternate1 Mode = 0b101
	ModeAlternate2 Mode = 0b110
	ModeAlternate3 Mode = 0b111
	ModeAlternate4 Mode = 0b011
	ModeAlternate5 Mode = 0b010
)

Defined modes

type Pin

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

Pin defines a GPIO pin.

func NewPin

func NewPin(pinNum uint32) (*Pin, error)

NewPin creates a new GPIO pin. Do not use physical pin number!

func (*Pin) AFallingEdgeDetect

func (p *Pin) AFallingEdgeDetect(targetStatus bool) error

AFallingEdgeDetect sets the falling edge detection of the GPIO pin. Async

func (*Pin) ARisingEdgeDetect

func (p *Pin) ARisingEdgeDetect(targetStatus bool) error

ARisingEdgeDetect sets the rising edge detection of the GPIO pin. Async

func (*Pin) ClearEvent

func (p *Pin) ClearEvent() error

ClearEvent clears a possible event. If event is still active, ClearEvent will have no effect.

func (*Pin) Event

func (p *Pin) Event() (bool, error)

Event returns if there was / is an event.

func (*Pin) FallingEdgeDetect

func (p *Pin) FallingEdgeDetect(targetStatus bool) error

FallingEdgeDetect sets the falling edge detection of the GPIO pin.

func (*Pin) Get

func (p *Pin) Get() (bool, error)

Get returns if the pin is high.

func (*Pin) HighDetect

func (p *Pin) HighDetect(targetStatus bool) error

HighDetect sets the high detection of the GPIO pin.

func (*Pin) Is

func (p *Pin) Is(pinNum uint32) bool

Is checks if the given pinNum corresponds to this pin.

func (*Pin) LowDetect

func (p *Pin) LowDetect(targetStatus bool) error

LowDetect sets the low detection of the GPIO pin.

func (*Pin) Mode

func (p *Pin) Mode(targetMode Mode) error

Mode sets the mode of the GPIO pin.

func (*Pin) Pull

func (p *Pin) Pull(targetMode PullMode) error

Pull sets the Pull-Up or Pull-Down resistors of the GPIO pin.

func (*Pin) RisingEdgeDetect

func (p *Pin) RisingEdgeDetect(targetStatus bool) error

RisingEdgeDetect sets the rising edge detection of the GPIO pin.

func (*Pin) Set

func (p *Pin) Set(targetValue int) error

Set sets the value of the GPIO pin.

func (*Pin) UInt32

func (p *Pin) UInt32() uint32

UInt32 returns the pinNum as an uint32.

type PullMode

type PullMode uint32

PullMode is used to set the Pull-Up and Pull-Down resistors

const (
	PullOff  PullMode = 0b00
	PullDown PullMode = 0b01
	PullUp   PullMode = 0b10
)

Defined pull modes

Jump to

Keyboard shortcuts

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