-
Notifications
You must be signed in to change notification settings - Fork 11
/
IRModule.py
284 lines (206 loc) · 7.86 KB
/
IRModule.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python3
"""IRModuleV2, module to use with IR sensor
created Apr 27, 2018
modified - Apr 30, 2018
modified Apr 1, 2020 - added repeat code functionality"""
"""
Copyright 2018, 2019, 2020 Owain Martin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import RPi.GPIO as GPIO
import time, threading
class IRRemote:
def __init__(self, callback = None):
self.decoding = False
self.pList = []
self.timer = time.time()
if callback == 'DECODE':
self.callback = self.print_ir_code
else:
self.callback = callback
self.checkTime = 150 # time in milliseconds
self.verbose = False
self.repeatCodeOn = True
self.lastIRCode = 0
self.maxPulseListLength = 70
def pWidth(self, pin):
"""pWidth, function to record the width of the highs and lows
of the IR remote signal and start the function to look for the
end of the IR remote signal"""
self.pList.append(time.time()-self.timer)
self.timer = time.time()
if self.decoding == False:
self.decoding = True
check_loop = threading.Thread(name='self.pulse_checker',target=self.pulse_checker)
check_loop.start()
return
def pulse_checker(self):
"""pulse_checker, function to look for the end of the IR remote
signal and activate the signal decode function followed by
the callback function.
End of signal is determined by 1 of 2 ways
1 - if the length of the pulse list is larger than self.maxPulseListLength
- used for initial button press codes
2 - if the length of time receiving the pulse is great than self.checkTime
- used for repeat codes"""
timer = time.time()
while True:
check = (time.time()-timer)*1000
if check > self.checkTime:
print(check, len(self.pList))
break
if len(self.pList) > self.maxPulseListLength:
print(check, len(self.pList))
break
time.sleep(0.001)
if len(self.pList) > self.maxPulseListLength:
decode = self.decode_pulse(self.pList)
self.lastIRCode = decode
# if the length of self.pList is less than 10
# assume repeat code found
elif len(self.pList) < 10:
if self.repeatCodeOn == True:
decode = self.lastIRCode
else:
decode = 0
self.lastIRCode = decode
else:
decode = 0
self.lastIRCode = decode
self.pList = []
self.decoding = False
if self.callback != None:
self.callback(decode)
return
def decode_pulse(self,pList):
"""decode_pulse, function to decode the high and low
timespans captured by the pWidth function into a binary
number"""
bitList = []
sIndex = -1
# convert the timespans in seconds to milli-seconds
# look for the start of the IR remote signal
for p in range(0,len(pList)):
try:
pList[p]=float(pList[p])*1000
if self.verbose == True:
print(pList[p])
if pList[p]<11:
if sIndex == -1:
sIndex = p
except:
pass
# if no acceptable start is found return -1
if sIndex == -1:
return -1
if sIndex+1 >= len(pList):
return -1
#print(sIndex, pList[sIndex], pList[sIndex+1])
if (pList[sIndex]<4 or pList[sIndex]>11):
return -1
if (pList[sIndex+1]<2 or pList[sIndex+1]>6):
return -1
""" pulses are made up of 2 parts, a fixed length low (approx 0.5-0.6ms)
and a variable length high. The length of the high determines whether or
not a 0,1 or control pulse/bit is being sent. Highes of length approx 0.5-0.6ms
indicate a 0, and length of approx 1.6-1.7 ms indicate a 1"""
for i in range(sIndex+2,len(pList),2):
if i+1 < len(pList):
if pList[i+1]< 0.9:
bitList.append(0)
elif pList[i+1]< 2.5:
bitList.append(1)
elif (pList[i+1]> 2.5 and pList[i+1]< 45):
#print('end of data found')
break
else:
break
if self.verbose == True:
print(bitList)
# convert the list of 1s and 0s into a
# binary number
pulse = 0
bitShift = 0
for b in bitList:
pulse = (pulse<<bitShift) + b
bitShift = 1
return pulse
def set_callback(self, callback = None):
"""set_callback, function to allow the user to set
or change the callback function used at any time"""
self.callback = callback
return
def remove_callback(self):
"""remove_callback, function to allow the user to remove
the callback function used at any time"""
self.callback = None
return
def print_ir_code(self, code):
"""print_ir_code, function to display IR code received"""
print(hex(code))
return
def set_verbose(self, verbose = True):
"""set_verbose, function to turn verbose mode
on or off. Used to print out pulse width list
and bit list"""
self.verbose = verbose
return
def set_repeat(self, repeat = True):
"""set_repeat, function to enable and disable
the IR repeat code functionality"""
self.repeatCodeOn = repeat
return
if __name__ == "__main__":
def remote_callback(code):
# Codes listed below are for the
# Sparkfun 9 button remote
#print(hex(code))
if code == 0x10EFD827:
print("Power")
elif code == 0x10EFF807:
print('A')
elif code == 0x10EF7887:
print('B')
elif code == 0x10EF58A7:
print('C')
elif code == 0x10EFA05F:
print('Up Arrow')
elif code == 0x10EF00FF:
print('Down Arrow')
elif code == 0x10EF10EF:
print('Left Arrow')
elif code == 0x10EF807F:
print('Right Arrow')
elif code == 0x10EF20DF:
print('Select')
else:
print('.') # unknown code
return
ir = IRRemote('DECODE')
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # uses numbering outside circles
GPIO.setup(16,GPIO.IN) # set pin 16 to input
GPIO.add_event_detect(16,GPIO.BOTH,callback=ir.pWidth)
ir.set_verbose()
print('Starting IR remote sensing using DECODE function')
time.sleep(5)
print('Setting up callback')
ir.set_verbose(False)
ir.set_callback(remote_callback)
ir.set_repeat(True)
try:
while True:
time.sleep(1)
except:
print('Removing callback and cleaning up GPIO')
ir.remove_callback()
GPIO.cleanup(16)