2014-03-23 09:39:31 +01:00
|
|
|
// Digital IO support.
|
|
|
|
// This driver requires kernel version 3.8+ and should work uniformly
|
|
|
|
// across all supported devices.
|
|
|
|
|
2014-04-06 03:20:09 +02:00
|
|
|
package generic
|
2014-03-22 04:38:23 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2014-03-22 05:54:29 +01:00
|
|
|
"strconv"
|
2014-04-05 00:36:40 +02:00
|
|
|
"time"
|
2014-04-06 03:20:09 +02:00
|
|
|
|
|
|
|
"github.com/kidoman/embd"
|
2014-03-22 04:38:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type digitalPin struct {
|
2014-04-06 02:00:54 +02:00
|
|
|
id string
|
|
|
|
n int
|
|
|
|
|
2014-04-06 03:20:09 +02:00
|
|
|
drv embd.GPIODriver
|
2014-03-22 04:38:23 +01:00
|
|
|
|
|
|
|
dir *os.File
|
|
|
|
val *os.File
|
|
|
|
activeLow *os.File
|
2014-03-22 05:54:29 +01:00
|
|
|
|
2014-04-05 00:36:40 +02:00
|
|
|
readBuf []byte
|
|
|
|
|
2014-03-22 05:54:29 +01:00
|
|
|
initialized bool
|
2014-03-22 04:38:23 +01:00
|
|
|
}
|
|
|
|
|
2014-04-06 03:20:09 +02:00
|
|
|
func NewDigitalPin(pd *embd.PinDesc, drv embd.GPIODriver) embd.DigitalPin {
|
2014-04-06 02:00:54 +02:00
|
|
|
return &digitalPin{id: pd.ID, n: pd.DigitalLogical, drv: drv, readBuf: make([]byte, 1)}
|
2014-03-22 04:38:23 +01:00
|
|
|
}
|
|
|
|
|
2014-03-23 00:29:35 +01:00
|
|
|
func (p *digitalPin) N() int {
|
|
|
|
return p.n
|
|
|
|
}
|
|
|
|
|
2014-03-22 04:38:23 +01:00
|
|
|
func (p *digitalPin) init() error {
|
2014-03-22 05:54:29 +01:00
|
|
|
if p.initialized {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-22 04:38:23 +01:00
|
|
|
var err error
|
2014-03-22 05:54:29 +01:00
|
|
|
if err = p.export(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-22 04:38:23 +01:00
|
|
|
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 05:54:29 +01:00
|
|
|
p.initialized = true
|
|
|
|
|
2014-03-22 04:38:23 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-22 05:54:29 +01:00
|
|
|
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))
|
|
|
|
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 04:38:23 +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-04-06 03:20:09 +02:00
|
|
|
func (p *digitalPin) SetDirection(dir embd.Direction) error {
|
2014-03-22 05:54:29 +01:00
|
|
|
if err := p.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-22 04:38:23 +01:00
|
|
|
str := "in"
|
2014-04-06 03:20:09 +02:00
|
|
|
if dir == embd.Out {
|
2014-03-22 04:38:23 +01:00
|
|
|
str = "out"
|
|
|
|
}
|
|
|
|
_, err := p.dir.WriteString(str)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-05 00:36:40 +02:00
|
|
|
func (p *digitalPin) read() (int, error) {
|
|
|
|
if _, err := p.val.ReadAt(p.readBuf, 0); err != nil {
|
2014-03-22 05:54:29 +01:00
|
|
|
return 0, err
|
|
|
|
}
|
2014-04-05 00:36:40 +02:00
|
|
|
if p.readBuf[0] == 49 {
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
2014-03-22 05:54:29 +01:00
|
|
|
|
2014-04-05 00:36:40 +02:00
|
|
|
func (p *digitalPin) Read() (int, error) {
|
|
|
|
if err := p.init(); err != nil {
|
2014-03-22 04:38:23 +01:00
|
|
|
return 0, err
|
|
|
|
}
|
2014-04-05 00:36:40 +02:00
|
|
|
|
|
|
|
return p.read()
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
lowBytes = []byte{48}
|
|
|
|
highBytes = []byte{49}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *digitalPin) write(val int) error {
|
|
|
|
bytes := lowBytes
|
2014-04-06 03:20:09 +02:00
|
|
|
if val == embd.High {
|
2014-04-05 00:36:40 +02:00
|
|
|
bytes = highBytes
|
2014-03-22 04:38:23 +01:00
|
|
|
}
|
2014-04-05 00:36:40 +02:00
|
|
|
_, err := p.val.Write(bytes)
|
|
|
|
return err
|
2014-03-22 04:38:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) Write(val int) error {
|
2014-03-22 05:54:29 +01:00
|
|
|
if err := p.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-05 00:36:40 +02:00
|
|
|
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 04:38:23 +01:00
|
|
|
}
|
2014-04-05 00:36:40 +02:00
|
|
|
|
2014-04-06 03:20:09 +02:00
|
|
|
aroundState := embd.Low
|
|
|
|
if state == embd.Low {
|
|
|
|
aroundState = embd.High
|
2014-04-05 00:36:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 04:38:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) ActiveLow(b bool) error {
|
2014-03-22 05:54:29 +01:00
|
|
|
if err := p.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-22 04:38:23 +01:00
|
|
|
str := "0"
|
|
|
|
if b {
|
|
|
|
str = "1"
|
|
|
|
}
|
|
|
|
_, err := p.activeLow.WriteString(str)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) PullUp() error {
|
2014-03-22 05:54:29 +01:00
|
|
|
return errors.New("gpio: not implemented")
|
2014-03-22 04:38:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) PullDown() error {
|
2014-03-22 05:54:29 +01:00
|
|
|
return errors.New("gpio: not implemented")
|
2014-03-22 04:38:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *digitalPin) Close() error {
|
2014-08-31 06:39:41 +02:00
|
|
|
if err := p.StopWatching(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-06 02:00:54 +02:00
|
|
|
if err := p.drv.Unregister(p.id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-29 00:54:36 +01:00
|
|
|
if err := p.unexport(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-22 05:54:29 +01:00
|
|
|
if !p.initialized {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-22 04:38:23 +01:00
|
|
|
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 05:54:29 +01:00
|
|
|
|
|
|
|
p.initialized = false
|
2014-03-22 04:38:23 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-31 06:39:41 +02: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)
|
|
|
|
}
|