Skip to content

Commit

Permalink
Green tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jul 15, 2014
1 parent f6c7252 commit c903beb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
38 changes: 19 additions & 19 deletions platforms/firmata/firmata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -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
})
}
Expand Down
38 changes: 30 additions & 8 deletions platforms/firmata/firmata_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ package firmata
import (
"fmt"
"testing"
"time"

"github.com/hybridgroup/gobot"
)

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
}

Expand All @@ -28,41 +32,59 @@ 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
gobot.Expect(t, a.AnalogRead("1"), -1)

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)
Expand All @@ -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) {
Expand Down
6 changes: 1 addition & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c903beb

Please sign in to comment.