2014-04-28 04:23:15 -07:00
|
|
|
package digispark
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
2014-11-19 18:44:55 -08:00
|
|
|
"errors"
|
2014-06-12 16:15:06 -07:00
|
|
|
"testing"
|
2014-11-04 20:37:07 -08:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2014-11-04 20: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-17 16:25:01 -08:00
|
|
|
func (l *mock) digitalWrite(pin uint8, state uint8) error {
|
2014-11-04 20:37:07 -08:00
|
|
|
l.pin = pin
|
|
|
|
l.state = state
|
2014-11-17 16:25:01 -08:00
|
|
|
return l.error()
|
2014-11-04 20:37:07 -08:00
|
|
|
}
|
2014-11-17 16:25:01 -08:00
|
|
|
func (l *mock) pinMode(pin uint8, mode uint8) error {
|
2014-11-04 20:37:07 -08:00
|
|
|
l.pin = pin
|
|
|
|
l.mode = mode
|
2014-11-17 16:25:01 -08:00
|
|
|
return l.error()
|
2014-11-04 20:37:07 -08:00
|
|
|
}
|
2014-11-17 16:25:01 -08:00
|
|
|
func (l *mock) pwmInit() error { return l.error() }
|
|
|
|
func (l *mock) pwmStop() error { return l.error() }
|
|
|
|
func (l *mock) pwmUpdateCompare(channelA uint8, channelB uint8) error {
|
2014-11-04 20:37:07 -08:00
|
|
|
l.pwmChannelA = channelA
|
|
|
|
l.pwmChannelB = channelB
|
2014-11-17 16:25:01 -08:00
|
|
|
return l.error()
|
2014-11-04 20:37:07 -08:00
|
|
|
}
|
2014-11-17 16:25:01 -08:00
|
|
|
func (l *mock) pwmUpdatePrescaler(value uint) error {
|
2014-11-04 20:37:07 -08:00
|
|
|
l.pwmPrescalerValue = value
|
2014-11-17 16:25:01 -08:00
|
|
|
return l.error()
|
2014-11-04 20:37:07 -08:00
|
|
|
}
|
2014-11-17 16:25:01 -08:00
|
|
|
func (l *mock) servoInit() error { return l.error() }
|
|
|
|
func (l *mock) servoUpdateLocation(locationA uint8, locationB uint8) error {
|
2014-11-04 20:37:07 -08:00
|
|
|
l.locationA = locationA
|
|
|
|
l.locationB = locationB
|
2014-11-17 16:25:01 -08:00
|
|
|
return l.error()
|
2014-11-04 20:37:07 -08:00
|
|
|
}
|
2014-11-17 16:25:01 -08:00
|
|
|
func (l *mock) error() error { return nil }
|
2014-11-04 20:37:07 -08:00
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func initTestDigisparkAdaptor() *DigisparkAdaptor {
|
|
|
|
a := NewDigisparkAdaptor("bot")
|
2014-11-17 16:25:01 -08:00
|
|
|
a.connect = func(a *DigisparkAdaptor) (err error) { return nil }
|
2014-11-04 20:37:07 -08:00
|
|
|
a.littleWire = new(mock)
|
2014-06-13 16:01:39 -07:00
|
|
|
return a
|
2014-06-12 16:15:06 -07:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-11-19 18:44:55 -08:00
|
|
|
func TestDigisparkAdaptorConnect(t *testing.T) {
|
|
|
|
a := NewDigisparkAdaptor("bot")
|
2014-11-19 23:21:19 -08:00
|
|
|
gobot.Assert(t, a.Connect()[0], errors.New("Error connecting to bot"))
|
2014-06-12 16:15:06 -07:00
|
|
|
}
|
2014-06-13 16:01:39 -07:00
|
|
|
|
2014-11-19 18:44:55 -08:00
|
|
|
func TestDigisparkAdaptorFinalize(t *testing.T) {
|
2014-06-13 16:01:39 -07:00
|
|
|
a := initTestDigisparkAdaptor()
|
2014-11-19 23:21:19 -08:00
|
|
|
gobot.Assert(t, len(a.Finalize()), 0)
|
2014-06-12 16:15:06 -07:00
|
|
|
}
|
2014-06-13 16:01:39 -07:00
|
|
|
|
2014-11-04 20:37:07 -08:00
|
|
|
func TestDigisparkAdaptorIO(t *testing.T) {
|
2014-06-13 16:01:39 -07:00
|
|
|
a := initTestDigisparkAdaptor()
|
2014-11-04 20:37:07 -08:00
|
|
|
a.InitServo()
|
|
|
|
a.DigitalRead("1")
|
|
|
|
a.DigitalWrite("0", uint8(1))
|
|
|
|
gobot.Assert(t, a.littleWire.(*mock).pin, uint8(0))
|
|
|
|
gobot.Assert(t, a.littleWire.(*mock).state, uint8(1))
|
|
|
|
a.PwmWrite("1", uint8(100))
|
|
|
|
gobot.Assert(t, a.littleWire.(*mock).pwmChannelA, uint8(100))
|
|
|
|
gobot.Assert(t, a.littleWire.(*mock).pwmChannelB, uint8(100))
|
|
|
|
a.ServoWrite("2", uint8(80))
|
|
|
|
gobot.Assert(t, a.littleWire.(*mock).locationA, uint8(80))
|
|
|
|
gobot.Assert(t, a.littleWire.(*mock).locationB, uint8(80))
|
2014-06-12 16:15:06 -07:00
|
|
|
}
|