Refactor gpio to use new driver interface

This commit is contained in:
Adrian Zankich 2014-11-22 12:21:28 -08:00
parent d38b95f41f
commit 88db6be3ac
9 changed files with 136 additions and 87 deletions

View File

@ -1,15 +1,21 @@
package gpio
import (
"github.com/hybridgroup/gobot"
"time"
"github.com/hybridgroup/gobot"
)
var _ gobot.DriverInterface = (*AnalogSensorDriver)(nil)
var _ gobot.Driver = (*AnalogSensorDriver)(nil)
// Represents an Analog Sensor
type AnalogSensorDriver struct {
gobot.Driver
name string
pin string
interval time.Duration
connection gobot.Connection
gobot.Eventer
gobot.Commander
}
// NewAnalogSensorDriver returns a new AnalogSensorDriver given an AnalogReader, name and pin.
@ -18,12 +24,12 @@ type AnalogSensorDriver struct {
// "Read" - See AnalogSensor.Read
func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
d := &AnalogSensorDriver{
Driver: *gobot.NewDriver(
name,
"AnalogSensorDriver",
a.(gobot.AdaptorInterface),
pin,
),
name: name,
connection: a.(gobot.Connection),
pin: pin,
Eventer: gobot.NewEventer(),
Commander: gobot.NewCommander(),
interval: 10 * time.Millisecond,
}
d.AddEvent("data")
@ -36,8 +42,8 @@ func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSenso
return d
}
func (a *AnalogSensorDriver) adaptor() AnalogReader {
return a.Adaptor().(AnalogReader)
func (a *AnalogSensorDriver) conn() AnalogReader {
return a.Connection().(AnalogReader)
}
// Starts the AnalogSensorDriver and reads the Analog Sensor at the given Driver.Interval().
@ -55,16 +61,19 @@ func (a *AnalogSensorDriver) Start() (errs []error) {
value = newValue
gobot.Publish(a.Event("data"), value)
}
<-time.After(a.Interval())
<-time.After(a.interval)
}
}()
return
}
// Halt returns true on a successful halt of the driver
func (a *AnalogSensorDriver) Halt() (errs []error) { return }
func (a *AnalogSensorDriver) Halt() (errs []error) { return }
func (a *AnalogSensorDriver) Name() string { return a.name }
func (a *AnalogSensorDriver) Pin() string { return a.pin }
func (a *AnalogSensorDriver) Connection() gobot.Connection { return a.connection }
// Read returns the current reading from the Analog Sensor
func (a *AnalogSensorDriver) Read() (val int, err error) {
return a.adaptor().AnalogRead(a.Pin())
return a.conn().AnalogRead(a.Pin())
}

View File

