2014-02-16 23:11:53 +01:00
|
|
|
package gpio
|
|
|
|
|
2014-03-01 15:49:44 +01:00
|
|
|
import "github.com/kidoman/embd/host"
|
|
|
|
|
2014-02-16 23:11:53 +01:00
|
|
|
type Direction int
|
|
|
|
|
|
|
|
const (
|
2014-02-26 23:54:53 +01:00
|
|
|
In Direction = iota
|
|
|
|
Out
|
2014-02-16 23:11:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-02-26 23:54:53 +01:00
|
|
|
Low int = iota
|
2014-02-16 23:11:53 +01:00
|
|
|
High
|
|
|
|
)
|
|
|
|
|
2014-02-26 23:54:53 +01:00
|
|
|
type DigitalPin interface {
|
|
|
|
Write(val int) error
|
|
|
|
Read() (int, error)
|
2014-02-16 23:11:53 +01:00
|
|
|
|
2014-03-01 15:49:44 +01:00
|
|
|
SetDirection(dir Direction) error
|
2014-02-26 23:54:53 +01:00
|
|
|
ActiveLow(b bool) error
|
2014-02-16 23:11:53 +01:00
|
|
|
|
2014-03-02 07:39:57 +01:00
|
|
|
PullUp() error
|
|
|
|
PullDown() error
|
|
|
|
|
2014-02-26 23:54:53 +01:00
|
|
|
Close() error
|
2014-02-16 23:11:53 +01:00
|
|
|
}
|
|
|
|
|
2014-03-01 15:49:44 +01:00
|
|
|
type gpio interface {
|
2014-02-26 23:54:53 +01:00
|
|
|
DigitalPin(key interface{}) (DigitalPin, error)
|
2014-02-16 23:11:53 +01:00
|
|
|
|
2014-02-26 23:54:53 +01:00
|
|
|
Close() error
|
2014-02-16 23:11:53 +01:00
|
|
|
}
|
2014-03-01 15:49:44 +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)
|
|
|
|
}
|