-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDali_cmd.cpp
309 lines (255 loc) · 7.36 KB
/
Dali_cmd.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright (C) 2020 Paul Barrett <pbarrett(at)bitsystems(dot)com(dot)au>
*
* This code is derived from https://github.com/cosino/dali2560.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Dali.h"
uint8_t dev_found;
void Dali::remap(readdr_type remap_type) {
uint8_t n_dev;
this->dali_status |= 0x01; /* Remapping */
/* Set the Short Address to all devices from 0x00 */
n_dev = this->setDevAddresses(0x00, remap_type);
// for (int i = 0; i < 8; i++) { /* Clean 'slaves' array */
// this->slaves[i] = 0;
// }
if (n_dev == 0xFF) {
/* Need a remap operation */
this->dali_status = (this->dali_status | 0x2) & 0xFE;
this->dali_cmd &= 0xFE;
dev_found = 0;
return;
}
/* Scan bus */
list_dev();
// /* Prepare 'slaves' array for future detects */
// for (int i = 0; i < dev_found; i++) {
// this->slaves[i / 8] |= 1 << (i % 8);
// }
// storeSlaves(slaves);
this->dali_status &= 0xFE; /* Finish remapping */
}
void Dali::remapStatic(uint8_t addr, readdr_type remap_type) {
uint8_t n_dev;
this->dali_status |= 0x01; /* Remapping */
/* Set the Short Address to all devices starting from specific address */
n_dev = this->setDevAddresses(addr, remap_type);
/* Scan bus */
list_dev();
this->dali_status &= 0xFE; /* Finish remapping */
}
void Dali::remapMove(uint8_t oldAddr, uint8_t newAddr, readdr_type remap_type) {
uint32_t addr_dev;
uint8_t longaddress[24];
this->dali_status |= 0x01; /* Remapping */
// Reset all device
this->sendCommand(32, BROADCAST, 0); /* RESET */
delay(400);
this->sendCommand(0, BROADCAST, 0); /* Turn off lamps */
this->sendExtCommand(258, 0x00); /* INITIALISE devices */
this->sendExtCommand(259, 0x00); /* RANDOMISE */
delay(400);
// Retrieve random address from device
this->sendCommand(194, SINGLE, oldAddr); /* Query Random Address High */
longaddress[0] = *(this->getReply());
this->sendCommand(195, SINGLE, oldAddr); /* Query Random Address Medium */
longaddress[1] = *(this->getReply());
this->sendCommand(196, SINGLE, oldAddr); /* Query Random Address Low */
longaddress[2] = *(this->getReply());
// Serial.println(longaddress[0], HEX);
// Serial.println(longaddress[1], HEX);
// Serial.println(longaddress[2], HEX);
// Create device long address
addr_dev = ((uint32_t)longaddress[0]<<16) | ((uint32_t)longaddress[1] << 8) | ((uint32_t)longaddress[2]);
if (debugMode <= 3 && debugMode != 0) {
Serial.print("DEBUG - Long Addr: ");
Serial.println(addr_dev, HEX);
}
// Select long address of device
this->setSearch(addr_dev);
// Program new short address
this->sendExtCommand(267, (newAddr << 1) | 0x01);
delay(400);
this->sendExtCommand(256, 0x00); /* TERMINATE */
delay(400);
/* Scan bus */
list_dev();
this->dali_status &= 0xFE; /* Finish remapping */
}
void Dali::abort_remap(void)
{
this->dali_cmd |= 0x01;
}
void Dali::list_dev(void)
{
for (int i = 0; i < 8; i++) { /* Clean 'slaves' array */
this->slaves[i] = 0;
}
for(int i = 0; i < 64; i++){
int8_t data = 0;
this->sendCommand(153, SINGLE, i); /* Query Device Tpe */
data = *(this->getReply());
if (debugMode <= 3 && debugMode != 0) {
Serial.print("DEBUG - Addr ");
Serial.print(i);
Serial.print(" : ");
Serial.println(data);
}
if (data > 0){
this->slaves[i / 8] |= 1 << (i % 8);
}
}
storeSlaves(slaves);
}
uint8_t Dali::sendDirect(uint8_t val, addr_type type_addr, uint8_t addr) {
uint8_t cmd[2];
if (type_addr == BROADCAST) {
cmd[0] = 0xFE;
}
else if (type_addr == GROUP) {
if (addr > 15) {
return -1;
}
cmd[0] = 0x80 | (addr << 1);
} else if (type_addr == SINGLE) {
if (addr > 63) {
return -1;
}
cmd[0] = (addr << 1);
}
cmd[1] = val;
this->sendwait(cmd, 2, 100);
}
uint8_t Dali::sendCommand(uint8_t val, addr_type type_addr, uint8_t addr) {
uint8_t cmd[2];
if (type_addr == BROADCAST) {
cmd[0] = 0xFF;
}
else if (type_addr == GROUP) {
if (addr > 15) {
return -1;
}
cmd[0] = 0x01 | (addr << 1);
} else if (type_addr == SINGLE) {
if (addr > 63) {
return -1;
}
cmd[0] = 0x01 | (addr << 1);
}
cmd[1] = val;
if (val >= 224 && val <= 255) {
sendExtCommand(272, 6); /* En Dev Type Cmd Response */
}
this->sendwait(cmd, 2, 100);
if (val >= 32 && val <= 128 || val >= 224 && val <= 255) {
this->sendwait(cmd, 2, 100);
}
}
uint8_t Dali::sendExtCommand(uint16_t com, uint8_t val) {
uint8_t cmd[2];
uint16_t appo;
if (com < 256 || com > 349) {
return -1;
}
if (com > 255 && com < 272) {
appo = ((com - 256) & 0x00FF) << 1;
cmd[0] = appo;
cmd[0] |= 0xA1;
} else if (com > 271 && com < 276) {
appo = ((com - 272) & 0x00FF) << 1;
cmd[0] = appo;
cmd[0] |= 0xC1;
}
cmd[1] = val;
this->sendwait(cmd, 2, 100);
if (com >= 258 && com <= 259) {
this->sendwait(cmd, 2, 100);
}
}
uint8_t *Dali::getReply(void) {
return (uint8_t *) this->rx_msg;
}
uint8_t Dali::setDevAddresses(uint8_t start_addr, readdr_type all) {
uint32_t addr_dev = 0xFF;
// Reset all device
this->sendCommand(32, BROADCAST, 0); /* RESET */
delay(400);
this->sendCommand(0, BROADCAST, 0); /* Turn off lamps */
this->sendExtCommand(258, all); /* INITIALISE devices */
this->sendExtCommand(259, 0x00); /* RANDOMISE */
while (1) {
/* Find the device */
addr_dev = this->findDev(0xFFFFFF, 0x800000, 0);
if (addr_dev == 0xFFFFFFEE) {
return 0xFF; /* Abort remapping */
}
else if (addr_dev == 0xFFFFFFFF) {
break;
}
/* Program the short address */
/* Set the right SEARCHADDR for the found dev */
this->setSearch(addr_dev);
/* Program his Short Address */
this->sendExtCommand(267, (start_addr << 1) | 0x01);
/* Remove him from the search operation */
this->sendExtCommand(261, 0x00);
start_addr++;
dev_found++;
}
this->sendExtCommand(256, 0x00); /* TERMINATE */
return start_addr;
}
uint32_t Dali::findDev(uint32_t base_addr, uint32_t delta_addr, uint8_t n) {
if (this->dali_cmd & 0x01){
return 0xFFFFFFEE;
}
this->setSearch(base_addr);
/* Control commands on hold during the remap (Abort or finish?) */
serialDali();
this->sendExtCommand(260, 0x00); /* COMPARE */
if (this->rx_msg[0]) {
if (n == 24) {
return base_addr;
} else
base_addr -= delta_addr;
} else {
if (n == 0) {
return 0xFFFFFFFF;
}
else if (n > 23) {
return base_addr + 1;
} else {
base_addr += delta_addr;
}
}
if (n != 23) {
delta_addr >>= 1;
}
n++;
base_addr = findDev(base_addr, delta_addr, n);
return base_addr;
}
void Dali::setSearch(uint32_t addr) {
/* Control commands on hold during the remap (Abort or finish?) */
serialDali();
this->sendExtCommand(264, (addr >> 16) & 0xFF);
/* Control commands on hold during the remap (Abort or finish?) */
serialDali();
this->sendExtCommand(265, (addr >> 8) & 0xFF);
/* Control commands on hold during the remap (Abort or finish?) */
serialDali();
this->sendExtCommand(266, addr & 0xFF);
}