2014-03-23 14:09:31 +05:30
|
|
|
// Digital IO support.
|
|
|
|
// This driver requires kernel version 3.8+ and should work uniformly
|
|
|
|
// across all supported devices.
|
|
|
|
|
2014-04-06 06:50:09 +05:30
|
|
|
package generic
|
2014-03-22 09:08:23 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2014-03-22 10:24:29 +05:30
|
|
|
"strconv"
|
2016-08-26 23:02:41 -07:00
|
|
|
"syscall"
|
2014-04-05 04:06:40 +05:30
|
|
|
"time"
|
2014-04-06 06:50:09 +05:30
|
|
|
|
|
|
|
"github.com/kidoman/embd"
|
2014-03-22 09:08:23 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type digitalPin struct {
|
2014-04-06 05:30:54 +05:30
|
|
|
id string
|
|
|
|
n int
|
|
|
|
|
2014-04-06 06:50:09 +05:30
|
|
|
drv embd.GPIODriver
|
2014-03-22 09:08:23 +05:30
|
|
|
|
|
|
|
dir *os.File
|
|
|
|
val *os.File
|
|
|
|
activeLow *os.File
|
2014-03-22 10:24:29 +05:30
|
|
|
|
2014-04-05 04:06:40 +05:30
|
|
|
readBuf []byte
|
|
|
|
|
2014-03-22 10:24:29 +05:30
|
|
|
initialized bool
|
2014-03-22 09:08:23 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 06:50:09 +05:30
|
|
|
func NewDigitalPin(pd *embd.PinDesc, drv embd.GPIODriver) embd.DigitalPin {
|
2014-04-06 05:30:54 +05:30
|
|
|
return &digitalPin{id: pd.ID, n: pd.DigitalLogical, drv: drv, readBuf: make([]byte, 1)}
|
2014-03-22 09:08:23 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 04:59:35 +05:30
|
|
|
func (p *digitalPin) N() int {
|
|
|
|
return p.n
|
|
|
|
}
|
|
|
|
|
2014-03-22 09:08:23 +05:30
|
|
|
func (p *digitalPin) init() error {
|
2014-03-22 10:24:29 +05:30
|
|
|
if p.initialized {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-22 09:08:23 +05:30
|
|
|
var err error
|
2014-03-22 10:24:29 +05:30
|
|
|
if err = p.export(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-22 09:08:23 +05:30
|
|
|
if p.dir, err = p.directionFile(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p.val, err = p.valueFile(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p.activeLow, err = p.activeLowFile(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-22 10:24:29 +05:30
|
|
|
p.initialized = true
|
|
|
|
|
2014-03-22 09:08:23 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-22 10:24:29 +05:30
|
|
|
func (p *digitalPin) export() error {
|
|
|
|
exporter, err := os.OpenFile("/sys/class/gpio/export", os.O_WRONLY, os.ModeExclusive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer exporter.Close()
|
|
|
|
_, err = exporter.WriteString(strconv.Itoa(p.n))
|
2016-08-26 23:02:41 -07:00
|
|
|
if e, ok := err.(*os.PathError); ok && e.Err == syscall.EBUSY {
|
|
|
|
return nil // EBUSY -> the pin has already been exported
|
|
|
|
}
|
2014-03-22 10:24:29 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) unexport() error {
|
|
|
|
unexporter, err := os.OpenFile("/sys/class/gpio/unexport", os.O_WRONLY, os.ModeExclusive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer unexporter.Close()
|
|
|
|
_, err = unexporter.WriteString(strconv.Itoa(p.n))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-22 09:08:23 +05:30
|
|
|
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-04-06 06:50:09 +05:30
|
|
|
func (p *digitalPin) SetDirection(dir embd.Direction) error {
|
2014-03-22 10:24:29 +05:30
|
|
|
if err := p.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-22 09:08:23 +05:30
|
|
|
str := "in"
|
2014-04-06 06:50:09 +05:30
|
|
|
if dir == embd.Out {
|
2014-03-22 09:08:23 +05:30
|
|
|
str = "out"
|
|
|
|
}
|
|
|
|
_, err := p.dir.WriteString(str)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-05 04:06:40 +05:30
|
|
|
func (p *digitalPin) read() (int, error) {
|
|
|
|
if _, err := p.val.ReadAt(p.readBuf, 0); err != nil {
|
2014-03-22 10:24:29 +05:30
|
|
|
return 0, err
|
|
|
|
}
|
2014-04-05 04:06:40 +05:30
|
|
|
if p.readBuf[0] == 49 {
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
2014-03-22 10:24:29 +05:30
|
|
|
|
2014-04-05 04:06:40 +05:30
|
|
|
func (p *digitalPin) Read() (int, error) {
|
|
|
|
if err := p.init(); err != nil {
|
2014-03-22 09:08:23 +05:30
|
|
|
return 0, err
|
|
|
|
}
|
2014-04-05 04:06:40 +05:30
|
|
|
|
|
|
|
return p.read()
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
lowBytes = []byte{48}
|
|
|
|
highBytes = []byte{49}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *digitalPin) write(val int) error {
|
|
|
|
bytes := lowBytes
|
2014-04-06 06:50:09 +05:30
|
|
|
if val == embd.High {
|
2014-04-05 04:06:40 +05:30
|
|
|
bytes = highBytes
|
2014-03-22 09:08:23 +05:30
|
|
|
}
|
2014-04-05 04:06:40 +05:30
|
|
|
_, err := p.val.Write(bytes)
|
|
|
|
return err
|
2014-03-22 09:08:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) Write(val int) error {
|
2014-03-22 10:24:29 +05:30
|
|
|
if err := p.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-05 04:06:40 +05:30
|
|
|
return p.write(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) TimePulse(state int) (time.Duration, error) {
|
|
|
|
if err := p.init(); err != nil {
|
|
|
|
return 0, err
|
2014-03-22 09:08:23 +05:30
|
|
|
}
|
2014-04-05 04:06:40 +05:30
|
|
|
|
2014-04-06 06:50:09 +05:30
|
|
|
aroundState := embd.Low
|
|
|
|
if state == embd.Low {
|
|
|
|
aroundState = embd.High
|
2014-04-05 04:06:40 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for any previous pulse to end
|
|
|
|
for {
|
|
|
|
v, err := p.read()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == aroundState {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until ECHO goes high
|
|
|
|
for {
|
|
|
|
v, err := p.read()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == state {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now() // Record time when ECHO goes high
|
|
|
|
|
|
|
|
// Wait until ECHO goes low
|
|
|
|
for {
|
|
|
|
v, err := p.read()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == aroundState {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Since(startTime), nil // Calculate time lapsed for ECHO to transition from high to low
|
2014-03-22 09:08:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) ActiveLow(b bool) error {
|
2014-03-22 10:24:29 +05:30
|
|
|
if err := p.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-22 09:08:23 +05:30
|
|
|
str := "0"
|
|
|
|
if b {
|
|
|
|
str = "1"
|
|
|
|
}
|
|
|
|
_, err := p.activeLow.WriteString(str)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) PullUp() error {
|
2014-03-22 10:24:29 +05:30
|
|
|
return errors.New("gpio: not implemented")
|
2014-03-22 09:08:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) PullDown() error {
|
2014-03-22 10:24:29 +05:30
|
|
|
return errors.New("gpio: not implemented")
|
2014-03-22 09:08:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) Close() error {
|
2014-08-31 00:39:41 -04:00
|
|
|
if err := p.StopWatching(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-06 05:30:54 +05:30
|
|
|
if err := p.drv.Unregister(p.id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-22 10:24:29 +05:30
|
|
|
if !p.initialized {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-22 09:08:23 +05:30
|
|
|
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
|
|
|
|
}
|
2014-03-22 10:24:29 +05:30
|
|
|
if err := p.unexport(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.initialized = false
|
2014-03-22 09:08:23 +05:30
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-31 00:39:41 -04:00
|
|
|
|
|
|
|
func (p *digitalPin) setEdge(edge embd.Edge) error {
|
|
|
|
file, err := p.openFile(path.Join(p.basePath(), "edge"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
_, err = file.Write([]byte(edge))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) Watch(edge embd.Edge, handler func(embd.DigitalPin)) error {
|
|
|
|
if err := p.setEdge(edge); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return registerInterrupt(p, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) StopWatching() error {
|
|
|
|
return unregisterInterrupt(p)
|
|
|
|
}
|