2014-04-28 19:23:15 +08:00
|
|
|
package digispark
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
2014-06-13 07:15:06 +08:00
|
|
|
"testing"
|
2014-11-05 12:37:07 +08:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-04-26 18:11:51 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *mock) digitalWrite(pin uint8, state uint8) {
|
|
|
|
l.pin = pin
|
|
|
|
l.state = state
|
|
|
|
}
|
|
|
|
func (l *mock) pinMode(pin uint8, mode uint8) {
|
|
|
|
l.pin = pin
|
|
|
|
l.mode = mode
|
|
|
|
}
|
|
|
|
func (l *mock) pwmInit() {}
|
|
|
|
func (l *mock) pwmStop() {}
|
|
|
|
func (l *mock) pwmUpdateCompare(channelA uint8, channelB uint8) {
|
|
|
|
l.pwmChannelA = channelA
|
|
|
|
l.pwmChannelB = channelB
|
|
|
|
}
|
|
|
|
func (l *mock) pwmUpdatePrescaler(value uint) {
|
|
|
|
l.pwmPrescalerValue = value
|
|
|
|
}
|
|
|
|
func (l *mock) servoInit() {}
|
|
|
|
func (l *mock) servoUpdateLocation(locationA uint8, locationB uint8) {
|
|
|
|
l.locationA = locationA
|
|
|
|
l.locationB = locationB
|
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func initTestDigisparkAdaptor() *DigisparkAdaptor {
|
|
|
|
a := NewDigisparkAdaptor("bot")
|
|
|
|
a.connect = func(a *DigisparkAdaptor) {}
|
2014-11-05 12:37:07 +08:00
|
|
|
a.littleWire = new(mock)
|
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
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestDigisparkAdaptorFinalize(t *testing.T) {
|
|
|
|
a := initTestDigisparkAdaptor()
|
2014-11-17 04:25:48 +08:00
|
|
|
gobot.Assert(t, a.Finalize(), nil)
|
2014-06-13 07:15:06 +08:00
|
|
|
}
|
2014-06-14 07:01:39 +08:00
|
|
|
|
|
|
|
func TestDigisparkAdaptorConnect(t *testing.T) {
|
|
|
|
a := initTestDigisparkAdaptor()
|
2014-11-17 04:25:48 +08:00
|
|
|
gobot.Assert(t, a.Connect(), nil)
|
2014-06-13 07:15:06 +08:00
|
|
|
}
|
2014-06-14 07:01:39 +08:00
|
|
|
|
2014-11-05 12:37:07 +08:00
|
|
|
func TestDigisparkAdaptorIO(t *testing.T) {
|
2014-06-14 07:01:39 +08:00
|
|
|
a := initTestDigisparkAdaptor()
|
2014-11-05 12: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-13 07:15:06 +08:00
|
|
|
}
|