From 1224148572d12d49b1cdca040c885c87c5853754 Mon Sep 17 00:00:00 2001 From: npotts Date: Sun, 10 Jan 2016 00:02:42 -0700 Subject: [PATCH] Added example to use 12 bit veriant Signed-off-by: npotts --- samples/mcp3208.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 samples/mcp3208.go diff --git a/samples/mcp3208.go b/samples/mcp3208.go new file mode 100644 index 0000000..497e56e --- /dev/null +++ b/samples/mcp3208.go @@ -0,0 +1,48 @@ +// +build ignore + +// this sample uses the mcp3008 package to interface with a similar MCP3208, which is a 12 bit variant. Works without code change on bbb and rpi +package main + +import ( + "flag" + "fmt" + "time" + + "github.com/kidoman/embd" + // "github.com/kidoman/embd/convertors/mcp3008" + _ "github.com/kidoman/embd/host/all" + "github.com/npotts/embd/convertors/mcp3008" +) + +const ( + channel = 0 + speed = 1000000 + bpw = 8 + delay = 0 +) + +func main() { + flag.Parse() + fmt.Println("this is a sample code for mcp3008 10bit 8 channel ADC") + + if err := embd.InitSPI(); err != nil { + panic(err) + } + defer embd.CloseSPI() + + spiBus := embd.NewSPIBus(embd.SPIMode0, channel, speed, bpw, delay) + defer spiBus.Close() + + adc := &mcp3008.MCP3008{Mode: mcp3008.SingleMode, Bus: spiBus, Bits: mcp3008.Bits12} + + // adc := mcp3008.New(mcp3008.SingleMode, spiBus) + + for i := 0; i < 20; i++ { + time.Sleep(1 * time.Second) + val, err := adc.AnalogValueAt(0) + if err != nil { + panic(err) + } + fmt.Printf("analog value is: %v\n", val) + } +}