rpio

package module
v3.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2015 License: MIT Imports: 8 Imported by: 0

README

go-rpio

Native GPIO-Gophers for your Pi!

go-rpio is a Go library for accessing GPIO-pins on the Raspberry Pi.

It requires no external c libraries such as WiringPI or bcm2835.

There's a tiny bit of additional information over at my blog.

raspberrypi-blink

Releases

  • 1.0.0 - Supports original rpi A/B/B+
  • 2.0.0 - Adds support for rpi 2, by @akramer
  • 3.0.0 - Add support for /dev/gpiomem, by @dotdoom

Usage

import "github.com/stianeikeland/go-rpio"

Open memory range for GPIO access in /dev/mem

err := rpio.Open()

Initialize a pin, run basic operations. Pin refers to the bcm2835 pin, not the physical pin on the raspberry pi header. Pin 10 here is exposed on the pin header as physical pin 19.

pin := rpio.Pin(10)

pin.Output()       // Output mode
pin.High()         // Set pin High
pin.Low()          // Set pin Low
pin.Toggle()       // Toggle pin (Low -> High -> Low)

pin.Input()        // Input mode
res := pin.Read()  // Read state from pin (High / Low)

pin.Mode(rpio.Output)   // Alternative syntax
pin.Write(rpio.High)    // Alternative syntax

Pull up/down/off can be set using:

pin.PullUp()
pin.PullDown()
pin.PullOff()

pin.Pull(rpio.PullUp)

Unmap memory when done

rpio.Close()

Also see example examples/blinker/blinker.go

Other

Currently, it supports basic functionality such as:

  • Pin Direction (Input / Output)
  • Write (High / Low)
  • Read (High / Low)
  • Pull (Up / Down / Off)

Would be nice to add in the future:

  • PWM
  • I2C
  • SPI
  • etc...

It works by memory-mapping the bcm2835 gpio range, and therefore require root/administrative-rights to run.

Using without root

This library can utilize the new /dev/gpiomem memory range if available.

You will probably need to upgrade to the latest kernel (or wait for the next raspbian release) if you're missing /dev/gpiomem. You will also need to add a gpio group, add your user to the group, and then set up udev rules. I would recommend using create_gpio_user_permissions.py if you're unsure how to do this.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

Close unmaps GPIO memory

func Open

func Open() (err error)

Open and memory map GPIO memory range from /dev/mem . Some reflection magic is used to convert it to a unsafe []uint32 pointer

func PinMode

func PinMode(pin Pin, direction Direction)

PinMode sets the direction of a given pin (Input or Output)

func PullMode

func PullMode(pin Pin, pull Pull)

func TogglePin

func TogglePin(pin Pin)

Toggle a pin state (high -> low -> high) TODO: probably possible to do this much faster without read

func WritePin

func WritePin(pin Pin, state State)

WritePin sets a given pin High or Low by setting the clear or set registers respectively

Types

type Direction

type Direction uint8
const (
	Input Direction = iota
	Output
)

Pin direction, a pin can be set in Input or Output mode

type Pin

type Pin uint8

func (Pin) High

func (pin Pin) High()

Set pin High

func (Pin) Input

func (pin Pin) Input()

Set pin as Input

func (Pin) Low

func (pin Pin) Low()

Set pin Low

func (Pin) Mode

func (pin Pin) Mode(dir Direction)

Set pin Direction

func (Pin) Output

func (pin Pin) Output()

Set pin as Output

func (Pin) Pull

func (pin Pin) Pull(pull Pull)

Set a given pull up/down mode

func (Pin) PullDown

func (pin Pin) PullDown()

Pull down pin

func (Pin) PullOff

func (pin Pin) PullOff()

Disable pullup/down on pin

func (Pin) PullUp

func (pin Pin) PullUp()

Pull up pin

func (Pin) Read

func (pin Pin) Read() State

Read pin state (high/low)

func (Pin) Toggle

func (pin Pin) Toggle()

Toggle pin state

func (Pin) Write

func (pin Pin) Write(state State)

Set pin state (high/low)

type Pull

type Pull uint8
const (
	PullOff Pull = iota
	PullDown
	PullUp
)

Pull Up / Down / Off

type State

type State uint8
const (
	Low State = iota
	High
)

State of pin, High / Low

func ReadPin

func ReadPin(pin Pin) State

Read the state of a pin

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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