use xum1541::{BusBuilder, CommunicationError, DeviceChannel, Error};
mod common;
use common::AppError;
use env_logger;
#[derive(PartialEq)]
enum Mode {
Listen,
Talk,
}
impl std::fmt::Display for Mode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Mode::Listen => write!(f, "Listen"),
Mode::Talk => write!(f, "Talk"),
}
}
}
fn main() -> Result<(), AppError> {
env_logger::init();
let mut bus = BusBuilder::new().build()?;
bus.initialize()?;
bus.reset()?;
let mut drive = 8;
let max_drive = 11;
let channel = 15;
let mut mode = Mode::Listen;
println!("Test for drives {drive} to {max_drive}");
loop {
let dc = DeviceChannel::new(drive, channel)?;
let start = std::time::Instant::now();
let result = match mode {
Mode::Listen => bus.listen(dc),
Mode::Talk => bus.talk(dc),
};
let elapsed = start.elapsed();
match result {
Err(Error::Communication {
kind: CommunicationError::StatusValue { value: _ },
}) => {
println!(
"Drive {drive} not detected via {mode} - check took {}ms",
elapsed.as_millis()
);
}
Err(e) => {
println!("Hit unexpected error trying to detect drive {drive} via {mode}: {e} - check took {}ms", elapsed.as_millis());
}
Ok(_) => {
let result = match mode {
Mode::Listen => bus.unlisten(),
Mode::Talk => bus.untalk(),
};
match result {
Err(e) => {
println!("Hit unexpected error trying to detect drive {drive} via {mode}: {e} - check took {}ms", elapsed.as_millis());
}
Ok(_) => (),
}
println!(
"Drive {drive} detected via {mode} - check took {}ms",
elapsed.as_millis()
);
}
}
drive += 1;
if mode == Mode::Listen {
mode = Mode::Talk;
} else {
mode = Mode::Listen;
}
if drive > max_drive {
break;
} else {
continue;
}
}
Ok(())
}