pub struct Search { /* private fields */ }Expand description
Enumerates the devices on a bus, one ROM code per call.
This is the binary tree walk the 1-Wire specification defines: after a
SEARCH_ROM every device sends each bit of its ROM code and then the complement,
the controller writes back the branch it takes, and the devices whose bit differs
drop out until one remains. Each call to next returns the next
device; the walk remembers where it branched and returns None once every device
has been named.
§Examples
use pamoja_hal::onewire::{OneWireBus, RomCode, Search};
fn thermometers<B: OneWireBus>(bus: &mut B) -> Result<Vec<RomCode>, B::Error> {
let mut found = Vec::new();
let mut search = Search::new();
while let Some(rom) = search.next(bus).map_err(|error| match error {
pamoja_hal::onewire::OneWireError::Pin(error) => error,
_ => unreachable!("a bus with no devices or a bad CRC ends the search"),
})? {
if rom.family() == 0x28 {
found.push(rom);
}
}
Ok(found)
}Implementations§
Source§impl Search
impl Search
Sourcepub fn alarms() -> Search
pub fn alarms() -> Search
Starts a search that visits only the devices with an alarm condition.
§Returns
The search, positioned before the first alarming device.
Sourcepub fn next<B: OneWireBus>(
&mut self,
bus: &mut B,
) -> Result<Option<RomCode>, OneWireError<B::Error>>
pub fn next<B: OneWireBus>( &mut self, bus: &mut B, ) -> Result<Option<RomCode>, OneWireError<B::Error>>
Finds the next device on bus.
§Arguments
bus- the bus to search.
§Returns
The next device’s ROM code, or None when every device has been returned or no
device answered the reset.
§Errors
Returns OneWireError::Crc if a ROM code arrived corrupted, which happens
when a device joins or drops mid-search, or the controller’s error.