Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
leoleo73 committed Feb 27, 2024
2 parents c36828c + a72afac commit fdfdf97
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/io/gpio/sysfs_gpio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::io::gpio::{GPIOError, GPIOManager, GPIOMode, GPIOState, PinUpdate};
use log::{debug, error, trace, warn};
use std::collections::HashMap;
use std::{collections::HashMap, thread::sleep, time::Duration};
use sysfs_gpio::{Direction, Error, Pin};
use tokio::sync::mpsc::Sender;

Expand Down Expand Up @@ -48,9 +48,22 @@ impl GPIOManager for SysFsGPIO {
self.gpios.insert(pin_id, pin);
return Ok(());
}
warn!("Actually having to set direction of pin {}", pin_id);
pin.set_direction(direction)
.expect("Expected to be able to set direction of pin");

const MAX_ATTEMPTS: usize = 5;
let mut attempt = 0;
while let Err(e) = pin.set_direction(direction) {
warn!(
"Failed to set direction of pin {} - Attempt {}",
pin_id, attempt
);
if attempt >= MAX_ATTEMPTS {
return Err(e.into());
}
attempt += 1;
sleep(Duration::from_millis(400));
}

warn!("Set direction of pin {} on attempt {}", pin_id, attempt);
self.gpios.insert(pin_id, pin);
Ok(())
}
Expand Down Expand Up @@ -113,4 +126,3 @@ impl From<sysfs_gpio::Error> for GPIOError {
}
}
}

0 comments on commit fdfdf97

Please sign in to comment.