-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsniff_decoder.py
110 lines (90 loc) · 3.02 KB
/
sniff_decoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import re
pulses = list()
pulses_short_list = []
pulses_long_list = []
spaces_short = []
spaces_long = []
spaces_leading = []
filename = 'mycode.txt'
with open(filename, 'r') as f:
file_content = f.read()
lines = file_content.splitlines()
p = re.compile(r'(?P<type>(space)|(pulse)) (?P<num>[0-9]+)')
for line in lines:
m = p.match(line)
if m:
num = int(m.group('num'))
if m.group('type') == 'pulse':
if num > 360 and num < 602:
pulses.append('p')
pulses_short_list.append(num)
elif num > 3400 and num < 3560:
pulses.append('P')
pulses_long_list.append(num)
else:
print("pulse ERROR - %s" % m.group('num'))
if m.group('type') == 'space':
if num > 265 and num < 510:
pulses.append('s')
spaces_short.append(num)
elif num > 1139 and num < 1360:
pulses.append('S')
spaces_long.append(num)
elif num > 1650 and num < 1850:
pulses.append('L')
spaces_leading.append(num)
elif num > 24500:
pulses.append('G')
else:
print("space ERROR - %s" % m.group('num'))
index = 0
output = ''
while len(pulses):
index = index + 1
pulse = pulses.pop(0)
if pulse == 'p':
if len(pulses) == 0:
break
pulse_2 = pulses.pop(0)
if pulse_2 == 's':
output = output + '0'
elif pulse_2 == 'S':
output = output + '1'
if pulse_2 == 'G':
output = output + '\n'
elif pulse == 'P': # leading long pulse + gap
if len(pulses) == 0:
break
pulse_2 = pulses.pop(0)
if pulse_2 == 'L':
pass
else:
raise Exception('Leading pulse-space error')
print('Response in binary')
print(output)
print('Response in binary with delimiters')
for line in output.splitlines():
newline = ''
for i, c in enumerate(line):
newline = newline + c + ';'
print(newline)
output2 = ''
for line in output.splitlines():
newline = str()
num = 0
for i,c in enumerate(line):
if c == '1':
num = num + 2**(i % 8)
if i % 8 == 7:
newline = newline + "%02X" % num
num = 0
if (i % 8 != 7):
newline = newline + "??"
output2 = output2 + newline + '\n'
print('Response in hex')
print(output2)
print("Short pulse length: %d" % int(round(sum(pulses_short_list)/len(pulses_short_list))))
print("Long pulse length: %d" % int(round(sum(pulses_long_list)/len(pulses_long_list))))
print("Short gap length: %d" % int(round(sum(spaces_short)/len(spaces_short))))
print("Long gap length: %d" % int(round(sum(spaces_long)/len(spaces_long))))
print("Leading gap length: %d" % int(round(sum(spaces_leading)/len(spaces_leading))))