This commit is contained in:
2025-10-26 09:24:49 -07:00
parent 5455935f3a
commit eefc3293b4
36 changed files with 886 additions and 516 deletions

View File

@@ -29,7 +29,12 @@ where
I2C::Error: 'static,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Mcp23017Driver<I2C={}> {{ address: {} }}", type_name::<I2C>(), self.address)
write!(
f,
"Mcp23017Driver<I2C={}> {{ address: {} }}",
type_name::<I2C>(),
self.address
)
}
}
@@ -41,7 +46,10 @@ where
I2C::Error: 'static,
{
pub fn new(i2c: I2C, address: u8) -> Self {
trace!("Mcp23017Driver<I2C={}>::new(i2c, address: 0x{address:02x})", type_name::<I2C>());
trace!(
"Mcp23017Driver<I2C={}>::new(i2c, address: 0x{address:02x})",
type_name::<I2C>()
);
Self {
i2c: i2c.into(),
address,
@@ -62,7 +70,7 @@ where
trace!("Mcp23017Driver::drop(self: {self:?})");
self.bank = 0; // We want all pins to be set back to 0
if let Err(e) = self.flush() {
error!("Mcp23017Driver: Failed to flush on drop. {self:?} Error: {e}")
error!("Mcp23017Driver: Failed to flush on drop. {self:?} Error: {e}");
}
}
}

View File

@@ -1,5 +1,5 @@
mod task;
mod driver;
mod task;
use anyhow::Result;
use embedded_hal::digital::PinState;

View File

@@ -107,37 +107,45 @@ impl PinData {
// Do this twice to check both the current and the current next
// If the current is currently invalid, we'd upgrade the next to current
for _ in 0..2 {
let is_current_valid = self.valid_until.map(|current| current >= now).unwrap_or(false);
let is_current_valid = self
.valid_until
.is_some_and(|current| current >= now);
if is_current_valid {
self.value = self.state;
return;
} else {
if self.valid_until.is_some() {
self.changed = true;
}
self.state = self.next_state;
self.valid_until = self.next_validity;
self.priority = self.next_priority;
self.next_validity = None;
self.next_priority = 0;
}
if self.valid_until.is_some() {
self.changed = true;
}
self.state = self.next_state;
self.valid_until = self.next_validity;
self.priority = self.next_priority;
self.next_validity = None;
self.next_priority = 0;
}
self.value = self.default;
}
fn set(&mut self, value: PinState, valid_until: Instant, priority: u8) {
trace!("PinData::set(self: {self:?}, value: {value:?}, valid_until: {valid_until:?}, priority: {priority})");
let can_replace_current = self.valid_until.map(|current| current <= valid_until).unwrap_or(true);
let can_replace_next = self.next_validity.map(|next| next <= valid_until).unwrap_or(true);
trace!(
"PinData::set(self: {self:?}, value: {value:?}, valid_until: {valid_until:?}, priority: {priority})"
);
let can_replace_current = self
.valid_until
.is_none_or(|current| current <= valid_until);
let can_replace_next = self
.next_validity
.is_none_or(|next| next <= valid_until);
if priority >= self.priority {
// This is now the highest priority thing (or most recent of equal priority)
if can_replace_current {
if can_replace_next {
self.next_validity = None;
self.next_priority = 0
self.next_priority = 0;
}
} else {
self.next_state = self.state;
@@ -182,15 +190,11 @@ impl<M: Mcp23017 + Debug> CyclicTask for Mcp23017Task<'_, M> {
fn get_data(&self) -> Self::Data {
trace!("Mcp23017Task::get_data(self: {self:?})");
Self::Data {
id: self.state.get_identifier()
id: self.state.get_identifier(),
}
}
fn step(
&mut self,
receiver: &Receiver<Self::Message>,
step_time: Instant,
) {
fn step(&mut self, receiver: &Receiver<Self::Message>, step_time: Instant) {
trace!("Mcp23017Task::step(self: {self:?}, receiver, step_time: {step_time:?})");
let mut changed = false;
@@ -200,7 +204,12 @@ impl<M: Mcp23017 + Debug> CyclicTask for Mcp23017Task<'_, M> {
while let Ok(recv) = receiver.try_recv() {
match recv {
Mcp23017Message::SetPin { pin, value, valid_until, priority } => {
Mcp23017Message::SetPin {
pin,
value,
valid_until,
priority,
} => {
if (0u8..16u8).contains(&pin) {
self.pins.pins[pin as usize].set(value, valid_until, priority);
}