2014-02-26 23:54:53 +01:00
|
|
|
package gpio
|
|
|
|
|
|
|
|
import (
|
2014-03-02 07:39:57 +01:00
|
|
|
"errors"
|
2014-02-26 23:54:53 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/kidoman/embd/gpio"
|
|
|
|
)
|
|
|
|
|
|
|
|
type digitalPin struct {
|
|
|
|
n int
|
|
|
|
|
|
|
|
dir *os.File
|
|
|
|
val *os.File
|
|
|
|
activeLow *os.File
|
|
|
|
edge *os.File
|
|
|
|
}
|
|
|
|
|
2014-03-01 15:49:44 +01:00
|
|
|
func newDigitalPin(n int) (*digitalPin, error) {
|
2014-03-02 07:39:57 +01:00
|
|
|
p := &digitalPin{n: n}
|
2014-03-01 15:49:44 +01:00
|
|
|
if err := p.init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return p, nil
|
2014-02-26 23:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-03-01 15:49:44 +01:00
|
|
|
func (p *digitalPin) init() error {
|
|
|
|
var err error
|
2014-02-26 23:54:53 +01:00
|
|
|
if p.dir, err = p.directionFile(); err != nil {
|
2014-03-01 15:49:44 +01:00
|
|
|
return err
|
2014-02-26 23:54:53 +01:00
|
|
|
}
|
|
|
|
if p.val, err = p.valueFile(); err != nil {
|
2014-03-01 15:49:44 +01:00
|
|
|
return err
|
2014-02-26 23:54:53 +01:00
|
|
|
}
|
|
|
|
if p.activeLow, err = p.activeLowFile(); err != nil {
|
2014-03-01 15:49:44 +01:00
|
|
|
return err
|
2014-02-26 23:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-03-01 15:49:44 +01:00
|
|
|
return nil
|
2014-02-26 23:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) basePath() string {
|
|
|
|
return fmt.Sprintf("/sys/class/gpio/gpio%v", p.n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) openFile(path string) (*os.File, error) {
|
|
|
|
return os.OpenFile(path, os.O_RDWR, os.ModeExclusive)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) directionFile() (*os.File, error) {
|
|
|
|
return p.openFile(path.Join(p.basePath(), "direction"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) valueFile() (*os.File, error) {
|
|
|
|
return p.openFile(path.Join(p.basePath(), "value"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) activeLowFile() (*os.File, error) {
|
|
|
|
return p.openFile(path.Join(p.basePath(), "active_low"))
|
|
|
|
}
|
|
|
|
|
2014-03-01 15:49:44 +01:00
|
|
|
func (p *digitalPin) SetDirection(dir gpio.Direction) error {
|
2014-02-26 23:54:53 +01:00
|
|
|
str := "in"
|
|
|
|
if dir == gpio.Out {
|
|
|
|
str = "out"
|
|
|
|
}
|
2014-03-01 15:49:44 +01:00
|
|
|
_, err := p.dir.WriteString(str)
|
2014-03-02 07:39:57 +01:00
|
|
|
return err
|
2014-02-26 23:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-03-01 15:49:44 +01:00
|
|
|
func (p *digitalPin) Read() (int, error) {
|
2014-02-26 23:54:53 +01:00
|
|
|
buf := make([]byte, 1)
|
2014-03-01 15:49:44 +01:00
|
|
|
if _, err := p.val.Read(buf); err != nil {
|
|
|
|
return 0, err
|
2014-02-26 23:54:53 +01:00
|
|
|
}
|
2014-03-01 15:49:44 +01:00
|
|
|
var val int
|
2014-02-26 23:54:53 +01:00
|
|
|
if buf[0] == '1' {
|
|
|
|
val = 1
|
|
|
|
}
|
2014-03-01 15:49:44 +01:00
|
|
|
return val, nil
|
2014-02-26 23:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-03-01 15:49:44 +01:00
|
|
|
func (p *digitalPin) Write(val int) error {
|
2014-02-26 23:54:53 +01:00
|
|
|
str := "0"
|
|
|
|
if val == gpio.High {
|
|
|
|
str = "1"
|
|
|
|
}
|
2014-03-01 15:49:44 +01:00
|
|
|
_, err := p.val.WriteString(str)
|
|
|
|
return err
|
2014-02-26 23:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-03-01 15:49:44 +01:00
|
|
|
func (p *digitalPin) ActiveLow(b bool) error {
|
2014-02-26 23:54:53 +01:00
|
|
|
str := "0"
|
|
|
|
if b {
|
|
|
|
str = "1"
|
|
|
|
}
|
2014-03-01 15:49:44 +01:00
|
|
|
_, err := p.activeLow.WriteString(str)
|
|
|
|
return err
|
2014-02-26 23:54:53 +01:00
|
|
|
}
|
|
|
|
|
2014-03-02 07:39:57 +01:00
|
|
|
func (p *digitalPin) PullUp() error {
|
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) PullDown() error {
|
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2014-02-26 23:54:53 +01:00
|
|
|
func (p *digitalPin) Close() error {
|
|
|
|
if err := p.dir.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := p.val.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := p.activeLow.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := p.edge.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|