|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import struct |
| 3 | +import sys |
| 4 | +import wave |
| 5 | + |
| 6 | + |
| 7 | +FILENAME = sys.argv[1] |
| 8 | + |
| 9 | +TRI_STATE_SEQUENCE_LENGTH = 12 |
| 10 | +IS_SYNC_BIT_LOW = lambda inter: inter[0] == 0 and 450 < inter[1] < 500 |
| 11 | +IS_LOW = lambda inter: inter[0] == 0 and 15 < inter[1] < 30 |
| 12 | +IS_HIGH = lambda inter: inter[0] == 1 and 15 < inter[1] < 30 |
| 13 | +IS_3LOW = lambda inter: inter[0] == 0 and 45 < inter[1] < 55 |
| 14 | +IS_3HIGH = lambda inter: inter[0] == 1 and 45 < inter[1] < 55 |
| 15 | + |
| 16 | + |
| 17 | +def read_pulses_sequence(intervals, index, pulses, bit): |
| 18 | + if all(is_pulse(inter) for is_pulse, inter in |
| 19 | + zip(pulses, intervals[index:index + len(pulses)])): |
| 20 | + return bit, index + len(pulses) |
| 21 | + return None, None |
| 22 | + |
| 23 | + |
| 24 | +def read_tri_state_0(intervals, index): |
| 25 | + pulses = [IS_HIGH, IS_3LOW, IS_HIGH, IS_3LOW] |
| 26 | + return read_pulses_sequence(intervals, index, pulses, '0') |
| 27 | + |
| 28 | + |
| 29 | +def read_tri_state_1(intervals, index): |
| 30 | + pulses = [IS_3HIGH, IS_LOW, IS_3HIGH, IS_LOW] |
| 31 | + return read_pulses_sequence(intervals, index, pulses, '1') |
| 32 | + |
| 33 | + |
| 34 | +def read_tri_state_F(intervals, index): |
| 35 | + pulses = [IS_HIGH, IS_3LOW, IS_3HIGH, IS_LOW] |
| 36 | + return read_pulses_sequence(intervals, index, pulses, 'F') |
| 37 | + |
| 38 | + |
| 39 | +def read_tri_state(intervals, index): |
| 40 | + read_funcs = [read_tri_state_0, read_tri_state_1, read_tri_state_F] |
| 41 | + for read_func in read_funcs: |
| 42 | + bit, new_index = read_func(intervals, index) |
| 43 | + if bit: |
| 44 | + return bit, new_index |
| 45 | + return None, None |
| 46 | + |
| 47 | + |
| 48 | +def read_tri_state_sequence(intervals, index): |
| 49 | + sequence = [] |
| 50 | + for i in range(TRI_STATE_SEQUENCE_LENGTH): |
| 51 | + tri_state, index = read_tri_state(intervals, index) |
| 52 | + if not tri_state: |
| 53 | + return None, None |
| 54 | + sequence.append(tri_state) |
| 55 | + return ''.join(sequence), index |
| 56 | + |
| 57 | + |
| 58 | +def read_sync_bit(intervals, index): |
| 59 | + if IS_HIGH(intervals[index]) and IS_SYNC_BIT_LOW(intervals[index + 1]): |
| 60 | + return True, index + 2 |
| 61 | + return None, None |
| 62 | + |
| 63 | + |
| 64 | +wf = wave.open(FILENAME) |
| 65 | + |
| 66 | +signal_intervals = [] |
| 67 | +signal = 0 |
| 68 | +signal_length = 0 |
| 69 | +for i in range(wf.getnframes()): |
| 70 | + frame_data = wf.readframes(1) |
| 71 | + frame = struct.unpack("<h", frame_data)[0] |
| 72 | + new_signal = 1 if frame > 0 else 0 |
| 73 | + if new_signal == signal: |
| 74 | + signal_length += 1 |
| 75 | + else: |
| 76 | + signal_intervals.append((signal, signal_length)) |
| 77 | + signal = new_signal |
| 78 | + signal_length = 1 |
| 79 | + |
| 80 | + |
| 81 | +tri_state_sequences = [] |
| 82 | +index = 0 |
| 83 | +while index < len(signal_intervals) - 1: |
| 84 | + is_sync_bit, new_index = read_sync_bit(signal_intervals, index) |
| 85 | + if not is_sync_bit: |
| 86 | + index += 1 |
| 87 | + continue |
| 88 | + index = new_index |
| 89 | + #print("# Sync bit:", index) |
| 90 | + |
| 91 | + tri_state_seq, new_index = read_tri_state_sequence(signal_intervals, index) |
| 92 | + if tri_state_seq: |
| 93 | + tri_state_sequences.append(tri_state_seq) |
| 94 | + index = new_index |
| 95 | + #print("# Read tri-state:", tri_state_seq) |
| 96 | + if tri_state_seq.startswith('FFFFF'): |
| 97 | + sys.stdout.write(chr(int(tri_state_seq[5:], 2))) |
0 commit comments