Skip to content

Commit

Permalink
Add 'Once' Event function
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jul 15, 2014
1 parent d8be3d1 commit 83babbb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
18 changes: 14 additions & 4 deletions event.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package gobot

type callback struct {
f func(interface{})
once bool
}

type Event struct {
Chan chan interface{}
Callbacks []func(interface{})
Callbacks []callback
}

func NewEvent() *Event {
e := &Event{
Chan: make(chan interface{}, 1),
Callbacks: []func(interface{}){},
Callbacks: []callback{},
}
go func() {
for {
Expand All @@ -26,9 +31,14 @@ func (e *Event) Write(data interface{}) {
}

func (e *Event) Read() {
tmp := []callback{}
for s := range e.Chan {
for _, f := range e.Callbacks {
go f(s)
for i := range e.Callbacks {
go e.Callbacks[i].f(s)
if !e.Callbacks[i].once {
tmp = append(tmp, e.Callbacks[i])
}
}
e.Callbacks = tmp
}
}
15 changes: 6 additions & 9 deletions platforms/firmata/firmata.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,15 @@ func (b *board) connect() {
}

func (b *board) initBoard() {
gobot.On(b.events["firmware_query"], func(data interface{}) {
b.events["firmware_query"] = gobot.NewEvent()
gobot.Once(b.events["firmware_query"], func(data interface{}) {
b.queryCapabilities()
})

gobot.On(b.events["capability_query"], func(data interface{}) {
b.events["capability_query"] = gobot.NewEvent()
gobot.Once(b.events["capability_query"], func(data interface{}) {
b.queryAnalogMapping()
})

gobot.On(b.events["analog_mapping_query"], func(data interface{}) {
b.events["analog_mapping_query"] = gobot.NewEvent()
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)
Expand Down Expand Up @@ -270,7 +267,7 @@ func (b *board) process(data []byte) {
pin := b.pins[pinNumber]
if byte(pin.mode) == input {
pin.value = int((portValue >> (byte(i) & 0x07)) & 0x01)
gobot.Publish(fmt.Sprintf("digital_read_%v", pinNumber),
gobot.Publish(b.events[fmt.Sprintf("digital_read_%v", pinNumber)],
[]byte{byte(pin.value & 0xff)})
}
}
Expand Down Expand Up @@ -301,7 +298,7 @@ func (b *board) process(data []byte) {
}
}
b.pins = append(b.pins, pin{modes, output, 0, 0})
b.Events[fmt.Sprintf("digital_read_%v", len(b.pins)-1)] = gobot.NewEvent()
b.events[fmt.Sprintf("digital_read_%v", len(b.pins)-1)] = gobot.NewEvent()
supportedModes = 0
n = 0
continue
Expand Down Expand Up @@ -360,7 +357,7 @@ func (b *board) process(data []byte) {
byte(currentBuffer[i])|byte(currentBuffer[i+1])<<7,
)
}
gobo.Publish(b.events["i2c_reply"], i2cReply)
gobot.Publish(b.events["i2c_reply"], i2cReply)
case firmwareQuery:
name := []byte{}
for _, val := range currentBuffer[4:(len(currentBuffer) - 1)] {
Expand Down
8 changes: 4 additions & 4 deletions platforms/firmata/firmata_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (f *FirmataAdaptor) DigitalRead(pin string) int {
f.board.togglePinReporting(byte(p), high, reportDigital)
f.board.readAndProcess()

gobot.On(f.board.events[fmt.Sprintf("digital_read_%v", pin)], func(data interface{}) {
gobot.Once(f.board.events[fmt.Sprintf("digital_read_%v", pin)], func(data interface{}) {
ret <- int(data.([]byte)[0])
})

Expand All @@ -94,7 +94,7 @@ func (f *FirmataAdaptor) AnalogRead(pin string) int {
f.board.togglePinReporting(byte(p), high, reportAnalog)
f.board.readAndProcess()

gobot.On(f.board.events[fmt.Sprintf("analog_read_%v", pin)], func(data interface{}) {
gobot.Once(f.board.events[fmt.Sprintf("analog_read_%v", pin)], func(data interface{}) {
b := data.([]byte)
ret <- int(uint(b[0])<<24 | uint(b[1])<<16 | uint(b[2])<<8 | uint(b[3]))
})
Expand All @@ -121,8 +121,8 @@ func (f *FirmataAdaptor) I2cRead(size uint) []byte {

f.board.readAndProcess()

gobot.On(f.board.events["i2c_reply"], func(data interface{}) {
ret <- data.(i2CReply)["data"]
gobot.Once(f.board.events["i2c_reply"], func(data interface{}) {
ret <- data.(map[string][]byte)["data"]
})

return <-ret
Expand Down
9 changes: 8 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ func Publish(e *Event, val interface{}) {
}

func On(e *Event, f func(s interface{})) {
e.Callbacks = append(e.Callbacks, f)
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 {
Expand Down

0 comments on commit 83babbb

Please sign in to comment.