2014-10-31 07:06:04 +08:00
|
|
|
package sysfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDigitalPin(t *testing.T) {
|
|
|
|
lastPath := ""
|
|
|
|
lastData := []byte{}
|
|
|
|
|
2014-11-04 11:02:20 +08:00
|
|
|
WriteFile = func(path string, data []byte) (i int, err error) {
|
2014-10-31 07:06:04 +08:00
|
|
|
lastPath = path
|
|
|
|
lastData = data
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-04 11:02:20 +08:00
|
|
|
ReadFile = func(path string) (b []byte, err error) {
|
2014-10-31 07:06:04 +08:00
|
|
|
lastPath = path
|
|
|
|
return []byte("0"), nil
|
|
|
|
}
|
|
|
|
|
2014-11-04 11:02:20 +08:00
|
|
|
pin := NewDigitalPin(10, "custom").(*digitalPin)
|
2014-10-31 07:06:04 +08:00
|
|
|
gobot.Assert(t, pin.pin, "10")
|
|
|
|
gobot.Assert(t, pin.label, "custom")
|
|
|
|
|
2014-11-04 11:02:20 +08:00
|
|
|
pin = NewDigitalPin(10).(*digitalPin)
|
2014-10-31 07:06:04 +08:00
|
|
|
gobot.Assert(t, pin.label, "gpio10")
|
|
|
|
|
|
|
|
pin.Unexport()
|
|
|
|
gobot.Assert(t, lastPath, "/sys/class/gpio/unexport")
|
|
|
|
gobot.Assert(t, string(lastData), "10")
|
|
|
|
|
|
|
|
pin.Export()
|
|
|
|
gobot.Assert(t, lastPath, "/sys/class/gpio/export")
|
|
|
|
gobot.Assert(t, string(lastData), "10")
|
|
|
|
|
|
|
|
pin.Write(1)
|
|
|
|
gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/value")
|
|
|
|
gobot.Assert(t, string(lastData), "1")
|
|
|
|
|
2014-11-04 11:02:20 +08:00
|
|
|
pin.Direction(IN)
|
2014-10-31 07:06:04 +08:00
|
|
|
gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/direction")
|
|
|
|
gobot.Assert(t, string(lastData), "in")
|
|
|
|
|
|
|
|
data, _ := pin.Read()
|
|
|
|
gobot.Assert(t, data, 0)
|
|
|
|
gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/value")
|
|
|
|
}
|