blob: 1010e8877f16c117b017112a2ac073c8468da907 [file] [log] [blame]
Brian Silverman8d3816a2017-07-03 18:52:15 -07001#!/usr/bin/python3
2
3# Pipe the binary data in and give the CSV output filename as an argument.
4
5import struct
6import sys
7import select
8
9DatapointStruct = struct.Struct('<11h')
10DATAPOINTS = 5000
11TOTAL_SIZE = DatapointStruct.size * DATAPOINTS
12
13data = bytes()
14while len(data) < TOTAL_SIZE:
15 read_now = sys.stdin.buffer.read(TOTAL_SIZE - len(data))
16 if not read_now:
17 print('EOF before data finished', file=sys.stderr)
18 sys.exit(1)
19 data += read_now
20print('%s' % len(data))
21
22readable, _, _ = select.select([sys.stdin.buffer], [], [], 1)
23if readable:
24 print('Extra bytes', file=sys.stderr)
25 sys.exit(1)
26
27decoded = []
28for i in range(DATAPOINTS):
29 datapoint = DatapointStruct.unpack_from(data, i * DatapointStruct.size)
30 decoded.append(datapoint)
31
32def current(reading, ref):
33 reading_voltage = reading / 4096 * 3.3 / 1.47 * (0.768 + 1.47)
34 #reading_ref = ref / 4096 * 3.3
35 reading_ref = 2.5
36 reading_ref = 0
37 return (reading_voltage - reading_ref) / 50 / 0.0003
38
39with open(sys.argv[1], 'w') as out:
40 out.write('current0.0,current1.0,current2.0,current0.1,current1.1,current2.1,count\n')
41 #for point in decoded[2000:7200]:
42 for point in decoded:
43 out.write(','.join(str(d) for d in (
44 current(point[0], point[6]),
45 current(point[1], point[6]),
46 current(point[2], point[6]),
47 #current(point[3], point[6]),
48 #current(point[4], point[6]),
49 #current(point[5], point[6]),
50 point[3] / 100.0,
51 point[4] / 100.0,
52 point[5] / 100.0,
53 point[6] / 100.0,
54 point[7] / 100.0,
55 point[8] / 100.0,
56 point[9] / 100.0,
57 point[10] / 100.0,
58 )) + '\n')
59
60print('all done')