-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode.py
261 lines (227 loc) · 8.87 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env python3
import fcntl
import array
import os
import struct
import select
import threading
import subprocess
import logging
logger = logging.getLogger()
PERCENT = 0.35
BITS = 0
ZERO_PULSE = -1
ZERO_SPACE = -1
ONE_PULSE = -1
ONE_SPACE = -1
PRE_DATA = 0x33B8
PRE_DATA_BITS = 0
HEADER_PULSE = -1
HEADER_SPACE = -1
bitTotal = 0
header_pulse_upper = 0
header_pulse_lower = 0
header_space_upper = 0
header_space_lower = 0
zero_pulse_upper = 0
zero_pulse_lower = 0
zero_space_upper = 0
zero_space_lower = 0
one_pulse_upper = 0
one_pulse_lower = 0
one_space_upper = 0
one_space_lower = 0
#get all *.conf files
def getConfFiles():
allConfFiles = []
files = os.listdir("/etc/lirc")
for file in files:
if file.endswith('.conf'):
allConfFiles.append(file)
return allConfFiles
#format file contents
def formatConfFiles(file):
contents = []
temp = []
for currentLine in file:
if currentLine.startswith("#"):
continue
else:
temp.append("".join(currentLine.strip()))
file.close()
for line in temp:
new_format = line.replace('\t', ' ')
contents.append(' '.join(new_format.split()))
while '' in contents:
contents.remove('')
contents = [x.split(" ") for x in contents]
return contents
#parse the config files
def parseConfFile(allDevices, contents):
index1 = -1
index2 = -1
name = ""
pre_data = ""
for i in range(len(contents)):
for j in range(len(contents[i])):
if contents[i][j] == 'begin' and contents[i][j+1] == 'remote':
index1 = i
if contents[i][j] == 'name':
name = contents[i][j+1]
j=j+1
if contents[i][j] == 'pre_data':
pre_data = contents[i][j+1][2:]
j=j+1
if contents[i][j] == 'begin' and contents[i][j+1] == 'codes':
for k in range(i+1,len(contents)):
if contents[k][0] == 'end' and contents[k][1] == 'codes':
i = k+1
break
tempVal = contents[k][1][2:]
contents[k][1] = pre_data + tempVal
if contents[i][j] == 'end' and contents[i][j+1] == 'remote':
index2 = i
allDevices[name] = contents[index1:index2+1]
break
return allDevices
#READ ALL .conf FILES IN /etc/lirc dir
def readConf():
allDevices = {}
allConfFiles = []
contents = []
allConfFiles = getConfFiles()
for filename in allConfFiles:
with open("/etc/lirc/"+filename, "r") as file:
contents = formatConfFiles(file)
allDevices = parseConfFile(allDevices, contents)
del contents[:]
return allDevices
#gather all header information
def getHeaderInfo(file, allDevices):
headerInfo = {
"bits":"",
"zero_pulse":"",
"zero_space":"",
"one_pulse":"",
"one_space":"",
"pre_data":"",
"pre_data_bits":"",
"header_pulse":"",
"header_space":""
}
if file in allDevices:
for key,value in enumerate(allDevices[file]):
for x in value:
if x == 'bits':
headerInfo["bits"] = allDevices[file][key][1]
elif x == 'zero':
headerInfo["zero_pulse"] = allDevices[file][key][1]
headerInfo["zero_space"] = allDevices[file][key][2]
elif x == 'one':
headerInfo["one_pulse"] = allDevices[file][key][1]
headerInfo["one_space"] = allDevices[file][key][2]
elif x == 'pre_data':
headerInfo["pre_data"] = allDevices[file][key][1][2:].upper()
elif x == 'pre_data_bits':
headerInfo["pre_data_bits"] = allDevices[file][key][1]
elif x == 'header':
headerInfo["header_pulse"] = allDevices[file][key][1]
headerInfo["header_space"] = allDevices[file][key][2]
return headerInfo
#set all global variables from header information
def setHeaderInfo(headerInfo):
global header_pulse_upper,header_pulse_lower,header_space_upper,header_space_lower,zero_pulse_upper,zero_pulse_lower,zero_space_upper,zero_space_lower,one_pulse_upper,one_pulse_lower,one_space_upper, one_space_lower, bitTotal
global ZERO_PULSE, ZERO_SPACE, ONE_PULSE, ONE_SPACE, PRE_DATA, PRE_DATA_BITS, BITS
#setting variables from header info
if len(headerInfo) > 0:
ZERO_PULSE = headerInfo["zero_pulse"]
ZERO_SPACE = headerInfo["zero_space"]
ONE_PULSE = headerInfo["one_pulse"]
ONE_SPACE = headerInfo["one_space"]
PRE_DATA = headerInfo["pre_data"]
if(headerInfo["bits"] != ""):
BITS = headerInfo["bits"]
else:
BITS = 0
if(headerInfo["pre_data_bits"] != ""):
PRE_DATA_BITS = headerInfo["pre_data_bits"]
else:
PRE_DATA_BITS = 0
HEADER_PULSE = headerInfo["header_pulse"]
HEADER_SPACE = headerInfo["header_space"]
else:
return -1
bitTotal = int(PRE_DATA_BITS) + int(BITS)
header_pulse_upper = float(HEADER_PULSE) + (float(HEADER_PULSE)*float(PERCENT))
header_pulse_lower = float(HEADER_PULSE) - (float(HEADER_PULSE)*float(PERCENT))
header_space_upper = float(HEADER_SPACE) + (float(HEADER_SPACE)*float(PERCENT))
header_space_lower = float(HEADER_SPACE) - (float(HEADER_SPACE)*float(PERCENT))
zero_pulse_upper = float(ZERO_PULSE) + (float(ZERO_PULSE)*float(PERCENT))
zero_pulse_lower = float(ZERO_PULSE) - (float(ZERO_PULSE)*float(PERCENT))
zero_space_upper = float(ZERO_SPACE) + (float(ZERO_SPACE)*float(PERCENT))
zero_space_lower = float(ZERO_SPACE) - (float(ZERO_SPACE)*float(PERCENT))
one_pulse_upper = float(ONE_PULSE) + (float(ONE_PULSE)*float(PERCENT))
one_pulse_lower = float(ONE_PULSE) - (float(ONE_PULSE)*float(PERCENT))
one_space_upper = float(ONE_SPACE) + (float(ONE_SPACE)*float(PERCENT))
one_space_lower = float(ONE_SPACE) - (float(ONE_SPACE)*float(PERCENT))
return 1
#functions to determine whether value lies between our threshold
def isWithinHeaderPulseRange(value):
return (float(value) >= header_pulse_lower and float(value) <= header_pulse_upper)
def isWithinHeaderSpaceRange(value):
return (float(value) >= header_space_lower and float(value) <= header_space_upper)
def isWithinOnePulseRange(value):
return (float(value) >= one_pulse_lower and float(value) <= one_pulse_upper)
def isWithinOneSpaceRange(value):
return (float(value) >= one_space_lower and float(value) <= one_space_upper)
def isWithinZeroPulseRange(value):
return (float(value) >= zero_pulse_lower and float(value) <= zero_pulse_upper)
def isWithinZeroSpaceRange(value):
return (float(value) >= zero_space_lower and float(value) <= zero_space_upper)
def decode(val, file, allDevices):
decoded_val = ""
headerInfo = {}
final = ""
count = 0
headerInfo = getHeaderInfo(file, allDevices)
if setHeaderInfo(headerInfo) != -1:
for i in range(len(val)):
#find header information from val data
if (i+1 < len(val)) and (isWithinHeaderPulseRange(float(val[i][1])) or isWithinHeaderSpaceRange(float(val[i+1][1]))):
logger.debug( "HEADER found at i = " + str(i) + " with value = " + str(val[i][1]) )
decoded_val = ""
#start reading after header is found, increment +2 due to space and pulse pair
for k in range(i+2,len(val),2):
logger.debug( "i=" + str(i) + " k=" + str(k) + " len=" + str(len(val)))
logger.debug("val[k][1] (PULSE-ZERO) = " + str(val[k][1]) + " " + str( zero_pulse_lower ) + " " + str( zero_pulse_upper ))
logger.debug("val[k+1][1] (SPACE-ZERO) = " + str(val[k+1][1]) + " " + str( zero_space_lower ) + " " + str( zero_space_upper ))
logger.debug("val[k][1] (PULSE-ONE) = " + str(val[k][1]) + " " + str( one_pulse_lower ) + " " + str( one_pulse_upper ))
logger.debug("val[k+1][1] (SPACE-ONE) = " + str(val[k+1][1]) + " " + str( one_space_lower ) + " " + str( one_space_upper ))
if isWithinZeroPulseRange(float(val[k][1])):
if isWithinZeroSpaceRange(float(val[k+1][1])):
decoded_val += "0"
count +=1
# assume that if the PULSE and SPACE equate to a "0" that they will NOT also equate to a "1"
if isWithinOnePulseRange(float(val[k][1])):
if isWithinOneSpaceRange(float(val[k+1][1])):
decoded_val += "1"
count +=1
logger.debug("decode_val = " + decoded_val)
# every 4 binary bits we get the hex value
if count % 4 == 0 and count != 0 :
try:
decimal = int(decoded_val,2)
except:
return "decoded_val contains an empty char \"\""
final += str(hex(decimal)[2:])
decoded_val = ""
logger.debug("count= " + str(count) + " bitTotal=" + str(bitTotal))
logger.debug("final= " + final.upper())
if count == bitTotal:
for key, value in enumerate(allDevices[file]):
for x in value:
if x.endswith(final.upper()):
return allDevices[file][key][0]
return "error"
logger.debug("Error: bit count does not equal total bits or config file error")
return