Add channel timeouts

This commit is contained in:
Adrian Zankich 2014-07-14 13:34:46 -07:00
parent 83babbbe87
commit f6c72529f0
2 changed files with 21 additions and 4 deletions

View File

@ -31,8 +31,8 @@ func (e *Event) Write(data interface{}) {
} }
func (e *Event) Read() { func (e *Event) Read() {
tmp := []callback{}
for s := range e.Chan { for s := range e.Chan {
tmp := []callback{}
for i := range e.Callbacks { for i := range e.Callbacks {
go e.Callbacks[i].f(s) go e.Callbacks[i].f(s)
if !e.Callbacks[i].once { if !e.Callbacks[i].once {

View File

@ -3,6 +3,7 @@ package firmata
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"time"
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
"github.com/tarm/goserial" "github.com/tarm/goserial"
@ -72,6 +73,7 @@ func (f *FirmataAdaptor) DigitalWrite(pin string, level byte) {
func (f *FirmataAdaptor) DigitalRead(pin string) int { func (f *FirmataAdaptor) DigitalRead(pin string) int {
ret := make(chan int) ret := make(chan int)
p, _ := strconv.Atoi(pin) p, _ := strconv.Atoi(pin)
f.board.setPinMode(byte(p), input) f.board.setPinMode(byte(p), input)
f.board.togglePinReporting(byte(p), high, reportDigital) f.board.togglePinReporting(byte(p), high, reportDigital)
@ -81,7 +83,12 @@ func (f *FirmataAdaptor) DigitalRead(pin string) int {
ret <- int(data.([]byte)[0]) ret <- int(data.([]byte)[0])
}) })
return <-ret select {
case data := <-ret:
return data
case <-time.After(10 * time.Millisecond):
}
return -1
} }
// NOTE pins are numbered A0-A5, which translate to digital pins 14-19 // NOTE pins are numbered A0-A5, which translate to digital pins 14-19
@ -99,7 +106,12 @@ func (f *FirmataAdaptor) AnalogRead(pin string) int {
ret <- int(uint(b[0])<<24 | uint(b[1])<<16 | uint(b[2])<<8 | uint(b[3])) ret <- int(uint(b[0])<<24 | uint(b[1])<<16 | uint(b[2])<<8 | uint(b[3]))
}) })
return <-ret select {
case data := <-ret:
return data
case <-time.After(10 * time.Millisecond):
}
return -1
} }
func (f *FirmataAdaptor) AnalogWrite(pin string, level byte) { func (f *FirmataAdaptor) AnalogWrite(pin string, level byte) {
@ -125,7 +137,12 @@ func (f *FirmataAdaptor) I2cRead(size uint) []byte {
ret <- data.(map[string][]byte)["data"] ret <- data.(map[string][]byte)["data"]
}) })
return <-ret select {
case data := <-ret:
return data
case <-time.After(10 * time.Millisecond):
}
return []byte{}
} }
func (f *FirmataAdaptor) I2cWrite(data []byte) { func (f *FirmataAdaptor) I2cWrite(data []byte) {