Fix unnecessary goroutine creation

This commit is contained in:
Adrian Zankich 2014-11-16 10:32:51 -08:00
parent 444d9ffbde
commit 6f7f6da110
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
}