Fix avr crate and add retry as parameter

This commit is contained in:
hodasemi 2023-03-25 15:41:52 +01:00
parent 7b95000dc0
commit 1bfa5d3482
3 changed files with 21 additions and 8 deletions

View file

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
panic-halt = "0.2.0" panic-halt = "0.2.0"
avr-device = "0.5.0" avr-device = { version = "0.5.0", features = ["atmega328p"] }
arduino-hal = { git = "https://github.com/Rahix/avr-hal", features = ["arduino-uno"], rev = "1aacefb335517f85d0de858231e11055d9768cdf" } arduino-hal = { git = "https://github.com/Rahix/avr-hal", features = ["arduino-uno"], rev = "1aacefb335517f85d0de858231e11055d9768cdf" }
ufmt = "0.2.0" ufmt = "0.2.0"

View file

@ -104,7 +104,7 @@ fn main() {
timeout: Duration::from_millis(2500), timeout: Duration::from_millis(2500),
}; };
if let Ok(port) = Port::open(usb_id, serialport_settings) { if let Ok(port) = Port::open(usb_id, serialport_settings, 50, 1) {
CONNECTED.store(true, SeqCst); CONNECTED.store(true, SeqCst);
*PORT.lock().unwrap() = Some(port); *PORT.lock().unwrap() = Some(port);
@ -130,7 +130,15 @@ fn main() {
rx.attach(None, move |message| { rx.attach(None, move |message| {
if !message.is_empty() { if !message.is_empty() {
buffer.insert(&mut buffer.end_iter(), &format!("{}\n", message)); match message.parse::<u32>() {
Ok(n) => {
buffer.insert(&mut buffer.end_iter(), &format!("{:#02x}\n", n));
}
Err(_) => {
buffer.insert(&mut buffer.end_iter(), &format!("{}\n", message));
}
}
serial_info.scroll_to_iter(&mut buffer.end_iter(), 0.0, true, 0.0, 0.0); serial_info.scroll_to_iter(&mut buffer.end_iter(), 0.0, true, 0.0, 0.0);
} }

View file

@ -30,8 +30,13 @@ pub struct Port {
} }
impl Port { impl Port {
pub fn open(usb_device: UsbId, settings: SerialPortSettings) -> Result<Self> { pub fn open(
let port_path = Self::loop_usb_devices(usb_device)?; usb_device: UsbId,
settings: SerialPortSettings,
retries: usize,
wait_time: u64,
) -> Result<Self> {
let port_path = Self::loop_usb_devices(usb_device, retries, wait_time)?;
println!("found device at path {}", port_path); println!("found device at path {}", port_path);
@ -68,8 +73,8 @@ impl Port {
Ok(()) Ok(())
} }
fn loop_usb_devices(usb_device: UsbId) -> Result<String> { fn loop_usb_devices(usb_device: UsbId, retries: usize, wait_time: u64) -> Result<String> {
for _ in 0..3 { for _ in 0..retries {
if let Some(device) = Self::find_macroboard(&usb_device)? { if let Some(device) = Self::find_macroboard(&usb_device)? {
return Ok(device); return Ok(device);
} }
@ -79,7 +84,7 @@ impl Port {
usb_device.vendor_id, usb_device.product_id usb_device.vendor_id, usb_device.product_id
); );
std::thread::sleep(Duration::from_secs(2)); std::thread::sleep(Duration::from_secs(wait_time));
} }
Err(anyhow!("no device found")) Err(anyhow!("no device found"))