mirror of
https://github.com/kidoman/embd
synced 2024-12-22 12:50:19 +01:00
gpio: simplify and test the logic
This commit is contained in:
parent
dfb7e63768
commit
f667c93b7a
@ -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
26
gpiodriver_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user