2014-05-03 21:35:09 +05:30
|
|
|
// +build ignore
|
|
|
|
|
2014-05-22 02:19:01 +05:30
|
|
|
// this sample uses the mcp3008 package to interface with the 8-bit ADC and works without code change on bbb and rpi
|
2014-05-03 21:35:09 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2014-05-08 23:28:29 +05:30
|
|
|
"github.com/kidoman/embd"
|
2014-05-22 02:19:01 +05:30
|
|
|
"github.com/kidoman/embd/convertors/mcp3008"
|
|
|
|
_ "github.com/kidoman/embd/host/all"
|
2014-05-03 21:35:09 +05:30
|
|
|
)
|
|
|
|
|
2014-05-22 03:14:33 +05:30
|
|
|
const (
|
|
|
|
channel = 0
|
|
|
|
speed = 1000000
|
|
|
|
bpw = 8
|
|
|
|
delay = 0
|
|
|
|
)
|
|
|
|
|
2014-05-03 21:35:09 +05:30
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2014-05-08 23:28:29 +05:30
|
|
|
fmt.Println("this is a sample code for mcp3008 10bit 8 channel ADC")
|
2014-05-03 21:35:09 +05:30
|
|
|
|
2014-05-08 23:28:29 +05:30
|
|
|
if err := embd.InitSPI(); err != nil {
|
2014-05-03 21:35:09 +05:30
|
|
|
panic(err)
|
|
|
|
}
|
2014-05-08 23:28:29 +05:30
|
|
|
defer embd.CloseSPI()
|
|
|
|
|
2014-05-22 02:19:01 +05:30
|
|
|
spiBus := embd.NewSPIBus(embd.SPIMode0, channel, speed, bpw, delay)
|
2014-05-08 23:28:29 +05:30
|
|
|
defer spiBus.Close()
|
|
|
|
|
|
|
|
adc := mcp3008.New(mcp3008.SingleMode, spiBus)
|
2014-05-03 21:35:09 +05:30
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
val, err := adc.AnalogValueAt(0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-05-08 23:28:29 +05:30
|
|
|
fmt.Printf("analog value is: %v\n", val)
|
2014-05-03 21:35:09 +05:30
|
|
|
}
|
|
|
|
}
|