2014-04-28 10:34:16 +08:00
|
|
|
package gpio
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
2014-12-17 05:42:48 +08:00
|
|
|
"errors"
|
2014-06-14 03:39:02 +08:00
|
|
|
"testing"
|
2014-09-28 02:45:52 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
var _ gobot.Driver = (*ServoDriver)(nil)
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func initTestServoDriver() *ServoDriver {
|
2016-09-25 19:36:18 +08:00
|
|
|
return NewServoDriver(newGpioTestAdaptor(), "1")
|
2014-06-14 03:39:02 +08:00
|
|
|
}
|
|
|
|
|
2014-12-17 05:42:48 +08:00
|
|
|
func TestServoDriver(t *testing.T) {
|
|
|
|
var err interface{}
|
|
|
|
|
|
|
|
d := initTestServoDriver()
|
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.Pin(), "1")
|
2016-09-25 19:36:18 +08:00
|
|
|
gobottest.Refute(t, d.Connection(), nil)
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
testAdaptorServoWrite = func() (err error) {
|
|
|
|
return errors.New("pwm error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Command("Min")(nil)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("pwm error"))
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
err = d.Command("Center")(nil)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("pwm error"))
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
err = d.Command("Max")(nil)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("pwm error"))
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
err = d.Command("Move")(map[string]interface{}{"angle": 100.0})
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("pwm error"))
|
2014-12-17 05:42:48 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestServoDriverStart(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
2016-11-08 00:12:14 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
2014-06-14 03:39:02 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestServoDriverHalt(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
2016-11-08 00:12:14 +08:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2014-06-14 03:39:02 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestServoDriverMove(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
|
|
|
d.Move(100)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.CurrentAngle, uint8(100))
|
2014-12-17 05:42:48 +08:00
|
|
|
err := d.Move(200)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, ErrServoOutOfRange)
|
2014-06-14 03:39:02 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestServoDriverMin(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
|
|
|
d.Min()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.CurrentAngle, uint8(0))
|
2014-06-14 03:39:02 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestServoDriverMax(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
|
|
|
d.Max()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.CurrentAngle, uint8(180))
|
2014-06-14 03:39:02 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestServoDriverCenter(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
|
|
|
d.Center()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.CurrentAngle, uint8(90))
|
2014-06-14 03:39:02 +08:00
|
|
|
}
|