From 41a04f76d0e115ac59d3c27f305f737682e02f00 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Wed, 13 Dec 2017 15:40:38 +0100 Subject: [PATCH] spi: correct support for MCP3004/MCP3008 A/D converters Signed-off-by: deadprogram --- drivers/spi/README.MD | 3 +++ drivers/spi/mcp3004.go | 19 ++++++++++++++++--- drivers/spi/mcp3004_test.go | 2 ++ drivers/spi/mcp3008.go | 19 ++++++++++++++++--- drivers/spi/mcp3008_test.go | 2 ++ 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/drivers/spi/README.MD b/drivers/spi/README.MD index 29b86d98..cf2907c0 100644 --- a/drivers/spi/README.MD +++ b/drivers/spi/README.MD @@ -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! :) diff --git a/drivers/spi/mcp3004.go b/drivers/spi/mcp3004.go index 9c53215b..6fb92b67 100644 --- a/drivers/spi/mcp3004.go +++ b/drivers/spi/mcp3004.go @@ -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 diff --git a/drivers/spi/mcp3004_test.go b/drivers/spi/mcp3004_test.go index 8f542c4f..48c8662b 100644 --- a/drivers/spi/mcp3004_test.go +++ b/drivers/spi/mcp3004_test.go @@ -32,4 +32,6 @@ func TestMCP3004DriverHalt(t *testing.T) { func TestMCP3004DriverRead(t *testing.T) { d := initTestMCP3004Driver() d.Start() + + // TODO: actual read test } diff --git a/drivers/spi/mcp3008.go b/drivers/spi/mcp3008.go index ddc740e1..2de0663f 100644 --- a/drivers/spi/mcp3008.go +++ b/drivers/spi/mcp3008.go @@ -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 diff --git a/drivers/spi/mcp3008_test.go b/drivers/spi/mcp3008_test.go index e7741219..c9677c7d 100644 --- a/drivers/spi/mcp3008_test.go +++ b/drivers/spi/mcp3008_test.go @@ -32,4 +32,6 @@ func TestMCP3008DriverHalt(t *testing.T) { func TestMCP3008DriverRead(t *testing.T) { d := initTestMCP3008Driver() d.Start() + + // TODO: actual read test }