From 6d3b3ae57195e679117f29b0a64a7b7ea6d387ac Mon Sep 17 00:00:00 2001 From: Karan Misra Date: Sat, 22 Mar 2014 10:24:29 +0530 Subject: [PATCH] gpio: digital pin is responsible for exporting/unexporting itself --- digitalpin.go | 69 ++++++++++++++++++++++++++++++++++++----- gpiogeneric.go | 84 +++++++++++++------------------------------------- 2 files changed, 82 insertions(+), 71 deletions(-) diff --git a/digitalpin.go b/digitalpin.go index 72d287f..856f6dd 100644 --- a/digitalpin.go +++ b/digitalpin.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path" + "strconv" ) type digitalPin struct { @@ -14,18 +15,23 @@ type digitalPin struct { val *os.File activeLow *os.File edge *os.File + + initialized bool } -func newDigitalPin(n int) (*digitalPin, error) { - p := &digitalPin{n: n} - if err := p.init(); err != nil { - return nil, err - } - return p, nil +func newDigitalPin(n int) *digitalPin { + return &digitalPin{n: n} } func (p *digitalPin) init() error { + if p.initialized { + return nil + } + var err error + if err = p.export(); err != nil { + return err + } if p.dir, err = p.directionFile(); err != nil { return err } @@ -36,9 +42,31 @@ func (p *digitalPin) init() error { return err } + p.initialized = true + 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 { 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 { + if err := p.init(); err != nil { + return err + } + str := "in" if dir == Out { str = "out" @@ -69,6 +101,10 @@ func (p *digitalPin) SetDirection(dir Direction) error { } func (p *digitalPin) Read() (int, error) { + if err := p.init(); err != nil { + return 0, err + } + buf := make([]byte, 1) if _, err := p.val.Read(buf); err != nil { return 0, err @@ -81,6 +117,10 @@ func (p *digitalPin) Read() (int, error) { } func (p *digitalPin) Write(val int) error { + if err := p.init(); err != nil { + return err + } + str := "0" if val == High { str = "1" @@ -90,6 +130,10 @@ func (p *digitalPin) Write(val int) error { } func (p *digitalPin) ActiveLow(b bool) error { + if err := p.init(); err != nil { + return err + } + str := "0" if b { str = "1" @@ -99,14 +143,18 @@ func (p *digitalPin) ActiveLow(b bool) error { } func (p *digitalPin) PullUp() error { - return errors.New("not implemented") + return errors.New("gpio: not implemented") } func (p *digitalPin) PullDown() error { - return errors.New("not implemented") + return errors.New("gpio: not implemented") } func (p *digitalPin) Close() error { + if !p.initialized { + return nil + } + if err := p.dir.Close(); err != nil { return err } @@ -119,6 +167,11 @@ func (p *digitalPin) Close() error { if err := p.edge.Close(); err != nil { return err } + if err := p.unexport(); err != nil { + return err + } + + p.initialized = false return nil } diff --git a/gpiogeneric.go b/gpiogeneric.go index 3f5406b..3e6beef 100644 --- a/gpiogeneric.go +++ b/gpiogeneric.go @@ -2,8 +2,6 @@ package embd import ( "fmt" - "os" - "strconv" "github.com/golang/glog" ) @@ -47,107 +45,67 @@ func (m PinMap) Lookup(k interface{}) (*PinDesc, bool) { return nil, false } +type pin interface { + Close() error +} + type gpioDriver struct { - exporter, unexporter *os.File - - initialized bool - pinMap PinMap - initializedPins map[int]*digitalPin + initializedPins map[int]pin } func newGPIODriver(pinMap PinMap) *gpioDriver { return &gpioDriver{ 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) { 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) { pd, found := io.lookupKey(key) if !found { - err := fmt.Errorf("gpio: could not find pin matching %q", key) - return nil, err + return nil, fmt.Errorf("gpio: could not find pin matching %q", key) } n := pd.N p, ok := io.initializedPins[n] 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 { - err := fmt.Errorf("gpio: sorry, pin %q cannot be used for GPIO", key) - return nil, err + return nil, fmt.Errorf("gpio: sorry, pin %q cannot be used for digital io", key) } 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 { - return nil, err - } + dp := newDigitalPin(n) + io.initializedPins[n] = dp - p, err := newDigitalPin(n) - if err != nil { - io.unexport(n) - return nil, err - } - - io.initializedPins[n] = p - - return p, nil + return dp, nil } func (io *gpioDriver) DigitalPin(key interface{}) (DigitalPin, error) { - if err := io.init(); err != nil { - return nil, err - } - return io.digitalPin(key) } func (io *gpioDriver) Close() error { - for n := range io.initializedPins { - io.unexport(n) + for _, p := range io.initializedPins { + if err := p.Close(); err != nil { + return err + } } - io.exporter.Close() - io.unexporter.Close() - return nil }