@ -1,28 +1,30 @@
package gpio
import (
"github.com/hybridgroup/gobot"
"time"
"github.com/hybridgroup/gobot"
)
var _ gobot.DriverInterface = (*ButtonDriver)(nil)
var _ gobot.Driver = (*ButtonDriver)(nil)
// Represents a digital Button
type ButtonDriver struct {
gobot.Driver
Active bool
Active bool
pin string
name string
connection gobot.Connection
gobot.Eventer
}
// NewButtonDriver return a new ButtonDriver given a DigitalReader, name and pin
func NewButtonDriver(a DigitalReader, name string, pin string) *ButtonDriver {
b := &ButtonDriver{
Driver: *gobot.NewDriver(
name,
"ButtonDriver",
a.(gobot.AdaptorInterface),
pin,
),
Active: false,
name: name,
connection: a.(gobot.Adaptor),
pin: pin,
Active: false,
Eventer: gobot.NewEventer(),
}
b.AddEvent("push")
@ -33,7 +35,7 @@ func NewButtonDriver(a DigitalReader, name string, pin string) *ButtonDriver {
}
func (b *ButtonDriver) adaptor() DigitalReader {
return b.Adaptor().(DigitalReader)
return b.Connection().(DigitalReader)
}
// Starts the ButtonDriver and reads the state of the button at the given Driver.Interval().
@ -54,7 +56,8 @@ func (b *ButtonDriver) Start() (errs []error) {
state = newValue
b.update(newValue)
}
<-time.After(b.Interval())
//<-time.After(b.Interval())
<-time.After(100 * time.Millisecond)
}
}()
return
@ -63,6 +66,21 @@ func (b *ButtonDriver) Start() (errs []error) {
// Halt returns true on a successful halt of the driver
func (b *ButtonDriver) Halt() (errs []error) { return }
func (b *ButtonDriver) Name() string { return b.name }
func (b *ButtonDriver) Pin() string { return b.pin }
func (b *ButtonDriver) Connection() gobot.Connection { return b.connection }
func (b *ButtonDriver) String() string { return "ButtonDriver" }
func (b *ButtonDriver) ToJSON() *gobot.JSONDevice {
return &gobot.JSONDevice{
Name: b.Name(),
Driver: b.String(),
Connection: b.Connection().Name(),
//Commands: l.Commands(),
//Commands: l.Commands(),
}
}
func (b *ButtonDriver) readState() (val int, err error) {
return b.adaptor().DigitalRead(b.Pin())
}

View File

@ -6,11 +6,14 @@ import (
"github.com/hybridgroup/gobot"
)
var _ gobot.DriverInterface = (*DirectPinDriver)(nil)
var _ gobot.Driver = (*DirectPinDriver)(nil)
// Represents a raw GPIO pin
type DirectPinDriver struct {
gobot.Driver
name string
pin string
connection gobot.Connection
gobot.Commander
}
// NewDirectPinDriver return a new DirectPinDriver given a DirectPin, name and pin.
@ -24,12 +27,10 @@ type DirectPinDriver struct {
// "ServoWrite" - See DirectPinDriver.ServoWrite
func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
d := &DirectPinDriver{
Driver: *gobot.NewDriver(
name,
"DirectPinDriver",
a.(gobot.AdaptorInterface),
pin,
),
name: name,
connection: a.(gobot.Connection),
pin: pin,
Commander: gobot.NewCommander(),
}
d.AddCommand("DigitalRead", func(params map[string]interface{}) interface{} {
@ -61,9 +62,13 @@ func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
}
func (d *DirectPinDriver) adaptor() DirectPin {
return d.Adaptor().(DirectPin)
return d.Connection().(DirectPin)
}
func (d *DirectPinDriver) Name() string { return d.name }
func (d *DirectPinDriver) Pin() string { return d.pin }
func (d *DirectPinDriver) Connection() gobot.Connection { return d.connection }
// Starts the DirectPinDriver. Returns true on successful start of the driver
func (d *DirectPinDriver) Start() (errs []error) { return }

View File

@ -2,12 +2,15 @@ package gpio
import "github.com/hybridgroup/gobot"
var _ gobot.DriverInterface = (*LedDriver)(nil)
var _ gobot.Driver = (*LedDriver)(nil)
// Represents a digital Led
type LedDriver struct {
gobot.Driver
high bool
pin string
name string
connection gobot.Connection
high bool
gobot.Commander
}
// NewLedDriver return a new LedDriver given a PwmDigitalWriter, name and pin.
@ -19,13 +22,11 @@ type LedDriver struct {
// "Off" - See LedDriver.Off
func NewLedDriver(a PwmDigitalWriter, name string, pin string) *LedDriver {
l := &LedDriver{
Driver: *gobot.NewDriver(
name,
"LedDriver",
pin,
a.(gobot.AdaptorInterface),
),
high: false,
name: name,
pin: pin,
connection: a.(gobot.Connection),
high: false,
Commander: gobot.NewCommander(),
}
l.AddCommand("Brightness", func(params map[string]interface{}) interface{} {
@ -49,7 +50,7 @@ func NewLedDriver(a PwmDigitalWriter, name string, pin string) *LedDriver {
}
func (l *LedDriver) adaptor() PwmDigitalWriter {
return l.Adaptor().(PwmDigitalWriter)
return l.Connection().(PwmDigitalWriter)
}
// Start starts the LedDriver. Returns true on successful start of the driver
@ -58,6 +59,10 @@ func (l *LedDriver) Start() (errs []error) { return }
// Halt halts the LedDriver. Returns true on successful halt of the driver
func (l *LedDriver) Halt() (errs []error) { return }
func (l *LedDriver) Name() string { return l.name }
func (l *LedDriver) Pin() string { return l.pin }
func (l *LedDriver) Connection() gobot.Connection { return l.connection }
// State return true if the led is On and false if the led is Off
func (l *LedDriver) State() bool {
return l.high

View File

@ -1,29 +1,33 @@
package gpio
import (
"github.com/hybridgroup/gobot"
"time"
"github.com/hybridgroup/gobot"
)
var _ gobot.DriverInterface = (*MakeyButtonDriver)(nil)
var _ gobot.Driver = (*MakeyButtonDriver)(nil)
// Represents a Makey Button
type MakeyButtonDriver struct {
gobot.Driver
Active bool
data []int
name string
pin string
connection gobot.Connection
Active bool
data []int
interval time.Duration
gobot.Eventer
}
// NewMakeyButtonDriver returns a new MakeyButtonDriver given a DigitalRead, name and pin.
func NewMakeyButtonDriver(a DigitalReader, name string, pin string) *MakeyButtonDriver {
m := &MakeyButtonDriver{
Driver: *gobot.NewDriver(
name,
"MakeyButtonDriver",
a.(gobot.AdaptorInterface),
pin,
),
Active: false,
name: name,
connection: a.(gobot.Connection),
pin: pin,
Active: false,
Eventer: gobot.NewEventer(),
interval: 10 * time.Millisecond,
}
m.AddEvent("error")
@ -33,8 +37,12 @@ func NewMakeyButtonDriver(a DigitalReader, name string, pin string) *MakeyButton
return m
}
func (b *MakeyButtonDriver) Name() string { return b.name }
func (b *MakeyButtonDriver) Pin() string { return b.pin }
func (b *MakeyButtonDriver) Connection() gobot.Connection { return b.connection }
func (b *MakeyButtonDriver) adaptor() DigitalReader {
return b.Adaptor().(DigitalReader)
return b.Connection().(DigitalReader)
}
// Starts the MakeyButtonDriver and reads the state of the button at the given Driver.Interval().
@ -61,7 +69,7 @@ func (m *MakeyButtonDriver) Start() (errs []error) {
}
}
}
<-time.After(m.Interval())
<-time.After(m.interval)
}()
return
}

View File

@ -4,11 +4,12 @@ import (
"github.com/hybridgroup/gobot"
)
var _ gobot.DriverInterface = (*MotorDriver)(nil)
var _ gobot.Driver = (*MotorDriver)(nil)
// Represents a Motor
type MotorDriver struct {
gobot.Driver
name string
connection gobot.Connection
SpeedPin string
SwitchPin string
DirectionPin string
@ -21,13 +22,10 @@ type MotorDriver struct {
}
// NewMotorDriver return a new MotorDriver given a PwmDigitalWriter, name and pin
func NewMotorDriver(a PwmDigitalWriter, name string, pin string) *MotorDriver {
func NewMotorDriver(a PwmDigitalWriter, name string) *MotorDriver {
return &MotorDriver{
Driver: *gobot.NewDriver(
name,
"MotorDriver",
a.(gobot.AdaptorInterface),
),
name: name,
connection: a.(gobot.Adaptor),
CurrentState: 0,
CurrentSpeed: 0,
CurrentMode: "digital",
@ -35,8 +33,11 @@ func NewMotorDriver(a PwmDigitalWriter, name string, pin string) *MotorDriver {
}
}
func (m *MotorDriver) Name() string { return m.name }
func (m *MotorDriver) Connection() gobot.Connection { return m.connection }
func (m *MotorDriver) adaptor() PwmDigitalWriter {
return m.Adaptor().(PwmDigitalWriter)
return m.Connection().(PwmDigitalWriter)
}
// Start starts the MotorDriver. Returns true on successful start of the driver

View File

@ -7,7 +7,7 @@ import (
)
func initTestMotorDriver() *MotorDriver {
return NewMotorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
return NewMotorDriver(newGpioTestAdaptor("adaptor"), "bot")
}
func TestMotorDriverStart(t *testing.T) {

View File

@ -6,11 +6,14 @@ import (
"github.com/hybridgroup/gobot"
)
var _ gobot.DriverInterface = (*ServoDriver)(nil)
var _ gobot.Driver = (*ServoDriver)(nil)
// Represents a Servo
type ServoDriver struct {
gobot.Driver
name string
pin string
connection gobot.Connection
gobot.Commander
CurrentAngle byte
}
@ -23,12 +26,10 @@ type ServoDriver struct {
// "Max" - See ServoDriver.Max
func NewServoDriver(a Servo, name string, pin string) *ServoDriver {
s := &ServoDriver{
Driver: *gobot.NewDriver(
name,
"ServoDriver",
a.(gobot.AdaptorInterface),
pin,
),
name: name,
connection: a.(gobot.Connection),
pin: pin,
Commander: gobot.NewCommander(),
CurrentAngle: 0,
}
@ -50,8 +51,11 @@ func NewServoDriver(a Servo, name string, pin string) *ServoDriver {
}
func (s *ServoDriver) Name() string { return s.name }
func (s *ServoDriver) Pin() string { return s.pin }
func (s *ServoDriver) Connection() gobot.Connection { return s.connection }
func (s *ServoDriver) adaptor() Servo {
return s.Adaptor().(Servo)
return s.Connection().(Servo)
}
// Start starts the ServoDriver. Returns true on successful start of the driver.

View File

@ -1,9 +1,8 @@
package gpio
import "github.com/hybridgroup/gobot"
type gpioTestAdaptor struct {
gobot.Adaptor
name string
port string
}
func (t *gpioTestAdaptor) AnalogWrite(string, byte) (err error) { return nil }
@ -19,12 +18,12 @@ func (t *gpioTestAdaptor) DigitalRead(string) (val int, err error) {
}
func (t *gpioTestAdaptor) Connect() (errs []error) { return }
func (t *gpioTestAdaptor) Finalize() (errs []error) { return }
func (t *gpioTestAdaptor) Name() string { return t.name }
func (t *gpioTestAdaptor) Port() string { return t.port }
func newGpioTestAdaptor(name string) *gpioTestAdaptor {
return &gpioTestAdaptor{
Adaptor: *gobot.NewAdaptor(
name,
"/dev/null",
),
name: name,
port: "/dev/null",
}
}