chip: create separate constructor for CHIP Pro, increase test coverage
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
1b777bffa8
commit
3b158402a7
|
@ -74,11 +74,10 @@ func main() {
|
|||
}
|
||||
```
|
||||
|
||||
If you want to use the C.H.I.P. Pro, use the `SetBoard()` function like this:
|
||||
If you want to use the C.H.I.P. Pro, use the `NewProAdaptor()` function like this:
|
||||
|
||||
```go
|
||||
chipProAdaptor := chip.NewAdaptor()
|
||||
chipProAdaptor.SetBoard("CHIP Pro")
|
||||
chipProAdaptor := chip.NewProAdaptor()
|
||||
```
|
||||
|
||||
## How to Connect
|
||||
|
|
|
@ -32,9 +32,19 @@ type Adaptor struct {
|
|||
// NewAdaptor creates a C.H.I.P. Adaptor
|
||||
func NewAdaptor() *Adaptor {
|
||||
c := &Adaptor{
|
||||
name: gobot.DefaultName("CHIP"),
|
||||
board: "CHIP",
|
||||
digitalPins: make(map[int]sysfs.DigitalPin),
|
||||
name: gobot.DefaultName("CHIP"),
|
||||
board: "chip",
|
||||
}
|
||||
|
||||
c.setPins()
|
||||
return c
|
||||
}
|
||||
|
||||
// NewAdaptor creates a C.H.I.P. Pro Adaptor
|
||||
func NewProAdaptor() *Adaptor {
|
||||
c := &Adaptor{
|
||||
name: gobot.DefaultName("CHIP Pro"),
|
||||
board: "pro",
|
||||
}
|
||||
|
||||
c.setPins()
|
||||
|
@ -49,7 +59,6 @@ func (c *Adaptor) SetName(n string) { c.name = n }
|
|||
|
||||
// Connect initializes the board
|
||||
func (c *Adaptor) Connect() (err error) {
|
||||
c.pwmPins = make(map[int]*sysfs.PWMPin)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -158,7 +167,7 @@ func (c *Adaptor) ServoWrite(pin string, angle byte) (err error) {
|
|||
|
||||
// SetBoard sets the name of the type of board
|
||||
func (c *Adaptor) SetBoard(n string) (err error) {
|
||||
if n == "CHIP Pro" || n == "CHIP" {
|
||||
if n == "chip" || n == "pro" {
|
||||
c.board = n
|
||||
c.setPins()
|
||||
return
|
||||
|
@ -167,7 +176,10 @@ func (c *Adaptor) SetBoard(n string) (err error) {
|
|||
}
|
||||
|
||||
func (c *Adaptor) setPins() {
|
||||
if c.board == "CHIP Pro" {
|
||||
c.digitalPins = make(map[int]sysfs.DigitalPin)
|
||||
c.pwmPins = make(map[int]*sysfs.PWMPin)
|
||||
|
||||
if c.board == "pro" {
|
||||
c.pinmap = chipProPins
|
||||
return
|
||||
}
|
||||
|
|
|
@ -20,10 +20,46 @@ var _ gpio.PwmWriter = (*Adaptor)(nil)
|
|||
var _ gpio.ServoWriter = (*Adaptor)(nil)
|
||||
var _ i2c.Connector = (*Adaptor)(nil)
|
||||
|
||||
func initTestChipAdaptor() *Adaptor {
|
||||
func initTestChipAdaptor() (*Adaptor, *sysfs.MockFilesystem) {
|
||||
a := NewAdaptor()
|
||||
a.Connect()
|
||||
return a
|
||||
fs := sysfs.NewMockFilesystem([]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",
|
||||
})
|
||||
|
||||
sysfs.SetFilesystem(fs)
|
||||
return a, fs
|
||||
}
|
||||
|
||||
func initTestChipProAdaptor() (*Adaptor, *sysfs.MockFilesystem) {
|
||||
a := NewProAdaptor()
|
||||
fs := sysfs.NewMockFilesystem([]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",
|
||||
})
|
||||
|
||||
sysfs.SetFilesystem(fs)
|
||||
return a, fs
|
||||
}
|
||||
|
||||
func TestChipAdaptorName(t *testing.T) {
|
||||
|
@ -34,17 +70,8 @@ func TestChipAdaptorName(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestChipAdaptorDigitalIO(t *testing.T) {
|
||||
a := initTestChipAdaptor()
|
||||
fs := sysfs.NewMockFilesystem([]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",
|
||||
})
|
||||
|
||||
sysfs.SetFilesystem(fs)
|
||||
a, fs := initTestChipAdaptor()
|
||||
a.Connect()
|
||||
|
||||
a.DigitalWrite("CSID7", 1)
|
||||
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio139/value"].Contents, "1")
|
||||
|
@ -58,18 +85,8 @@ func TestChipAdaptorDigitalIO(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestChipProAdaptorDigitalIO(t *testing.T) {
|
||||
a := initTestChipAdaptor()
|
||||
a.SetBoard("CHIP Pro")
|
||||
fs := sysfs.NewMockFilesystem([]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",
|
||||
})
|
||||
|
||||
sysfs.SetFilesystem(fs)
|
||||
a, fs := initTestChipProAdaptor()
|
||||
a.Connect()
|
||||
|
||||
a.DigitalWrite("CSID7", 1)
|
||||
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio139/value"].Contents, "1")
|
||||
|
@ -82,8 +99,26 @@ func TestChipProAdaptorDigitalIO(t *testing.T) {
|
|||
gobottest.Assert(t, a.Finalize(), nil)
|
||||
}
|
||||
|
||||
func TestAdaptorDigitalWriteError(t *testing.T) {
|
||||
a, fs := initTestChipAdaptor()
|
||||
fs.WithWriteError = true
|
||||
|
||||
err := a.DigitalWrite("CSID7", 1)
|
||||
gobottest.Assert(t, err, errors.New("write error"))
|
||||
}
|
||||
|
||||
func TestAdaptorDigitalReadWriteError(t *testing.T) {
|
||||
a, fs := initTestChipAdaptor()
|
||||
fs.WithWriteError = true
|
||||
|
||||
_, err := a.DigitalRead("CSID7")
|
||||
gobottest.Assert(t, err, errors.New("write error"))
|
||||
}
|
||||
|
||||
func TestChipAdaptorI2c(t *testing.T) {
|
||||
a := initTestChipAdaptor()
|
||||
a := NewAdaptor()
|
||||
a.Connect()
|
||||
|
||||
fs := sysfs.NewMockFilesystem([]string{
|
||||
"/dev/i2c-1",
|
||||
})
|
||||
|
@ -102,7 +137,8 @@ func TestChipAdaptorI2c(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestChipAdaptorInvalidPWMPin(t *testing.T) {
|
||||
a := initTestChipAdaptor()
|
||||
a, _ := initTestChipAdaptor()
|
||||
a.Connect()
|
||||
|
||||
err := a.PwmWrite("LCD-D2", 42)
|
||||
gobottest.Refute(t, err, nil)
|
||||
|
@ -112,16 +148,8 @@ func TestChipAdaptorInvalidPWMPin(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestChipAdaptorPWM(t *testing.T) {
|
||||
a := initTestChipAdaptor()
|
||||
fs := sysfs.NewMockFilesystem([]string{
|
||||
"/sys/class/pwm/pwmchip0/export",
|
||||
"/sys/class/pwm/pwmchip0/unexport",
|
||||
"/sys/class/pwm/pwmchip0/pwm0/enable",
|
||||
"/sys/class/pwm/pwmchip0/pwm0/period",
|
||||
"/sys/class/pwm/pwmchip0/pwm0/duty_cycle",
|
||||
"/sys/class/pwm/pwmchip0/pwm0/polarity",
|
||||
})
|
||||
sysfs.SetFilesystem(fs)
|
||||
a, fs := initTestChipAdaptor()
|
||||
a.Connect()
|
||||
|
||||
err := a.PwmWrite("PWM0", 100)
|
||||
gobottest.Assert(t, err, nil)
|
||||
|
@ -143,13 +171,29 @@ func TestChipAdaptorPWM(t *testing.T) {
|
|||
gobottest.Assert(t, a.Finalize(), nil)
|
||||
}
|
||||
|
||||
func TestAdaptorPwmWriteError(t *testing.T) {
|
||||
a, fs := initTestChipAdaptor()
|
||||
fs.WithWriteError = true
|
||||
|
||||
err := a.PwmWrite("PWM0", 100)
|
||||
gobottest.Assert(t, err, errors.New("write error"))
|
||||
}
|
||||
|
||||
func TestAdaptorPwmReadError(t *testing.T) {
|
||||
a, fs := initTestChipAdaptor()
|
||||
fs.WithReadError = true
|
||||
|
||||
err := a.PwmWrite("PWM0", 100)
|
||||
gobottest.Assert(t, err, errors.New("read error"))
|
||||
}
|
||||
|
||||
func TestChipDefaultBus(t *testing.T) {
|
||||
a := initTestChipAdaptor()
|
||||
a, _ := initTestChipAdaptor()
|
||||
gobottest.Assert(t, a.GetDefaultBus(), 1)
|
||||
}
|
||||
|
||||
func TestChipGetConnectionInvalidBus(t *testing.T) {
|
||||
a := initTestChipAdaptor()
|
||||
a, _ := initTestChipAdaptor()
|
||||
_, err := a.GetConnection(0x01, 99)
|
||||
gobottest.Assert(t, err, errors.New("Bus number 99 out of range"))
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Package chip contains the Gobot adaptor for the CHIP
|
||||
Package chip contains the Gobot adaptor for the CHIP and CHIP Pro
|
||||
|
||||
For further information refer to the chip README:
|
||||
https://github.com/hybridgroup/gobot/blob/master/platforms/chip/README.md
|
||||
|
|
Loading…
Reference in New Issue