mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-26 13:48:49 +08:00
spi: add support for MCP3008 A/D converter
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
2e7e59b1ce
commit
55d3655d29
74
drivers/spi/mcp3008.go
Normal file
74
drivers/spi/mcp3008.go
Normal file
@ -0,0 +1,74 @@
|
||||
package spi
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"gobot.io/x/gobot"
|
||||
)
|
||||
|
||||
// MCP3008Driver is a driver for the MCP3008 A/D converter.
|
||||
type MCP3008Driver struct {
|
||||
name string
|
||||
connector Connector
|
||||
connection Connection
|
||||
}
|
||||
|
||||
// NewMCP3008Driver creates a new Gobot Driver for MCP3008Driver A/D converter
|
||||
//
|
||||
// Params:
|
||||
// a *Adaptor - the Adaptor to use with this Driver
|
||||
//
|
||||
func NewMCP3008Driver(a Connector) *MCP3008Driver {
|
||||
d := &MCP3008Driver{
|
||||
name: gobot.DefaultName("MCP3008"),
|
||||
connector: a,
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// Name returns the name of the device.
|
||||
func (d *MCP3008Driver) Name() string { return d.name }
|
||||
|
||||
// SetName sets the name of the device.
|
||||
func (d *MCP3008Driver) SetName(n string) { d.name = n }
|
||||
|
||||
// Connection returns the Connection of the device.
|
||||
func (d *MCP3008Driver) Connection() gobot.Connection { return d.connection.(gobot.Connection) }
|
||||
|
||||
// Start initializes the driver.
|
||||
func (d *MCP3008Driver) Start() (err error) {
|
||||
bus := d.connector.GetSpiDefaultBus()
|
||||
mode := d.connector.GetSpiDefaultMode()
|
||||
maxSpeed := d.connector.GetSpiDefaultMaxSpeed()
|
||||
d.connection, err = d.connector.GetSpiConnection(bus, mode, maxSpeed)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Halt stops the driver.
|
||||
func (d *MCP3008Driver) Halt() (err error) {
|
||||
d.connection.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// Read reads the current analog data for the desired channel.
|
||||
func (d *MCP3008Driver) Read(channel int) (int, error) {
|
||||
tx := make([]byte, 3)
|
||||
tx[0] = 0x01
|
||||
tx[1] = 0x80 + (byte(channel) << 4)
|
||||
tx[2] = 0x00
|
||||
|
||||
d.connection.Tx(tx, nil)
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// AnalogRead returns value from analog reading of specified pin
|
||||
func (d *MCP3008Driver) AnalogRead(pin string) (value int, err error) {
|
||||
channel, _ := strconv.Atoi(pin)
|
||||
value, err = d.Read(channel)
|
||||
|
||||
return
|
||||
}
|
35
drivers/spi/mcp3008_test.go
Normal file
35
drivers/spi/mcp3008_test.go
Normal file
@ -0,0 +1,35 @@
|
||||
package spi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gobot.io/x/gobot"
|
||||
"gobot.io/x/gobot/drivers/aio"
|
||||
"gobot.io/x/gobot/gobottest"
|
||||
)
|
||||
|
||||
var _ gobot.Driver = (*MCP3008Driver)(nil)
|
||||
|
||||
// must implement the AnalogReader interface
|
||||
var _ aio.AnalogReader = (*MCP3008Driver)(nil)
|
||||
|
||||
func initTestMCP3008Driver() *MCP3008Driver {
|
||||
d := NewMCP3008Driver(&TestConnector{})
|
||||
return d
|
||||
}
|
||||
|
||||
func TestMCP3008DriverStart(t *testing.T) {
|
||||
d := initTestMCP3008Driver()
|
||||
gobottest.Assert(t, d.Start(), nil)
|
||||
}
|
||||
|
||||
func TestMCP3008DriverHalt(t *testing.T) {
|
||||
d := initTestMCP3008Driver()
|
||||
d.Start()
|
||||
gobottest.Assert(t, d.Halt(), nil)
|
||||
}
|
||||
|
||||
func TestMCP3008DriverRead(t *testing.T) {
|
||||
d := initTestMCP3008Driver()
|
||||
d.Start()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user