-
Notifications
You must be signed in to change notification settings - Fork 1
/
poll_mcp3008.py
executable file
·65 lines (53 loc) · 2.55 KB
/
poll_mcp3008.py
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
#!/usr/bin/env python
"""
Demonstrate how to use the MCP3008 class
"""
import spi.mcp3008 as mcp3008
import argparse
import time
def poll_mcp(delay=5.0, voltage=None, resistances=None):
"""
Periodically poll MCP3008 device and report values for each channel. If
reference voltage is supplied, report the measured voltage; if reference
voltage and series resistance is reported, report measured resistance.
"""
if resistances is not None and len(resistances) != 8:
raise Exception("Resistances must be specified for each channel")
dev = mcp3008.MCP3008(clk=18, cs=25, mosi=24, miso=23, verbose=False)
print "%8s Ch#0 Ch#1 Ch#2 Ch#3 Ch#4 Ch#5 Ch#6 Ch#7" % "Time"
while True:
measurements = time.strftime("%H:%M:%S")
### if we know the reference voltage, we can report voltage drop
if voltage is not None and resistances is None:
for channel in range(8):
measurements += " %4.2f" % ( voltage * dev.measure(channel) / 1023.0 )
### if we know reference voltage and series resistances, we can
### calculate resistance corresponding to voltage drop
elif voltage is not None and resistances is not None:
for channel in range(8):
reading = dev.measure(channel)
if reading == 0:
value = 0
else:
value = (resistances[channel] / (1023.0 / reading - 1.0))
measurements += " %4d" % value
### no input voltage means we can only report raw measurements
else:
for channel in range(8):
measurements += " %4d" % dev.measure(channel)
print measurements
time.sleep(delay)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--voltage', '-v', type=float, help="reference voltage")
parser.add_argument('--resistance', '-r', type=str, help="comma-separated list of series resistances (ohms)")
parser.add_argument('--delay', '-d', type=float, default=5.0, help="delay between measurements (Seconds)")
args = parser.parse_args()
### --resistance can be a single value (same resistance for all channels)
### or a comma-separated list of eight resistances (one for each channel)
resistances=None
if args.resistance is not None:
resistances = [ int(x) for x in args.resistance.split(',') ]
if len(resistances) == 1:
resistances = [ resistances[0] ] * 8
poll_mcp(delay=args.delay, voltage=args.voltage, resistances=resistances)