blob: 27e17570017bd20a3734d09af6270a09046762f3 [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)
Brian Silvermandabdf902017-10-21 15:34:40 -040034 reading_voltage = reading / 4096 * 3.3 / 18.0 * (18.0 + 10.0)
Brian Silverman8d3816a2017-07-03 18:52:15 -070035 #reading_ref = ref / 4096 * 3.3
36 reading_ref = 2.5
Brian Silvermandabdf902017-10-21 15:34:40 -040037 #reading_ref = 0
38 #return (reading_voltage - reading_ref) / 50 / 0.0003
39 return (reading_voltage - reading_ref) / 0.195
Brian Silverman8d3816a2017-07-03 18:52:15 -070040
41with open(sys.argv[1], 'w') as out:
Brian Silvermandabdf902017-10-21 15:34:40 -040042 out.write('balanced0,balanced1,balanced2,current0.0,current1.0,current2.0,current0.1,current1.1,current2.1,count\n')
Brian Silverman8d3816a2017-07-03 18:52:15 -070043 #for point in decoded[2000:7200]:
44 for point in decoded:
45 out.write(','.join(str(d) for d in (
46 current(point[0], point[6]),
Brian Silvermandabdf902017-10-21 15:34:40 -040047 current(point[1], point[6]),
48 current(point[2], point[6]),
49 current(point[3], point[6]),
50 current(point[4], point[6]),
51 current(point[5], point[6]),
52 current(point[6], point[6]),
53 current(point[7], point[6]),
54 current(point[8], point[6]),
55 #point[6] / 100.0,
56 #point[7] / 100.0,
57 #point[8] / 100.0,
Brian Silverman8d3816a2017-07-03 18:52:15 -070058 point[9] / 100.0,
59 point[10] / 100.0,
60 )) + '\n')
61
62print('all done')