Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructuring channel power state to an enum #183

Merged
merged 10 commits into from
Feb 3, 2022
14 changes: 9 additions & 5 deletions src/hardware/rf_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use minimq::embedded_time::{duration::Extensions, Clock, Instant};

use super::{clock::SystemTimer, platform, I2cBusManager, I2cProxy};
use crate::{
settings::{channel_settings::ChannelSettings, BoosterChannelSettings},
settings::{
channel_settings::ChannelSettings, channel_settings::ChannelState, BoosterChannelSettings,
},
Error,
};
use embedded_hal::blocking::delay::DelayUs;
Expand Down Expand Up @@ -708,7 +710,7 @@ impl sm::StateMachineContext for RfChannel {
}

// Do not enable output if it shouldn't be disabled due to settings.
if !settings.enabled || settings.rf_disable {
if !matches!(settings.power_state, ChannelState::Enabled) {
return Err(());
}

Expand Down Expand Up @@ -844,11 +846,13 @@ impl sm::StateMachine<RfChannel> {
pub fn handle_settings(&mut self, settings: &ChannelSettings) -> Result<(), Error> {
self.context_mut().apply_settings(settings)?;

if !settings.enabled {
if !matches!(settings.power_state, ChannelState::Enabled) {
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved
// If settings has us disabled, it's always okay to blindly power down.
self.process_event(sm::Events::Disable).ok();
} else if settings.enabled != self.context().pins.enable_power.is_high().unwrap()
|| settings.rf_disable != self.context().pins.signal_on.is_low().unwrap()
} else if matches!(settings.power_state, ChannelState::Enabled)
!= self.context().pins.enable_power.is_high().unwrap()
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved
|| matches!(settings.power_state, ChannelState::Powered)
!= self.context().pins.signal_on.is_low().unwrap()
{
// Our current power state has a mismatch with the settings. Reset ourselves into the
// updated state.
Expand Down
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use hardware::{
Channel, CPU_FREQ,
};

use settings::channel_settings::ChannelSettings;
use settings::channel_settings::{ChannelSettings, ChannelState};

use watchdog::{WatchdogClient, WatchdogManager};

Expand Down Expand Up @@ -95,8 +95,7 @@ const APP: () = {
.channel_mut(idx)
.map(|(channel, _)| settings.channel[idx as usize] = *channel.context().settings())
.unwrap_or_else(|| {
settings.channel[idx as usize].enabled = false;
settings.channel[idx as usize].rf_disable = true;
settings.channel[idx as usize].power_state = ChannelState::Off;
settings.channel[idx as usize].bias_voltage = 0.0;
});
}
Expand Down
30 changes: 21 additions & 9 deletions src/settings/channel_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@ const EXPECTED_VERSION: SemVersion = SemVersion {
patch: 1,
};

/// Indicates the enabled state of a channel.
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved
#[derive(serde::Serialize, serde::Deserialize, Miniconf, Copy, Clone, PartialEq)]
pub enum ChannelState {
Off = 0,
// For compatibility reasons, Enabled is stored with the value equivalent to "true"
Enabled = 1,

Powered = 2,
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved
}

/// Represents booster channel-specific configuration values.
#[derive(Miniconf, serde::Serialize, serde::Deserialize, Copy, Clone, PartialEq)]
pub struct ChannelSettings {
pub output_interlock_threshold: f32,
pub bias_voltage: f32,
pub enabled: bool,
pub power_state: ChannelState,
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved
pub input_power_transform: LinearTransformation,
pub output_power_transform: LinearTransformation,
pub reflected_power_transform: LinearTransformation,

// Note: This field is not persisted to external memory.
#[serde(skip)]
pub rf_disable: bool,
}

impl Default for ChannelSettings {
Expand All @@ -39,8 +45,7 @@ impl Default for ChannelSettings {
Self {
output_interlock_threshold: 0.0,
bias_voltage: -3.2,
enabled: false,
rf_disable: false,
power_state: ChannelState::Off,

// When operating at 100MHz, the power detectors specify the following output
// characteristics for -10 dBm to 10 dBm (the equation uses slightly different coefficients
Expand All @@ -63,7 +68,7 @@ impl Default for ChannelSettings {
}

/// Represents versioned channel-specific configuration values.
#[derive(serde::Serialize, serde::Deserialize)]
#[derive(serde::Serialize, serde::Deserialize, Copy, Clone)]
struct VersionedChannelData {
version: SemVersion,
settings: ChannelSettings,
Expand Down Expand Up @@ -108,8 +113,15 @@ impl VersionedChannelData {
/// # Args
/// * `config` - The sinara configuration to serialize the booster configuration into.
pub fn serialize_into(&self, config: &mut SinaraConfiguration) {
// We will never store `Powered` in EEPROM, since this is never desired. Cache the current
// power state while we serialize to ensure we only serialize Enabled and Off.
let mut versioned_copy = *self;
if matches!(versioned_copy.settings.power_state, ChannelState::Powered) {
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved
versioned_copy.settings.power_state = ChannelState::Off;
}

let mut buffer: [u8; 64] = [0; 64];
let serialized = postcard::to_slice(self, &mut buffer).unwrap();
let serialized = postcard::to_slice(&versioned_copy, &mut buffer).unwrap();
config.board_data[..serialized.len()].copy_from_slice(serialized);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use channel_settings::BoosterChannelSettings;
pub use global_settings::BoosterSettings;

/// A semantic version control for recording software versions.
#[derive(serde::Serialize, serde::Deserialize, PartialEq)]
#[derive(serde::Serialize, serde::Deserialize, PartialEq, Copy, Clone)]
pub struct SemVersion {
major: u8,
minor: u8,
Expand Down