1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 21:00:05 +01:00
embd/convertors/mcp3008/mcp3008.go

47 lines
1.0 KiB
Go
Raw Normal View History

2014-05-22 12:10:14 +05:30
// Package mcp3008 allows interfacing with the mcp3008 8-channel, 10-bit ADC through SPI protocol.
package mcp3008
import (
"github.com/golang/glog"
"github.com/kidoman/embd"
)
// MCP3008 represents a mcp3008 8bit DAC.
2014-05-22 02:19:01 +05:30
type MCP3008 struct {
Mode byte
Bus embd.SPIBus
}
const (
// SingleMode represents the single-ended mode for the mcp3008.
SingleMode = 1
// DifferenceMode represents the diffenrential mode for the mcp3008.
DifferenceMode = 0
)
// New creates a representation of the mcp3008 convertor
2014-05-22 02:19:01 +05:30
func New(mode byte, bus embd.SPIBus) *MCP3008 {
return &MCP3008{mode, bus}
}
2014-05-22 02:19:01 +05:30
const (
startBit = 1
)
// AnalogValueAt returns the analog value at the given channel of the convertor.
2014-05-22 02:19:01 +05:30
func (m *MCP3008) AnalogValueAt(chanNum int) (int, error) {
var data [3]uint8
data[0] = startBit
data[1] = uint8(m.Mode)<<7 | uint8(chanNum)<<4
data[2] = 0
glog.V(2).Infof("mcp3008: sendingdata buffer %v", data)
if err := m.Bus.TransferAndRecieveData(data[:]); err != nil {
return 0, err
}
return int(uint16(data[1]&0x03)<<8 | uint16(data[2])), nil
}