2014-10-31 07:06:04 +08:00
|
|
|
package sysfs
|
|
|
|
|
|
|
|
import (
|
2014-12-24 04:43:55 +08:00
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2014-10-31 07:06:04 +08:00
|
|
|
"testing"
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-10-31 07:06:04 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDigitalPin(t *testing.T) {
|
2014-11-08 08:21:39 +08:00
|
|
|
fs := NewMockFilesystem([]string{
|
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/unexport",
|
|
|
|
"/sys/class/gpio/gpio10/value",
|
|
|
|
"/sys/class/gpio/gpio10/direction",
|
|
|
|
})
|
2014-10-31 07:06:04 +08:00
|
|
|
|
2014-11-08 08:21:39 +08:00
|
|
|
SetFilesystem(fs)
|
2014-10-31 07:06:04 +08:00
|
|
|
|
2017-04-28 18:50:36 +08:00
|
|
|
pin := NewDigitalPin(10, "custom")
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, pin.pin, "10")
|
|
|
|
gobottest.Assert(t, pin.label, "custom")
|
2014-10-31 07:06:04 +08:00
|
|
|
|
2017-04-28 18:50:36 +08:00
|
|
|
pin = NewDigitalPin(10)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, pin.pin, "10")
|
|
|
|
gobottest.Assert(t, pin.label, "gpio10")
|
|
|
|
gobottest.Assert(t, pin.value, nil)
|
2014-10-31 07:06:04 +08:00
|
|
|
|
2016-02-19 04:08:48 +08:00
|
|
|
err := pin.Unexport()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/gpio/unexport"].Contents, "10")
|
2014-10-31 07:06:04 +08:00
|
|
|
|
2016-02-19 04:08:48 +08:00
|
|
|
err = pin.Export()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/gpio/export"].Contents, "10")
|
|
|
|
gobottest.Refute(t, pin.value, nil)
|
2014-10-31 07:06:04 +08:00
|
|
|
|
2016-02-19 04:08:48 +08:00
|
|
|
err = pin.Write(1)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio10/value"].Contents, "1")
|
2014-10-31 07:06:04 +08:00
|
|
|
|
2016-02-19 04:08:48 +08:00
|
|
|
err = pin.Direction(IN)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio10/direction"].Contents, "in")
|
2014-10-31 07:06:04 +08:00
|
|
|
|
|
|
|
data, _ := pin.Read()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, 1, data)
|
2014-11-08 08:21:39 +08:00
|
|
|
|
|
|
|
pin2 := NewDigitalPin(30, "custom")
|
2016-02-19 04:08:48 +08:00
|
|
|
err = pin2.Write(1)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Refute(t, err, nil)
|
2014-11-08 08:21:39 +08:00
|
|
|
|
|
|
|
data, err = pin2.Read()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Refute(t, err, nil)
|
|
|
|
gobottest.Assert(t, data, 0)
|
2014-12-24 04:43:55 +08:00
|
|
|
|
2016-02-19 04:08:48 +08:00
|
|
|
writeFile = func(File, []byte) (int, error) {
|
2014-12-24 04:43:55 +08:00
|
|
|
return 0, &os.PathError{Err: syscall.EINVAL}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pin.Unexport()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
2014-12-24 04:43:55 +08:00
|
|
|
|
2016-02-19 04:08:48 +08:00
|
|
|
writeFile = func(File, []byte) (int, error) {
|
2014-12-24 04:43:55 +08:00
|
|
|
return 0, &os.PathError{Err: errors.New("write error")}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pin.Unexport()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err.(*os.PathError).Err, errors.New("write error"))
|
2014-12-24 04:43:55 +08:00
|
|
|
|
2016-02-19 04:08:48 +08:00
|
|
|
writeFile = func(File, []byte) (int, error) {
|
2014-12-24 04:43:55 +08:00
|
|
|
return 0, &os.PathError{Err: syscall.EBUSY}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pin.Export()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
2014-12-24 04:43:55 +08:00
|
|
|
|
2016-02-19 04:08:48 +08:00
|
|
|
writeFile = func(File, []byte) (int, error) {
|
2014-12-24 04:43:55 +08:00
|
|
|
return 0, &os.PathError{Err: errors.New("write error")}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pin.Export()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err.(*os.PathError).Err, errors.New("write error"))
|
2014-10-31 07:06:04 +08:00
|
|
|
}
|
2017-04-09 18:00:14 +08:00
|
|
|
|
|
|
|
func TestDigitalPinExportError(t *testing.T) {
|
|
|
|
fs := NewMockFilesystem([]string{
|
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/unexport",
|
|
|
|
"/sys/class/gpio/gpio10/value",
|
|
|
|
"/sys/class/gpio/gpio10/direction",
|
|
|
|
})
|
|
|
|
|
|
|
|
SetFilesystem(fs)
|
|
|
|
|
2017-04-28 18:50:36 +08:00
|
|
|
pin := NewDigitalPin(10, "custom")
|
2017-04-09 18:00:14 +08:00
|
|
|
writeFile = func(File, []byte) (int, error) {
|
|
|
|
return 0, &os.PathError{Err: syscall.EBUSY}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := pin.Export()
|
|
|
|
gobottest.Refute(t, err, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDigitalPinUnexportError(t *testing.T) {
|
|
|
|
fs := NewMockFilesystem([]string{
|
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/unexport",
|
|
|
|
"/sys/class/gpio/gpio10/value",
|
|
|
|
"/sys/class/gpio/gpio10/direction",
|
|
|
|
})
|
|
|
|
|
|
|
|
SetFilesystem(fs)
|
|
|
|
|
2017-04-28 18:50:36 +08:00
|
|
|
pin := NewDigitalPin(10, "custom")
|
2017-04-09 18:00:14 +08:00
|
|
|
writeFile = func(File, []byte) (int, error) {
|
|
|
|
return 0, &os.PathError{Err: syscall.EBUSY}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := pin.Unexport()
|
|
|
|
gobottest.Refute(t, err, nil)
|
|
|
|
}
|