-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelldus_switch_ctrl.py
103 lines (90 loc) · 2.07 KB
/
telldus_switch_ctrl.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
#!/usr/bin/python3
# author: Mats Nilsson
import pigpio
import time
import sys
from collections import Counter
pi = pigpio.pi()
T = 255
t0 = 0
t1 = 0
tx_gpio = 17
rx_gpio = 27
pi.set_mode(rx_gpio, pigpio.INPUT) # set GPIO 15 as an input
pi.set_mode(tx_gpio, pigpio.OUTPUT) # set GPIO 14 as an output
msg = []
msg_start = False
msg_list = []
# SYNC
sync_wf = []
sync_wf.append(pigpio.pulse(1<<tx_gpio, 0, T))
sync_wf.append(pigpio.pulse(0, 1<<tx_gpio, 10*T))
# 1
one_wf = []
one_wf.append(pigpio.pulse(1<<tx_gpio, 0, T))
one_wf.append(pigpio.pulse(0, 1<<tx_gpio, 1*T))
# 0
zero_wf = []
zero_wf.append(pigpio.pulse(1<<tx_gpio, 0, T))
zero_wf.append(pigpio.pulse(0, 1<<tx_gpio, 5*T))
# P
pause_wf = []
pause_wf.append(pigpio.pulse(1<<tx_gpio, 0, T))
pause_wf.append(pigpio.pulse(0, 1<<tx_gpio, 40*T))
def decode(pulselength):
if pulselength == 2:
return "1"
elif pulselength == 6:
return "0"
elif pulselength == 11 or pulselength == 12:
return "S"
elif pulselength >= 41 and pulselength <= 43:
return "P"
else:
return "X"
def rx_callback(gpio, level, tick):
global t0, t1, msg, msg_start
t1 = tick
pulse = abs((t1 - t0)) / T
c = decode(round(pulse))
if c == "S":
msg_start = True
if c == "P":
msg.append(c)
tmp_msg = "".join(msg)
if len(tmp_msg) > 5:
msg_list.append(tmp_msg)
print(msg_list[-1])
msg_start = False
msg = []
if msg_start:
msg.append(c)
t0 = t1
def tx(word):
global sync_wf, one_wf, zero_wf, pause_wf
wf = []
for c in word:
if c == "S":
wf.extend(sync_wf)
elif c == "1":
wf.extend(one_wf)
elif c == "0":
wf.extend(zero_wf)
elif c == "P":
wf.extend(pause_wf)
else:
print("this is bad")
pi.wave_add_generic(wf)
wave = pi.wave_create()
for x in range(1,6):
print("Sending wave {0}...".format(x))
pi.wave_send_once(wave)
time.sleep(0.1)
if len(sys.argv) > 1:
tx(sys.argv[1])
else:
cb1 = pi.callback(rx_gpio, pigpio.RISING_EDGE, rx_callback)
time.sleep(15)
print("Most likely string:")
c = Counter(msg_list)
print(c.most_common(1))