Skip to content

Commit

Permalink
go lint and documentation tweaks for the gpio package
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jan 2, 2015
1 parent de71de8 commit 5995982
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 87 deletions.
39 changes: 29 additions & 10 deletions platforms/gpio/analog_sensor_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ import (

var _ gobot.Driver = (*AnalogSensorDriver)(nil)

// Represents an Analog Sensor
// AnalogSensorDriver represents an Analog Sensor
type AnalogSensorDriver struct {
name string
pin string
halt chan bool
interval time.Duration
connection AnalogReader
gobot.Eventer
gobot.Commander
}

// NewAnalogSensorDriver returns a new AnalogSensorDriver given an AnalogReader, name and pin.
// NewAnalogSensorDriver returns a new AnalogSensorDriver with a polling interval of
// 10 Milliseconds given an AnalogReader, name and pin.
//
// Optinally accepts:
// time.Duration: Interval at which the AnalogSensor is polled for new information
//
// Adds the following API Commands:
// "Read" - See AnalogSensor.Read
Expand All @@ -30,6 +35,7 @@ func NewAnalogSensorDriver(a AnalogReader, name string, pin string, v ...time.Du
Eventer: gobot.NewEventer(),
Commander: gobot.NewCommander(),
interval: 10 * time.Millisecond,
halt: make(chan bool),
}

if len(v) > 0 {
Expand All @@ -47,10 +53,10 @@ func NewAnalogSensorDriver(a AnalogReader, name string, pin string, v ...time.Du
return d
}

// Starts the AnalogSensorDriver and reads the Analog Sensor at the given Driver.Interval().
// Returns true on successful start of the driver.
// Start starts the AnalogSensorDriver and reads the Analog Sensor at the given interval.
// Emits the Events:
// "data" int - Event is emitted on change and represents the current reading from the sensor.
// Data int - Event is emitted on change and represents the current reading from the sensor.
// Error error - Event is emitted on error reading from the sensor.
func (a *AnalogSensorDriver) Start() (errs []error) {
value := 0
go func() {
Expand All @@ -62,16 +68,29 @@ func (a *AnalogSensorDriver) Start() (errs []error) {
value = newValue
gobot.Publish(a.Event(Data), value)
}
<-time.After(a.interval)
select {
case <-time.After(a.interval):
case <-a.halt:
return
}
}
}()
return
}

// Halt returns true on a successful halt of the driver
func (a *AnalogSensorDriver) Halt() (errs []error) { return }
func (a *AnalogSensorDriver) Name() string { return a.name }
func (a *AnalogSensorDriver) Pin() string { return a.pin }
// Halt stops polling the analog sensor for new information
func (a *AnalogSensorDriver) Halt() (errs []error) {
a.halt <- true
return
}

// Name returns the AnalogSensorDrivers name
func (a *AnalogSensorDriver) Name() string { return a.name }

// Pin returns the AnalogSensorDrivers pin
func (a *AnalogSensorDriver) Pin() string { return a.pin }

// Connection returns the AnalogSensorDrivers Connection
func (a *AnalogSensorDriver) Connection() gobot.Connection { return a.connection.(gobot.Connection) }

// Read returns the current reading from the Analog Sensor
Expand Down
24 changes: 22 additions & 2 deletions platforms/gpio/analog_sensor_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestAnalogSensorDriverStart(t *testing.T) {

gobot.Assert(t, len(d.Start()), 0)

gobot.On(d.Event(Data), func(data interface{}) {
gobot.Once(d.Event(Data), func(data interface{}) {
gobot.Assert(t, data.(int), 100)
sem <- true
})
Expand All @@ -49,7 +49,7 @@ func TestAnalogSensorDriverStart(t *testing.T) {
t.Errorf("AnalogSensor Event \"Data\" was not published")
}

gobot.On(d.Event(Error), func(data interface{}) {
gobot.Once(d.Event(Error), func(data interface{}) {
gobot.Assert(t, data.(error).Error(), "read error")
sem <- true
})
Expand All @@ -64,9 +64,29 @@ func TestAnalogSensorDriverStart(t *testing.T) {
case <-time.After(15 * time.Millisecond):
t.Errorf("AnalogSensor Event \"Error\" was not published")
}

gobot.Once(d.Event(Data), func(data interface{}) {
sem <- true
})

testAdaptorAnalogRead = func() (val int, err error) {
val = 200
return
}

d.halt <- true

select {
case <-sem:
t.Errorf("AnalogSensor Event should not published")
case <-time.After(30 * time.Millisecond):
}
}

func TestAnalogSensorDriverHalt(t *testing.T) {
d := NewAnalogSensorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
go func() {
<-d.halt
}()
gobot.Assert(t, len(d.Halt()), 0)
}
41 changes: 29 additions & 12 deletions platforms/gpio/button_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ import (

var _ gobot.Driver = (*ButtonDriver)(nil)

// Represents a digital Button
// ButtonDriver Represents a digital Button
type ButtonDriver struct {
Active bool
pin string
name string
halt chan bool
interval time.Duration
connection DigitalReader
gobot.Eventer
}

// NewButtonDriver return a new ButtonDriver given a DigitalReader, name and pin
// NewButtonDriver returns a new ButtonDriver with a polling interval of
// 10 Milliseconds given a DigitalReader, name and pin.
//
// Optinally accepts:
// time.Duration: Interval at which the ButtonDriver is polled for new information
func NewButtonDriver(a DigitalReader, name string, pin string, v ...time.Duration) *ButtonDriver {
b := &ButtonDriver{
name: name,
Expand All @@ -27,6 +32,7 @@ func NewButtonDriver(a DigitalReader, name string, pin string, v ...time.Duratio
Active: false,
Eventer: gobot.NewEventer(),
interval: 10 * time.Millisecond,
halt: make(chan bool),
}

if len(v) > 0 {
Expand All @@ -40,13 +46,12 @@ func NewButtonDriver(a DigitalReader, name string, pin string, v ...time.Duratio
return b
}

// Starts the ButtonDriver and reads the state of the button at the given Driver.Interval().
// Returns true on successful start of the driver.
// Start starts the ButtonDriver and polls the state of the button at the given interval.
//
// Emits the Events:
// "push" int - On button push
// "release" int - On button release
// "error" error - On button error
// Push int - On button push
// Release int - On button release
// Error error - On button error
func (b *ButtonDriver) Start() (errs []error) {
state := 0
go func() {
Expand All @@ -58,17 +63,29 @@ func (b *ButtonDriver) Start() (errs []error) {
state = newValue
b.update(newValue)
}
<-time.After(b.interval)
select {
case <-time.After(b.interval):
case <-b.halt:
return
}
}
}()
return
}

// Halt returns true on a successful halt of the driver
func (b *ButtonDriver) Halt() (errs []error) { return }
// Halt stops polling the button for new information
func (b *ButtonDriver) Halt() (errs []error) {
b.halt <- true
return
}

// Name returns the ButtonDrivers name
func (b *ButtonDriver) Name() string { return b.name }

// Pin returns the ButtonDrivers pin
func (b *ButtonDriver) Pin() string { return b.pin }

func (b *ButtonDriver) Name() string { return b.name }
func (b *ButtonDriver) Pin() string { return b.pin }
// Connection returns the ButtonDrivers Connection
func (b *ButtonDriver) Connection() gobot.Connection { return b.connection.(gobot.Connection) }

func (b *ButtonDriver) update(newValue int) {
Expand Down
27 changes: 24 additions & 3 deletions platforms/gpio/button_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func initTestButtonDriver() *ButtonDriver {

func TestButtonDriverHalt(t *testing.T) {
d := initTestButtonDriver()
go func() {
<-d.halt
}()
gobot.Assert(t, len(d.Halt()), 0)
}

Expand All @@ -36,7 +39,7 @@ func TestButtonDriverStart(t *testing.T) {
return
}

gobot.On(d.Event(Push), func(data interface{}) {
gobot.Once(d.Event(Push), func(data interface{}) {
gobot.Assert(t, d.Active, true)
sem <- true
})
Expand All @@ -52,7 +55,7 @@ func TestButtonDriverStart(t *testing.T) {
return
}

gobot.On(d.Event(Release), func(data interface{}) {
gobot.Once(d.Event(Release), func(data interface{}) {
gobot.Assert(t, d.Active, false)
sem <- true
})
Expand All @@ -68,7 +71,7 @@ func TestButtonDriverStart(t *testing.T) {
return
}

gobot.On(d.Event(Error), func(data interface{}) {
gobot.Once(d.Event(Error), func(data interface{}) {
sem <- true
})

Expand All @@ -77,4 +80,22 @@ func TestButtonDriverStart(t *testing.T) {
case <-time.After(15 * time.Millisecond):
t.Errorf("Button Event \"Error\" was not published")
}

testAdaptorDigitalRead = func() (val int, err error) {
val = 1
return
}

gobot.Once(d.Event(Push), func(data interface{}) {
sem <- true
})

d.halt <- true

select {
case <-sem:
t.Errorf("Button Event \"Press\" should not published")
case <-time.After(15 * time.Millisecond):
}

}
23 changes: 14 additions & 9 deletions platforms/gpio/direct_pin_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (

var _ gobot.Driver = (*DirectPinDriver)(nil)

// Represents a raw GPIO pin
// DirectPinDriver represents a GPIO pin
type DirectPinDriver struct {
name string
pin string
connection gobot.Connection
gobot.Commander
}

// NewDirectPinDriver return a new DirectPinDriver given a DirectPin, name and pin.
// NewDirectPinDriver return a new DirectPinDriver given a Connection, name and pin.
//
// Adds the following API Commands:
// "DigitalRead" - See DirectPinDriver.DigitalRead
Expand Down Expand Up @@ -57,14 +57,19 @@ func NewDirectPinDriver(a gobot.Connection, name string, pin string) *DirectPinD
return d
}

func (d *DirectPinDriver) Name() string { return d.name }
func (d *DirectPinDriver) Pin() string { return d.pin }
// Name returns the DirectPinDrivers name
func (d *DirectPinDriver) Name() string { return d.name }

// Pin returns the DirectPinDrivers pin
func (d *DirectPinDriver) Pin() string { return d.pin }

// Connection returns the DirectPinDrivers Connection
func (d *DirectPinDriver) Connection() gobot.Connection { return d.connection }

// Starts the DirectPinDriver. Returns true on successful start of the driver
// Start implements the Driver interface
func (d *DirectPinDriver) Start() (errs []error) { return }

// Halts the DirectPinDriver. Returns true on successful halt of the driver
// Halt implements the Driver interface
func (d *DirectPinDriver) Halt() (errs []error) { return }

// DigitalRead returns the current digital state of the pin
Expand All @@ -76,7 +81,7 @@ func (d *DirectPinDriver) DigitalRead() (val int, err error) {
return
}

// DigitalWrite writes to the pin
// DigitalWrite writes to the pin. Acceptable values are 1 or 0
func (d *DirectPinDriver) DigitalWrite(level byte) (err error) {
if writer, ok := d.Connection().(DigitalWriter); ok {
return writer.DigitalWrite(d.Pin(), level)
Expand All @@ -94,7 +99,7 @@ func (d *DirectPinDriver) AnalogRead() (val int, err error) {
return
}

// PwmWrite writes to the pin
// PwmWrite writes the 0-254 value to the specified pin
func (d *DirectPinDriver) PwmWrite(level byte) (err error) {
if writer, ok := d.Connection().(PwmWriter); ok {
return writer.PwmWrite(d.Pin(), level)
Expand All @@ -103,7 +108,7 @@ func (d *DirectPinDriver) PwmWrite(level byte) (err error) {
return
}

// ServoWrite writes to the pin
// ServoWrite writes value to the specified pin
func (d *DirectPinDriver) ServoWrite(level byte) (err error) {
if writer, ok := d.Connection().(ServoWriter); ok {
return writer.ServoWrite(d.Pin(), level)
Expand Down
2 changes: 1 addition & 1 deletion platforms/gpio/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Package gpio provides Gobot drivers for General Purpose Input/Output devices.
Installing:
go get github.com/hybridgroup/gobot/platforms/gpio
go get -d -u github.com/hybridgroup/gobot/... && go install github.com/hybridgroup/gobot/platforms/gpio
For further information refer to gpio README:
https://github.com/hybridgroup/gobot/blob/master/platforms/gpio/README.md
Expand Down
Loading

0 comments on commit 5995982

Please sign in to comment.