From ce1ddbc4b94935d268830334264f272ed4b2a123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Fri, 2 Feb 2024 11:37:37 +0100 Subject: [PATCH] refactor [nfc] --- src/hardware/metadata.rs | 4 +- src/main.rs | 98 ++++++++++++++++------------------------ src/net/mod.rs | 17 +++++-- src/output_channel.rs | 2 +- 4 files changed, 56 insertions(+), 65 deletions(-) diff --git a/src/hardware/metadata.rs b/src/hardware/metadata.rs index 8e6a5c9..d022c72 100644 --- a/src/hardware/metadata.rs +++ b/src/hardware/metadata.rs @@ -6,6 +6,7 @@ mod build_info { #[derive(Serialize)] pub struct ApplicationMetadata { + pub app: &'static str, pub firmware_version: &'static str, pub rust_version: &'static str, pub profile: &'static str, @@ -23,7 +24,8 @@ impl ApplicationMetadata { /// A reference to the global metadata. pub fn new() -> &'static ApplicationMetadata { cortex_m::singleton!(: ApplicationMetadata = ApplicationMetadata { - firmware_version: build_info::GIT_VERSION.unwrap_or("Unspecified"), + app: env!("CARGO_BIN_NAME"), + firmware_version: build_info::GIT_VERSION.unwrap_or(build_info::PKG_VERSION), rust_version: build_info::RUSTC_VERSION, profile: build_info::PROFILE, git_dirty: build_info::GIT_DIRTY.unwrap_or(false), diff --git a/src/main.rs b/src/main.rs index 0b2c85a..93e7831 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,11 +70,8 @@ impl Default for Settings { fn default() -> Self { Self { telemetry_period: 1.0, - output_channel: [Default::default(); 4], - alarm: Alarm { - period: 1.0, - ..Default::default() - }, + output_channel: Default::default(), + alarm: Default::default(), } } } @@ -99,7 +96,7 @@ pub struct Monitor { } /// Thermostat-EEM Telemetry. -#[derive(Serialize, Copy, Clone, Default, Debug)] +#[derive(Serialize, Copy, Clone, Debug, Default)] pub struct Telemetry { /// see [Monitor] monitor: Monitor, @@ -117,6 +114,7 @@ mod app { #[monotonic(binds = SysTick, default = true)] type Mono = Systick<1_000>; // 1ms resolution + #[shared] struct Shared { network: NetworkUsers, @@ -139,79 +137,62 @@ mod app { #[init] fn init(c: init::Context) -> (Shared, Local, init::Monotonics) { // Initialize monotonic - let systick = c.core.SYST; let clock = SystemTimer::new(|| monotonics::now().ticks()); // setup Thermostat hardware let thermostat = hardware::setup::setup(c.device, clock); - let mono = Systick::new(systick, thermostat.clocks.sysclk().to_Hz()); - - let local = Local { - adc_sm: thermostat.adc_sm, - pwm: thermostat.pwm, - adc_internal: thermostat.adc_internal, - iir_state: Default::default(), - dac: thermostat.dac, - }; - - let mut settings = Settings::default(); - - // Initialize enabled temperatures, statistics buffers and alarm. - let mut telemetry = Telemetry::default(); - for phy_i in 0..4 { - for ch_i in 0..4 { - if thermostat.adc_channels[phy_i][ch_i] { - telemetry.alarm[phy_i][ch_i] = Some(false); - telemetry.statistics[phy_i][ch_i] = Some(Default::default()); - settings.alarm.temperature_limits[phy_i][ch_i] = - Some([f32::NEG_INFINITY, f32::INFINITY]); - // Initialize the output weights. - for ch in settings.output_channel.iter_mut() { - ch.weights[phy_i][ch_i] = 0.; - } - } - } - } + let settings = Settings::default(); - let id = { - let mut mac_addr = heapless::String::<64>::new(); - write!( - &mut mac_addr, - "{}-{}", - env!("CARGO_BIN_NAME"), - thermostat.net.mac_address - ) - .unwrap(); - mac_addr - }; + let mut id = heapless::String::<64>::new(); + write!( + &mut id, + "{}-{}", + thermostat.metadata.app, thermostat.net.mac_address + ) + .unwrap(); let network = NetworkUsers::new( thermostat.net.stack, thermostat.net.phy, clock, - env!("CARGO_BIN_NAME"), &id, option_env!("BROKER").unwrap_or("mqtt"), settings.clone(), thermostat.metadata, ); - settings_update::spawn(settings.clone()).unwrap(); - ethernet_link::spawn().unwrap(); - telemetry_task::spawn().unwrap(); - mqtt_alarm::spawn().unwrap(); + let local = Local { + adc_sm: thermostat.adc_sm, + pwm: thermostat.pwm, + adc_internal: thermostat.adc_internal, + iir_state: Default::default(), + dac: thermostat.dac, + }; let shared = Shared { network, - settings, - telemetry, + settings: settings.clone(), + telemetry: Default::default(), gpio: thermostat.gpio, temperature: Default::default(), statistics_buff: Default::default(), }; - (shared, local, init::Monotonics(mono)) + // Apply initial settings + settings_update::spawn(settings).unwrap(); + ethernet_link::spawn().unwrap(); + telemetry_task::spawn().unwrap(); + mqtt_alarm::spawn().unwrap(); + + ( + shared, + local, + init::Monotonics(Systick::new( + c.core.SYST, + thermostat.clocks.sysclk().to_Hz(), + )), + ) } #[idle(shared=[network])] @@ -287,10 +268,9 @@ mod app { // Finalize temperature telemetry and reset buffer for phy_i in 0..4 { for cfg_i in 0..4 { - let stat = &mut telemetry.statistics[phy_i][cfg_i]; - if stat.is_some() { + if let Some(stat) = &mut telemetry.statistics[phy_i][cfg_i] { c.shared.statistics_buff.lock(|buff| { - *stat = Some(buff[phy_i][cfg_i].into()); + *stat = buff[phy_i][cfg_i].into(); buff[phy_i][cfg_i] = Default::default(); }); } @@ -311,11 +291,11 @@ mod app { let alarm = c.shared.settings.lock(|settings| settings.alarm.clone()); if alarm.armed { let temperatures = c.shared.temperature.lock(|temp| *temp); - let mut alarms: [[Option; 4]; 4] = Default::default(); + let mut alarms = [[None; 4]; 4]; let mut alarm_state = false; for phy_i in 0..4 { for cfg_i in 0..4 { - if let Some(l) = alarm.temperature_limits[phy_i][cfg_i] { + if let Some(l) = &alarm.temperature_limits[phy_i][cfg_i] { let a = !(l[0]..l[1]).contains(&(temperatures[phy_i][cfg_i] as _)); alarms[phy_i][cfg_i] = Some(a); alarm_state |= a; diff --git a/src/net/mod.rs b/src/net/mod.rs index ae9a17e..e357d66 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -80,7 +80,6 @@ where /// * `stack` - The network stack that will be used to share with all network users. /// * `phy` - The ethernet PHY connecting the network. /// * `clock` - System timer clock. - /// * `app` - The name of the application. /// * `mac` - The MAC address of the network. /// * `broker` - The IP address of the MQTT broker to use. /// * `id` - The MQTT client ID base to use. @@ -93,7 +92,6 @@ where stack: NetworkStack, phy: EthernetPhy, clock: SystemTimer, - app: &str, id: &str, broker: &str, settings: S, @@ -104,7 +102,7 @@ where let processor = NetworkProcessor::new(stack_manager.acquire_stack(), phy); - let prefix = get_device_prefix(app, id); + let prefix = get_device_prefix(metadata.app, id); let store = cortex_m::singleton!(: MqttStorage = MqttStorage::default()).unwrap(); @@ -215,7 +213,7 @@ pub fn get_device_prefix(app: &str, id: &str) -> String<128> { /// /// The alarm is non-latching. If alarm was "true" for a while and the temperatures come within /// limits again, alarm will be "false" again. -#[derive(Clone, Debug, Tree, Default)] +#[derive(Clone, Debug, Tree)] pub struct Alarm { /// Set the alarm to armed (true) or disarmed (false). /// If the alarm is armed, the device will publish it's alarm state onto the [target]. @@ -255,3 +253,14 @@ pub struct Alarm { #[tree(depth(4))] pub temperature_limits: [[Option<[f32; 2]>; 4]; 4], } + +impl Default for Alarm { + fn default() -> Self { + Self { + armed: false, + target: Default::default(), + period: 1.0, + temperature_limits: [[Some([f32::NEG_INFINITY, f32::INFINITY]); 4]; 4], + } + } +} diff --git a/src/output_channel.rs b/src/output_channel.rs index 64cf217..59bfb22 100644 --- a/src/output_channel.rs +++ b/src/output_channel.rs @@ -55,7 +55,7 @@ impl Default for OutputChannel { shutdown: true, hold: false, voltage_limit: 0.0, - iir: iir::Biquad::default(), + iir: Default::default(), weights: [[0.0; 4]; 4], } }