2017-04-20 19:32:38 +02:00
|
|
|
package tinkerboard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-05-01 16:12:39 +02:00
|
|
|
"fmt"
|
2017-04-20 19:32:38 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/drivers/i2c"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2022-11-20 19:22:26 +01:00
|
|
|
"gobot.io/x/gobot/system"
|
2017-04-20 19:32:38 +02:00
|
|
|
)
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
const (
|
|
|
|
gpio17Path = "/sys/class/gpio/gpio17/"
|
|
|
|
gpio160Path = "/sys/class/gpio/gpio160/"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
pwm2Dir = "/sys/devices/platform/ff680020.pwm/pwm/pwmchip2/"
|
|
|
|
pwm2pwm0Dir = pwm2Dir + "pwm0/"
|
|
|
|
pwm2ExportPath = pwm2Dir + "export"
|
|
|
|
pwm2UnexportPath = pwm2Dir + "unexport"
|
|
|
|
pwm2EnablePath = pwm2pwm0Dir + "enable"
|
|
|
|
pwm2PeriodPath = pwm2pwm0Dir + "period"
|
|
|
|
pwm2DutyCyclePath = pwm2pwm0Dir + "duty_cycle"
|
|
|
|
pwm2PolarityPath = pwm2pwm0Dir + "polarity"
|
|
|
|
)
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
var pwmMockPaths = []string{
|
|
|
|
pwm2ExportPath,
|
|
|
|
pwm2UnexportPath,
|
|
|
|
pwm2EnablePath,
|
|
|
|
pwm2PeriodPath,
|
|
|
|
pwm2DutyCyclePath,
|
|
|
|
pwm2PolarityPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
var gpioMockPaths = []string{
|
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/unexport",
|
|
|
|
gpio17Path + "value",
|
|
|
|
gpio17Path + "direction",
|
|
|
|
gpio160Path + "value",
|
|
|
|
gpio160Path + "direction",
|
|
|
|
}
|
|
|
|
|
2022-11-20 19:22:26 +01:00
|
|
|
// make sure that this Adaptor fulfills all the required interfaces
|
2017-04-20 19:32:38 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2022-11-20 19:22:26 +01:00
|
|
|
var _ gobot.DigitalPinnerProvider = (*Adaptor)(nil)
|
|
|
|
var _ gobot.PWMPinnerProvider = (*Adaptor)(nil)
|
2017-04-20 19:32:38 +02:00
|
|
|
var _ gpio.DigitalReader = (*Adaptor)(nil)
|
|
|
|
var _ gpio.DigitalWriter = (*Adaptor)(nil)
|
2017-04-22 15:03:07 +02:00
|
|
|
var _ gpio.PwmWriter = (*Adaptor)(nil)
|
|
|
|
var _ gpio.ServoWriter = (*Adaptor)(nil)
|
2017-04-20 19:32:38 +02:00
|
|
|
var _ i2c.Connector = (*Adaptor)(nil)
|
|
|
|
|
2022-11-20 19:22:26 +01:00
|
|
|
func preparePwmFs(fs *system.MockFilesystem) {
|
2022-11-05 07:42:28 +01:00
|
|
|
fs.Files[pwm2EnablePath].Contents = "0"
|
|
|
|
fs.Files[pwm2PeriodPath].Contents = "0"
|
|
|
|
fs.Files[pwm2DutyCyclePath].Contents = "0"
|
|
|
|
fs.Files[pwm2PolarityPath].Contents = pwmInverted
|
2022-05-01 16:12:39 +02:00
|
|
|
}
|
2017-04-20 19:32:38 +02:00
|
|
|
|
2022-11-20 19:22:26 +01:00
|
|
|
func initTestAdaptorWithMockedFilesystem(mockPaths []string) (*Adaptor, *system.MockFilesystem) {
|
2022-05-01 16:12:39 +02:00
|
|
|
a := NewAdaptor()
|
2022-11-20 19:22:26 +01:00
|
|
|
fs := a.sys.UseMockFilesystem(mockPaths)
|
2022-12-01 17:33:33 +01:00
|
|
|
if err := a.Connect(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-11-05 07:42:28 +01:00
|
|
|
return a, fs
|
2022-10-26 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestName(t *testing.T) {
|
2017-04-27 12:21:38 +02:00
|
|
|
a := NewAdaptor()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(a.Name(), "Tinker Board"), true)
|
|
|
|
a.SetName("NewName")
|
|
|
|
gobottest.Assert(t, a.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
func Test_translateDigitalPin(t *testing.T) {
|
|
|
|
var tests = map[string]struct {
|
|
|
|
access string
|
|
|
|
pin string
|
|
|
|
wantChip string
|
|
|
|
wantLine int
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
"cdev_ok": {
|
|
|
|
access: "cdev",
|
|
|
|
pin: "7",
|
|
|
|
wantChip: "gpiochip0",
|
|
|
|
wantLine: 17,
|
|
|
|
},
|
|
|
|
"sysfs_ok": {
|
|
|
|
access: "sysfs",
|
|
|
|
pin: "7",
|
|
|
|
wantChip: "",
|
|
|
|
wantLine: 17,
|
|
|
|
},
|
|
|
|
"unknown_pin": {
|
|
|
|
pin: "99",
|
|
|
|
wantChip: "",
|
|
|
|
wantLine: -1,
|
|
|
|
wantErr: fmt.Errorf("'99' is not a valid id for a digital pin"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
a := NewAdaptor()
|
|
|
|
a.sys.UseDigitalPinAccessWithMockFs(tc.access, []string{})
|
|
|
|
// act
|
|
|
|
chip, line, err := a.translateDigitalPin(tc.pin)
|
|
|
|
// assert
|
|
|
|
gobottest.Assert(t, err, tc.wantErr)
|
|
|
|
gobottest.Assert(t, chip, tc.wantChip)
|
|
|
|
gobottest.Assert(t, line, tc.wantLine)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestDigitalIO(t *testing.T) {
|
2022-12-01 17:33:33 +01:00
|
|
|
// only basic tests needed, further tests are done in "digitalpinsadaptor.go"
|
2022-11-05 07:42:28 +01:00
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem(gpioMockPaths)
|
2017-04-20 19:32:38 +02:00
|
|
|
|
|
|
|
a.DigitalWrite("7", 1)
|
2022-05-01 16:12:39 +02:00
|
|
|
gobottest.Assert(t, fs.Files[gpio17Path+"value"].Contents, "1")
|
2017-04-20 19:32:38 +02:00
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
fs.Files[gpio160Path+"value"].Contents = "1"
|
2017-04-20 19:32:38 +02:00
|
|
|
i, _ := a.DigitalRead("10")
|
|
|
|
gobottest.Assert(t, i, 1)
|
|
|
|
|
2022-12-01 17:33:33 +01:00
|
|
|
gobottest.Assert(t, a.DigitalWrite("99", 1), errors.New("'99' is not a valid id for a digital pin"))
|
2017-04-29 12:50:39 +02:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2017-04-20 19:32:38 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestInvalidPWMPin(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem(pwmMockPaths)
|
|
|
|
preparePwmFs(fs)
|
2017-04-22 15:03:07 +02:00
|
|
|
|
|
|
|
err := a.PwmWrite("666", 42)
|
2022-11-05 07:42:28 +01:00
|
|
|
gobottest.Assert(t, err.Error(), "Not a valid PWM pin")
|
2017-04-22 15:03:07 +02:00
|
|
|
|
|
|
|
err = a.ServoWrite("666", 120)
|
2022-11-05 07:42:28 +01:00
|
|
|
gobottest.Assert(t, err.Error(), "Not a valid PWM pin")
|
2017-04-27 12:21:38 +02:00
|
|
|
|
|
|
|
err = a.PwmWrite("3", 42)
|
2022-11-05 07:42:28 +01:00
|
|
|
gobottest.Assert(t, err.Error(), "Not a valid PWM pin")
|
2017-04-27 12:21:38 +02:00
|
|
|
|
|
|
|
err = a.ServoWrite("3", 120)
|
2022-11-05 07:42:28 +01:00
|
|
|
gobottest.Assert(t, err.Error(), "Not a valid PWM pin")
|
2017-04-22 15:03:07 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestPwmWrite(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem(pwmMockPaths)
|
|
|
|
preparePwmFs(fs)
|
2017-04-22 15:03:07 +02:00
|
|
|
|
2017-04-27 12:21:38 +02:00
|
|
|
err := a.PwmWrite("33", 100)
|
2017-04-22 15:03:07 +02:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
gobottest.Assert(t, fs.Files[pwm2ExportPath].Contents, "0")
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2EnablePath].Contents, "1")
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2PeriodPath].Contents, fmt.Sprintf("%d", pwmPeriodDefault))
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2DutyCyclePath].Contents, "3921568")
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2PolarityPath].Contents, "normal")
|
2017-04-22 15:03:07 +02:00
|
|
|
|
2017-04-27 12:21:38 +02:00
|
|
|
err = a.ServoWrite("33", 0)
|
2017-04-22 15:03:07 +02:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
gobottest.Assert(t, fs.Files[pwm2DutyCyclePath].Contents, "500000")
|
2017-04-22 15:03:07 +02:00
|
|
|
|
2017-04-27 12:21:38 +02:00
|
|
|
err = a.ServoWrite("33", 180)
|
2017-04-22 15:03:07 +02:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
gobottest.Assert(t, fs.Files[pwm2DutyCyclePath].Contents, "2000000")
|
2017-04-22 15:03:07 +02:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestPwmWriteError(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem(pwmMockPaths)
|
|
|
|
preparePwmFs(fs)
|
2017-04-27 12:21:38 +02:00
|
|
|
fs.WithWriteError = true
|
|
|
|
|
|
|
|
err := a.PwmWrite("33", 100)
|
2022-05-01 16:12:39 +02:00
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "write error"), true)
|
2017-04-27 12:21:38 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestPwmWriteReadError(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem(pwmMockPaths)
|
|
|
|
preparePwmFs(fs)
|
2017-04-27 12:21:38 +02:00
|
|
|
fs.WithReadError = true
|
|
|
|
|
|
|
|
err := a.PwmWrite("33", 100)
|
2022-05-01 16:12:39 +02:00
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "read error"), true)
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestSetPeriod(t *testing.T) {
|
2022-05-01 16:12:39 +02:00
|
|
|
// arrange
|
2022-11-05 07:42:28 +01:00
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem(pwmMockPaths)
|
|
|
|
preparePwmFs(fs)
|
2022-05-01 16:12:39 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
newPeriod := uint32(2550000)
|
2022-05-01 16:12:39 +02:00
|
|
|
// act
|
|
|
|
err := a.SetPeriod("33", newPeriod)
|
|
|
|
// assert
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2ExportPath].Contents, "0")
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2EnablePath].Contents, "1")
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2PeriodPath].Contents, fmt.Sprintf("%d", newPeriod))
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2DutyCyclePath].Contents, "0")
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2PolarityPath].Contents, "normal")
|
|
|
|
|
|
|
|
// arrange test for automatic adjustment of duty cycle to lower value
|
|
|
|
err = a.PwmWrite("33", 127) // 127 is a little bit smaller than 50% of period
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2DutyCyclePath].Contents, fmt.Sprintf("%d", 1270000))
|
|
|
|
newPeriod = newPeriod / 10
|
|
|
|
|
|
|
|
// act
|
|
|
|
err = a.SetPeriod("33", newPeriod)
|
|
|
|
|
|
|
|
// assert
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2DutyCyclePath].Contents, fmt.Sprintf("%d", 127000))
|
|
|
|
|
|
|
|
// arrange test for automatic adjustment of duty cycle to higher value
|
|
|
|
newPeriod = newPeriod * 20
|
|
|
|
|
|
|
|
// act
|
|
|
|
err = a.SetPeriod("33", newPeriod)
|
|
|
|
|
|
|
|
// assert
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, fs.Files[pwm2DutyCyclePath].Contents, fmt.Sprintf("%d", 2540000))
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestI2c(t *testing.T) {
|
|
|
|
a, _ := initTestAdaptorWithMockedFilesystem([]string{"/dev/i2c-1"})
|
2022-11-20 19:22:26 +01:00
|
|
|
a.sys.UseMockSyscall()
|
2022-05-01 16:12:39 +02:00
|
|
|
|
|
|
|
con, err := a.GetConnection(0xff, 1)
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
_, err = con.Write([]byte{0x00, 0x01})
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
2022-05-01 16:12:39 +02:00
|
|
|
data := []byte{42, 42}
|
2022-11-05 07:42:28 +01:00
|
|
|
_, err = con.Read(data)
|
|
|
|
gobottest.Assert(t, err, nil)
|
2022-05-01 16:12:39 +02:00
|
|
|
gobottest.Assert(t, data, []byte{0x00, 0x01})
|
|
|
|
|
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2017-04-27 12:21:38 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestDefaultBus(t *testing.T) {
|
2022-05-01 16:12:39 +02:00
|
|
|
a := NewAdaptor()
|
2017-04-20 19:32:38 +02:00
|
|
|
gobottest.Assert(t, a.GetDefaultBus(), 1)
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestGetConnectionInvalidBus(t *testing.T) {
|
2022-05-01 16:12:39 +02:00
|
|
|
a := NewAdaptor()
|
2017-04-20 19:32:38 +02:00
|
|
|
_, err := a.GetConnection(0x01, 99)
|
|
|
|
gobottest.Assert(t, err, errors.New("Bus number 99 out of range"))
|
|
|
|
}
|
2017-04-29 12:50:39 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestFinalizeErrorAfterGPIO(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem(gpioMockPaths)
|
2017-04-29 12:50:39 +02:00
|
|
|
|
|
|
|
gobottest.Assert(t, a.DigitalWrite("7", 1), nil)
|
|
|
|
|
|
|
|
fs.WithWriteError = true
|
|
|
|
|
|
|
|
err := a.Finalize()
|
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "write error"), true)
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestFinalizeErrorAfterPWM(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem(pwmMockPaths)
|
|
|
|
preparePwmFs(fs)
|
2017-04-29 12:50:39 +02:00
|
|
|
|
|
|
|
gobottest.Assert(t, a.PwmWrite("33", 1), nil)
|
|
|
|
|
|
|
|
fs.WithWriteError = true
|
|
|
|
|
|
|
|
err := a.Finalize()
|
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "write error"), true)
|
|
|
|
}
|