2014-05-07 19:53:29 +02:00
|
|
|
// +build ignore
|
|
|
|
|
2014-05-03 18:05:09 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kidoman/embd"
|
|
|
|
_ "github.com/kidoman/embd/host/all"
|
|
|
|
)
|
|
|
|
|
2014-05-21 23:44:33 +02:00
|
|
|
const (
|
|
|
|
channel = 0
|
|
|
|
speed = 1000000
|
|
|
|
bpw = 8
|
|
|
|
delay = 0
|
|
|
|
)
|
|
|
|
|
2014-05-03 18:05:09 +02:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
if err := embd.InitSPI(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-05-21 21:41:45 +02:00
|
|
|
defer embd.CloseSPI()
|
2014-05-03 18:05:09 +02:00
|
|
|
|
2014-05-21 22:49:01 +02:00
|
|
|
bus := embd.NewSPIBus(embd.SPIMode0, channel, speed, bpw, delay)
|
2014-05-21 21:41:45 +02:00
|
|
|
defer bus.Close()
|
2014-05-03 18:05:09 +02:00
|
|
|
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
time.Sleep(1 * time.Second)
|
2014-05-21 22:49:01 +02:00
|
|
|
val, err := getSensorValue(bus)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-05-03 18:05:09 +02:00
|
|
|
fmt.Printf("value is: %v\n", val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSensorValue(bus embd.SPIBus) (uint16, error) {
|
2014-05-21 23:01:42 +02:00
|
|
|
data := [3]uint8{1, 128, 0}
|
|
|
|
|
2014-05-03 18:05:09 +02:00
|
|
|
var err error
|
2014-05-21 23:01:42 +02:00
|
|
|
err = bus.TransferAndRecieveData(data[:])
|
2014-05-03 18:05:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return uint16(0), err
|
|
|
|
}
|
|
|
|
return uint16(data[1]&0x03)<<8 | uint16(data[2]), nil
|
|
|
|
}
|