diff --git a/examples/opencv_face_detect.go b/examples/opencv_face_detect.go index da7c36d1..d023fe3d 100644 --- a/examples/opencv_face_detect.go +++ b/examples/opencv_face_detect.go @@ -3,7 +3,7 @@ package main import ( cv "github.com/hybridgroup/go-opencv/opencv" "github.com/hybridgroup/gobot" - "github.com/hybridgroup/gobot/opencv" + "github.com/hybridgroup/gobot/platform/opencv" "path" "runtime" ) @@ -12,11 +12,10 @@ func main() { _, currentfile, _, _ := runtime.Caller(0) cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml") - window := opencv.NewWindowDriver() - window.Name = "window" + gbot := gobot.NewGobot() - camera := opencv.NewCameraDriver() - camera.Name = "camera" + window := opencv.NewWindowDriver("window") + camera := opencv.NewCameraDriver("camera", 0) work := func() { var image *cv.IplImage @@ -36,11 +35,8 @@ func main() { }() } - robot := gobot.Robot{ - Connections: []gobot.Connection{}, - Devices: []gobot.Device{window, camera}, - Work: work, - } + gbot.Robots = append(gbot.Robots, + gobot.NewRobot("faceBot", []gobot.Connection{}, []gobot.Device{window, camera}, work)) - robot.Start() + gbot.Start() } diff --git a/examples/opencv_window.go b/examples/opencv_window.go index 958313e8..13cdc810 100644 --- a/examples/opencv_window.go +++ b/examples/opencv_window.go @@ -3,16 +3,14 @@ package main import ( cv "github.com/hybridgroup/go-opencv/opencv" "github.com/hybridgroup/gobot" - "github.com/hybridgroup/gobot/opencv" + "github.com/hybridgroup/gobot/platforms/opencv" ) func main() { + gbot := gobot.NewGobot() - window := opencv.NewWindowDriver() - window.Name = "window" - - camera := opencv.NewCameraDriver() - camera.Name = "camera" + window := opencv.NewWindowDriver("window") + camera := opencv.NewCameraDriver("camera", 0) work := func() { gobot.On(camera.Events["Frame"], func(data interface{}) { @@ -20,11 +18,8 @@ func main() { }) } - robot := gobot.Robot{ - Connections: []gobot.Connection{}, - Devices: []gobot.Device{window, camera}, - Work: work, - } + gbot.Robots = append(gbot.Robots, + gobot.NewRobot("cameraBot", []gobot.Connection{}, []gobot.Device{window, camera}, work)) - robot.Start() + gbot.Start()} } diff --git a/platforms/opencv/camera_driver.go b/platforms/opencv/camera_driver.go index fd230c9c..2644faae 100644 --- a/platforms/opencv/camera_driver.go +++ b/platforms/opencv/camera_driver.go @@ -8,26 +8,32 @@ import ( type CameraDriver struct { gobot.Driver camera *cv.Capture - Source string + Source interface{} } -func NewCameraDriver() *CameraDriver { +func NewCameraDriver(name string, source interface{}) *CameraDriver { return &CameraDriver{ Driver: gobot.Driver{ + Name: name, Commands: []string{}, Events: map[string]chan interface{}{ "Frame": make(chan interface{}, 0), }, }, + Source: source, } } func (c *CameraDriver) Start() bool { - if c.Source != "" { - c.camera = cv.NewFileCapture(c.Source) - } else { - c.camera = cv.NewCameraCapture(0) + switch v := c.Source.(type) { + case string: + c.camera = cv.NewFileCapture(v) + case int: + c.camera = cv.NewCameraCapture(v) + default: + panic("unknown camera source") } + go func() { for { if c.camera.GrabFrame() { diff --git a/platforms/opencv/camera_driver_test.go b/platforms/opencv/camera_driver_test.go index 01ac505b..53e7cba7 100644 --- a/platforms/opencv/camera_driver_test.go +++ b/platforms/opencv/camera_driver_test.go @@ -11,7 +11,7 @@ var _ = Describe("Camera", func() { ) BeforeEach(func() { - c = NewCameraDriver() + c = NewCameraDriver("bot", 0) }) PIt("Must be able to Start", func() { diff --git a/platforms/opencv/window_driver.go b/platforms/opencv/window_driver.go index fb2ac155..9b4436fd 100644 --- a/platforms/opencv/window_driver.go +++ b/platforms/opencv/window_driver.go @@ -10,8 +10,12 @@ type WindowDriver struct { window *cv.Window } -func NewWindowDriver() *WindowDriver { - return &WindowDriver{} +func NewWindowDriver(name string) *WindowDriver { + return &WindowDriver{ + Driver: gobot.Driver{ + Name: name, + }, + } } func (w *WindowDriver) Start() bool { diff --git a/platforms/opencv/window_driver_test.go b/platforms/opencv/window_driver_test.go index f34c7459..c7c5268a 100644 --- a/platforms/opencv/window_driver_test.go +++ b/platforms/opencv/window_driver_test.go @@ -11,7 +11,7 @@ var _ = Describe("Window", func() { ) BeforeEach(func() { - w = NewWindowDriver() + w = NewWindowDriver("bot") }) PIt("Must be able to Start", func() {