-
Notifications
You must be signed in to change notification settings - Fork 4
/
csv2ir.py
executable file
·109 lines (89 loc) · 3.38 KB
/
csv2ir.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
#!/usr/bin/python3
import argparse
import os
import sys
import csv
# Ref: http://www.hifi-remote.com/johnsfine/DecodeIR.html
# IRDB referances over 80 IR-protocols
# some of these mapping are if'y due to inconsistent naming
IR_PROTO_REMAP = {
"Sony12" : "SIRC",
"Sony15" : "SIRC15",
"Sony20" : "SIRC20",
"Tivo unit=0" : "NECext", #hack
"NECx1" : "NECext",
"NECx2" : "NECext",
"NEC-f16" : "NECext",
"NEC2-f16" : "NECext",
"RC5-7F" : "RC5X"
}
# supported by Fipper
# see lib/infrared/encoder_decoder/*/*spec.c
SUPPORTED_IR = [
"NEC", "NECext", "NEC42", "NEC42ext",
"RC5", "RC5X", "RC6",
"Samsung32",
"SIRC", "SIRC15", "SIRC20",
]
def convert(csv_in, ir_out):
with open(csv_in, newline="") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")
next(csv_reader) # skip header
# check ir_protocol
ir_protocol = next(csv_reader)[1]
if ir_protocol in IR_PROTO_REMAP:
ir_protocol = IR_PROTO_REMAP[ir_protocol]
if ir_protocol not in SUPPORTED_IR:
print("file {} used IR Protocal {}: Not Supported, Skipped".format(
csv_in, ir_protocol))
return
csv_file.seek(0)
next(csv_reader) # skip header
with open(ir_out, "w") as ir_file:
ir_file.write("Filetype: IR signals file\n")
ir_file.write("Version: 1\n")
for row in csv_reader:
if not row[0]:
continue
ir_file.write("#\n")
function_name = row[0].replace(" ", "_")
ir_file.write(f"name: {function_name}\n")
ir_file.write("type: parsed\n")
ir_file.write(f"protocol: {ir_protocol}\n")
device_id = f"{int(row[2]):02X}"
subdevice_id = (
"00"
if (row[3] == "-1")
else f"{int(row[3]):02X}"
)
command = f"{int(row[4]):02X}"
ir_file.write(f"address: {device_id} {subdevice_id} 00 00\n")
ir_file.write(f"command: {command} 00 00 00\n")
def main():
parser = argparse.ArgumentParser(description="Convert .csv files to .ir files")
parser.add_argument("input_path", type=str, help="Input file or directory")
parser.add_argument("output_path", type=str, help="Output file or directory")
# parser.add_argument("--protocol", type=str, default="NECext", help="IR protocol")
args = parser.parse_args()
if not os.path.exists(args.input_path):
sys.exit(f"Input path not found: {args.input_path}")
if os.path.isdir(args.input_path):
for directory in os.walk(args.input_path):
output_path = os.path.join(
args.output_path, os.path.relpath(directory[0], args.input_path)
)
try:
os.makedirs(output_path)
except FileExistsError:
pass
for input_file in directory[2]:
if input_file.endswith(".csv"):
output_file = os.path.splitext(input_file)[0] + ".ir"
convert(
os.path.join(directory[0], input_file),
os.path.join(output_path, output_file)
)
else:
convert(args.input_path, args.output_path)
if __name__ == "__main__":
main()