-
Notifications
You must be signed in to change notification settings - Fork 193
/
mcp3008.go
93 lines (77 loc) · 1.87 KB
/
mcp3008.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Package mcp3008 implements a driver for the MCP3008 Analog to Digital Converter.
//
// Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21295d.pdf
package mcp3008 // import "tinygo.org/x/drivers/mcp3008"
import (
"errors"
"machine"
"tinygo.org/x/drivers"
)
// Device wraps MCP3008 SPI ADC.
type Device struct {
bus drivers.SPI
cs machine.Pin
tx []byte
rx []byte
CH0 ADCPin
CH1 ADCPin
CH2 ADCPin
CH3 ADCPin
CH4 ADCPin
CH5 ADCPin
CH6 ADCPin
CH7 ADCPin
}
// ADCPin is the implementation of the ADConverter interface.
type ADCPin struct {
machine.Pin
d *Device
}
// New returns a new MCP3008 driver. Pass in a fully configured SPI bus.
func New(b drivers.SPI, csPin machine.Pin) *Device {
d := &Device{bus: b,
cs: csPin,
tx: make([]byte, 3),
rx: make([]byte, 3),
}
// setup all channels
d.CH0 = d.GetADC(0)
d.CH1 = d.GetADC(1)
d.CH2 = d.GetADC(2)
d.CH3 = d.GetADC(3)
d.CH4 = d.GetADC(4)
d.CH5 = d.GetADC(5)
d.CH6 = d.GetADC(6)
d.CH7 = d.GetADC(7)
return d
}
// Configure sets up the device for communication
func (d *Device) Configure() {
d.cs.Configure(machine.PinConfig{Mode: machine.PinOutput})
}
// Read analog data from channel
func (d *Device) Read(ch int) (uint16, error) {
if ch < 0 || ch > 7 {
return 0, errors.New("invalid channel for MCP3008 Read")
}
return d.GetADC(ch).Get(), nil
}
// GetADC returns an ADC for a specific channel.
func (d *Device) GetADC(ch int) ADCPin {
return ADCPin{machine.Pin(ch), d}
}
// Get the current reading for a specific ADCPin.
func (p ADCPin) Get() uint16 {
p.d.tx[0] = 0x01
p.d.tx[1] = byte(8+p.Pin) << 4
p.d.tx[2] = 0x00
p.d.cs.Low()
p.d.bus.Tx(p.d.tx, p.d.rx)
// scale result to 16bit value like other ADCs
result := uint16((p.d.rx[1]&0x3))<<(8+6) + uint16(p.d.rx[2])<<6
p.d.cs.High()
return result
}
// Configure here just for interface compatibility.
func (p ADCPin) Configure() {
}