Perform a search. If this function returns a '1' then it has enumerated the next device and you may retrieve the ROM from the OneWire::address variable. If there are no devices, no further devices, or something horrible happens in the middle of the enumeration then a 0 is returned. If a new device is found then its address is copied to newAddr. Use OneWire::reset_search() to start over. --- Rep
| 365 | // FALSE : device not found, end of search |
| 366 | // |
| 367 | uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) |
| 368 | { |
| 369 | uint8_t id_bit_number; |
| 370 | uint8_t last_zero, rom_byte_number, search_result; |
| 371 | uint8_t id_bit, cmp_id_bit; |
| 372 | |
| 373 | unsigned char rom_byte_mask, search_direction; |
| 374 | |
| 375 | // initialize for search |
| 376 | id_bit_number = 1; |
| 377 | last_zero = 0; |
| 378 | rom_byte_number = 0; |
| 379 | rom_byte_mask = 1; |
| 380 | search_result = 0; |
| 381 | |
| 382 | // if the last call was not the last one |
| 383 | if (!LastDeviceFlag) |
| 384 | { |
| 385 | // 1-Wire reset |
| 386 | if (!reset()) |
| 387 | { |
| 388 | // reset the search |
| 389 | LastDiscrepancy = 0; |
| 390 | LastDeviceFlag = FALSE; |
| 391 | LastFamilyDiscrepancy = 0; |
| 392 | return FALSE; |
| 393 | } |
| 394 | |
| 395 | // issue the search command |
| 396 | if (search_mode == true) { |
| 397 | write(0xF0); // NORMAL SEARCH |
| 398 | } else { |
| 399 | write(0xEC); // CONDITIONAL SEARCH |
| 400 | } |
| 401 | |
| 402 | // loop to do the search |
| 403 | do |
| 404 | { |
| 405 | // read a bit and its complement |
| 406 | id_bit = read_bit(); |
| 407 | cmp_id_bit = read_bit(); |
| 408 | |
| 409 | // check for no devices on 1-wire |
| 410 | if ((id_bit == 1) && (cmp_id_bit == 1)) |
| 411 | break; |
| 412 | else |
| 413 | { |
| 414 | // all devices coupled have 0 or 1 |
| 415 | if (id_bit != cmp_id_bit) |
| 416 | search_direction = id_bit; // bit write value for search |
| 417 | else |
| 418 | { |
| 419 | // if this discrepancy if before the Last Discrepancy |
| 420 | // on a previous next then pick the same as last time |
| 421 | if (id_bit_number < LastDiscrepancy) |
| 422 | search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0); |
| 423 | else |
| 424 | // if equal to last pick 1, if not then pick 0 |
no outgoing calls
no test coverage detected