1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00

spi: remove types from const declarations and added few more go docs

This commit is contained in:
kunalpowar 2014-05-22 02:58:15 +05:30
parent 2d3dee8764
commit 32c9a96af5
2 changed files with 21 additions and 15 deletions

View File

@ -5,16 +5,22 @@ import (
"github.com/kidoman/embd" "github.com/kidoman/embd"
) )
// MCP3008 represents a mcp3008 8bit DAC // MCP3008 represents a mcp3008 8bit DAC.
type MCP3008 struct { type MCP3008 struct {
mode byte Mode byte
bus embd.SPIBus Bus embd.SPIBus
} }
var SingleMode byte = 1 const (
var DifferenceMode byte = 0 // 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
func New(mode byte, bus embd.SPIBus) *MCP3008 { func New(mode byte, bus embd.SPIBus) *MCP3008 {
return &MCP3008{mode, bus} return &MCP3008{mode, bus}
} }
@ -23,14 +29,15 @@ const (
startBit = 1 startBit = 1
) )
// AnalogValueAt returns the analog value at the given channel of the convertor.
func (m *MCP3008) AnalogValueAt(chanNum int) (int, error) { func (m *MCP3008) AnalogValueAt(chanNum int) (int, error) {
var data [3]uint8 var data [3]uint8
data[0] = startBit data[0] = startBit
data[1] = uint8(m.mode)<<7 | uint8(chanNum)<<4 data[1] = uint8(m.Mode)<<7 | uint8(chanNum)<<4
data[2] = 0 data[2] = 0
glog.V(2).Infof("mcp3008: sendingdata buffer %v", data) glog.V(2).Infof("mcp3008: sendingdata buffer %v", data)
if err := m.bus.TransferAndRecieveData(data[:]); err != nil { if err := m.Bus.TransferAndRecieveData(data[:]); err != nil {
return 0, err return 0, err
} }

View File

@ -23,9 +23,9 @@ const (
spiIOCMessage0 = 1073769216 //0x40006B00 spiIOCMessage0 = 1073769216 //0x40006B00
spiIOCIncrementor = 2097152 //0x200000 spiIOCIncrementor = 2097152 //0x200000
defaultDelayms = uint16(0) defaultDelayms = 0
defaultSPIBPW = uint8(8) defaultSPIBPW = 8
defaultSPISpeed = uint32(1000000) defaultSPISpeed = 1000000
) )
type spiIOCTransfer struct { type spiIOCTransfer struct {
@ -115,12 +115,11 @@ func (b *spiBus) init() error {
func (b *spiBus) setMode() error { func (b *spiBus) setMode() error {
var mode = uint8(b.mode) var mode = uint8(b.mode)
var err error
glog.V(3).Infof("spi: setting spi mode to %v", mode) glog.V(3).Infof("spi: setting spi mode to %v", mode)
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), spiIOCWrMode, uintptr(unsafe.Pointer(&mode))) _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), spiIOCWrMode, uintptr(unsafe.Pointer(&mode)))
if errno != 0 { if errno != 0 {
err = syscall.Errno(errno) err := syscall.Errno(errno)
glog.V(3).Infof("spi: failed to set mode due to %v", err.Error()) glog.V(3).Infof("spi: failed to set mode due to %v", err.Error())
return err return err
} }
@ -129,7 +128,7 @@ func (b *spiBus) setMode() error {
} }
func (b *spiBus) setSpeed() error { func (b *spiBus) setSpeed() error {
speed := defaultSPISpeed var speed uint32 = defaultSPISpeed
if b.speed > 0 { if b.speed > 0 {
speed = uint32(b.speed) speed = uint32(b.speed)
} }
@ -148,7 +147,7 @@ func (b *spiBus) setSpeed() error {
} }
func (b *spiBus) setBPW() error { func (b *spiBus) setBPW() error {
bpw := defaultSPIBPW var bpw uint8 = defaultSPIBPW
if b.bpw > 0 { if b.bpw > 0 {
bpw = uint8(b.bpw) bpw = uint8(b.bpw)
} }
@ -166,7 +165,7 @@ func (b *spiBus) setBPW() error {
} }
func (b *spiBus) setDelay() { func (b *spiBus) setDelay() {
delay := defaultDelayms var delay uint16 = defaultDelayms
if b.delayms > 0 { if b.delayms > 0 {
delay = uint16(b.delayms) delay = uint16(b.delayms)
} }