Skip to content

Commit

Permalink
refactor [nfc]
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens committed Feb 2, 2024
1 parent 14a4a03 commit ce1ddbc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 65 deletions.
4 changes: 3 additions & 1 deletion src/hardware/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
98 changes: 39 additions & 59 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand All @@ -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,
Expand All @@ -117,6 +114,7 @@ mod app {

#[monotonic(binds = SysTick, default = true)]
type Mono = Systick<1_000>; // 1ms resolution

#[shared]
struct Shared {
network: NetworkUsers<Settings, Telemetry, 6>,
Expand All @@ -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])]
Expand Down Expand Up @@ -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();
});
}
Expand All @@ -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<bool>; 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;
Expand Down
17 changes: 13 additions & 4 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -93,7 +92,6 @@ where
stack: NetworkStack,
phy: EthernetPhy,
clock: SystemTimer,
app: &str,
id: &str,
broker: &str,
settings: S,
Expand All @@ -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();

Expand Down Expand Up @@ -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].
Expand Down Expand Up @@ -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],
}
}
}
2 changes: 1 addition & 1 deletion src/output_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
}
}
Expand Down

0 comments on commit ce1ddbc

Please sign in to comment.