Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame^] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | # Pipe the binary data in and give the CSV output filename as an argument. |
| 4 | |
| 5 | import struct |
| 6 | import sys |
| 7 | import select |
| 8 | |
| 9 | DatapointStruct = struct.Struct('<11h') |
| 10 | DATAPOINTS = 5000 |
| 11 | TOTAL_SIZE = DatapointStruct.size * DATAPOINTS |
| 12 | |
| 13 | data = bytes() |
| 14 | while 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 |
| 20 | print('%s' % len(data)) |
| 21 | |
| 22 | readable, _, _ = select.select([sys.stdin.buffer], [], [], 1) |
| 23 | if readable: |
| 24 | print('Extra bytes', file=sys.stderr) |
| 25 | sys.exit(1) |
| 26 | |
| 27 | decoded = [] |
| 28 | for i in range(DATAPOINTS): |
| 29 | datapoint = DatapointStruct.unpack_from(data, i * DatapointStruct.size) |
| 30 | decoded.append(datapoint) |
| 31 | |
| 32 | def 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 | |
| 39 | with 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 | |
| 60 | print('all done') |