Green tests

This commit is contained in:
Adrian Zankich 2014-07-14 14:10:04 -07:00
parent f6c72529f0
commit c903bebc20
3 changed files with 50 additions and 32 deletions

View File

@ -51,14 +51,15 @@ const (
)
type board struct {
serial io.ReadWriteCloser
pins []pin
analogPins []byte
firmwareName string
majorVersion byte
minorVersion byte
connected bool
events map[string]*gobot.Event
serial io.ReadWriteCloser
pins []pin
analogPins []byte
firmwareName string
majorVersion byte
minorVersion byte
connected bool
events map[string]*gobot.Event
initTimeInterval time.Duration
}
type pin struct {
@ -70,14 +71,15 @@ type pin struct {
func newBoard(sp io.ReadWriteCloser) *board {
board := &board{
majorVersion: 0,
minorVersion: 0,
serial: sp,
firmwareName: "",
pins: []pin{},
analogPins: []byte{},
connected: false,
events: make(map[string]*gobot.Event),
majorVersion: 0,
minorVersion: 0,
serial: sp,
firmwareName: "",
pins: []pin{},
analogPins: []byte{},
connected: false,
events: make(map[string]*gobot.Event),
initTimeInterval: 1 * time.Second,
}
for _, s := range []string{
@ -102,7 +104,7 @@ func (b *board) connect() {
for {
b.queryReportVersion()
<-time.After(1 * time.Second)
<-time.After(b.initTimeInterval)
b.readAndProcess()
if b.connected == true {
break
@ -122,9 +124,7 @@ func (b *board) initBoard() {
gobot.Once(b.events["analog_mapping_query"], func(data interface{}) {
b.togglePinReporting(0, high, reportDigital)
<-time.After(50 * time.Millisecond)
b.togglePinReporting(1, high, reportDigital)
<-time.After(50 * time.Millisecond)
b.connected = true
})
}

View File

@ -3,6 +3,7 @@ package firmata
import (
"fmt"
"testing"
"time"
"github.com/hybridgroup/gobot"
)
@ -10,13 +11,16 @@ import (
func initTestFirmataAdaptor() *FirmataAdaptor {
a := NewFirmataAdaptor("board", "/dev/null")
a.connect = func(f *FirmataAdaptor) {
f.Board = newBoard(gobot.NullReadWriteCloser{})
f.Board.Events = append(f.Board.Events, event{Name: "firmware_query"})
f.Board.Events = append(f.Board.Events, event{Name: "capability_query"})
f.Board.Events = append(f.Board.Events, event{Name: "analog_mapping_query"})
f.board = newBoard(gobot.NullReadWriteCloser{})
f.board.initTimeInterval = 0 * time.Second
f.board.pins = make([]pin, 100)
f.board.events["digital_read_1"] = gobot.NewEvent()
f.board.events["analog_read_1"] = gobot.NewEvent()
gobot.Publish(f.board.events["firmware_query"], nil)
gobot.Publish(f.board.events["capability_query"], nil)
gobot.Publish(f.board.events["analog_mapping_query"], nil)
}
a.Connect()
a.Board.Pins = make([]pin, 100)
return a
}
@ -28,31 +32,38 @@ func TestFirmataAdaptorConnect(t *testing.T) {
a := initTestFirmataAdaptor()
gobot.Expect(t, a.Connect(), true)
}
func TestFirmataAdaptorInitServo(t *testing.T) {
a := initTestFirmataAdaptor()
a.InitServo()
}
func TestFirmataAdaptorServoWrite(t *testing.T) {
a := initTestFirmataAdaptor()
a.ServoWrite("1", 50)
}
func TestFirmataAdaptorPwmWrite(t *testing.T) {
a := initTestFirmataAdaptor()
a.PwmWrite("1", 50)
}
func TestFirmataAdaptorDigitalWrite(t *testing.T) {
a := initTestFirmataAdaptor()
a.DigitalWrite("1", 1)
}
func TestFirmataAdaptorDigitalRead(t *testing.T) {
a := initTestFirmataAdaptor()
// -1 on no data
gobot.Expect(t, a.DigitalRead("1"), -1)
pinNumber := "1"
a.Board.Events = append(a.Board.Events, event{Name: fmt.Sprintf("digital_read_%v", pinNumber), Data: []byte{0x01}})
gobot.Publish(a.board.events[fmt.Sprintf("digital_read_%v", pinNumber)],
[]byte{0x01})
gobot.Expect(t, a.DigitalRead(pinNumber), 0x01)
}
func TestFirmataAdaptorAnalogRead(t *testing.T) {
a := initTestFirmataAdaptor()
// -1 on no data
@ -60,9 +71,20 @@ func TestFirmataAdaptorAnalogRead(t *testing.T) {
pinNumber := "1"
value := 133
a.Board.Events = append(a.Board.Events, event{Name: fmt.Sprintf("analog_read_%v", pinNumber), Data: []byte{byte(value >> 24), byte(value >> 16), byte(value >> 8), byte(value & 0xff)}})
gobot.Publish(a.board.events[fmt.Sprintf("analog_read_%v", pinNumber)],
[]byte{
byte(value >> 24),
byte(value >> 16),
byte(value >> 8),
byte(value & 0xff),
},
)
gobot.Expect(t, a.AnalogRead(pinNumber), 133)
}
func TestFirmataAdaptorAnalogWrite(t *testing.T) {
a := initTestFirmataAdaptor()
a.AnalogWrite("1", 50)
}
func TestFirmataAdaptorI2cStart(t *testing.T) {
a := initTestFirmataAdaptor()
a.I2cStart(0x00)
@ -75,7 +97,7 @@ func TestFirmataAdaptorI2cRead(t *testing.T) {
i := []byte{100}
i2cReply := map[string][]byte{}
i2cReply["data"] = i
a.Board.Events = append(a.Board.Events, event{Name: "i2c_reply", I2cReply: i2cReply})
gobot.Publish(a.board.events["i2c_reply"], i2cReply)
gobot.Expect(t, a.I2cRead(1), i)
}
func TestFirmataAdaptorI2cWrite(t *testing.T) {

View File

@ -32,18 +32,14 @@ func Publish(e *Event, val interface{}) {
func On(e *Event, f func(s interface{})) {
e.Callbacks = append(e.Callbacks, callback{f, false})
//e.Callbacks[f] = false
}
func Once(e *Event, f func(s interface{})) {
//e.Callbacks = append(e.Callbacks, f)
e.Callbacks = append(e.Callbacks, callback{f, true})
//e.Callbacks[f] = true
}
func Rand(max int) int {
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
return r.Intn(max)
return rand.New(rand.NewSource(time.Now().UTC().UnixNano())).Intn(max)
}
func FromScale(input, min, max float64) float64 {