-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.py
76 lines (65 loc) · 2.15 KB
/
send.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
import RPi.GPIO as GPIO
from time import time
def setup():
GPIO.setmode(GPIO.BCM) # Numbers GPIOs by physical location
GPIO.setup(15, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN)
def binary_aquire(pin, duration):
# aquires data as quickly as possible
t0 = time()
results = []
while (time() - t0) < duration:
results.append(GPIO.input(pin))
return results
def on_ir_receive(pinNo, bouncetime=150):
# when edge detect is called (which requires less CPU than constant
# data acquisition), we acquire data as quickly as possible
data = binary_aquire(pinNo, bouncetime/1000.0)
if len(data) < bouncetime:
return
rate = len(data) / (bouncetime / 1000.0)
pulses = []
i_break = 0
# detect run lengths using the acquisition rate to turn the times in to microseconds
for i in range(1, len(data)):
if (data[i] != data[i-1]) or (i == len(data)-1):
pulses.append((data[i-1], int((i-i_break)/rate*1e6)))
i_break = i
# decode ( < 1 ms "1" pulse is a 1, > 1 ms "1" pulse is a 1, longer than 2 ms pulse is something else)
# does not decode channel, which may be a piece of the information after the long 1 pulse in the middle
outbin = ""
for val, us in pulses:
if val != 1:
continue
if outbin and us > 2000:
break
elif us < 1000:
outbin += "0"
elif 1000 < us < 2000:
outbin += "1"
try:
return int(outbin, 2)
except ValueError:
# probably an empty code
return None
def destroy():
GPIO.cleanup()
if __name__ == "__main__":
setup()
try:
print("Starting IR Listener")
while True:
print("Waiting for signal")
GPIO.wait_for_edge(4, GPIO.FALLING)
code = on_ir_receive(4)
if code:
print(str(hex(code)))
else:
print("Invalid code")
except KeyboardInterrupt:
pass
except RuntimeError:
# this gets thrown when control C gets pressed
# because wait_for_edge doesn't properly pass this on
pass
print("Quitting")
destroy()