1
0
mirror of https://github.com/kidoman/embd synced 2024-10-31 23:38:44 +01:00

gpio: digital pin is responsible for exporting/unexporting itself

This commit is contained in:
Karan Misra 2014-03-22 10:24:29 +05:30
parent df15190e33
commit 6d3b3ae571
2 changed files with 82 additions and 71 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"strconv"
) )
type digitalPin struct { type digitalPin struct {
@ -14,18 +15,23 @@ type digitalPin struct {
val *os.File val *os.File
activeLow *os.File activeLow *os.File
edge *os.File edge *os.File
initialized bool
} }
func newDigitalPin(n int) (*digitalPin, error) { func newDigitalPin(n int) *digitalPin {
p := &digitalPin{n: n} return &digitalPin{n: n}
if err := p.init(); err != nil {
return nil, err
}
return p, nil
} }
func (p *digitalPin) init() error { func (p *digitalPin) init() error {
if p.initialized {
return nil
}
var err error var err error
if err = p.export(); err != nil {
return err
}
if p.dir, err = p.directionFile(); err != nil { if p.dir, err = p.directionFile(); err != nil {
return err return err
} }
@ -36,9 +42,31 @@ func (p *digitalPin) init() error {
return err return err
} }
p.initialized = true
return nil return nil
} }
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
}
func (p *digitalPin) basePath() string { func (p *digitalPin) basePath() string {
return fmt.Sprintf("/sys/class/gpio/gpio%v", p.n) return fmt.Sprintf("/sys/class/gpio/gpio%v", p.n)
} }
@ -60,6 +88,10 @@ func (p *digitalPin) activeLowFile() (*os.File, error) {
} }
func (p *digitalPin) SetDirection(dir Direction) error { func (p *digitalPin) SetDirection(dir Direction) error {
if err := p.init(); err != nil {
return err
}
str := "in" str := "in"
if dir == Out { if dir == Out {
str = "out" str = "out"
@ -69,6 +101,10 @@ func (p *digitalPin) SetDirection(dir Direction) error {
} }
func (p *digitalPin) Read() (int, error) { func (p *digitalPin) Read() (int, error) {
if err := p.init(); err != nil {
return 0, err
}
buf := make([]byte, 1) buf := make([]byte, 1)
if _, err := p.val.Read(buf); err != nil { if _, err := p.val.Read(buf); err != nil {
return 0, err return 0, err
@ -81,6 +117,10 @@ func (p *digitalPin) Read() (int, error) {
} }
func (p *digitalPin) Write(val int) error { func (p *digitalPin) Write(val int) error {
if err := p.init(); err != nil {
return err
}
str := "0" str := "0"
if val == High { if val == High {
str = "1" str = "1"
@ -90,6 +130,10 @@ func (p *digitalPin) Write(val int) error {
} }
func (p *digitalPin) ActiveLow(b bool) error { func (p *digitalPin) ActiveLow(b bool) error {
if err := p.init(); err != nil {
return err
}
str := "0" str := "0"
if b { if b {
str = "1" str = "1"
@ -99,14 +143,18 @@ func (p *digitalPin) ActiveLow(b bool) error {
} }
func (p *digitalPin) PullUp() error { func (p *digitalPin) PullUp() error {
return errors.New("not implemented") return errors.New("gpio: not implemented")
} }
func (p *digitalPin) PullDown() error { func (p *digitalPin) PullDown() error {
return errors.New("not implemented") return errors.New("gpio: not implemented")
} }
func (p *digitalPin) Close() error { func (p *digitalPin) Close() error {
if !p.initialized {
return nil
}
if err := p.dir.Close(); err != nil { if err := p.dir.Close(); err != nil {
return err return err
} }
@ -119,6 +167,11 @@ func (p *digitalPin) Close() error {
if err := p.edge.Close(); err != nil { if err := p.edge.Close(); err != nil {
return err return err
} }
if err := p.unexport(); err != nil {
return err
}
p.initialized = false
return nil return nil
} }

View File

@ -2,8 +2,6 @@ package embd
import ( import (
"fmt" "fmt"
"os"
"strconv"
"github.com/golang/glog" "github.com/golang/glog"
) )
@ -47,107 +45,67 @@ func (m PinMap) Lookup(k interface{}) (*PinDesc, bool) {
return nil, false return nil, false
} }
type pin interface {
Close() error
}
type gpioDriver struct { type gpioDriver struct {
exporter, unexporter *os.File
initialized bool
pinMap PinMap pinMap PinMap
initializedPins map[int]*digitalPin initializedPins map[int]pin
} }
func newGPIODriver(pinMap PinMap) *gpioDriver { func newGPIODriver(pinMap PinMap) *gpioDriver {
return &gpioDriver{ return &gpioDriver{
pinMap: pinMap, pinMap: pinMap,
initializedPins: map[int]*digitalPin{}, initializedPins: map[int]pin{},
} }
} }
func (io *gpioDriver) init() error {
if io.initialized {
return nil
}
var err error
if io.exporter, err = os.OpenFile("/sys/class/gpio/export", os.O_WRONLY, os.ModeExclusive); err != nil {
return err
}
if io.unexporter, err = os.OpenFile("/sys/class/gpio/unexport", os.O_WRONLY, os.ModeExclusive); err != nil {
return err
}
io.initialized = true
return nil
}
func (io *gpioDriver) lookupKey(key interface{}) (*PinDesc, bool) { func (io *gpioDriver) lookupKey(key interface{}) (*PinDesc, bool) {
return io.pinMap.Lookup(key) return io.pinMap.Lookup(key)
} }
func (io *gpioDriver) export(n int) error {
_, err := io.exporter.WriteString(strconv.Itoa(n))
return err
}
func (io *gpioDriver) unexport(n int) error {
_, err := io.unexporter.WriteString(strconv.Itoa(n))
return err
}
func (io *gpioDriver) digitalPin(key interface{}) (*digitalPin, error) { func (io *gpioDriver) digitalPin(key interface{}) (*digitalPin, error) {
pd, found := io.lookupKey(key) pd, found := io.lookupKey(key)
if !found { if !found {
err := fmt.Errorf("gpio: could not find pin matching %q", key) return nil, fmt.Errorf("gpio: could not find pin matching %q", key)
return nil, err
} }
n := pd.N n := pd.N
p, ok := io.initializedPins[n] p, ok := io.initializedPins[n]
if ok { if ok {
return p, nil dp, ok := p.(*digitalPin)
if !ok {
return nil, fmt.Errorf("gpio: sorry, pin %q is already initialized for a different mode", key)
}
return dp, nil
} }
if pd.Caps&CapNormal == 0 { if pd.Caps&CapNormal == 0 {
err := fmt.Errorf("gpio: sorry, pin %q cannot be used for GPIO", key) return nil, fmt.Errorf("gpio: sorry, pin %q cannot be used for digital io", key)
return nil, err
} }
if pd.Caps != CapNormal { if pd.Caps != CapNormal {
glog.Infof("gpio: pin %q is not a dedicated GPIO pin. please refer to the system reference manual for more details", key) glog.Infof("gpio: pin %q is not a dedicated digital io pin. please refer to the system reference manual for more details", key)
} }
if err := io.export(n); err != nil { dp := newDigitalPin(n)
return nil, err io.initializedPins[n] = dp
}
p, err := newDigitalPin(n) return dp, nil
if err != nil {
io.unexport(n)
return nil, err
}
io.initializedPins[n] = p
return p, nil
} }
func (io *gpioDriver) DigitalPin(key interface{}) (DigitalPin, error) { func (io *gpioDriver) DigitalPin(key interface{}) (DigitalPin, error) {
if err := io.init(); err != nil {
return nil, err
}
return io.digitalPin(key) return io.digitalPin(key)
} }
func (io *gpioDriver) Close() error { func (io *gpioDriver) Close() error {
for n := range io.initializedPins { for _, p := range io.initializedPins {
io.unexport(n) if err := p.Close(); err != nil {
return err
}
} }
io.exporter.Close()
io.unexporter.Close()
return nil return nil
} }