forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aio: separate analog drivers from gpio drivers
Signed-off-by: deadprogram <[email protected]>
- Loading branch information
1 parent
070edf0
commit c186638
Showing
27 changed files
with
461 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
2 changes: 1 addition & 1 deletion
2
drivers/gpio/analog_sensor_driver.go → drivers/aio/analog_sensor_driver.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gpio | ||
package aio | ||
|
||
import ( | ||
"time" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...), | ||
} | ||
} |
Oops, something went wrong.