2014-03-23 02:32:26 +05:30
|
|
|
package embd
|
|
|
|
|
2014-04-06 04:08:44 +05:30
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
2014-03-23 02:32:26 +05:30
|
|
|
|
2014-03-23 04:59:35 +05:30
|
|
|
type fakeDigitalPin struct {
|
2014-04-06 05:30:54 +05:30
|
|
|
id string
|
|
|
|
n int
|
|
|
|
|
|
|
|
drv GPIODriver
|
2014-03-23 04:59:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (p *fakeDigitalPin) N() int {
|
|
|
|
return p.n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*fakeDigitalPin) SetDirection(dir Direction) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*fakeDigitalPin) Read() (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*fakeDigitalPin) Write(val int) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-06 04:08:44 +05:30
|
|
|
func (*fakeDigitalPin) TimePulse(state int) (time.Duration, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2014-03-23 04:59:35 +05:30
|
|
|
func (*fakeDigitalPin) ActiveLow(b bool) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*fakeDigitalPin) PullUp() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*fakeDigitalPin) PullDown() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-06 05:30:54 +05:30
|
|
|
func (p *fakeDigitalPin) Close() error {
|
|
|
|
return p.drv.Unregister(p.id)
|
2014-03-23 04:59:35 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 05:30:54 +05:30
|
|
|
func newFakeDigitalPin(pd *PinDesc, drv GPIODriver) DigitalPin {
|
|
|
|
return &fakeDigitalPin{id: pd.ID, n: pd.DigitalLogical, drv: drv}
|
2014-03-23 04:59:35 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 02:32:26 +05:30
|
|
|
func TestGpioDriverDigitalPin(t *testing.T) {
|
2014-04-06 04:05:18 +05:30
|
|
|
tests := []struct {
|
2014-03-23 02:32:26 +05:30
|
|
|
key interface{}
|
|
|
|
n int
|
|
|
|
}{
|
|
|
|
{1, 1},
|
|
|
|
}
|
2014-04-06 04:05:18 +05:30
|
|
|
pinMap := PinMap{
|
2014-03-23 14:11:09 +05:30
|
|
|
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapDigital, DigitalLogical: 1},
|
2014-03-23 02:32:26 +05:30
|
|
|
}
|
2014-04-06 06:50:09 +05:30
|
|
|
driver := NewGPIODriver(pinMap, newFakeDigitalPin, nil, nil)
|
2014-03-23 04:59:35 +05:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeAnalogPin struct {
|
2014-04-06 05:30:54 +05:30
|
|
|
id string
|
|
|
|
n int
|
|
|
|
|
|
|
|
drv GPIODriver
|
2014-03-23 04:59:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (p *fakeAnalogPin) N() int {
|
|
|
|
return p.n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*fakeAnalogPin) Read() (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*fakeAnalogPin) Write(val int) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*fakeAnalogPin) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-06 05:30:54 +05:30
|
|
|
func newFakeAnalogPin(pd *PinDesc, drv GPIODriver) AnalogPin {
|
|
|
|
return &fakeAnalogPin{id: pd.ID, n: pd.AnalogLogical, drv: drv}
|
2014-03-23 04:59:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestGpioDriverAnalogPin(t *testing.T) {
|
2014-04-06 04:05:18 +05:30
|
|
|
tests := []struct {
|
2014-03-23 04:59:35 +05:30
|
|
|
key interface{}
|
|
|
|
n int
|
|
|
|
}{
|
|
|
|
{1, 1},
|
|
|
|
}
|
2014-04-06 04:05:18 +05:30
|
|
|
pinMap := PinMap{
|
2014-03-23 04:59:35 +05:30
|
|
|
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapAnalog, AnalogLogical: 1},
|
|
|
|
}
|
2014-04-06 06:50:09 +05:30
|
|
|
driver := NewGPIODriver(pinMap, nil, newFakeAnalogPin, nil)
|
2014-03-23 02:32:26 +05:30
|
|
|
for _, test := range tests {
|
2014-03-23 04:59:35 +05:30
|
|
|
pin, err := driver.AnalogPin(test.key)
|
2014-03-23 02:32:26 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Looking up %v: unexpected error: %v", test.key, err)
|
|
|
|
continue
|
|
|
|
}
|
2014-03-23 04:59:35 +05:30
|
|
|
if pin.N() != test.n {
|
|
|
|
t.Errorf("Looking up %v: got %v, want %v", test.key, pin.N(), test.n)
|
2014-03-23 02:32:26 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-23 04:59:35 +05:30
|
|
|
|
|
|
|
func TestGpioDriverUnavailablePinType(t *testing.T) {
|
2014-04-06 04:05:18 +05:30
|
|
|
pinMap := PinMap{
|
2014-03-23 14:11:09 +05:30
|
|
|
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapDigital, DigitalLogical: 1},
|
2014-03-23 04:59:35 +05:30
|
|
|
&PinDesc{ID: "P1_2", Aliases: []string{"1"}, Caps: CapAnalog, AnalogLogical: 1},
|
|
|
|
}
|
2014-04-06 06:50:09 +05:30
|
|
|
driver := NewGPIODriver(pinMap, nil, nil, nil)
|
2014-03-23 04:59:35 +05:30
|
|
|
_, err := driver.DigitalPin(1)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Looking up digital pin 1: did not get error")
|
|
|
|
}
|
|
|
|
expected := "gpio: digital io not supported on this host"
|
|
|
|
if err.Error() != expected {
|
|
|
|
t.Fatalf("Looking up digital pin 1: got error %q, expected %q", err, expected)
|
|
|
|
}
|
|
|
|
_, err = driver.AnalogPin(1)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Looking up analog pin 1: did not get error")
|
|
|
|
}
|
|
|
|
expected = "gpio: analog io not supported on this host"
|
|
|
|
if err.Error() != expected {
|
|
|
|
t.Fatalf("Looking up analog pin 1: got error %q, expected %q", err, expected)
|
|
|
|
}
|
|
|
|
}
|
2014-04-06 05:30:54 +05:30
|
|
|
|
|
|
|
func TestGpioPinCaching(t *testing.T) {
|
|
|
|
pinMap := PinMap{
|
|
|
|
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapDigital},
|
|
|
|
}
|
2014-04-06 06:50:09 +05:30
|
|
|
driver := NewGPIODriver(pinMap, newFakeDigitalPin, nil, nil)
|
2014-04-06 05:30:54 +05:30
|
|
|
pin, err := driver.DigitalPin(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Looking up digital pin 1: got %v", err)
|
|
|
|
}
|
|
|
|
// Lookup the same pin again
|
|
|
|
pin2, err := driver.DigitalPin(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Looking up digital pin 1: got %v", err)
|
|
|
|
}
|
|
|
|
if pin != pin2 {
|
|
|
|
t.Fatalf("Looking up digital pin 1 for the second time: got %v, want %v", &pin2, &pin)
|
|
|
|
}
|
|
|
|
// Looking up a closed pin
|
|
|
|
pin.Close()
|
|
|
|
pin3, err := driver.DigitalPin(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Looking up digital pin 1: got %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if pin == pin3 {
|
|
|
|
t.Fatal("Looking up a closed pin, but got the same old instance")
|
|
|
|
}
|
|
|
|
}
|