-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathIRModuleExample.py
93 lines (75 loc) · 2.71 KB
/
IRModuleExample.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
#!/usr/bin/env python3
"""IRModuleExample1, program to practice using the IRModule
Created Apr 30, 2018"""
"""
Copyright 2018 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 IRModule
import time
def remote_callback(code):
# Codes listed below are for the
# Sparkfun 9 button remote
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
# set up IR pi pin and IR remote object
irPin = 16
ir = IRModule.IRRemote(callback='DECODE')
# using 'DECODE' option for callback will print out
# the IR code received in hexadecimal
# this can used to get the codes for whichever NEC
# compatable remote you are using
# set up GPIO options and set callback function required
# by the IR remote module (ir.pWidth)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # uses numbering outside circles
GPIO.setup(irPin,GPIO.IN) # set irPin to input
GPIO.add_event_detect(irPin,GPIO.BOTH,callback=ir.pWidth)
ir.set_verbose() # verbose option prints outs high and low width durations (ms)
print('Starting IR remote sensing using DECODE function and verbose setting equal True ')
print('Use ctrl-c to exit program')
try:
time.sleep(5)
# turn off verbose option and change callback function
# to the function created above - remote_callback()
print('Turning off verbose setting and setting up callback')
ir.set_verbose(False)
ir.set_callback(remote_callback)
# This is where you could do other stuff
# Blink a light, turn a motor, run a webserver
# count sheep or mine bitcoin
while True:
time.sleep(1)
except:
print('Removing callback and cleaning up GPIO')
ir.remove_callback()
GPIO.cleanup(irPin)