-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblinds.py
126 lines (102 loc) · 3.87 KB
/
blinds.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
import time
import sys
import RPi.GPIO as GPIO
# Modulation delays.
DELAY_SHORT = 0.000284
DELAY_LONG = 0.000592
DELAY_EXTENDED = 0.005
# GPIO data pin that is connected to transmitter's data pin.
GPIO_DATA_PIN_TRANSMIT = 24
# Helper function to convert hex into binary string representation.
def hex2binary(hexadecimal):
end_length = len(hexadecimal) * 4
hex_as_int = int(hexadecimal, 16)
hex_as_binary = bin(hex_as_int)
padded_binary = hex_as_binary[2:].zfill(end_length)
return padded_binary
# Main method.
if __name__ == '__main__':
# Capture target blind and action to perform.
blind = sys.argv[1]
action = sys.argv[2]
open_percentage = 0
# Set codes to execute.
if blind == 'livingroom_left':
if action == 'up_step':
codes = ["5CC7C7ADFEFFF431", "5CC7C7ADFEFFDB18"]
open_percentage = 100
elif action == 'up':
codes = ["5CC7C7ADFEFFF431", "5CC7C7ADFEFFF431", "5CC7C7ADFEFF74B1"]
open_percentage = 100
elif action == 'stop':
codes = ["5CC7C7ADFEFFDC19"]
elif action == 'down_step':
codes = ["5CC7C7ADFEFFBCF9", "5CC7C7ADFEFFDB18"]
elif action == 'down':
codes = ["5CC7C7ADFEFFBCF9", "5CC7C7ADFEFFBCF9", "5CC7C7ADFEFF3C79"]
else:
sys.exit("Unknown action: " + action)
elif blind == 'livingroom_right':
if action == 'up_step':
codes = ["5CC7C7A6FEFFF42A", "5CC7C7A6FEFFDB11"]
open_percentage = 100
elif action == 'up':
codes = ["5CC7C7A6FEFFF42A", "5CC7C7A6FEFFF42A", "5CC7C7A6FEFF74AA"]
open_percentage = 100
elif action == 'stop':
codes = ["5CC7C7A6FEFFDC12"]
elif action == 'down_step':
codes = ["5CC7C7A6FEFFBCF2", "5CC7C7A6FEFFDB11"]
elif action == 'down':
codes = ["5CC7C7A6FEFFBCF2", "5CC7C7A6FEFFBCF2", "5CC7C7A6FEFF3C72"]
else:
sys.exit("Unknown action: " + action)
else:
sys.exit("Unknown blind: " + blind)
# Setup GPIO pin to transmit.
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_DATA_PIN_TRANSMIT, GPIO.OUT)
# Process each code required.
for code in codes:
# Convert to binary string.
code = hex2binary(code)
# Wait before starting.
time.sleep(DELAY_EXTENDED)
# Transmit preamble (remote transmits 6 times).
for i in range(6):
GPIO.output(GPIO_DATA_PIN_TRANSMIT, 1)
time.sleep(DELAY_SHORT)
GPIO.output(GPIO_DATA_PIN_TRANSMIT, 0)
time.sleep(DELAY_LONG)
# Transmit main code (remote transmits 6 times).
for t in range(6):
# Transmit starting bits.
GPIO.output(GPIO_DATA_PIN_TRANSMIT, 1)
time.sleep(DELAY_EXTENDED)
GPIO.output(GPIO_DATA_PIN_TRANSMIT, 0)
time.sleep(DELAY_LONG)
# Transmit each bit.
for i in code:
if i == '1':
GPIO.output(GPIO_DATA_PIN_TRANSMIT, 1)
time.sleep(DELAY_SHORT)
GPIO.output(GPIO_DATA_PIN_TRANSMIT, 0)
time.sleep(DELAY_LONG)
elif i == '0':
GPIO.output(GPIO_DATA_PIN_TRANSMIT, 1)
time.sleep(DELAY_LONG)
GPIO.output(GPIO_DATA_PIN_TRANSMIT, 0)
time.sleep(DELAY_SHORT)
else:
continue
# Transmit ending bits.
GPIO.output(GPIO_DATA_PIN_TRANSMIT, 1)
time.sleep(DELAY_LONG)
GPIO.output(GPIO_DATA_PIN_TRANSMIT, 0)
time.sleep(DELAY_SHORT)
GPIO.output(GPIO_DATA_PIN_TRANSMIT, 0)
time.sleep(DELAY_EXTENDED)
# Cleanup GPIO for next usage.
GPIO.cleanup()
# Print open percentage for Homebridge integration.
print(open_percentage)