Skip to content

Commit

Permalink
aio: separate analog drivers from gpio drivers
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Dec 20, 2016
1 parent 070edf0 commit c186638
Show file tree
Hide file tree
Showing 27 changed files with 461 additions and 198 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PACKAGES := gobot gobot/api gobot/drivers/gpio gobot/drivers/i2c gobot/platforms/firmata/client gobot/platforms/intel-iot/edison gobot/platforms/intel-iot/joule gobot/platforms/parrot/ardrone gobot/platforms/parrot/bebop gobot/platforms/parrot/minidrone gobot/platforms/sphero/ollie gobot/platforms/sphero/bb8 gobot/sysfs $(shell ls ./platforms | sed -e 's/^/gobot\/platforms\//')
PACKAGES := gobot gobot/api gobot/drivers/gpio gobot/drivers/aio gobot/drivers/i2c gobot/platforms/firmata/client gobot/platforms/intel-iot/edison gobot/platforms/intel-iot/joule gobot/platforms/parrot/ardrone gobot/platforms/parrot/bebop gobot/platforms/parrot/minidrone gobot/platforms/sphero/ollie gobot/platforms/sphero/bb8 gobot/sysfs $(shell ls ./platforms | sed -e 's/^/gobot\/platforms\//')
.PHONY: test cover robeaux examples

test:
Expand Down
2 changes: 1 addition & 1 deletion ci/travis.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
PACKAGES=('gobot' 'gobot/api' 'gobot/sysfs' 'gobot/drivers/gpio' 'gobot/drivers/i2c' 'gobot/platforms/firmata/client' 'gobot/platforms/intel-iot/edison' 'gobot/platforms/intel-iot/joule' 'gobot/platforms/parrot/ardrone' 'gobot/platforms/parrot/bebop' 'gobot/platforms/parrot/minidrone' 'gobot/platforms/sphero/ollie' 'gobot/platforms/sphero/bb8' $(ls ./platforms | sed -e 's/^/gobot\/platforms\//'))
PACKAGES=('gobot' 'gobot/api' 'gobot/sysfs' 'gobot/drivers/gpio' 'gobot/drivers/aio' 'gobot/drivers/i2c' 'gobot/platforms/firmata/client' 'gobot/platforms/intel-iot/edison' 'gobot/platforms/intel-iot/joule' 'gobot/platforms/parrot/ardrone' 'gobot/platforms/parrot/bebop' 'gobot/platforms/parrot/minidrone' 'gobot/platforms/sphero/ollie' 'gobot/platforms/sphero/bb8' $(ls ./platforms | sed -e 's/^/gobot\/platforms\//'))
EXITCODE=0

echo "mode: set" > profile.cov
Expand Down
13 changes: 13 additions & 0 deletions drivers/aio/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2013-2016 The Hybrid Group

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
21 changes: 21 additions & 0 deletions drivers/aio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# AIO

