2016-01-30 02:00:27 -08:00
|
|
|
package chip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-04-06 09:53:52 +02:00
|
|
|
"strings"
|
2016-01-30 02:00:27 -08:00
|
|
|
"testing"
|
|
|
|
|
2016-12-08 13:24:03 +01:00
|
|
|
"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"
|
2016-01-30 02:00:27 -08:00
|
|
|
)
|
|
|
|
|
2022-11-20 19:22:26 +01:00
|
|
|
// make sure that this Adaptor fulfills all the required interfaces
|
2016-09-25 20:19:19 +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)
|
2016-09-25 20:19:19 +02:00
|
|
|
var _ gpio.DigitalReader = (*Adaptor)(nil)
|
|
|
|
var _ gpio.DigitalWriter = (*Adaptor)(nil)
|
2017-04-22 15:04:20 +02:00
|
|
|
var _ gpio.PwmWriter = (*Adaptor)(nil)
|
2017-04-22 11:50:39 +02:00
|
|
|
var _ gpio.ServoWriter = (*Adaptor)(nil)
|
2017-02-10 11:08:32 +01:00
|
|
|
var _ i2c.Connector = (*Adaptor)(nil)
|
2016-08-26 14:23:03 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
var mockPaths = []string{
|
|
|
|
"/sys/class/gpio/export",
|
|
|
|
"/sys/class/gpio/unexport",
|
|
|
|
"/sys/class/gpio/gpio50/value",
|
|
|
|
"/sys/class/gpio/gpio50/direction",
|
|
|
|
"/sys/class/gpio/gpio139/value",
|
|
|
|
"/sys/class/gpio/gpio139/direction",
|
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm0/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm0/duty_cycle",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm0/polarity",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm0/period",
|
|
|
|
}
|
|
|
|
|
2022-11-20 19:22:26 +01:00
|
|
|
func initTestAdaptorWithMockedFilesystem() (*Adaptor, *system.MockFilesystem) {
|
2016-09-25 20:19:19 +02:00
|
|
|
a := NewAdaptor()
|
2022-11-20 19:22:26 +01:00
|
|
|
fs := a.sys.UseMockFilesystem(mockPaths)
|
2017-04-27 10:04:54 +02:00
|
|
|
return a, fs
|
2017-04-06 09:53:52 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 19:22:26 +01:00
|
|
|
func initTestProAdaptorWithMockedFilesystem() (*Adaptor, *system.MockFilesystem) {
|
2017-04-27 10:04:54 +02:00
|
|
|
a := NewProAdaptor()
|
2022-11-20 19:22:26 +01:00
|
|
|
fs := a.sys.UseMockFilesystem(mockPaths)
|
2017-04-27 10:04:54 +02:00
|
|
|
return a, fs
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestName(t *testing.T) {
|
2017-04-27 10:04:54 +02:00
|
|
|
a := NewAdaptor()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(a.Name(), "CHIP"), true)
|
|
|
|
a.SetName("NewName")
|
|
|
|
gobottest.Assert(t, a.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestNewAdaptor(t *testing.T) {
|
2017-04-27 11:22:15 +02:00
|
|
|
a := NewAdaptor()
|
|
|
|
a.SetBoard("pro")
|
|
|
|
gobottest.Assert(t, a.board, "pro")
|
2017-04-27 11:49:29 +02:00
|
|
|
|
|
|
|
gobottest.Assert(t, a.SetBoard("bad"), errors.New("Invalid board type"))
|
2017-04-27 11:22:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestFinalizeErrorAfterGPIO(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem()
|
2017-04-27 11:22:15 +02:00
|
|
|
gobottest.Assert(t, a.Connect(), nil)
|
|
|
|
gobottest.Assert(t, a.DigitalWrite("CSID7", 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()
|
2017-04-27 11:22:15 +02:00
|
|
|
gobottest.Assert(t, a.Connect(), nil)
|
|
|
|
gobottest.Assert(t, a.PwmWrite("PWM0", 100), 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 TestDigitalIO(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem()
|
2017-04-27 10:04:54 +02:00
|
|
|
a.Connect()
|
2016-01-30 02:00:27 -08:00
|
|
|
|
2017-01-11 22:21:07 +01:00
|
|
|
a.DigitalWrite("CSID7", 1)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio139/value"].Contents, "1")
|
2016-01-30 02:00:27 -08:00
|
|
|
|
2017-01-11 22:21:07 +01:00
|
|
|
fs.Files["/sys/class/gpio/gpio50/value"].Contents = "1"
|
|
|
|
i, _ := a.DigitalRead("TWI2-SDA")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, i, 1)
|
2016-01-30 02:00:27 -08:00
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.DigitalWrite("XIO-P10", 1), errors.New("Not a valid pin"))
|
2017-04-22 12:03:01 +02:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2016-01-30 02:00:27 -08:00
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestProDigitalIO(t *testing.T) {
|
|
|
|
a, fs := initTestProAdaptorWithMockedFilesystem()
|
2017-04-27 10:04:54 +02:00
|
|
|
a.Connect()
|
2017-04-22 13:34:19 +02:00
|
|
|
|
|
|
|
a.DigitalWrite("CSID7", 1)
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio139/value"].Contents, "1")
|
|
|
|
|
|
|
|
fs.Files["/sys/class/gpio/gpio50/value"].Contents = "1"
|
|
|
|
i, _ := a.DigitalRead("TWI2-SDA")
|
|
|
|
gobottest.Assert(t, i, 1)
|
|
|
|
|
|
|
|
gobottest.Assert(t, a.DigitalWrite("XIO-P0", 1), errors.New("Not a valid pin"))
|
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestDigitalWriteError(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem()
|
2017-04-27 10:04:54 +02:00
|
|
|
fs.WithWriteError = true
|
|
|
|
|
|
|
|
err := a.DigitalWrite("CSID7", 1)
|
|
|
|
gobottest.Assert(t, err, errors.New("write error"))
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestDigitalReadWriteError(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem()
|
2017-04-27 10:04:54 +02:00
|
|
|
fs.WithWriteError = true
|
|
|
|
|
|
|
|
_, err := a.DigitalRead("CSID7")
|
|
|
|
gobottest.Assert(t, err, errors.New("write error"))
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestI2c(t *testing.T) {
|
2017-04-27 10:04:54 +02:00
|
|
|
a := NewAdaptor()
|
2022-11-20 19:22:26 +01:00
|
|
|
a.sys.UseMockFilesystem([]string{"/dev/i2c-1"})
|
|
|
|
a.sys.UseMockSyscall()
|
2017-04-27 10:04:54 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
a.Connect()
|
2017-02-06 14:50:14 +01:00
|
|
|
|
2017-02-10 11:08:32 +01:00
|
|
|
con, err := a.GetConnection(0xff, 1)
|
2017-02-06 14:50:14 +01:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
_, err = con.Write([]byte{0x00, 0x01})
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
2017-02-06 14:50:14 +01:00
|
|
|
data := []byte{42, 42}
|
2022-11-05 07:42:28 +01:00
|
|
|
_, err = con.Read(data)
|
|
|
|
gobottest.Assert(t, err, nil)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, data, []byte{0x00, 0x01})
|
2016-01-30 02:00:27 -08:00
|
|
|
|
2016-11-07 17:43:55 +01:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2016-01-30 02:00:27 -08:00
|
|
|
}
|
2017-03-13 22:46:26 +01:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestInvalidPWMPin(t *testing.T) {
|
|
|
|
a, _ := initTestAdaptorWithMockedFilesystem()
|
2017-04-27 10:04:54 +02:00
|
|
|
a.Connect()
|
2017-03-13 22:46:26 +01:00
|
|
|
|
|
|
|
err := a.PwmWrite("LCD-D2", 42)
|
2022-11-05 07:42:28 +01:00
|
|
|
gobottest.Assert(t, err.Error(), "Not a PWM pin")
|
2017-03-13 22:46:26 +01:00
|
|
|
|
2017-04-22 11:50:39 +02:00
|
|
|
err = a.ServoWrite("LCD-D2", 120)
|
2022-11-05 07:42:28 +01:00
|
|
|
gobottest.Assert(t, err.Error(), "Not a PWM pin")
|
2017-03-13 22:46:26 +01:00
|
|
|
}
|
2017-03-14 00:11:06 +01:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestPWM(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem()
|
2017-04-27 10:04:54 +02:00
|
|
|
a.Connect()
|
2017-03-14 00:11:06 +01:00
|
|
|
|
2017-04-22 10:41:47 +02:00
|
|
|
err := a.PwmWrite("PWM0", 100)
|
2017-03-14 00:11:06 +01:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/export"].Contents, "0")
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/enable"].Contents, "1")
|
2017-04-22 11:50:39 +02:00
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "3921568")
|
2017-03-14 00:11:06 +01:00
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/polarity"].Contents, "normal")
|
|
|
|
|
2017-04-22 11:50:39 +02:00
|
|
|
err = a.ServoWrite("PWM0", 0)
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
|
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "500000")
|
|
|
|
|
|
|
|
err = a.ServoWrite("PWM0", 180)
|
|
|
|
gobottest.Assert(t, err, nil)
|
2017-03-14 00:11:06 +01:00
|
|
|
|
2017-04-22 11:50:39 +02:00
|
|
|
gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "2000000")
|
2017-04-22 12:03:01 +02:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2017-03-14 00:11:06 +01:00
|
|
|
}
|
2017-04-11 18:35:18 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestPwmWriteError(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem()
|
2017-04-27 10:04:54 +02:00
|
|
|
fs.WithWriteError = true
|
|
|
|
|
|
|
|
err := a.PwmWrite("PWM0", 100)
|
2022-09-25 14:26:57 +02:00
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "write error"), true)
|
2017-04-27 10:04:54 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestPwmReadError(t *testing.T) {
|
|
|
|
a, fs := initTestAdaptorWithMockedFilesystem()
|
2017-04-27 10:04:54 +02:00
|
|
|
fs.WithReadError = true
|
|
|
|
|
|
|
|
err := a.PwmWrite("PWM0", 100)
|
2022-09-25 14:26:57 +02:00
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "read error"), true)
|
2017-04-27 10:04:54 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestDefaultBus(t *testing.T) {
|
|
|
|
a, _ := initTestAdaptorWithMockedFilesystem()
|
2017-04-11 18:35:18 +02:00
|
|
|
gobottest.Assert(t, a.GetDefaultBus(), 1)
|
|
|
|
}
|
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
func TestGetConnectionInvalidBus(t *testing.T) {
|
|
|
|
a, _ := initTestAdaptorWithMockedFilesystem()
|
2017-04-11 18:35:18 +02:00
|
|
|
_, err := a.GetConnection(0x01, 99)
|
|
|
|
gobottest.Assert(t, err, errors.New("Bus number 99 out of range"))
|
|
|
|
}
|