forked from Flipper-XFW/Xtreme-Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infrared.py
executable file
·79 lines (68 loc) · 2.65 KB
/
infrared.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
#!/usr/bin/env python3
from os import path
from flipper.app import App
from flipper.utils.fff import *
class Main(App):
def init(self):
# Subparsers
self.subparsers = self.parser.add_subparsers(help="sub-command help")
self.parser_cleanup = self.subparsers.add_parser(
"cleanup", help="Cleanup duplicate remotes"
)
self.parser_cleanup.add_argument("filename", type=str)
self.parser_cleanup.set_defaults(func=self.cleanup)
def cleanup(self):
f = FlipperFormatFile()
f.load(self.args.filename)
filetype, version = f.getHeader()
if filetype != "IR library file" or version != 1:
self.logger.error(f"Incorrect file type({filetype}) or version({version})")
return 1
data = []
unique = {}
while True:
try:
d = {}
d["name"] = f.readKey("name")
d["type"] = f.readKey("type")
key = None
if d["type"] == "parsed":
d["protocol"] = f.readKey("protocol")
d["address"] = f.readKey("address")
d["command"] = f.readKey("command")
key = f'{d["protocol"]}{d["address"]}{d["command"]}'
elif d["type"] == "raw":
d["frequency"] = f.readKey("frequency")
d["duty_cycle"] = f.readKey("duty_cycle")
d["data"] = f.readKey("data")
key = f'{d["frequency"]}{d["duty_cycle"]}{d["data"]}'
else:
raise Exception(f'Unknown type: {d["type"]}')
if not key in unique:
unique[key] = d
data.append(d)
else:
self.logger.warn(f"Duplicate key: {key}")
except EOFError:
break
# Form new file
f = FlipperFormatFile()
f.setHeader(filetype, version)
for i in data:
f.writeComment(None)
f.writeKey("name", i["name"])
f.writeKey("type", i["type"])
if i["type"] == "parsed":
f.writeKey("protocol", i["protocol"])
f.writeKey("address", i["address"])
f.writeKey("command", i["command"])
elif i["type"] == "raw":
f.writeKey("frequency", i["frequency"])
f.writeKey("duty_cycle", i["duty_cycle"])
f.writeKey("data", i["data"])
else:
raise Exception(f'Unknown type: {i["type"]}')
f.save(self.args.filename)
return 0
if __name__ == "__main__":
Main()()