This package provides drivers for [Analog Input/Output (AIO)]() devices. It is normally used by connecting an adaptor such as [firmata](https://gobot.io/x/gobot/platforms/firmata) that supports the needed interfaces for AIO devices.

## Getting Started

## Installing
```
go get -d -u gobot.io/x/gobot/...
```

## Hardware Support
Gobot has a extensible system for connecting to hardware devices. The following AIO devices are currently supported:
- Analog Sensor
- Direct Pin
- Grove Light Sensor
- Grove Rotary Dial
- Grove Sound Sensor
- Grove Temperature Sensor

More drivers are coming soon...
28 changes: 28 additions & 0 deletions drivers/aio/aio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package aio

import (
"errors"

"gobot.io/x/gobot"
)

var (
// ErrAnalogReadUnsupported is error resulting when a driver attempts to use
// hardware capabilities which a connection does not support
ErrAnalogReadUnsupported = errors.New("AnalogRead is not supported by this platform")
)

const (
// Error event
Error = "error"
// Data event
Data = "data"
// Vibration event
Vibration = "vibration"
)

// AnalogReader interface represents an Adaptor which has Analog capabilities
type AnalogReader interface {
gobot.Adaptor
AnalogRead(string) (val int, err error)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gpio
package aio

import (
"time"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gpio
package aio

import (
"errors"
Expand All @@ -12,12 +12,12 @@ import (
var _ gobot.Driver = (*AnalogSensorDriver)(nil)

func TestAnalogSensorDriver(t *testing.T) {
d := NewAnalogSensorDriver(newGpioTestAdaptor(), "1")
d := NewAnalogSensorDriver(newAioTestAdaptor(), "1")
gobottest.Refute(t, d.Connection(), nil)
// default interval
gobottest.Assert(t, d.interval, 10*time.Millisecond)

d = NewAnalogSensorDriver(newGpioTestAdaptor(), "42", 30*time.Second)
d = NewAnalogSensorDriver(newAioTestAdaptor(), "42", 30*time.Second)
gobottest.Assert(t, d.Pin(), "42")
gobottest.Assert(t, d.interval, 30*time.Second)

Expand All @@ -34,7 +34,7 @@ func TestAnalogSensorDriver(t *testing.T) {
func TestAnalogSensorDriverStart(t *testing.T) {
sem := make(chan bool, 1)

d := NewAnalogSensorDriver(newGpioTestAdaptor(), "1")
d := NewAnalogSensorDriver(newAioTestAdaptor(), "1")

gobottest.Assert(t, d.Start(), nil)

Expand Down Expand Up @@ -94,7 +94,7 @@ func TestAnalogSensorDriverStart(t *testing.T) {
}

func TestAnalogSensorDriverHalt(t *testing.T) {
d := NewAnalogSensorDriver(newGpioTestAdaptor(), "1")
d := NewAnalogSensorDriver(newAioTestAdaptor(), "1")
done := make(chan struct{})
go func() {
<-d.halt
Expand Down
60 changes: 60 additions & 0 deletions drivers/aio/direct_pin_driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package aio

import (
"gobot.io/x/gobot"
)

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

// NewDirectPinDriver return a new DirectPinDriver given a Connection and pin.
//
// Adds the following API Command:
// "AnalogRead" - See DirectPinDriver.AnalogRead
func NewDirectPinDriver(a gobot.Connection, pin string) *DirectPinDriver {
d := &DirectPinDriver{
name: "DirectPin",
connection: a,
pin: pin,
Commander: gobot.NewCommander(),
}

d.AddCommand("AnalogRead", func(params map[string]interface{}) interface{} {
val, err := d.AnalogRead()
return map[string]interface{}{"val": val, "err": err}
})

return d
}

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

// SetName sets the DirectPinDrivers name
func (d *DirectPinDriver) SetName(n string) { d.name = n }

// 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 }

// Start implements the Driver interface
func (d *DirectPinDriver) Start() (err error) { return }

// Halt implements the Driver interface
func (d *DirectPinDriver) Halt() (err error) { return }

// AnalogRead reads the current analog reading of the pin
func (d *DirectPinDriver) AnalogRead() (val int, err error) {
if reader, ok := d.Connection().(AnalogReader); ok {
return reader.AnalogRead(d.Pin())
}
err = ErrAnalogReadUnsupported
return
}
51 changes: 51 additions & 0 deletions drivers/aio/direct_pin_driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package aio

import (
"testing"

"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)

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

func initTestDirectPinDriver(conn gobot.Connection) *DirectPinDriver {
testAdaptorAnalogRead = func() (val int, err error) {
val = 80
return
}
return NewDirectPinDriver(conn, "1")
}

func TestDirectPinDriver(t *testing.T) {
var ret map[string]interface{}

d := initTestDirectPinDriver(newAioTestAdaptor())
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)

ret = d.Command("AnalogRead")(nil).(map[string]interface{})

gobottest.Assert(t, ret["val"].(int), 80)
gobottest.Assert(t, ret["err"], nil)
}

func TestDirectPinDriverStart(t *testing.T) {
d := initTestDirectPinDriver(newAioTestAdaptor())
gobottest.Assert(t, d.Start(), nil)
}

func TestDirectPinDriverHalt(t *testing.T) {
d := initTestDirectPinDriver(newAioTestAdaptor())
gobottest.Assert(t, d.Halt(), nil)
}

func TestDirectPinDriverAnalogRead(t *testing.T) {
d := initTestDirectPinDriver(newAioTestAdaptor())
ret, err := d.AnalogRead()
gobottest.Assert(t, ret, 80)

d = initTestDirectPinDriver(&aioTestBareAdaptor{})
ret, err = d.AnalogRead()
gobottest.Assert(t, err, ErrAnalogReadUnsupported)
}
11 changes: 11 additions & 0 deletions drivers/aio/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Package aio provides Gobot drivers for Analog Input/Output devices.
Installing:
go get -d -u gobot.io/x/gobot/...
For further information refer to gpio README:
https://gobot.io/x/gobot/blob/master/platforms/aio/README.md
*/
package aio
94 changes: 94 additions & 0 deletions drivers/aio/grove_drivers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package aio

import (
"time"
)

// GroveRotaryDriver represents an analog rotary dial with a Grove connector
type GroveRotaryDriver struct {
*AnalogSensorDriver
}

// NewGroveRotaryDriver returns a new GroveRotaryDriver with a polling interval of
// 10 Milliseconds given an AnalogReader and pin.
//
// Optionally accepts:
// time.Duration: Interval at which the AnalogSensor is polled for new information
//
// Adds the following API Commands:
// "Read" - See AnalogSensor.Read
func NewGroveRotaryDriver(a AnalogReader, pin string, v ...time.Duration) *GroveRotaryDriver {
return &GroveRotaryDriver{
AnalogSensorDriver: NewAnalogSensorDriver(a, pin, v...),
}
}

// GroveLightSensorDriver represents an analog light sensor
// with a Grove connector
type GroveLightSensorDriver struct {
*AnalogSensorDriver
}

// NewGroveLightSensorDriver returns a new GroveLightSensorDriver with a polling interval of
// 10 Milliseconds given an AnalogReader and pin.
//
// Optionally accepts:
// time.Duration: Interval at which the AnalogSensor is polled for new information
//
// Adds the following API Commands:
// "Read" - See AnalogSensor.Read
func NewGroveLightSensorDriver(a AnalogReader, pin string, v ...time.Duration) *GroveLightSensorDriver {
return &GroveLightSensorDriver{
AnalogSensorDriver: NewAnalogSensorDriver(a, pin, v...),
}
}

// GrovePiezoVibrationSensorDriver represents an analog vibration sensor
// with a Grove connector
type GrovePiezoVibrationSensorDriver struct {
*AnalogSensorDriver
}

// NewGrovePiezoVibrationSensorDriver returns a new GrovePiezoVibrationSensorDriver with a polling interval of
// 10 Milliseconds given an AnalogReader and pin.
//
// Optionally accepts:
// time.Duration: Interval at which the AnalogSensor is polled for new information
//
// Adds the following API Commands:
// "Read" - See AnalogSensor.Read
func NewGrovePiezoVibrationSensorDriver(a AnalogReader, pin string, v ...time.Duration) *GrovePiezoVibrationSensorDriver {
sensor := &GrovePiezoVibrationSensorDriver{
AnalogSensorDriver: NewAnalogSensorDriver(a, pin, v...),
}

sensor.AddEvent(Vibration)

sensor.On(sensor.Event(Data), func(data interface{}) {
if data.(int) > 1000 {
sensor.Publish(sensor.Event(Vibration), data)
}
})

return sensor
}

// GroveSoundSensorDriver represents a analog sound sensor
// with a Grove connector
type GroveSoundSensorDriver struct {
*AnalogSensorDriver
}

// NewGroveSoundSensorDriver returns a new GroveSoundSensorDriver with a polling interval of
// 10 Milliseconds given an AnalogReader and pin.
//
// Optionally accepts:
// time.Duration: Interval at which the AnalogSensor is polled for new information
//
// Adds the following API Commands:
// "Read" - See AnalogSensor.Read
func NewGroveSoundSensorDriver(a AnalogReader, pin string, v ...time.Duration) *GroveSoundSensorDriver {
return &GroveSoundSensorDriver{
AnalogSensorDriver: NewAnalogSensorDriver(a, pin, v...),
}
}
Loading

0 comments on commit c186638

Please sign in to comment.