mirror of
https://github.com/kidoman/embd
synced 2025-07-04 04:17:41 +02:00
mcp3008: changes to use external spiBus
This commit is contained in:
parent
1bb2bac8f1
commit
dbc97bd15c
2 changed files with 15 additions and 24 deletions
|
@ -16,35 +16,20 @@ type mcp3008 struct {
|
|||
var SingleMode byte = 1
|
||||
var DifferenceMode byte = 0
|
||||
|
||||
func New(mode byte, spiChan, speed int) (*mcp3008, error) {
|
||||
if err := embd.InitSPI(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
glog.V(3).Infof("mcp3008: getting spiBus with mode: %v, channel: %v, speed: %v", mode, spiChan, speed)
|
||||
spiBus := embd.NewSPIBus(embd.SpiMode0, byte(spiChan), speed, 0, 0)
|
||||
return &mcp3008{mode, spiBus}, nil
|
||||
func New(mode byte, bus embd.SPIBus) *mcp3008 {
|
||||
return &mcp3008{mode, bus}
|
||||
}
|
||||
|
||||
func (m *mcp3008) AnalogValueAt(chanNum int) (int, error) {
|
||||
var data [3]uint8
|
||||
data := make([]uint8, 3)
|
||||
data[0] = 1
|
||||
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
|
||||
}
|
||||
|
||||
func (m *mcp3008) Close() error {
|
||||
glog.V(2).Infoln("mcp3008: performing cleanup")
|
||||
if err := m.bus.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := embd.CloseSPI(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue