diff --git a/examples/roll.go b/examples/roll.go index b2226dd..224bd4e 100644 --- a/examples/roll.go +++ b/examples/roll.go @@ -2,6 +2,7 @@ package main import ( "github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot-sphero" + "fmt" ) func main() { @@ -25,6 +26,13 @@ func main() { sphero.Stop() + go func() { + for{ + gobot.On(sphero.Events["Collision"]) + fmt.Println("Collision Detected!") + } + }() + gobot.Every("1s", func(){ sphero.Roll(100, uint16(gobot.Random(0, 360))) }) diff --git a/sphero_driver.go b/sphero_driver.go index 90c86c1..37587cc 100644 --- a/sphero_driver.go +++ b/sphero_driver.go @@ -2,7 +2,6 @@ package gobotSphero import ( "github.com/hybridgroup/gobot" - "fmt" ) type packet struct { @@ -20,6 +19,7 @@ type SpheroDriver struct { func NewSphero(sa *SpheroAdaptor) *SpheroDriver { s := new(SpheroDriver) + s.Events = make(map[string]chan interface{}) s.SpheroAdaptor = sa return s } @@ -60,6 +60,7 @@ func (sd *SpheroDriver) ConfigureCollisionDetection() { dlen := len(packet.body) + 1 packet.header = []uint8{0xFF, 0xFF, 0x02, 0x12, sd.seq, uint8(dlen)} packet.checksum = sd.calculateChecksum(packet) + sd.Events["Collision"] = make(chan interface{}, 1024) sd.write(packet) } @@ -116,8 +117,7 @@ func (sd *SpheroDriver) handleMessageEvents() { } func (sd *SpheroDriver) handleCollisionDetected(data []uint8) { - //publish(event_topic_name("collision"), data) - fmt.Println("Collision") + sd.Events["Collision"] <- data } func (sd *SpheroDriver) readHeader() []uint8 {