Update opencv package error handling

This commit is contained in:
Adrian Zankich 2014-11-19 15:59:17 -08:00
parent acbc388378
commit d730ccf739
2 changed files with 16 additions and 17 deletions

View File

@ -1,6 +1,8 @@
package opencv
import (
"errors"
cv "github.com/hybridgroup/go-opencv/opencv"
"github.com/hybridgroup/gobot"
)
@ -11,7 +13,7 @@ type CameraDriver struct {
gobot.Driver
camera capture
Source interface{}
start func(*CameraDriver)
start func(*CameraDriver) (err error)
}
// NewCameraDriver creates a new driver with specified name and source.
@ -23,15 +25,16 @@ func NewCameraDriver(name string, source interface{}) *CameraDriver {
"CameraDriver",
),
Source: source,
start: func(c *CameraDriver) {
start: func(c *CameraDriver) (err error) {
switch v := c.Source.(type) {
case string:
c.camera = cv.NewFileCapture(v)
case int:
c.camera = cv.NewCameraCapture(v)
default:
panic("unknown camera source")
return errors.New("Unknown camera source")
}
return
},
}
@ -42,8 +45,10 @@ func NewCameraDriver(name string, source interface{}) *CameraDriver {
// Start initializes camera by grabbing a frame
// every `interval` and publishing an frame event
func (c *CameraDriver) Start() error {
c.start(c)
func (c *CameraDriver) Start() (err error) {
if err = c.start(c); err != nil {
return err
}
gobot.Every(c.Interval(), func() {
if c.camera.GrabFrame() {
image := c.camera.RetrieveFrame(1)

View File

@ -9,8 +9,9 @@ import (
func initTestCameraDriver() *CameraDriver {
d := NewCameraDriver("bot", "")
d.start = func(c *CameraDriver) {
d.start = func(c *CameraDriver) (err error) {
d.camera = &testCapture{}
return nil
}
return d
}
@ -31,19 +32,12 @@ func TestCameraDriverStart(t *testing.T) {
}
func TestCameraDriver(t *testing.T) {
d := NewCameraDriver("bot", "")
d.Start()
gobot.Refute(t, d.camera, nil)
err := d.Start()
gobot.Assert(t, err, nil)
defer func() {
r := recover()
if r != nil {
gobot.Assert(t, "unknown camera source", r)
} else {
t.Errorf("Did not return Unknown camera error")
}
}()
d = NewCameraDriver("bot", true)
d.Start()
err = d.Start()
gobot.Refute(t, err, nil)
}
func TestCameraDriverHalt(t *testing.T) {