1
0
mirror of https://github.com/kidoman/embd synced 2024-11-11 12:08:53 +01:00
embd/gpio/gpio.go

92 lines
1.3 KiB
Go
Raw Normal View History

2014-02-16 23:11:53 +01:00
package gpio
import "github.com/kidoman/embd/host"
2014-02-16 23:11:53 +01:00
type Direction int
const (
In Direction = iota
Out
2014-02-16 23:11:53 +01:00
)
const (
Low int = iota
2014-02-16 23:11:53 +01:00
High
)
type DigitalPin interface {
Write(val int) error
Read() (int, error)
2014-02-16 23:11:53 +01:00
SetDirection(dir Direction) error
ActiveLow(b bool) error
2014-02-16 23:11:53 +01:00
2014-03-02 07:39:57 +01:00
PullUp() error
PullDown() error
Close() error
2014-02-16 23:11:53 +01:00
}
type gpio interface {
DigitalPin(key interface{}) (DigitalPin, error)
2014-02-16 23:11:53 +01:00
Close() error
2014-02-16 23:11:53 +01:00
}
var instance gpio
func Open() error {
desc, err := host.Describe()
if err != nil {
return err
}
instance = desc.GPIO().(gpio)
return nil
}
func Close() error {
return instance.Close()
}
func NewDigitalPin(key interface{}) (DigitalPin, error) {
return instance.DigitalPin(key)
}
func DigitalWrite(key interface{}, val int) error {
pin, err := NewDigitalPin(key)
if err != nil {
return err
}
return pin.Write(val)
}
func DigitalRead(key interface{}) (int, error) {
pin, err := NewDigitalPin(key)
if err != nil {
return 0, err
}
return pin.Read()
}
func SetDirection(key interface{}, dir Direction) error {
pin, err := NewDigitalPin(key)
if err != nil {
return err
}
return pin.SetDirection(dir)
}
func ActiveLow(key interface{}, b bool) error {
pin, err := NewDigitalPin(key)
if err != nil {
return err
}
return pin.ActiveLow(b)
}