forked from jdevelop/golang-rpi-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.go
47 lines (40 loc) · 856 Bytes
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package mcp3008
import (
"fmt"
"golang.org/x/exp/io/spi"
)
type Mode int
const (
Mode0 = Mode(0)
Mode1 = Mode(1)
Mode2 = Mode(2)
Mode3 = Mode(3)
)
type MCP3008 struct {
bus int
channel int
dev *spi.Device
}
type MeasureAnalog interface {
Measure(channel int) (result int, err error)
}
func NewMCP3008(bus int, busChannel int, mode Mode, maxSpeed int64) (mcp MCP3008, resErr error) {
var dev, err = spi.Open(&spi.Devfs{
Dev: fmt.Sprintf("/dev/spidev%d.%d", bus, busChannel),
Mode: spi.Mode(mode),
MaxSpeed: maxSpeed,
})
if err != nil {
resErr = err
return
}
mcp.dev = dev
return
}
func (r MCP3008) Measure(channel int) (result int, err error) {
resp := make([]byte, 3)
if err := r.dev.Tx([]byte{1, byte((8 + channel) << 4), 0}, resp); err == nil {
result = int(resp[1]&3)<<8 + int(resp[2])
}
return
}