1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00

spi: correct support for MCP3004/MCP3008 A/D converters

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-12-13 15:40:38 +01:00
parent 55d3655d29
commit 41a04f76d0
5 changed files with 39 additions and 6 deletions

View File

@ -14,6 +14,9 @@ Gobot has a extensible system for connecting to hardware devices.
The following spi Devices are currently supported:
- APA102 Programmable LEDs
- MCP3004 Analog/Digital Converter
- MCP3008 Analog/Digital Converter
- GoPiGo3 Robot
Drivers wanted! :)

View File

@ -1,11 +1,15 @@
package spi
import (
"errors"
"strconv"
"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.
type MCP3004Driver struct {
name string
@ -54,15 +58,24 @@ func (d *MCP3004Driver) Halt() (err error) {
}
// 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[0] = 0x01
tx[1] = 0x80 + (byte(channel) << 4)
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

View File

@ -32,4 +32,6 @@ func TestMCP3004DriverHalt(t *testing.T) {
func TestMCP3004DriverRead(t *testing.T) {
d := initTestMCP3004Driver()
d.Start()
// TODO: actual read test
}

View File

@ -1,11 +1,15 @@
package spi
import (
"errors"
"strconv"
"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.
type MCP3008Driver struct {
name string
@ -54,15 +58,24 @@ func (d *MCP3008Driver) Halt() (err error) {
}
// 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[0] = 0x01
tx[1] = 0x80 + (byte(channel) << 4)
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

View File

@ -32,4 +32,6 @@ func TestMCP3008DriverHalt(t *testing.T) {
func TestMCP3008DriverRead(t *testing.T) {
d := initTestMCP3008Driver()
d.Start()
// TODO: actual read test
}