-
Notifications
You must be signed in to change notification settings - Fork 0
/
dali.py
189 lines (146 loc) · 3 KB
/
dali.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python
# Documentazione comandi DALI
# http://www.tanzolab.it/www/CM3-HOME_test/dali_commands.pdf
import RPi.GPIO as GPIO
import time
import sys
#GPIO line used for LIGHT-BUS
GPIO_TX_LINE=31
BIT_DELAY=0.00034
LED_RED=22
LED_GREEN=23
LED_BLUE=24
LED_WHITE=25
def send_start():
global BIT_DELAY
global GPIO_TX_LINE
GPIO.output(GPIO_TX_LINE,GPIO.LOW)
time.sleep(BIT_DELAY)
GPIO.output(GPIO_TX_LINE,GPIO.HIGH)
time.sleep(BIT_DELAY)
def send_1():
global BIT_DELAY
global GPIO_TX_LINE
GPIO.output(GPIO_TX_LINE,GPIO.LOW)
time.sleep(BIT_DELAY)
GPIO.output(GPIO_TX_LINE,GPIO.HIGH)
time.sleep(BIT_DELAY)
def send_0():
global BIT_DELAY
global GPIO_TX_LINE
GPIO.output(GPIO_TX_LINE,GPIO.HIGH)
time.sleep(BIT_DELAY)
GPIO.output(GPIO_TX_LINE,GPIO.LOW)
time.sleep(BIT_DELAY)
def send_stop():
global BIT_DELAY
global GPIO_TX_LINE
GPIO.output(GPIO_TX_LINE,GPIO.HIGH)
time.sleep(BIT_DELAY)
GPIO.output(GPIO_TX_LINE,GPIO.HIGH)
time.sleep(BIT_DELAY)
def send_value(value):
global BIT_DELAY
global GPIO_TX_LINE
for i in range(8):
value=value&0xFF
#print "%02x" % value
if (value & 0x80) == 0:
send_0()
#print "send 0"
else:
send_1()
#print "send 1"
value=value<<1
def send_short_address(addr):
global BIT_DELAY
global GPIO_TX_LINE
# Send first byte
send_0() # Y=0 Short Address
# Send 6 bit address. Most first
if addr & 0x20:
send_1()
else:
send_0()
if addr & 0x10:
send_1()
else:
send_0()
if addr & 0x8:
send_1()
else:
send_0()
if addr & 0x4:
send_1()
else:
send_0()
if addr & 0x2:
send_1()
else:
send_0()
if addr & 0x1:
send_1()
else:
send_0()
send_0() # A=0 Direct arc power level
def send_color_value(led,value):
if value>254:
return
if value<0:
return
send_start()
send_short_address(led)
send_value(value)
send_stop()
send_stop()
time.sleep(0.001)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(GPIO_TX_LINE,GPIO.OUT)
# Initial state
GPIO.output(GPIO_TX_LINE,GPIO.HIGH)
send_start()
send_short_address(22)
send_value(0)
send_stop()
send_stop()
time.sleep(0.001)
send_start()
send_short_address(23)
send_value(0)
send_stop()
send_stop()
time.sleep(0.001)
send_start()
send_short_address(24)
send_value(0)
send_stop()
send_stop()
time.sleep(0.001)
send_start()
send_short_address(25)
send_value(0)
send_stop()
send_stop()
time.sleep(0.001)
#Demo mode
if len(sys.argv)==2:
print "Demo mode"
led=LED_WHITE
for i in range(0,255,1):
send_color_value(led,i)
for i in range(255,-1,-1):
send_color_value(led,i)
#Final state
GPIO.output(GPIO_TX_LINE,GPIO.HIGH)
quit()
if len(sys.argv)<=5:
print "dali.py red green blue white"
print " The color range is 0 to 254"
if len(sys.argv)==5:
send_color_value(LED_RED,int(sys.argv[1]))
send_color_value(LED_GREEN,int(sys.argv[2]))
send_color_value(LED_BLUE,int(sys.argv[3]))
send_color_value(LED_WHITE,int(sys.argv[4]))
#Final state
GPIO.output(GPIO_TX_LINE,GPIO.HIGH)