Refactor opencv to use new driver interfaces
This commit is contained in:
parent
903f7470af
commit
dcfc8e47cc
|
@ -3,29 +3,31 @@ package opencv
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
|
||||||
cv "github.com/hybridgroup/go-opencv/opencv"
|
cv "github.com/hybridgroup/go-opencv/opencv"
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ gobot.DriverInterface = (*CameraDriver)(nil)
|
var _ gobot.Driver = (*CameraDriver)(nil)
|
||||||
|
|
||||||
type CameraDriver struct {
|
type CameraDriver struct {
|
||||||
gobot.Driver
|
name string
|
||||||
camera capture
|
camera capture
|
||||||
Source interface{}
|
interval time.Duration
|
||||||
start func(*CameraDriver) (err error)
|
Source interface{}
|
||||||
|
start func(*CameraDriver) (err error)
|
||||||
|
gobot.Eventer
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCameraDriver creates a new driver with specified name and source.
|
// NewCameraDriver creates a new driver with specified name and source.
|
||||||
// It also creates a start function to either set camera as a File or Camera capture.
|
// It also creates a start function to either set camera as a File or Camera capture.
|
||||||
func NewCameraDriver(name string, source interface{}) *CameraDriver {
|
func NewCameraDriver(name string, source interface{}) *CameraDriver {
|
||||||
c := &CameraDriver{
|
c := &CameraDriver{
|
||||||
Driver: *gobot.NewDriver(
|
name: name,
|
||||||
name,
|
Eventer: gobot.NewEventer(),
|
||||||
"CameraDriver",
|
Source: source,
|
||||||
),
|
interval: 10 * time.Millisecond,
|
||||||
Source: source,
|
|
||||||
start: func(c *CameraDriver) (err error) {
|
start: func(c *CameraDriver) (err error) {
|
||||||
switch v := c.Source.(type) {
|
switch v := c.Source.(type) {
|
||||||
case string:
|
case string:
|
||||||
|
@ -44,6 +46,9 @@ func NewCameraDriver(name string, source interface{}) *CameraDriver {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CameraDriver) Name() string { return c.name }
|
||||||
|
func (c *CameraDriver) Connection() gobot.Connection { return nil }
|
||||||
|
|
||||||
// Start initializes camera by grabbing a frame
|
// Start initializes camera by grabbing a frame
|
||||||
// every `interval` and publishing an frame event
|
// every `interval` and publishing an frame event
|
||||||
func (c *CameraDriver) Start() (errs []error) {
|
func (c *CameraDriver) Start() (errs []error) {
|
||||||
|
@ -58,7 +63,7 @@ func (c *CameraDriver) Start() (errs []error) {
|
||||||
gobot.Publish(c.Event("frame"), image)
|
gobot.Publish(c.Event("frame"), image)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
<-time.After(c.Interval())
|
<-time.After(c.interval)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return
|
return
|
||||||
|
|
|
@ -5,10 +5,10 @@ import (
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ gobot.DriverInterface = (*WindowDriver)(nil)
|
var _ gobot.Driver = (*WindowDriver)(nil)
|
||||||
|
|
||||||
type WindowDriver struct {
|
type WindowDriver struct {
|
||||||
gobot.Driver
|
name string
|
||||||
window window
|
window window
|
||||||
start func(*WindowDriver)
|
start func(*WindowDriver)
|
||||||
}
|
}
|
||||||
|
@ -17,16 +17,16 @@ type WindowDriver struct {
|
||||||
// It adds an start function to initialize window
|
// It adds an start function to initialize window
|
||||||
func NewWindowDriver(name string) *WindowDriver {
|
func NewWindowDriver(name string) *WindowDriver {
|
||||||
return &WindowDriver{
|
return &WindowDriver{
|
||||||
Driver: *gobot.NewDriver(
|
name: name,
|
||||||
name,
|
|
||||||
"WindowDriver",
|
|
||||||
),
|
|
||||||
start: func(w *WindowDriver) {
|
start: func(w *WindowDriver) {
|
||||||
w.window = cv.NewWindow(w.Name(), cv.CV_WINDOW_NORMAL)
|
w.window = cv.NewWindow(w.Name(), cv.CV_WINDOW_NORMAL)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *WindowDriver) Name() string { return w.name }
|
||||||
|
func (w *WindowDriver) Connection() gobot.Connection { return nil }
|
||||||
|
|
||||||
// Start starts window thread and driver
|
// Start starts window thread and driver
|
||||||
func (w *WindowDriver) Start() (errs []error) {
|
func (w *WindowDriver) Start() (errs []error) {
|
||||||
cv.StartWindowThread()
|
cv.StartWindowThread()
|
||||||
|
|
Loading…
Reference in New Issue