mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
spi: correct support for MCP3004/MCP3008 A/D converters
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
55d3655d29
commit
41a04f76d0
@ -14,6 +14,9 @@ Gobot has a extensible system for connecting to hardware devices.
|
|||||||
|
|
||||||
The following spi Devices are currently supported:
|
The following spi Devices are currently supported:
|
||||||
|
|
||||||
|
- APA102 Programmable LEDs
|
||||||
|
- MCP3004 Analog/Digital Converter
|
||||||
|
- MCP3008 Analog/Digital Converter
|
||||||
- GoPiGo3 Robot
|
- GoPiGo3 Robot
|
||||||
|
|
||||||
Drivers wanted! :)
|
Drivers wanted! :)
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
package spi
|
package spi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"gobot.io/x/gobot"
|
"gobot.io/x/gobot"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MCP3004DriverMaxChannel is the number of channels (plus one) of this A/D converter.
|
||||||
|
const MCP3004DriverMaxChannel = 3
|
||||||
|
|
||||||
// MCP3004Driver is a driver for the MCP3008 A/D converter.
|
// MCP3004Driver is a driver for the MCP3008 A/D converter.
|
||||||
type MCP3004Driver struct {
|
type MCP3004Driver struct {
|
||||||
name string
|
name string
|
||||||
@ -54,15 +58,24 @@ func (d *MCP3004Driver) Halt() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read reads the current analog data for the desired channel.
|
// Read reads the current analog data for the desired channel.
|
||||||
func (d *MCP3004Driver) Read(channel int) (int, error) {
|
func (d *MCP3004Driver) Read(channel int) (result int, err error) {
|
||||||
|
if channel < 0 || channel > MCP3004DriverMaxChannel {
|
||||||
|
return 0, errors.New("Invalid channel for read")
|
||||||
|
}
|
||||||
|
|
||||||
tx := make([]byte, 3)
|
tx := make([]byte, 3)
|
||||||
tx[0] = 0x01
|
tx[0] = 0x01
|
||||||
tx[1] = 0x80 + (byte(channel) << 4)
|
tx[1] = 0x80 + (byte(channel) << 4)
|
||||||
tx[2] = 0x00
|
tx[2] = 0x00
|
||||||
|
|
||||||
d.connection.Tx(tx, nil)
|
rx := make([]byte, 3)
|
||||||
|
|
||||||
return 0, nil
|
err = d.connection.Tx(tx, rx)
|
||||||
|
if err == nil && len(rx) == 3 {
|
||||||
|
result = int(((rx[1] & 0x03) << 8) + rx[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnalogRead returns value from analog reading of specified pin
|
// AnalogRead returns value from analog reading of specified pin
|
||||||
|
@ -32,4 +32,6 @@ func TestMCP3004DriverHalt(t *testing.T) {
|
|||||||
func TestMCP3004DriverRead(t *testing.T) {
|
func TestMCP3004DriverRead(t *testing.T) {
|
||||||
d := initTestMCP3004Driver()
|
d := initTestMCP3004Driver()
|
||||||
d.Start()
|
d.Start()
|
||||||
|
|
||||||
|
// TODO: actual read test
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
package spi
|
package spi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"gobot.io/x/gobot"
|
"gobot.io/x/gobot"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MCP3008DriverMaxChannel is the number of channels (plus one) of this A/D converter.
|
||||||
|
const MCP3008DriverMaxChannel = 7
|
||||||
|
|
||||||
// MCP3008Driver is a driver for the MCP3008 A/D converter.
|
// MCP3008Driver is a driver for the MCP3008 A/D converter.
|
||||||
type MCP3008Driver struct {
|
type MCP3008Driver struct {
|
||||||
name string
|
name string
|
||||||
@ -54,15 +58,24 @@ func (d *MCP3008Driver) Halt() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read reads the current analog data for the desired channel.
|
// Read reads the current analog data for the desired channel.
|
||||||
func (d *MCP3008Driver) Read(channel int) (int, error) {
|
func (d *MCP3008Driver) Read(channel int) (result int, err error) {
|
||||||
|
if channel < 0 || channel > MCP3008DriverMaxChannel {
|
||||||
|
return 0, errors.New("Invalid channel for read")
|
||||||
|
}
|
||||||
|
|
||||||
tx := make([]byte, 3)
|
tx := make([]byte, 3)
|
||||||
tx[0] = 0x01
|
tx[0] = 0x01
|
||||||
tx[1] = 0x80 + (byte(channel) << 4)
|
tx[1] = 0x80 + (byte(channel) << 4)
|
||||||
tx[2] = 0x00
|
tx[2] = 0x00
|
||||||
|
|
||||||
d.connection.Tx(tx, nil)
|
rx := make([]byte, 3)
|
||||||
|
|
||||||
return 0, nil
|
err = d.connection.Tx(tx, rx)
|
||||||
|
if err == nil && len(rx) == 3 {
|
||||||
|
result = int(((rx[1] & 0x03) << 8) + rx[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnalogRead returns value from analog reading of specified pin
|
// AnalogRead returns value from analog reading of specified pin
|
||||||
|
@ -32,4 +32,6 @@ func TestMCP3008DriverHalt(t *testing.T) {
|
|||||||
func TestMCP3008DriverRead(t *testing.T) {
|
func TestMCP3008DriverRead(t *testing.T) {
|
||||||
d := initTestMCP3008Driver()
|
d := initTestMCP3008Driver()
|
||||||
d.Start()
|
d.Start()
|
||||||
|
|
||||||
|
// TODO: actual read test
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user