Merge pull request #150 from hybridgroup/gh-148

Fix unnecessary goroutine creation
This commit is contained in:
Ron Evans 2014-11-17 14:11:17 -05:00
commit 322aefd32b
1 changed files with 10 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package opencv
import (
cv "github.com/hybridgroup/go-opencv/opencv"
"github.com/hybridgroup/gobot"
"time"
)
type CameraDriver struct {
@ -42,14 +43,17 @@ func NewCameraDriver(name string, source interface{}) *CameraDriver {
// every `interval` and publishing an frame event
func (c *CameraDriver) Start() bool {
c.start(c)
gobot.Every(c.Interval(), func() {
if c.camera.GrabFrame() {
image := c.camera.RetrieveFrame(1)
if image != nil {
gobot.Publish(c.Event("frame"), image)
go func() {
for {
if c.camera.GrabFrame() {
image := c.camera.RetrieveFrame(1)
if image != nil {
gobot.Publish(c.Event("frame"), image)
}
}
<-time.After(c.Interval())
}
})
}()
return true
}