diff --git a/fan-controller/src/fan.rs b/fan-controller/src/fan.rs index 0e648fc..a4f4704 100644 --- a/fan-controller/src/fan.rs +++ b/fan-controller/src/fan.rs @@ -1,7 +1,6 @@ //! ebm-pabst [RadiCal centrifugal fans in scroll housings for residential ventilation](https://www.ebmpapst.com/us/en/campaigns/product-campaigns/centrifugal-fans/radical-with-scroll-housing.html) //! specific configuration and constants -use crate::string_buffer::StringBuffer; use crate::{configuration, modbus}; use cortex_m::prelude::_embedded_hal_serial_Write; use defmt::{error, info, Format}; diff --git a/fan-controller/src/main.rs b/fan-controller/src/main.rs index 311c53b..9b6aefd 100644 --- a/fan-controller/src/main.rs +++ b/fan-controller/src/main.rs @@ -46,7 +46,6 @@ use mqtt::{Encode, TryDecode}; use rand::RngCore; use reqwless::client::{TlsConfig, TlsVerify}; use static_cell::StaticCell; -use string_buffer::StringBuffer; use {defmt_rtt as _, panic_probe as _}; @@ -72,7 +71,6 @@ mod configuration; mod fan; mod modbus; mod mqtt; -mod string_buffer; bind_interrupts!(struct Irqs { PIO0_IRQ_0 => PioInterruptHandler; @@ -703,7 +701,15 @@ async fn mqtt_task( Message::PredefinedPublish(publish) => match publish { PredefinedPublish::FanPercentageState { setting } => { info!("Sending percentage state publish {}", setting); - let buffer: StringBuffer<5> = setting.into(); + // let buffer: StringBuffer<5> = setting.into(); + let buffer = match heapless::String::<5>::try_from(setting.0) { + Ok(buffer) => buffer, + Err(error) => { + error!("Error converting setting to string: {:?}", error); + continue; + } + }; + let packet = Publish { topic_name: "fancontroller/speed/percentage_state", payload: buffer.as_bytes(), diff --git a/fan-controller/src/string_buffer.rs b/fan-controller/src/string_buffer.rs deleted file mode 100644 index b979d92..0000000 --- a/fan-controller/src/string_buffer.rs +++ /dev/null @@ -1,37 +0,0 @@ -use crate::fan; - -/// Like a string with length and capacity of 5. Used for sending publish packets to Home Assistant through MQTT -pub(crate) struct StringBuffer { - buffer: [u8; N], - start_index: usize, -} - -impl StringBuffer { - pub(crate) fn as_bytes(&self) -> &[u8] { - &self.buffer[self.start_index..] - } -} - -impl From for StringBuffer<5> { - fn from(setting: fan::Setting) -> Self { - // The largest value the set point can assume is 64000 which is 5 characters long - let mut buffer = [0; 5]; - let mut index = 4; - let mut remainder = setting.0; - loop { - let digit = remainder % 10; - // Convert digit to ASCII (which is also valid utf-8) - buffer[index] = digit as u8 + b'0'; - remainder /= 10; - if remainder <= 0 { - break; - } - index -= 1; - } - - Self { - buffer, - start_index: index, - } - } -}