-
Notifications
You must be signed in to change notification settings - Fork 7
/
DALIDriver.cpp
724 lines (652 loc) · 22.5 KB
/
DALIDriver.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/* DALI Driver
* Copyright (c) 2018 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "DALIDriver.h"
DALIDriver::DALIDriver(PinName out_pin, PinName in_pin, int baud,
bool idle_state)
: encoder(out_pin, in_pin, baud, idle_state)
{
}
DALIDriver::~DALIDriver()
{
}
bool DALIDriver::add_to_group(uint8_t addr, uint8_t group)
{
// Send the command to add to group
send_twice(addr, ADD_TO_GROUP + group);
// Query upper or lower bits of gearGroups 16 bit variable
uint8_t cmd = group < 8 ? QUERY_GEAR_GROUPS_L : QUERY_GEAR_GROUPS_H;
// Send query command
send_command_standard(addr, cmd);
// Receive gearGroups variable
uint8_t resp = encoder.recv();
// Group bit will be set if this light is a memeber of that group
uint8_t mask = 1 << (group % 8);
bool contained = resp & mask;
// Return whether light is part of group
return contained;
}
bool DALIDriver::remove_from_group(uint8_t addr, uint8_t group)
{
// Send the command to remove from group
send_twice(addr, REMOVE_FROM_GROUP + group);
// Query upper or lower bits of gearGroups 16 bit variable
uint8_t cmd = group < 8 ? QUERY_GEAR_GROUPS_L : QUERY_GEAR_GROUPS_H;
// Send query command
send_command_standard(addr, cmd);
// Receive gearGroups variable
uint8_t resp = encoder.recv();
// Group bit will be set if this light is a memeber of that group
uint8_t mask = 1 << (group % 8);
bool contained = resp & mask;
// Return whether light is not part of group
return !contained;
}
void DALIDriver::set_level(uint8_t addr, uint8_t level)
{
send_command_direct(addr, level);
}
void DALIDriver::turn_off(uint8_t addr)
{
send_command_standard(addr, OFF);
}
uint8_t DALIDriver::get_level(uint8_t addr)
{
send_command_standard(addr, QUERY_ACTUAL_LEVEL);
uint8_t resp = encoder.recv();
return resp;
}
uint8_t DALIDriver::get_error(uint8_t addr)
{
send_command_standard(addr, QUERY_ERROR);
uint8_t resp = encoder.recv();
return resp & 0x03;
}
uint8_t DALIDriver::get_phm(uint8_t addr)
{
send_command_standard(addr, QUERY_PHM);
uint8_t resp = encoder.recv();
return resp;
}
uint8_t DALIDriver::get_fade(uint8_t addr)
{
send_command_standard(addr, QUERY_FADE);
uint8_t resp = encoder.recv();
return resp;
}
ColorType DALIDriver::get_color_type(uint8_t addr) {
uint8_t channels = query_rgbwaf_channels(addr);
if (channels == 4) {
return RGB;
} else if (query_temperature_capable(addr)) {
return TEMPERATURE;
}
return UNSUPPORTED;
}
uint8_t DALIDriver::query_color_type_features(uint8_t addr)
{
encoder.set_recv_frame_length(8);
//send command to enable device type 8
send_command_special(ENABLE_DEVICE_TYPE, 0x08);
send_command_standard(addr, QUERY_COLOR_TYPE_FEATURES);
uint8_t resp = encoder.recv();
return resp;
}
uint8_t DALIDriver::query_rgbwaf_channels(uint8_t addr)
{
uint8_t resp = query_color_type_features(addr);
return (resp & 0xE0) >> 5;
}
bool DALIDriver::query_temperature_capable(uint8_t addr)
{
uint8_t resp = query_color_type_features(addr);
return (resp & 0x02) >> 1;
}
void DALIDriver::set_color_temp(uint8_t addr, uint16_t temp)
{
// Calculate Mirek from Kelvin
temp = 1000000/temp;
// Set Temp
send_command_special(DTR0, temp & 0x00FF);
send_command_special(DTR1, temp >> 8);
//send command to enable device type 8
send_command_special(ENABLE_DEVICE_TYPE, 0x08);
// Set the temporary color to the temperature
send_command_standard(addr, SET_TEMP_TEMPC);
}
void DALIDriver::set_color_scene(uint8_t addr, uint8_t scene, uint16_t temp)
{
set_color_temp(addr, temp);
// Get the current scene level
send_command_standard(addr, QUERY_SCENE_LEVEL + scene);
uint8_t scene_level = encoder.recv();
send_command_special(DTR0, scene_level);
// Store what is in the temperorary color as scene color and also scene level to DTR0
send_twice(addr, STORE_DTR_AS_SCENE + scene);
}
void DALIDriver::set_color(uint8_t addr, uint16_t temp)
{
set_color_temp(addr, temp);
// Activate color
//send command to enable device type 8
send_command_special(ENABLE_DEVICE_TYPE, 0x08);
send_command_standard(addr, COLOR_ACTIVATE);
}
void DALIDriver::set_color_temp(uint8_t addr, uint8_t r, uint8_t g, uint8_t b, uint8_t dim)
{
// Set RGB
send_command_special(DTR0, r);
send_command_special(DTR1, g);
send_command_special(DTR2, b);
//send command to enable device type 8
send_command_special(ENABLE_DEVICE_TYPE, 0x08);
send_command_standard(addr, SET_TEMP_RGB_DIM);
// Set dim
send_command_special(DTR0, dim);
//send command to enable device type 8
send_command_special(ENABLE_DEVICE_TYPE, 0x08);
send_command_standard(addr, SET_TEMP_WAF_DIM);
}
void DALIDriver::set_color_scene(uint8_t addr, uint8_t scene, uint8_t r, uint8_t g, uint8_t b, uint8_t dim)
{
set_color_temp(addr, r, g, b, dim);
// Get the current scene level
send_command_standard(addr, QUERY_SCENE_LEVEL + scene);
uint8_t scene_level = encoder.recv();
send_command_special(DTR0, scene_level);
// Store what is in the temperorary color as scene color and also scene level to DTR0
send_twice(addr, STORE_DTR_AS_SCENE + scene);
}
void DALIDriver::set_color(uint8_t addr, uint8_t r, uint8_t g, uint8_t b, uint8_t dim)
{
set_color_temp(addr, r, g, b, dim);
// Activate color
//send command to enable device type 8
send_command_special(ENABLE_DEVICE_TYPE, 0x08);
send_command_standard(addr, COLOR_ACTIVATE);
}
uint32_t DALIDriver::recv()
{
return encoder.recv();
}
uint32_t DALIDriver::query_instances(uint8_t addr)
{
encoder.set_recv_frame_length(8);
send_command_standard_input(addr, 0xFE, 0x35);
uint32_t resp = encoder.recv();
return resp;
}
void DALIDriver::turn_on(uint8_t addr)
{
send_command_standard(addr, ON_AND_STEP_UP);
}
void DALIDriver::send_twice(uint8_t addr, uint8_t opcode)
{
send_command_standard(addr, opcode);
send_command_standard(addr, opcode);
}
void DALIDriver::set_fade_time(uint8_t addr, uint8_t time)
{
// Send twice command
send_command_special(DTR0, time);
send_twice(addr, SET_FADE_TIME);
}
void DALIDriver::set_fade_rate(uint8_t addr, uint8_t rate)
{
// Send twice command
send_command_special(DTR0, rate);
send_twice(addr, SET_FADE_RATE);
}
void DALIDriver::set_scene(uint8_t addr, uint8_t scene, uint8_t level)
{
send_command_special(DTR0, level);
// Send twice command
send_twice(addr, SET_SCENE + scene);
}
void DALIDriver::remove_from_scene(uint8_t addr, uint8_t scene)
{
send_twice(addr, REMOVE_FROM_SCENE + scene);
}
void DALIDriver::go_to_scene(uint8_t addr, uint8_t scene)
{
send_twice(addr, GO_TO_SCENE + scene);
//send command to enable device type 8
send_command_special(ENABLE_DEVICE_TYPE, 0x08);
// Activate color scene
send_command_standard(addr, COLOR_ACTIVATE);
}
event_msg DALIDriver::parse_event(uint32_t data)
{
event_msg msg;
msg.addr = data >> 17;
msg.inst_type = (data >> 10) & 0x7F;
msg.info = data & 0x03FF;
return msg;
}
void DALIDriver::attach(mbed::Callback<void(uint32_t)> status_cb)
{
quiet_mode(false);
encoder.attach(status_cb);
}
void DALIDriver::detach()
{
quiet_mode(true);
encoder.detach();
}
void DALIDriver::reattach()
{
quiet_mode(false);
encoder.reattach();
}
void DALIDriver::send_command_special(uint8_t address, uint8_t opcode)
{
encoder.send(((uint16_t)address << 8) | opcode);
}
void DALIDriver::send_command_special_input(uint8_t instance, uint8_t opcode)
{
encoder.send_24(((uint32_t)0xC1 << 16) | ((uint16_t)instance << 8) |
opcode);
}
void DALIDriver::send_command_standard_input(uint8_t address, uint8_t instance,
uint8_t opcode)
{
// Get the upper bit
uint8_t mask = address & 0x80;
// Change address to have 1 in LSb to signify 'standard command'
address = mask | ((address << 1) + 1);
encoder.send_24(((uint32_t)address << 16) | ((uint16_t)instance << 8) |
opcode);
}
void DALIDriver::send_command_standard(uint8_t address, uint8_t opcode)
{
// Get the upper bit
uint8_t mask = address & 0x80;
// Change address to have 1 in LSb to signify 'standard command'
address = mask | ((address << 1) + 1);
encoder.send(((uint16_t)address << 8) | opcode);
}
void DALIDriver::send_command_direct(uint8_t address, uint8_t opcode)
{
// Get the upper bit
uint8_t mask = address & 0x80;
// Change address to have 0 in LSb to signify 'direct arc power'
address = mask | (address << 1);
encoder.send(((uint16_t)address << 8) | opcode);
}
bool DALIDriver::check_response(uint8_t expected)
{
int response = encoder.recv();
if (response < 0)
return false;
return (response == expected);
}
int DALIDriver::getIndexOfLogicalUnit(uint8_t addr)
{
send_command_special(DTR1, 0x00);
send_command_special(DTR0, 0x1A);
send_command_special(READ_MEM_LOC, (addr << 1) + 1);
return encoder.recv();
}
void DALIDriver::set_search_address(uint32_t val)
{
send_command_special(SEARCHADDRH, val >> 16);
send_command_special(SEARCHADDRM, (val >> 8) & (0x00FF));
send_command_special(SEARCHADDRL, val & 0x0000FF);
}
void DALIDriver::set_search_address_input(uint32_t val)
{
send_command_special_input(0x05, val >> 16);
send_command_special_input(0x06, (val >> 8) & (0x00FF));
send_command_special_input(0x07, val & 0x0000FF);
}
uint8_t DALIDriver::get_group_addr(uint8_t group_number)
{
uint8_t mask = 1 << 7;
// Make MSb a 1 to signify >1 device being addressed
return mask | group_number;
}
void DALIDriver::quiet_mode(bool on)
{
if (on) {
send_command_standard_input(0xFF, 0xFE, 0x1D);
} else {
send_command_standard_input(0xFF, 0xFE, 0x1E);
}
}
float DALIDriver::get_temperature(uint8_t addr, uint8_t instance)
{
send_command_standard_input(addr, instance, 0x8C);
int temp = encoder.recv();
send_command_standard_input(addr, instance, 0x8D);
int temp2 = encoder.recv();
// Temperature, 10 bit, resolution 0.1C, -5C - 60C (value of 0 = -5C, 1 =
// -4.9C, etc.)
return ((float)((temp << 2) | (temp2 >> 6)) - 50.0f) * 0.1f;
}
float DALIDriver::get_humidity(uint8_t addr, uint8_t instance)
{
send_command_standard_input(addr, instance, 0x8C);
int humidity = encoder.recv();
// Humidity, 8 bit, resolution 0.5%, 0-100%
return ((float)humidity) / 2.0f;
}
int DALIDriver::init_lights()
{
quiet_mode(true);
// TODO: does this need to happen every time controller boots?
num_lights = assign_addresses();
return num_lights;
}
int DALIDriver::init_inputs()
{
quiet_mode(true);
num_inputs = assign_addresses_input(true, num_lights) - num_lights;
return num_inputs;
}
int DALIDriver::init()
{
num_logical_units = num_lights + num_inputs;
init_lights();
init_inputs();
// Set the event scheme for all events to be address / instance id / event
// info
set_event_scheme(0xFF, 0xFF, 0x01);
wait(1);
for (int i = num_lights; i < num_inputs + num_lights; i++) {
int inst = query_instances(i);
for (int j = 0; j < inst; j++) {
int inst_type = get_instance_type(i, j);
if (inst_type == 4) {
// Disable lumen
disable_instance(i, j);
continue;
}
enable_instance(i, j);
// Filter events for PIR, only movement/no movement
if (inst_type == 3) {
set_event_filter(i, j, 0x1C);
}
}
}
return num_lights + num_inputs;
}
void DALIDriver::set_event_scheme(uint8_t addr, uint8_t inst, uint8_t scheme)
{
// Put scheme in DTR0
send_command_special_input(0x30, scheme);
// Set the event scheme
send_command_standard_input(addr, inst, 0x67);
send_command_standard_input(addr, inst, 0x67);
}
void DALIDriver::set_event_filter(uint8_t addr, uint8_t inst, uint8_t filter)
{
// Put filter in DTR0
send_command_special_input(0x30, filter);
// Set the event filter
send_command_standard_input(addr, inst, 0x68);
send_command_standard_input(addr, inst, 0x68);
}
uint8_t DALIDriver::get_instance_type(uint8_t addr, uint8_t inst)
{
send_command_standard_input(addr, inst, 0x80);
return encoder.recv();
}
uint8_t DALIDriver::get_instance_status(uint8_t addr, uint8_t inst)
{
send_command_standard_input(addr, inst, 0x86);
return encoder.recv();
}
void DALIDriver::disable_instance(uint8_t addr, uint8_t inst)
{
send_command_standard_input(addr, inst, 0x63);
send_command_standard_input(addr, inst, 0x63);
}
void DALIDriver::enable_instance(uint8_t addr, uint8_t inst)
{
send_command_standard_input(addr, inst, 0x62);
send_command_standard_input(addr, inst, 0x62);
}
int DALIDriver::get_highest_address()
{
int highestAssigned = -1;
// Start initialization phase
send_command_special(INITIALISE, 0x00);
send_command_special(INITIALISE, 0x00);
// Assign all units a random address
send_command_special(RANDOMISE, 0x00);
send_command_special(RANDOMISE, 0x00);
wait_ms(100);
while (true) {
// Set the search address to the highest range
set_search_address(0xFFFFFF);
// Compare logical units search address to global search address
send_command_special(COMPARE, 0x00);
// Check if any device responds yes
bool yes = check_response(YES);
// If no devices are unassigned (all withdrawn), we are done
if (!yes) {
break;
}
uint32_t searchAddr = 0xFFFFFF;
for (int i = 23; i >= 0; i--) {
uint32_t mask = 1 << i;
searchAddr = searchAddr & (~mask);
// Set a new search address
set_search_address(searchAddr);
send_command_special(COMPARE, 0x00);
// Check if any devices match
bool yes = check_response(YES);
if (!yes) {
// No unit here, revert the mask
searchAddr = searchAddr | mask;
}
// If yes, then we found at least one device
}
set_search_address(searchAddr);
send_command_special(COMPARE, 0x00);
yes = check_response(YES);
if (yes) {
// Get the current short address
send_command_special(QUERY_SHORT_ADDR, 0x00);
uint8_t short_addr = encoder.recv();
if (short_addr != 0xFF) {
short_addr = short_addr >> 1;
if (short_addr > highestAssigned) {
highestAssigned = short_addr;
}
}
// Tell unit to withdraw (no longer respond to search queries)
send_command_special(WITHDRAW, 0x00);
}
// Refresh initialization state
send_command_special(INITIALISE, 0x00);
send_command_special(INITIALISE, 0x00);
}
send_command_special(TERMINATE, 0x00);
return highestAssigned;
}
// Return number of logical units on the bus
int DALIDriver::assign_addresses(bool reset)
{
uint8_t numAssignedShortAddresses = 0;
int assignedAddresses[63] = {false};
int highestAssigned = -1;
if (!reset) {
// Get the highest address already assigned
int highest_addr = get_highest_address();
if (highest_addr >= 0) {
numAssignedShortAddresses = highest_addr + 1;
for (int i = 0; i < numAssignedShortAddresses; i++) {
assignedAddresses[i] = true;
}
}
}
// Start initialization phase for devices w/o a short address
uint8_t opcode = reset ? 0x00 : 0xFF;
send_command_special(INITIALISE, opcode);
send_command_special(INITIALISE, opcode);
// Assign all units a random address
send_command_special(RANDOMISE, 0x00);
send_command_special(RANDOMISE, 0x00);
wait_ms(100);
while (true) {
// Set the search address to the highest range
set_search_address(0xFFFFFF);
// Compare logical units search address to global search address
send_command_special(COMPARE, 0x00);
// Check if any device responds yes
bool yes = check_response(YES);
// If no devices are unassigned (all withdrawn), we are done
if (!yes) {
break;
}
if (numAssignedShortAddresses < 63) {
uint32_t searchAddr = 0xFFFFFF;
for (int i = 23; i >= 0; i--) {
uint32_t mask = 1 << i;
searchAddr = searchAddr & (~mask);
// Set a new search address
set_search_address(searchAddr);
send_command_special(COMPARE, 0x00);
// Check if any devices match
bool yes = check_response(YES);
if (!yes) {
// No unit here, revert the mask
searchAddr = searchAddr | mask;
}
// If yes, then we found at least one device
}
set_search_address(searchAddr);
send_command_special(COMPARE, 0x00);
bool yes = check_response(YES);
if (yes) {
// We found a unit, let's program the short address with a new
// address Give it a temporary short address
uint8_t new_addr = numAssignedShortAddresses;
if (new_addr < 63) {
if (assignedAddresses[new_addr] == true) {
// Duplicate addr?
} else {
// Program new address as short address
send_command_special(PROGRAM_SHORT_ADDR,
(new_addr << 1) + 1);
// Tell unit to withdraw (no longer respond to search
// queries)
send_command_special(WITHDRAW, 0x00);
numAssignedShortAddresses++;
assignedAddresses[new_addr] = true;
if (new_addr > highestAssigned)
highestAssigned = new_addr;
}
} else {
// expected < 63 ?
}
} else {
// No device found
}
}
// Refresh initialization state
send_command_special(INITIALISE, 0x00);
send_command_special(INITIALISE, 0x00);
}
send_command_special(TERMINATE, 0x00);
return numAssignedShortAddresses;
}
// Return number of logical units on the bus
int DALIDriver::assign_addresses_input(bool reset, int num_found)
{
send_command_special(TERMINATE, 0x00);
uint8_t numAssignedShortAddresses = num_found;
int assignedAddresses[63] = {false};
int highestAssigned = -1;
// Put 0x00 in DTR0
send_command_special_input(0x30, 0x00);
// Set operating mode to DTR0
send_command_standard_input(0xFF, 0xFE, 0x18);
send_command_standard_input(0xFF, 0xFE, 0x18);
// DTR0 MASK
send_command_special_input(0x30, 0xFF);
// Set short address to DTR0
send_command_standard_input(0x7F, 0xFE, 0x14);
send_command_standard_input(0x7F, 0xFE, 0x14);
// Start initialization phase for devices
send_command_special_input(0x01, 0xFF);
send_command_special_input(0x01, 0xFF);
// Assign all units a random address
send_command_special_input(0x02, 0x00);
send_command_special_input(0x02, 0x00);
wait_ms(100);
while (true) {
// Set the search address to the highest range
set_search_address_input(0xFFFFFF);
// Compare logical units search address to global search address
send_command_special_input(0x03, 0x00);
// Check if any device responds yes
bool yes = check_response(YES);
// If no devices are unassigned (all withdrawn), we are done
if (!yes) {
break;
}
if (numAssignedShortAddresses < 63) {
uint32_t searchAddr = 0xFFFFFF;
for (int i = 23; i >= 0; i--) {
uint32_t mask = 1 << i;
searchAddr = searchAddr & (~mask);
// Set a new search address
set_search_address_input(searchAddr);
send_command_special_input(0x03, 0x00);
// Check if any devices match
bool yes = check_response(YES);
if (!yes) {
// No unit here, revert the mask
searchAddr = searchAddr | mask;
}
// If yes, then we found at least one device
}
set_search_address_input(searchAddr);
send_command_special_input(0x03, 0x00);
bool yes = check_response(YES);
if (yes) {
// We found a unit, let's program the short address with a new
// address Give it a temporary short address
uint8_t new_addr = numAssignedShortAddresses;
if (new_addr < 63) {
if (assignedAddresses[new_addr] == true) {
// Duplicate addr?
} else {
// Program new address as short address
send_command_special_input(0x08, new_addr);
// Tell unit to withdraw (no longer respond to search
// queries)
send_command_special_input(0x04, 0x00);
numAssignedShortAddresses++;
assignedAddresses[new_addr] = true;
if (new_addr > highestAssigned)
highestAssigned = new_addr;
}
} else {
// expected < 63 ?
}
} else {
// No device found
}
}
// Refresh initialization state
send_command_special_input(0x01, 0x7F);
send_command_special_input(0x01, 0x7F);
}
send_command_special_input(0x00, 0x00);
return numAssignedShortAddresses;
}