2017-12-14 03:08:31 +08:00
|
|
|
package spi
|
|
|
|
|
|
|
|
import (
|
2022-12-17 18:56:11 +08:00
|
|
|
"fmt"
|
2017-12-14 03:08:31 +08:00
|
|
|
"strconv"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2017-12-14 03:08:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// MCP3304DriverMaxChannel is the number of channels of this A/D converter.
|
|
|
|
const MCP3304DriverMaxChannel = 8
|
|
|
|
|
|
|
|
// MCP3304Driver is a driver for the MCP3304 A/D converter.
|
|
|
|
type MCP3304Driver struct {
|
2022-12-17 18:56:11 +08:00
|
|
|
*Driver
|
2017-12-14 03:08:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMCP3304Driver creates a new Gobot Driver for MCP3304Driver A/D converter
|
|
|
|
//
|
|
|
|
// Params:
|
2023-05-20 20:25:21 +08:00
|
|
|
//
|
|
|
|
// a *Adaptor - the Adaptor to use with this Driver
|
2017-12-14 03:08:31 +08:00
|
|
|
//
|
2018-04-12 02:22:59 +08:00
|
|
|
// Optional params:
|
|
|
|
//
|
2023-05-20 20:25:21 +08:00
|
|
|
// spi.WithBusNumber(int): bus to use with this driver
|
|
|
|
// spi.WithChipNumber(int): chip to use with this driver
|
|
|
|
// spi.WithMode(int): mode to use with this driver
|
|
|
|
// spi.WithBitCount(int): number of bits to use with this driver
|
|
|
|
// spi.WithSpeed(int64): speed in Hz to use with this driver
|
2018-04-12 02:22:59 +08:00
|
|
|
func NewMCP3304Driver(a Connector, options ...func(Config)) *MCP3304Driver {
|
2017-12-14 03:08:31 +08:00
|
|
|
d := &MCP3304Driver{
|
2022-12-17 18:56:11 +08:00
|
|
|
Driver: NewDriver(a, "MCP3304"),
|
2018-04-12 02:22:59 +08:00
|
|
|
}
|
|
|
|
for _, option := range options {
|
|
|
|
option(d)
|
2017-12-14 03:08:31 +08:00
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads the current analog data for the desired channel.
|
2023-11-16 03:51:52 +08:00
|
|
|
func (d *MCP3304Driver) Read(channel int) (int, error) {
|
2017-12-14 03:08:31 +08:00
|
|
|
if channel < 0 || channel > MCP3304DriverMaxChannel-1 {
|
2022-12-17 18:56:11 +08:00
|
|
|
return 0, fmt.Errorf("Invalid channel '%d' for read", channel)
|
2017-12-14 03:08:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tx := make([]byte, 3)
|
|
|
|
tx[0] = 0x0c + (byte(channel) >> 1)
|
|
|
|
tx[1] = (byte(channel) & 0x01) << 7
|
|
|
|
tx[2] = 0x00
|
|
|
|
|
|
|
|
rx := make([]byte, 3)
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
if err := d.connection.ReadCommandData(tx, rx); err != nil || len(rx) != 3 {
|
|
|
|
return 0, err
|
2017-12-14 03:08:31 +08:00
|
|
|
}
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
result := int((rx[1]&0xf))<<8 + int(rx[2])
|
|
|
|
|
|
|
|
return result, nil
|
2017-12-14 03:08:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AnalogRead returns value from analog reading of specified pin, scaled to 0-1023 value.
|
2023-11-16 03:51:52 +08:00
|
|
|
func (d *MCP3304Driver) AnalogRead(pin string) (int, error) {
|
2017-12-14 03:08:31 +08:00
|
|
|
channel, _ := strconv.Atoi(pin)
|
2023-11-16 03:51:52 +08:00
|
|
|
value, err := d.Read(channel)
|
2017-12-14 03:08:31 +08:00
|
|
|
if err != nil {
|
2023-11-16 03:51:52 +08:00
|
|
|
return 0, err
|
2017-12-14 03:08:31 +08:00
|
|
|
}
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
return int(gobot.ToScale(gobot.FromScale(float64(value), 0, 4095), 0, 1023)), err
|
2017-12-14 03:08:31 +08:00
|
|
|
}
|