forked from vinnyspb/nexa-controller-rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch_nexa.py
84 lines (67 loc) · 2.4 KB
/
switch_nexa.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
import sys
import time
sys.path.append("/storage/.kodi/addons/python.RPi.GPIO/lib")
import RPi.GPIO as GPIO
class NexaSwitcher:
def __init__(self, data_pin, transmitter_code):
self._data_pin = data_pin
self._transmitter_code = transmitter_code
print "Created NexaSwitcher for data PIN #" + str(self._data_pin) + " and transmitter code: " +\
str(self._transmitter_code)
def sleep_T(self, T_num):
time.sleep(T_num*250/1000000.0)
def send_physical_bit(self, bit_value):
if bit_value:
GPIO.output(self._data_pin, True)
self.sleep_T(1)
GPIO.output(self._data_pin, False)
self.sleep_T(1)
else:
GPIO.output(self._data_pin, True)
self.sleep_T(1)
GPIO.output(self._data_pin, False)
self.sleep_T(5)
def send_bit(self, bit_value):
self.send_physical_bit(bit_value)
self.send_physical_bit(not bit_value)
def send_sync(self):
GPIO.output(self._data_pin, True)
self.sleep_T(1)
GPIO.output(self._data_pin, False)
self.sleep_T(10)
def send_pause(self):
GPIO.output(self._data_pin, True)
self.sleep_T(1)
GPIO.output(self._data_pin, False)
self.sleep_T(40)
def send_on_off(self, on_off):
self.send_sync()
#transmitter code
binary_number_string = format(self._transmitter_code, '08b')
for digit in binary_number_string:
bit = digit == '1'
self.send_bit(bit)
#group code
self.send_bit(True)
#on/off bit, on = 0, off = 1
self.send_bit(not on_off)
#Channel bits. Proove/Anslut = 00, Nexa = 11.
self.send_bit(True)
self.send_bit(True)
# Unit bits. Device to be turned on or off.
# Nexa Unit #1 = 11, #2 = 10, #3 = 01.
self.send_bit(True)
self.send_bit(True)
self.send_pause()
def switch(self, on_off):
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self._data_pin, GPIO.OUT)
for x in range(0, 5):
self.send_on_off(on_off)
# Send the signal one more time,
# sometimes it happens not to be decoded correctly
time.sleep(1)
for x in range(0, 5):
self.send_on_off(on_off)
GPIO.output(self._data_pin, False) # Make sure that we do not leave PIN in 'on' state
GPIO.cleanup()