mirror of
https://github.com/kidoman/embd
synced 2024-12-22 21:00:05 +01:00
mcp3008: changes to use external spiBus
This commit is contained in:
parent
1bb2bac8f1
commit
dbc97bd15c
@ -16,35 +16,20 @@ type mcp3008 struct {
|
|||||||
var SingleMode byte = 1
|
var SingleMode byte = 1
|
||||||
var DifferenceMode byte = 0
|
var DifferenceMode byte = 0
|
||||||
|
|
||||||
func New(mode byte, spiChan, speed int) (*mcp3008, error) {
|
func New(mode byte, bus embd.SPIBus) *mcp3008 {
|
||||||
if err := embd.InitSPI(); err != nil {
|
return &mcp3008{mode, bus}
|
||||||
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 (m *mcp3008) AnalogValueAt(chanNum int) (int, error) {
|
func (m *mcp3008) AnalogValueAt(chanNum int) (int, error) {
|
||||||
var data [3]uint8
|
data := make([]uint8, 3)
|
||||||
data[0] = 1
|
data[0] = 1
|
||||||
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)
|
||||||
if err := m.bus.TransferAndRecieveData(data); err != nil {
|
if err := m.bus.TransferAndRecieveData(data); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return int(uint16(data[1]&0x03)<<8 | uint16(data[2])), nil
|
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
|
|
||||||
}
|
|
||||||
|
@ -8,17 +8,23 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kidoman/embd/convertors/mcp3008"
|
"github.com/kidoman/embd/convertors/mcp3008"
|
||||||
|
|
||||||
|
"github.com/kidoman/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
fmt.Println("This is a sample code for mcp3008 10bit 8 channel ADC")
|
fmt.Println("this is a sample code for mcp3008 10bit 8 channel ADC")
|
||||||
|
|
||||||
adc, err := mcp3008.New(mcp3008.SingleMode, 0, 1000000)
|
if err := embd.InitSPI(); err != nil {
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer adc.Close()
|
defer embd.CloseSPI()
|
||||||
|
|
||||||
|
spiBus := embd.NewSPIBus(embd.SpiMode0, 0, 1000000, 8, 0)
|
||||||
|
defer spiBus.Close()
|
||||||
|
|
||||||
|
adc := mcp3008.New(mcp3008.SingleMode, spiBus)
|
||||||
|
|
||||||
for i := 0; i < 20; i++ {
|
for i := 0; i < 20; i++ {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
@ -27,7 +33,7 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Analog value is: %v\n", val)
|
fmt.Printf("analog value is: %v\n", val)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user