-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.py
104 lines (84 loc) · 2.93 KB
/
decode.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
import json
import argparse
# 入力された赤外線LEDの点灯・消灯時間を1周期の時間で割って、正規化する
def normalize(code, T=425):
normalized_code = []
frame = []
for i in range(0, len(code)):
period = round(code[i] / T)
# Trailer(Frame終端)の場合
if period > 8:
normalized_code.append(frame)
frame = []
else:
frame.append(period)
normalized_code.append(frame)
return normalized_code
# AEHAフォーマットに従って、信号をdecodeする
def decode(normalized_code):
decoded_code = []
data_frame = ""
for frame in normalized_code:
for i in range(0, len(frame) - 1, 2):
# Leader
if frame[i] == 8 and frame[i + 1] == 4:
continue
# 0 (Data bit)
elif frame[i] == 1 and frame[i + 1] == 1:
data_frame += "0"
# 1 (Data bit)
elif frame[i] == 1 and frame[i + 1] == 3:
data_frame += "1"
else:
raise ("Unabled to decode.")
decoded_code.append(data_frame)
data_frame = ""
assert all(
df == decoded_code[0] for df in decoded_code
), "Each data frame is not identical."
return decoded_code[0], len(decoded_code)
def parse_decoded_code(decoded_code):
customer_code1 = int(decoded_code[7::-1], 2)
customer_code2 = int(decoded_code[15:7:-1], 2)
parity_code = int(decoded_code[19:15:-1], 2)
data0 = int(decoded_code[23:19:-1], 2)
decoded_code = decoded_code[24:]
data = []
while len(decoded_code) > 0:
data.append(int(decoded_code[7::-1], 2))
decoded_code = decoded_code[8:]
data.insert(0, data0)
frame = {
"customer_code1": customer_code1,
"customer_code2": customer_code2,
"parity_code": parity_code,
"data": data,
}
return frame
def parse_decoded_code_as_hex(decoded_code):
hex_code = ""
while len(decoded_code) > 0:
hex_code += "{:02x}".format(int(decoded_code[7::-1], 2))
decoded_code = decoded_code[8:]
return hex_code
def analyze_ir_signal(code) -> None:
normalized_code = normalize(code)
decoded_code = decode(normalized_code)[0]
return parse_decoded_code(decoded_code)
def main() -> None:
# 引数をパース フォーマットはirrp.pyと同じ
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("-f", "--file", help="Filename", required=True)
arg_parser.add_argument("id", nargs="+", type=str, help="IR codes")
args = arg_parser.parse_args()
with open(args.file, "r") as f:
records = json.load(f)
# 各引数の表す信号に対してdecodeする
for arg in args.id:
if arg in records:
code = records[arg]
analyze_ir_signal(code)
else:
raise (f"Can't find '{arg}' in '{f}'.")
if __name__ == "__main__":
main()