2014-04-28 19:23:15 +08:00
|
|
|
package digispark
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
2014-11-20 10:44:55 +08:00
|
|
|
"errors"
|
2017-04-06 15:55:04 +08:00
|
|
|
"strings"
|
2014-06-13 07:15:06 +08:00
|
|
|
"testing"
|
2014-11-05 12:37:07 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2016-07-14 01:01:36 +08:00
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
var _ gpio.DigitalWriter = (*Adaptor)(nil)
|
|
|
|
var _ gpio.PwmWriter = (*Adaptor)(nil)
|
|
|
|
var _ gpio.ServoWriter = (*Adaptor)(nil)
|
2016-07-14 01:01:36 +08:00
|
|
|
|
2014-11-05 12:37:07 +08:00
|
|
|
type mock struct {
|
|
|
|
locationA uint8
|
|
|
|
locationB uint8
|
|
|
|
pwmChannelA uint8
|
|
|
|
pwmChannelB uint8
|
|
|
|
pwmPrescalerValue uint
|
|
|
|
pin uint8
|
|
|
|
mode uint8
|
|
|
|
state uint8
|
|
|
|
}
|
|
|
|
|
2014-11-18 08:25:01 +08:00
|
|
|
func (l *mock) digitalWrite(pin uint8, state uint8) error {
|
2014-11-05 12:37:07 +08:00
|
|
|
l.pin = pin
|
|
|
|
l.state = state
|
2014-11-18 08:25:01 +08:00
|
|
|
return l.error()
|
2014-11-05 12:37:07 +08:00
|
|
|
}
|
2014-11-18 08:25:01 +08:00
|
|
|
func (l *mock) pinMode(pin uint8, mode uint8) error {
|
2014-11-05 12:37:07 +08:00
|
|
|
l.pin = pin
|
|
|
|
l.mode = mode
|
2014-11-18 08:25:01 +08:00
|
|
|
return l.error()
|
2014-11-05 12:37:07 +08:00
|
|
|
}
|
2014-12-19 04:37:17 +08:00
|
|
|
|
|
|
|
var pwmInitErrorFunc = func() error { return nil }
|
|
|
|
|
|
|
|
func (l *mock) pwmInit() error { return pwmInitErrorFunc() }
|
2014-11-18 08:25:01 +08:00
|
|
|
func (l *mock) pwmStop() error { return l.error() }
|
|
|
|
func (l *mock) pwmUpdateCompare(channelA uint8, channelB uint8) error {
|
2014-11-05 12:37:07 +08:00
|
|
|
l.pwmChannelA = channelA
|
|
|
|
l.pwmChannelB = channelB
|
2014-11-18 08:25:01 +08:00
|
|
|
return l.error()
|
2014-11-05 12:37:07 +08:00
|
|
|
}
|
2014-11-18 08:25:01 +08:00
|
|
|
func (l *mock) pwmUpdatePrescaler(value uint) error {
|
2014-11-05 12:37:07 +08:00
|
|
|
l.pwmPrescalerValue = value
|
2014-11-18 08:25:01 +08:00
|
|
|
return l.error()
|
2014-11-05 12:37:07 +08:00
|
|
|
}
|
2014-11-18 08:25:01 +08:00
|
|
|
func (l *mock) servoInit() error { return l.error() }
|
|
|
|
func (l *mock) servoUpdateLocation(locationA uint8, locationB uint8) error {
|
2014-11-05 12:37:07 +08:00
|
|
|
l.locationA = locationA
|
|
|
|
l.locationB = locationB
|
2014-11-18 08:25:01 +08:00
|
|
|
return l.error()
|
2014-11-05 12:37:07 +08:00
|
|
|
}
|
2014-12-19 04:37:17 +08:00
|
|
|
|
|
|
|
var errorFunc = func() error { return nil }
|
|
|
|
|
|
|
|
func (l *mock) error() error { return errorFunc() }
|
2014-11-05 12:37:07 +08:00
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
func initTestAdaptor() *Adaptor {
|
|
|
|
a := NewAdaptor()
|
|
|
|
a.connect = func(a *Adaptor) (err error) { return nil }
|
2014-11-05 12:37:07 +08:00
|
|
|
a.littleWire = new(mock)
|
2014-12-19 04:37:17 +08:00
|
|
|
errorFunc = func() error { return nil }
|
|
|
|
pwmInitErrorFunc = func() error { return nil }
|
2014-06-14 07:01:39 +08:00
|
|
|
return a
|
2014-06-13 07:15:06 +08:00
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2017-04-06 15:55:04 +08:00
|
|
|
func TestDigisparkAdaptorName(t *testing.T) {
|
|
|
|
a := NewAdaptor()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(a.Name(), "Digispark"), true)
|
|
|
|
a.SetName("NewName")
|
|
|
|
gobottest.Assert(t, a.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
func TestAdaptorConnect(t *testing.T) {
|
|
|
|
a := NewAdaptor()
|
2016-11-08 02:10:22 +08:00
|
|
|
gobottest.Assert(t, a.Connect(), ErrConnection)
|
2014-12-19 04:37:17 +08:00
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
a = initTestAdaptor()
|
2016-11-08 02:10:22 +08:00
|
|
|
gobottest.Assert(t, a.Connect(), nil)
|
2014-06-13 07:15:06 +08:00
|
|
|
}
|
2014-06-14 07:01:39 +08:00
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
func TestAdaptorFinalize(t *testing.T) {
|
|
|
|
a := initTestAdaptor()
|
2016-11-08 02:10:22 +08:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2014-06-13 07:15:06 +08:00
|
|
|
}
|
2014-06-14 07:01:39 +08:00
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
func TestAdaptorDigitalWrite(t *testing.T) {
|
|
|
|
a := initTestAdaptor()
|
2014-12-19 04:37:17 +08:00
|
|
|
err := a.DigitalWrite("0", uint8(1))
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, a.littleWire.(*mock).pin, uint8(0))
|
|
|
|
gobottest.Assert(t, a.littleWire.(*mock).state, uint8(1))
|
2014-12-19 04:37:17 +08:00
|
|
|
|
|
|
|
err = a.DigitalWrite("?", uint8(1))
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Refute(t, err, nil)
|
2014-12-19 04:37:17 +08:00
|
|
|
|
|
|
|
errorFunc = func() error { return errors.New("pin mode error") }
|
|
|
|
err = a.DigitalWrite("0", uint8(1))
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, errors.New("pin mode error"))
|
2014-12-19 04:37:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
func TestAdaptorServoWrite(t *testing.T) {
|
|
|
|
a := initTestAdaptor()
|
2014-12-19 04:37:17 +08:00
|
|
|
err := a.ServoWrite("2", uint8(80))
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, a.littleWire.(*mock).locationA, uint8(80))
|
|
|
|
gobottest.Assert(t, a.littleWire.(*mock).locationB, uint8(80))
|
2014-12-19 04:37:17 +08:00
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
a = initTestAdaptor()
|
2014-12-19 04:37:17 +08:00
|
|
|
errorFunc = func() error { return errors.New("servo error") }
|
|
|
|
err = a.ServoWrite("2", uint8(80))
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, errors.New("servo error"))
|
2014-12-19 04:37:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
func TestAdaptorPwmWrite(t *testing.T) {
|
|
|
|
a := initTestAdaptor()
|
2014-12-19 04:37:17 +08:00
|
|
|
err := a.PwmWrite("1", uint8(100))
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, a.littleWire.(*mock).pwmChannelA, uint8(100))
|
|
|
|
gobottest.Assert(t, a.littleWire.(*mock).pwmChannelB, uint8(100))
|
2014-12-19 04:37:17 +08:00
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
a = initTestAdaptor()
|
2014-12-19 04:37:17 +08:00
|
|
|
pwmInitErrorFunc = func() error { return errors.New("pwminit error") }
|
|
|
|
err = a.PwmWrite("1", uint8(100))
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, errors.New("pwminit error"))
|
2014-12-19 04:37:17 +08:00
|
|
|
|
2016-09-26 02:43:09 +08:00
|
|
|
a = initTestAdaptor()
|
2014-12-19 04:37:17 +08:00
|
|
|
errorFunc = func() error { return errors.New("pwm error") }
|
|
|
|
err = a.PwmWrite("1", uint8(100))
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, errors.New("pwm error"))
|
2014-06-13 07:15:06 +08:00
|
|
|
}
|