• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

stianeikeland/go-rpio: Raspberry Pi GPIO library for go-lang

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

stianeikeland/go-rpio

开源软件地址:

https://github.com/stianeikeland/go-rpio

开源编程语言:

Go 100.0%

开源软件介绍:

go-rpio

Native GPIO-Gophers for your Pi!

Documentation: GoDoc

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 - Adds support for /dev/gpiomem, by @dotdoom
  • 4.0.0 - Adds support for PWM and Clock modes, by @drahoslove
  • 4.1.0 - Adds support for edge detection, by @drahoslove
  • 4.2.0 - Faster write and toggle of output pins, by @drahoslove
  • 4.3.0 - Adds support for SPI, by @drahoslove
  • 4.4.0 - Support for disabling interrupts (workaround for #35), by @drahoslove
  • 4.5.0 - Improve rpi 4 support, by @wfd3
  • 4.6.0 - Adds Balanced PWM mode, by @youngkin

Usage

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

If you're using an older go.mod incompatible you should instead use:

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

SPI

setup/teardown

  • rpio.SpiBegin(rpio.Spi0) has to be called first before using any Spi func. It will change pin modes to Spi and initialize default setting.
  • rpio.SpiEnd(rpio.Spi0) should be called at the end, it will switch pin modes to Input.

transferring data

  • rpio.SpiTransmit(byte) or rpio.SpiTransmit(bytes...) will transmit byte or bytes to slave.
  • rpio.SpiReceive(n) will return n bytes received from slave.
  • rpio.SpiExchange(buffer) will simultaneously transmit data from the buffer to slave and data from slave to the same buffer in full duplex way.

settings

  • rpio.SpiSpeed(hz) will set transmit speed of SPI
  • rpio.SpiChipSelect(n) will select chip/slave (ce0, ce1, or ce2) to which transferring will be done
  • rpio.SpiChipSelectPolarity(n, pol) set chip select polarity (low enabled is used by default which usually works most of the time)
  • rpio.SpiMode(cpol, cpha) set clock/communication mode (=combination of clock polarity and clock phase; cpol=0, cpha=0 is used by default which usually works most of the time)

Other

Currently, it supports basic functionality such as:

  • Pin Direction (Input / Output)
  • Write (High / Low)
  • Read (High / Low)
  • Pull (Up / Down / Off)
  • PWM (hardware, on supported pins)
  • Clock
  • Edge detection

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.

PWM modes will still require root.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
OpenBazaar/openbazaar-go: OpenBazaar 2.0 Server Daemon in Go发布时间:2022-06-13
下一篇:
kkdai/project52: 52 weeks, 52 Go projects发布时间:2022-06-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap