gpio: simplify and test the logic

This commit is contained in:
Karan Misra 2014-03-23 02:32:26 +05:30
parent dfb7e63768
commit f667c93b7a
2 changed files with 27 additions and 12 deletions

View File

@ -32,23 +32,12 @@ func (io *gpioDriver) digitalPin(key interface{}) (*digitalPin, error) {
return nil, fmt.Errorf("gpio: could not find pin matching %q", key)
}
id := pd.ID
p, ok := io.initializedPins[id]
if ok {
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 {
glog.Infof("gpio: pin %q is not a dedicated digital io pin. please refer to the system reference manual for more details", key)
}
dp := newDigitalPin(pd.DigitalLogical)
io.initializedPins[id] = dp
io.initializedPins[pd.ID] = dp
return dp, nil
}

26
gpiodriver_test.go Normal file
View File

@ -0,0 +1,26 @@
package embd
import "testing"
func TestGpioDriverDigitalPin(t *testing.T) {
var tests = []struct {
key interface{}
n int
}{
{1, 1},
}
var pinMap = PinMap{
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapNormal, DigitalLogical: 1},
}
driver := newGPIODriver(pinMap)
for _, test := range tests {
pin, err := driver.digitalPin(test.key)
if err != nil {
t.Errorf("Looking up %v: unexpected error: %v", test.key, err)
continue
}
if pin.n != test.n {
t.Errorf("Looking up %v: got %v, want %v", test.key, pin.n, test.n)
}
}
}