blob: 5d44694a1942575f93a06be4cca1c8a253b0f2ad [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:
Ravago Jones5127ccc2022-07-31 16:32:45 -070015 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
Brian Silverman8d3816a2017-07-03 18:52:15 -070020print('%s' % len(data))
21
22readable, _, _ = select.select([sys.stdin.buffer], [], [], 1)
23if readable:
Ravago Jones5127ccc2022-07-31 16:32:45 -070024 print('Extra bytes', file=sys.stderr)
25 sys.exit(1)
Brian Silverman8d3816a2017-07-03 18:52:15 -070026
27decoded = []
28for i in range(DATAPOINTS):
Ravago Jones5127ccc2022-07-31 16:32:45 -070029 datapoint = DatapointStruct.unpack_from(data, i * DatapointStruct.size)
30 decoded.append(datapoint)
31
Brian Silverman8d3816a2017-07-03 18:52:15 -070032
33def current(reading, ref):
Ravago Jones5127ccc2022-07-31 16:32:45 -070034 reading_voltage = reading / 4096 * 3.3 / 1.47 * (0.768 + 1.47)
35 reading_voltage = reading / 4096 * 3.3 / 18.0 * (18.0 + 10.0)
36 #reading_ref = ref / 4096 * 3.3
37 reading_ref = 2.5
38 #reading_ref = 0
39 #return (reading_voltage - reading_ref) / 50 / 0.0003
40 return (reading_voltage - reading_ref) / 0.195
41
Brian Silverman8d3816a2017-07-03 18:52:15 -070042
43with open(sys.argv[1], 'w') as out:
Ravago Jones5127ccc2022-07-31 16:32:45 -070044 out.write(
45 'balanced0,balanced1,balanced2,current0.0,current1.0,current2.0,current0.1,current1.1,current2.1,count\n'
46 )
47 #for point in decoded[2000:7200]:
48 for point in decoded:
49 out.write(','.join(
50 str(d) for d in (
51 current(point[0], point[6]),
52 current(point[1], point[6]),
53 current(point[2], point[6]),
54 current(point[3], point[6]),
55 current(point[4], point[6]),
56 current(point[5], point[6]),
57 current(point[6], point[6]),
58 current(point[7], point[6]),
59 current(point[8], point[6]),
60 #point[6] / 100.0,
61 #point[7] / 100.0,
62 #point[8] / 100.0,
63 point[9] / 100.0,
64 point[10] / 100.0,
65 )) + '\n')
Brian Silverman8d3816a2017-07-03 18:52:15 -070066
67print('all done')