Skip to content

Commit

Permalink
Use new Event type for events
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 11, 2014
1 parent c129da6 commit db44941
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 59 deletions.
10 changes: 5 additions & 5 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package gobot
import "time"

type Driver struct {
Interval time.Duration `json:"interval"`
Pin string `json:"pin"`
Name string `json:"name"`
Commands []string `json:"commands"`
Events map[string]chan interface{} `json:"-"`
Interval time.Duration `json:"interval"`
Pin string `json:"pin"`
Name string `json:"name"`
Commands []string `json:"commands"`
Events map[string]*Event `json:"-"`
}

type DriverInterface interface {
Expand Down
34 changes: 34 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gobot

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

func NewEvent() *Event {
e := &Event{
Chan: make(chan interface{}, 1),
Callbacks: []func(interface{}){},
}
go func() {
for {
e.Read()
}
}()
return e
}

func (e *Event) Write(data interface{}) {
select {
case e.Chan <- data:
default:
}
}

func (e *Event) Read() {
for s := range e.Chan {
for _, f := range e.Callbacks {
go f(s)
}
}
}
9 changes: 9 additions & 0 deletions examples/firmata_button.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
Expand All @@ -21,6 +22,14 @@ func main() {
gobot.On(button.Events["release"], func(data interface{}) {
led.Off()
})

gobot.On(button.Events["push"], func(data interface{}) {
fmt.Println("IVE BEEN PUSHED")
})

gobot.On(button.Events["release"], func(data interface{}) {
fmt.Println("IVE BEEN RELEASED")
})
}

gbot.Robots = append(gbot.Robots,
Expand Down
4 changes: 2 additions & 2 deletions platforms/ardrone/ardrone_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func NewArdroneDriver(adaptor DroneInterface, name string) *ArdroneDriver {
return &ArdroneDriver{
Driver: gobot.Driver{
Name: name,
Events: map[string]chan interface{}{
"Flying": make(chan interface{}, 1),
Events: map[string]*gobot.Event{
"Flying": gobot.NewEvent(),
},
},
Adaptor: adaptor,
Expand Down
6 changes: 3 additions & 3 deletions platforms/gpio/button_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func NewButtonDriver(a DigitalReader, name string, pin string) *ButtonDriver {
Driver: gobot.Driver{
Name: name,
Pin: pin,
Events: map[string]chan interface{}{
"push": make(chan interface{}),
"release": make(chan interface{}),
Events: map[string]*gobot.Event{
"push": gobot.NewEvent(),
"release": gobot.NewEvent(),
},
},
Active: false,
Expand Down
6 changes: 3 additions & 3 deletions platforms/gpio/makey_button_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func NewMakeyButtonDriver(a DigitalReader, name string, pin string) *MakeyButton
Driver: gobot.Driver{
Name: name,
Pin: pin,
Events: map[string]chan interface{}{
"push": make(chan interface{}),
"release": make(chan interface{}),
Events: map[string]*gobot.Event{
"push": gobot.NewEvent(),
"release": gobot.NewEvent(),
},
},
Active: false,
Expand Down
8 changes: 4 additions & 4 deletions platforms/i2c/wiichuck_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
return &WiichuckDriver{
Driver: gobot.Driver{
Name: name,
Events: map[string]chan interface{}{
"z_button": make(chan interface{}),
"c_button": make(chan interface{}),
"joystick": make(chan interface{}),
Events: map[string]*gobot.Event{
"z_button": gobot.NewEvent(),
"c_button": gobot.NewEvent(),
"joystick": gobot.NewEvent(),
},
},
joystick: map[string]float64{
Expand Down
10 changes: 5 additions & 5 deletions platforms/joystick/joystick_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type joystickConfig struct {
func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *JoystickDriver {
d := &JoystickDriver{
Driver: gobot.Driver{
Events: make(map[string]chan interface{}),
Events: make(map[string]*gobot.Event),
},
Adaptor: a,
}
Expand All @@ -50,14 +50,14 @@ func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *Joystick
json.Unmarshal(file, &jsontype)
d.config = jsontype
for _, value := range d.config.Buttons {
d.Events[fmt.Sprintf("%s_press", value.Name)] = make(chan interface{}, 0)
d.Events[fmt.Sprintf("%s_release", value.Name)] = make(chan interface{}, 0)
d.Events[fmt.Sprintf("%s_press", value.Name)] = gobot.NewEvent()
d.Events[fmt.Sprintf("%s_release", value.Name)] = gobot.NewEvent()
}
for _, value := range d.config.Axis {
d.Events[value.Name] = make(chan interface{}, 0)
d.Events[value.Name] = gobot.NewEvent()
}
for _, value := range d.config.Hats {
d.Events[value.Name] = make(chan interface{}, 0)
d.Events[value.Name] = gobot.NewEvent()
}
return d
}
Expand Down
4 changes: 2 additions & 2 deletions platforms/leap/leap_motion_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
return &LeapMotionDriver{
Driver: gobot.Driver{
Name: name,
Events: map[string]chan interface{}{
"Message": make(chan interface{}),
Events: map[string]*gobot.Event{
"Message": gobot.NewEvent(),
},
},
Adaptor: a,
Expand Down
16 changes: 8 additions & 8 deletions platforms/neurosky/neurosky_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func NewNeuroskyDriver(a *NeuroskyAdaptor, name string) *NeuroskyDriver {
return &NeuroskyDriver{
Driver: gobot.Driver{
Name: name,
Events: map[string]chan interface{}{
"Extended": make(chan interface{}),
"Signal": make(chan interface{}),
"Attention": make(chan interface{}),
"Meditation": make(chan interface{}),
"Blink": make(chan interface{}),
"Wave": make(chan interface{}),
"EEG": make(chan interface{}),
Events: map[string]*gobot.Event{
"Extended": gobot.NewEvent(),
"Signal": gobot.NewEvent(),
"Attention": gobot.NewEvent(),
"Meditation": gobot.NewEvent(),
"Blink": gobot.NewEvent(),
"Wave": gobot.NewEvent(),
"EEG": gobot.NewEvent(),
},
},
Adaptor: a,
Expand Down
4 changes: 2 additions & 2 deletions platforms/opencv/camera_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func NewCameraDriver(name string, source interface{}) *CameraDriver {
Driver: gobot.Driver{
Name: name,
Commands: []string{},
Events: map[string]chan interface{}{
"Frame": make(chan interface{}, 0),
Events: map[string]*gobot.Event{
"Frame": gobot.NewEvent(),
},
},
Source: source,
Expand Down
8 changes: 4 additions & 4 deletions platforms/pebble/pebble_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func NewPebbleDriver(adaptor *PebbleAdaptor, name string) *PebbleDriver {
return &PebbleDriver{
Driver: gobot.Driver{
Name: name,
Events: map[string]chan interface{}{
"button": make(chan interface{}),
"accel": make(chan interface{}),
"tap": make(chan interface{}),
Events: map[string]*gobot.Event{
"button": gobot.NewEvent(),
"accel": gobot.NewEvent(),
"tap": gobot.NewEvent(),
},
Commands: []string{
"PublishEventC",
Expand Down
7 changes: 4 additions & 3 deletions platforms/sphero/sphero_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ type SpheroDriver struct {
func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
return &SpheroDriver{
Driver: gobot.Driver{
Name: name,
Events: make(map[string]chan interface{}),
Name: name,
Events: map[string]*gobot.Event{
"Collision": gobot.NewEvent(),
},
Commands: []string{
"SetRGBC",
"RollC",
Expand Down Expand Up @@ -137,7 +139,6 @@ func (s *SpheroDriver) Stop() {
}

func (s *SpheroDriver) configureCollisionDetection() {
s.Events["Collision"] = make(chan interface{})
s.packetChannel <- s.craftPacket([]uint8{0x01, 0x40, 0x40, 0x50, 0x50, 0x60}, 0x12)
}

Expand Down
15 changes: 4 additions & 11 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,12 @@ func After(t time.Duration, f func()) {
time.AfterFunc(t, f)
}

func Publish(c chan interface{}, val interface{}) {
select {
case c <- val:
default:
}
func Publish(e *Event, val interface{}) {
e.Write(val)
}

func On(c chan interface{}, f func(s interface{})) {
go func() {
for s := range c {
f(s)
}
}()
func On(e *Event, f func(s interface{})) {
e.Callbacks = append(e.Callbacks, f)
}

func Rand(max int) int {
Expand Down
15 changes: 8 additions & 7 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@ var _ = Describe("Utils", func() {
Expect(i).To(Equal(1))
})
It("should Publish message to channel without blocking", func() {
c := make(chan interface{}, 1)
Publish(c, 1)
Publish(c, 2)
i := <-c
e := &Event{Chan: make(chan interface{}, 1)}
Publish(e, 1)
Publish(e, 2)
i := <-e.Chan
Expect(i.(int)).To(Equal(1))
})
It("should execution function on event", func() {
c := make(chan interface{})
var i int
On(c, func(data interface{}) {
e := NewEvent()
On(e, func(data interface{}) {
i = data.(int)
})
c <- 10
Publish(e, 10)
time.Sleep(1 * time.Millisecond)
Expect(i).To(Equal(10))
})
It("should scale the value between 0...1", func() {
Expand Down

0 comments on commit db44941

Please sign in to comment.