Commit bea85f0ebc1e231d1b8451fe19174ff1aaf2f320
Committed by
GitHub
1 parent
9a4c5621
Update Dali.cpp
Showing
1 changed file
with
144 additions
and
33 deletions
Dali.cpp
| 1 | 1 | ||
| 2 | + | ||
| 2 | #include "Dali.h" | 3 | #include "Dali.h" |
| 3 | #include <SoftwareSerial.h> | 4 | #include <SoftwareSerial.h> |
| 4 | 5 | ||
| @@ -54,6 +55,7 @@ void Dali::setupTransmit(uint8_t pin) | @@ -54,6 +55,7 @@ void Dali::setupTransmit(uint8_t pin) | ||
| 54 | #else | 55 | #else |
| 55 | delay1 = (HALF_BIT_INTERVAL >> speedFactor) - compensationFactor; | 56 | delay1 = (HALF_BIT_INTERVAL >> speedFactor) - compensationFactor; |
| 56 | delay2 = (HALF_BIT_INTERVAL >> speedFactor) - 2; | 57 | delay2 = (HALF_BIT_INTERVAL >> speedFactor) - 2; |
| 58 | + period = delay1 + delay2; | ||
| 57 | 59 | ||
| 58 | #if F_CPU == 1000000UL | 60 | #if F_CPU == 1000000UL |
| 59 | delay2 -= 22; //22+2 = 24 is divisible by 8 | 61 | delay2 -= 22; //22+2 = 24 is divisible by 8 |
| @@ -186,7 +188,7 @@ int Dali::maxResponseLevel() | @@ -186,7 +188,7 @@ int Dali::maxResponseLevel() | ||
| 186 | 188 | ||
| 187 | 189 | ||
| 188 | for (idalistep = 0; idalistep < dali.daliTimeout; idalistep = idalistep + dalistep) { | 190 | for (idalistep = 0; idalistep < dali.daliTimeout; idalistep = idalistep + dalistep) { |
| 189 | - dalidata = analogRead(RxAnalogPin); | 191 | + dalidata = analogRead(dali.RxAnalogPin); |
| 190 | if (dalidata > rxmax) { | 192 | if (dalidata > rxmax) { |
| 191 | rxmax = dalidata; | 193 | rxmax = dalidata; |
| 192 | }; | 194 | }; |
| @@ -205,44 +207,65 @@ void Dali::scanShortAdd() | @@ -205,44 +207,65 @@ void Dali::scanShortAdd() | ||
| 205 | const uint8_t finish_ind_adress = 127; | 207 | const uint8_t finish_ind_adress = 127; |
| 206 | uint8_t add_byte; | 208 | uint8_t add_byte; |
| 207 | uint8_t device_short_add; | 209 | uint8_t device_short_add; |
| 208 | - String response; | 210 | + uint8_t response; |
| 209 | 211 | ||
| 210 | dali.transmit(BROADCAST_C, OFF_C); // Broadcast Off | 212 | dali.transmit(BROADCAST_C, OFF_C); // Broadcast Off |
| 211 | delay(delayTime); | 213 | delay(delayTime); |
| 212 | - Serial.println("Short addresses:"); | 214 | + |
| 215 | + if (dali.msgMode) { | ||
| 216 | + Serial.println("Short addresses:"); | ||
| 217 | + } | ||
| 213 | 218 | ||
| 214 | for (device_short_add = start_ind_adress; device_short_add <= 63; device_short_add++) { | 219 | for (device_short_add = start_ind_adress; device_short_add <= 63; device_short_add++) { |
| 215 | 220 | ||
| 216 | add_byte = 1 + (device_short_add << 1); // convert short address to address byte | 221 | add_byte = 1 + (device_short_add << 1); // convert short address to address byte |
| 217 | - | ||
| 218 | 222 | ||
| 219 | - dali.transmit(add_byte, QUERY_STATUS); //query status | ||
| 220 | - | 223 | + |
| 224 | + dali.transmit(add_byte, 0xA1); | ||
| 225 | + | ||
| 226 | + response = dali.receive(); | ||
| 227 | + | ||
| 228 | + if (dali.getResponse) { | ||
| 221 | 229 | ||
| 222 | - if (minResponseLevel() < dali.analogLevel) { | ||
| 223 | - response = "YES"; | ||
| 224 | dali.transmit(add_byte, ON_C); // switch on | 230 | dali.transmit(add_byte, ON_C); // switch on |
| 225 | delay(1000); | 231 | delay(1000); |
| 226 | dali.transmit(add_byte, OFF_C); // switch off | 232 | dali.transmit(add_byte, OFF_C); // switch off |
| 227 | delay(1000); | 233 | delay(1000); |
| 234 | + | ||
| 228 | } | 235 | } |
| 229 | else { | 236 | else { |
| 230 | - response = "NO"; | 237 | + response = 0; |
| 231 | } | 238 | } |
| 232 | 239 | ||
| 233 | 240 | ||
| 234 | - Serial.print("BIN: "); | ||
| 235 | - Serial.print(device_short_add, BIN); | ||
| 236 | - Serial.print(" "); | ||
| 237 | - Serial.print("DEC: "); | ||
| 238 | - Serial.print(device_short_add, DEC); | ||
| 239 | - Serial.print(" "); | ||
| 240 | - Serial.print("HEX: "); | ||
| 241 | - Serial.print(device_short_add, HEX); | ||
| 242 | - Serial.print(" "); | ||
| 243 | - Serial.print("Response: "); | ||
| 244 | - Serial.print(response); | ||
| 245 | - Serial.println(); | 241 | + |
| 242 | + if (dali.msgMode) { | ||
| 243 | + Serial.print("BIN: "); | ||
| 244 | + Serial.print(device_short_add, BIN); | ||
| 245 | + Serial.print(" "); | ||
| 246 | + Serial.print("DEC: "); | ||
| 247 | + Serial.print(device_short_add, DEC); | ||
| 248 | + Serial.print(" "); | ||
| 249 | + Serial.print("HEX: "); | ||
| 250 | + Serial.print(device_short_add, HEX); | ||
| 251 | + Serial.print(" "); | ||
| 252 | + if (dali.getResponse) { | ||
| 253 | + Serial.print("Get response"); | ||
| 254 | + } | ||
| 255 | + else { | ||
| 256 | + Serial.print("No response"); | ||
| 257 | + } | ||
| 258 | + Serial.println(); | ||
| 259 | + } | ||
| 260 | + else { | ||
| 261 | + if (dali.getResponse) { | ||
| 262 | + Serial.println(255, BIN); | ||
| 263 | + } | ||
| 264 | + else { | ||
| 265 | + Serial.println(0, BIN); | ||
| 266 | + } | ||
| 267 | + | ||
| 268 | + } | ||
| 246 | 269 | ||
| 247 | } | 270 | } |
| 248 | 271 | ||
| @@ -303,7 +326,6 @@ void Dali::initialisation() { | @@ -303,7 +326,6 @@ void Dali::initialisation() { | ||
| 303 | uint8_t short_add = 0; | 326 | uint8_t short_add = 0; |
| 304 | uint8_t cmd2; | 327 | uint8_t cmd2; |
| 305 | 328 | ||
| 306 | - | ||
| 307 | delay(delaytime); | 329 | delay(delaytime); |
| 308 | dali.transmit(BROADCAST_C, RESET); | 330 | dali.transmit(BROADCAST_C, RESET); |
| 309 | delay(delaytime); | 331 | delay(delaytime); |
| @@ -319,7 +341,9 @@ void Dali::initialisation() { | @@ -319,7 +341,9 @@ void Dali::initialisation() { | ||
| 319 | delay(delaytime); | 341 | delay(delaytime); |
| 320 | dali.transmit(0b10100111, 0b00000000); //randomise | 342 | dali.transmit(0b10100111, 0b00000000); //randomise |
| 321 | 343 | ||
| 322 | - Serial.println("Searching fo long addresses:"); | 344 | + if (dali.msgMode) { |
| 345 | + Serial.println("Searching fo long addresses:"); | ||
| 346 | + } | ||
| 323 | 347 | ||
| 324 | while (longadd <= 0xFFFFFF - 2 and short_add <= 64) { | 348 | while (longadd <= 0xFFFFFF - 2 and short_add <= 64) { |
| 325 | while ((high_longadd - low_longadd) > 1) { | 349 | while ((high_longadd - low_longadd) > 1) { |
| @@ -345,15 +369,20 @@ void Dali::initialisation() { | @@ -345,15 +369,20 @@ void Dali::initialisation() { | ||
| 345 | 369 | ||
| 346 | longadd = (low_longadd + high_longadd) / 2; //center | 370 | longadd = (low_longadd + high_longadd) / 2; //center |
| 347 | 371 | ||
| 348 | - Serial.print("BIN: "); | ||
| 349 | - Serial.print(longadd + 1, BIN); | ||
| 350 | - Serial.print(" "); | ||
| 351 | - Serial.print("DEC: "); | ||
| 352 | - Serial.print(longadd + 1, DEC); | ||
| 353 | - Serial.print(" "); | ||
| 354 | - Serial.print("HEX: "); | ||
| 355 | - Serial.print(longadd + 1, HEX); | ||
| 356 | - Serial.println(); | 372 | + if (dali.msgMode) { |
| 373 | + Serial.print("BIN: "); | ||
| 374 | + Serial.print(longadd + 1, BIN); | ||
| 375 | + Serial.print(" "); | ||
| 376 | + Serial.print("DEC: "); | ||
| 377 | + Serial.print(longadd + 1, DEC); | ||
| 378 | + Serial.print(" "); | ||
| 379 | + Serial.print("HEX: "); | ||
| 380 | + Serial.print(longadd + 1, HEX); | ||
| 381 | + Serial.println(); | ||
| 382 | + } | ||
| 383 | + else { | ||
| 384 | + Serial.println(longadd + 1); | ||
| 385 | + } | ||
| 357 | } // second while | 386 | } // second while |
| 358 | 387 | ||
| 359 | 388 | ||
| @@ -376,14 +405,18 @@ void Dali::initialisation() { | @@ -376,14 +405,18 @@ void Dali::initialisation() { | ||
| 376 | delay(delaytime); | 405 | delay(delaytime); |
| 377 | short_add++; | 406 | short_add++; |
| 378 | 407 | ||
| 408 | + if (dali.msgMode) { | ||
| 379 | Serial.println("Assigning a short address"); | 409 | Serial.println("Assigning a short address"); |
| 410 | + } | ||
| 380 | 411 | ||
| 381 | high_longadd = 0xFFFFFF; | 412 | high_longadd = 0xFFFFFF; |
| 382 | longadd = (low_longadd + high_longadd) / 2; | 413 | longadd = (low_longadd + high_longadd) / 2; |
| 383 | 414 | ||
| 384 | } | 415 | } |
| 385 | else { | 416 | else { |
| 386 | - Serial.println("End"); | 417 | + if (dali.msgMode) { |
| 418 | + Serial.println("End"); | ||
| 419 | + } | ||
| 387 | } | 420 | } |
| 388 | } // first while | 421 | } // first while |
| 389 | 422 | ||
| @@ -393,6 +426,84 @@ void Dali::initialisation() { | @@ -393,6 +426,84 @@ void Dali::initialisation() { | ||
| 393 | } | 426 | } |
| 394 | 427 | ||
| 395 | 428 | ||
| 429 | +uint8_t Dali::receive() { | ||
| 430 | + | ||
| 431 | + | ||
| 432 | + | ||
| 433 | + unsigned long startFuncTime = 0; | ||
| 434 | + bool previousLogicLevel = 1; | ||
| 435 | + bool currentLogicLevel = 1; | ||
| 436 | + uint8_t arrLength = 20; | ||
| 437 | + int timeArray[arrLength]; | ||
| 438 | + int i = 0; | ||
| 439 | + int k = 0; | ||
| 440 | + bool logicLevelArray[arrLength]; | ||
| 441 | + int response = 0; | ||
| 442 | + | ||
| 443 | + dali.getResponse = false; | ||
| 444 | + startFuncTime = micros(); | ||
| 445 | + | ||
| 446 | + // add check for micros overlap here!!! | ||
| 447 | + | ||
| 448 | + while (micros() - startFuncTime < dali.daliTimeout and i < arrLength) | ||
| 449 | + { | ||
| 450 | + // geting response | ||
| 451 | + if (analogRead(dali.RxAnalogPin) > dali.analogLevel) { | ||
| 452 | + currentLogicLevel = 1; | ||
| 453 | + } | ||
| 454 | + else { | ||
| 455 | + currentLogicLevel = 0; | ||
| 456 | + } | ||
| 457 | + | ||
| 458 | + if (previousLogicLevel != currentLogicLevel) { | ||
| 459 | + timeArray[i] = micros() - startFuncTime; | ||
| 460 | + logicLevelArray[i] = currentLogicLevel; | ||
| 461 | + previousLogicLevel = currentLogicLevel; | ||
| 462 | + dali.getResponse = true; | ||
| 463 | + i++; | ||
| 464 | + | ||
| 465 | + } | ||
| 466 | + } | ||
| 467 | + | ||
| 468 | + | ||
| 469 | + | ||
| 470 | + arrLength = i; | ||
| 471 | + | ||
| 472 | + //decoding to manchester | ||
| 473 | + for (i = 0; i < arrLength - 1; i++) { | ||
| 474 | + if ((timeArray[i + 1] - timeArray[i]) > 0.75 * dali.period) { | ||
| 475 | + for (k = arrLength; k > i; k--) { | ||
| 476 | + timeArray[k] = timeArray[k - 1]; | ||
| 477 | + logicLevelArray[k] = logicLevelArray[k - 1]; | ||
| 478 | + } | ||
| 479 | + arrLength++; | ||
| 480 | + timeArray[i + 1] = (timeArray[i] + timeArray[i + 2]) / 2; | ||
| 481 | + logicLevelArray[i + 1] = logicLevelArray[i]; | ||
| 482 | + } | ||
| 483 | + } | ||
| 484 | + | ||
| 485 | + | ||
| 486 | + | ||
| 487 | + | ||
| 488 | + | ||
| 489 | + k = 8; | ||
| 490 | + | ||
| 491 | + for (i = 1; i < arrLength; i++) { | ||
| 492 | + if (logicLevelArray[i] == 1) { | ||
| 493 | + if ((int)round((timeArray[i] - timeArray[0]) / (0.5 * dali.period)) & 1) { | ||
| 494 | + response = response + (1 << k); | ||
| 495 | + } | ||
| 496 | + k--; | ||
| 497 | + } | ||
| 498 | + } | ||
| 499 | + | ||
| 500 | + | ||
| 501 | + //remove start bit | ||
| 502 | + response = (uint8_t)response; | ||
| 503 | + | ||
| 504 | + return response; | ||
| 505 | + | ||
| 506 | +} | ||
| 396 | 507 | ||
| 397 | 508 | ||
| 398 | 509 |