diff --git a/fan-controller/src/main.rs b/fan-controller/src/main.rs index c63a027..cdb0681 100644 --- a/fan-controller/src/main.rs +++ b/fan-controller/src/main.rs @@ -775,17 +775,26 @@ async fn fan_control_routine( ) { let modbus_mutex = modbus.get().await; + let fan_identifier = match *fan_address { + 2 => "[Fan 1]", + 3 => "[Fan 2]", + other => "Unknown (oops)", + }; + //TODO load initial fan speed through modbus from fan and make current_speed non optional let mut current_set_point: Option = None; 'signal_loop: loop { let mut set_point = current_fan_speed.wait().await; if current_set_point.is_some_and(|speed| speed == speed) { //TODO consider to update fan display state nontheless - info!("Fan state update received but has same state"); + info!( + "{} Fan state update received but has same state", + fan_identifier + ); continue; } - info!("Received fan state"); + info!("{} Received fan state", fan_identifier); // Instruct modbus to send update let mut modbus = modbus_mutex.lock().await; @@ -806,7 +815,10 @@ async fn fan_control_routine( // Release lock so other tasks get a chance to access modbus for sending messages to devices drop(modbus); - error!("Failed to send fan state update with attempt {}", attempt); + error!( + "{} Failed to send fan state update with attempt {}", + fan_identifier, attempt + ); attempt += 1; if current_fan_speed.signaled() { @@ -821,8 +833,8 @@ async fn fan_control_routine( if attempt > MAX_ATTEMPTS { error!( - "Failed to send fan state update after {} attempts", - MAX_ATTEMPTS + "{} Failed to send fan state update after {} attempts", + fan_identifier, MAX_ATTEMPTS ); //TODO don't try to update other fan speed if we have a setting to allow fans to run out of sync @@ -836,7 +848,10 @@ async fn fan_control_routine( continue; } - info!("Fan state updated after {} attempts", attempt); + info!( + "{} Fan state updated after {} attempts", + fan_identifier, attempt + ); // On success send update to fan display logic unit display_state.send_if_modified(|current| { diff --git a/fan-controller/src/modbus/client.rs b/fan-controller/src/modbus/client.rs index dae8dcb..8535e3f 100644 --- a/fan-controller/src/modbus/client.rs +++ b/fan-controller/src/modbus/client.rs @@ -123,22 +123,29 @@ impl<'a, UART: uart::Instance, PIN: Pin> Client<'a, UART, PIN> { } pub(crate) async fn send_3(&mut self, message: &WriteHoldingRegister) -> Result<(), Error> { + // For debugging + let fan_identifier = match *message.device_address() { + 2 => "[Fan 1]", + 3 => "[Fan 2]", + other => "Unknown (oops)", + }; + // Write then read // Set pin setting DE (driver enable) to on (high) on the MAX845 to send data self.driver_enable.set_high(); let bytes = message.as_ref(); - info!("Sending message to fan: {:?}", bytes); + info!("{} Sending message to fan: {:?}", fan_identifier, bytes); // As ref because &[u8; 8] is not the same as &[u8] let result = with_timeout(configuration::FAN_TIMEOUT, self.uart.write_all(&bytes)).await?; - info!("uart write result: {:?}", result); + info!("{} UART write result: {:?}", fan_identifier, result); // Before closing we need to flush the buffer to ensure that all data is written // This requires blocking or we get a WouldBlock error. I don't understand why (TODO) let result = self.uart.blocking_flush(); if let Err(error) = result { - error!("uart flush error"); + error!("{} UART flush error", fan_identifier); } // In addition to flushing we need to wait for some time before turning off data in on the @@ -157,7 +164,7 @@ impl<'a, UART: uart::Instance, PIN: Pin> Client<'a, UART, PIN> { // Read // Read response from fan. The response can vary in length let mut response_buffer: [u8; 8] = [0; 8]; - info!("Waiting for response from fan"); + info!("{} Waiting for response from fan", fan_identifier); let bytes_read = with_timeout( configuration::FAN_TIMEOUT, //TODO test this does not wait for bytes to fill the buffer @@ -166,7 +173,10 @@ impl<'a, UART: uart::Instance, PIN: Pin> Client<'a, UART, PIN> { ) .await??; - info!("response from fan: {:?} {:?}", bytes_read, response_buffer); + info!( + "{} Response from fan: {:?} {:?}", + fan_identifier, bytes_read, response_buffer + ); //TODO validate response from fan // Read the correct number of bytes diff --git a/fan-controller/src/modbus/function/write_holding_register.rs b/fan-controller/src/modbus/function/write_holding_register.rs index e0ce8bf..8722108 100644 --- a/fan-controller/src/modbus/function/write_holding_register.rs +++ b/fan-controller/src/modbus/function/write_holding_register.rs @@ -29,6 +29,10 @@ impl WriteHoldingRegister { data[7] = checksum[0]; Self(data) } + + pub(crate) fn device_address(&self) -> modbus::device::Address { + self.0[0].into() + } } impl AsRef<[u8]> for WriteHoldingRegister {