core: Refactor Keyboard platform for new Driver creation signatures

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-09-25 21:27:37 +02:00
parent 8144d450b5
commit c8845a245c
4 changed files with 19 additions and 20 deletions

View File

@ -25,7 +25,7 @@ import (
func main() {
gbot := gobot.NewGobot()
keys := keyboard.NewKeyboardDriver("keyboard")
keys := keyboard.NewDriver("keyboard")
work := func() {
gobot.On(keys.Event("key"), func(data interface{}) {

View File

@ -21,7 +21,7 @@ Example:
func main() {
gbot := gobot.NewGobot()
keys := keyboard.NewKeyboardDriver("keyboard")
keys := keyboard.NewDriver()
work := func() {
gobot.On(keys.Event("key"), func(data interface{}) {

View File

@ -12,18 +12,17 @@ const (
Key = "key"
)
type KeyboardDriver struct {
type Driver struct {
name string
connect func(*KeyboardDriver) (err error)
listen func(*KeyboardDriver)
connect func(*Driver) (err error)
listen func(*Driver)
stdin *os.File
gobot.Eventer
}
func NewKeyboardDriver(name string) *KeyboardDriver {
k := &KeyboardDriver{
name: name,
connect: func(k *KeyboardDriver) (err error) {
func NewDriver() *Driver {
k := &Driver{
connect: func(k *Driver) (err error) {
if err := configure(); err != nil {
return err
}
@ -31,7 +30,7 @@ func NewKeyboardDriver(name string) *KeyboardDriver {
k.stdin = os.Stdin
return
},
listen: func(k *KeyboardDriver) {
listen: func(k *Driver) {
ctrlc := bytes{3}
for {
@ -60,12 +59,13 @@ func NewKeyboardDriver(name string) *KeyboardDriver {
return k
}
func (k *KeyboardDriver) Name() string { return k.name }
func (k *KeyboardDriver) Connection() gobot.Connection { return nil }
func (k *Driver) Name() string { return k.name }
func (k *Driver) SetName(n string) { k.name = n }
func (k *Driver) Connection() gobot.Connection { return nil }
// Start initializes keyboard by grabbing key events as they come in and
// publishing a key event
func (k *KeyboardDriver) Start() (errs []error) {
func (k *Driver) Start() (errs []error) {
if err := k.connect(k); err != nil {
return []error{err}
}
@ -76,7 +76,7 @@ func (k *KeyboardDriver) Start() (errs []error) {
}
// Halt stops camera driver
func (k *KeyboardDriver) Halt() (errs []error) {
func (k *Driver) Halt() (errs []error) {
if originalState != "" {
return restore()
}

View File

@ -8,21 +8,20 @@ import (
"github.com/hybridgroup/gobot/gobottest"
)
var _ gobot.Driver = (*KeyboardDriver)(nil)
var _ gobot.Driver = (*Driver)(nil)
func initTestKeyboardDriver() *KeyboardDriver {
d := NewKeyboardDriver("keyboard")
d.connect = func(k *KeyboardDriver) (err error) {
func initTestKeyboardDriver() *Driver {
d := NewDriver()
d.connect = func(k *Driver) (err error) {
k.stdin = &os.File{}
return nil
}
d.listen = func(k *KeyboardDriver) {}
d.listen = func(k *Driver) {}
return d
}
func TestKeyboardDriver(t *testing.T) {
d := initTestKeyboardDriver()
gobottest.Assert(t, d.Name(), "keyboard")
gobottest.Assert(t, d.Connection(), (gobot.Connection)(nil))
}