2017-04-21 15:41:11 +08:00
|
|
|
package sysfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPwmPin(t *testing.T) {
|
|
|
|
fs := NewMockFilesystem([]string{
|
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/period",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
|
2017-04-22 05:06:45 +08:00
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/polarity",
|
2017-04-21 15:41:11 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
SetFilesystem(fs)
|
|
|
|
|
2017-04-22 01:50:17 +08:00
|
|
|
pin := NewPWMPin(10)
|
2017-04-21 15:41:11 +08:00
|
|
|
gobottest.Assert(t, pin.pin, "10")
|
|
|
|
|
|
|
|
err := pin.Unexport()
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/unexport"].Contents, "10")
|
|
|
|
|
|
|
|
err = pin.Export()
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/export"].Contents, "10")
|
|
|
|
|
|
|
|
gobottest.Refute(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/enable"].Contents, "1")
|
2017-04-22 05:06:45 +08:00
|
|
|
err = pin.Enable(true)
|
2017-04-21 15:41:11 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/enable"].Contents, "1")
|
|
|
|
|
|
|
|
fs.Files["/sys/class/pwm/pwmchip0/pwm10/period"].Contents = "6"
|
|
|
|
data, _ := pin.Period()
|
|
|
|
gobottest.Assert(t, data, "6")
|
2017-04-21 16:05:59 +08:00
|
|
|
|
2017-04-22 05:06:45 +08:00
|
|
|
gobottest.Assert(t, pin.SetPolarityInverted(true), nil)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].Contents, "inverted")
|
|
|
|
gobottest.Assert(t, pin.SetPolarityInverted(false), nil)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].Contents, "normal")
|
|
|
|
|
2017-04-21 16:05:59 +08:00
|
|
|
gobottest.Refute(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents, "1")
|
2017-04-22 05:06:45 +08:00
|
|
|
err = pin.SetDutyCycle(100)
|
2017-04-21 16:05:59 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents, "100")
|
2017-04-21 15:41:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPwmPinExportError(t *testing.T) {
|
|
|
|
fs := NewMockFilesystem([]string{
|
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/period",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
|
|
|
|
})
|
|
|
|
|
|
|
|
SetFilesystem(fs)
|
|
|
|
|
2017-04-22 01:50:17 +08:00
|
|
|
pin := NewPWMPin(10)
|
2017-04-21 15:41:11 +08:00
|
|
|
pin.write = func(string, []byte) (int, error) {
|
|
|
|
return 0, &os.PathError{Err: syscall.EBUSY}
|
|
|
|
}
|
|
|
|
|
|
|
|
gobottest.Refute(t, pin.Export(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPwmPinUnxportError(t *testing.T) {
|
|
|
|
fs := NewMockFilesystem([]string{
|
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/period",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
|
|
|
|
})
|
|
|
|
|
|
|
|
SetFilesystem(fs)
|
|
|
|
|
2017-04-22 01:50:17 +08:00
|
|
|
pin := NewPWMPin(10)
|
2017-04-21 15:41:11 +08:00
|
|
|
pin.write = func(string, []byte) (int, error) {
|
|
|
|
return 0, &os.PathError{Err: syscall.EBUSY}
|
|
|
|
}
|
|
|
|
|
|
|
|
gobottest.Refute(t, pin.Unexport(), nil)
|
|
|
|
}
|
2017-04-21 16:05:59 +08:00
|
|
|
|
|
|
|
func TestPwmPinPeriodError(t *testing.T) {
|
|
|
|
fs := NewMockFilesystem([]string{
|
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/period",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
|
|
|
|
})
|
|
|
|
|
|
|
|
SetFilesystem(fs)
|
|
|
|
|
2017-04-22 01:50:17 +08:00
|
|
|
pin := NewPWMPin(10)
|
2017-04-21 16:05:59 +08:00
|
|
|
pin.read = func(string) ([]byte, error) {
|
|
|
|
return nil, &os.PathError{Err: syscall.EBUSY}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := pin.Period()
|
|
|
|
gobottest.Refute(t, err, nil)
|
|
|
|
}
|