Renaming variables, moving broker parsing

master
Ryan Summers 2021-08-02 16:13:01 +02:00
parent d2b184e8e9
commit c4c3593bae
4 changed files with 47 additions and 50 deletions

View File

@ -40,7 +40,6 @@ use stabilizer::{
adc::{Adc0Input, Adc1Input, AdcCode},
afe::Gain,
dac::{Dac0Output, Dac1Output, DacCode},
design_parameters::DEFAULT_MQTT_BROKER,
embedded_hal::digital::v2::InputPin,
hal,
signal_generator::{self, SignalGenerator},
@ -48,6 +47,7 @@ use stabilizer::{
DigitalInput0, DigitalInput1, AFE0, AFE1,
},
net::{
self,
data_stream::{FrameGenerator, StreamFormat, StreamTarget},
miniconf::Miniconf,
serde::Deserialize,
@ -62,7 +62,7 @@ const SCALE: f32 = i16::MAX as _;
const IIR_CASCADE_LENGTH: usize = 1;
// The number of samples in each batch process
const SAMPLE_BUFFER_SIZE: usize = 8;
const BATCH_SIZE: usize = 8;
// The logarithm of the number of 100MHz timer ticks between each sample. With a value of 2^7 =
// 128, there is 1.28uS per sample, corresponding to a sampling frequency of 781.25 KHz.
@ -190,7 +190,7 @@ const APP: () = {
let (mut stabilizer, _pounder) = hardware::setup::setup(
c.core,
c.device,
SAMPLE_BUFFER_SIZE,
BATCH_SIZE,
1 << SAMPLE_TICKS_LOG2,
);
@ -200,27 +200,11 @@ const APP: () = {
stabilizer.cycle_counter,
env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address,
option_env!("BROKER")
.and_then(|data| {
data.parse::<minimq::embedded_nal::IpAddr>().map_or_else(
|err| {
log::error!(
"{:?}: Failed to parse broker IP ({:?}) - Falling back to default",
err,
data
);
None
},
|ip| Some(ip),
)
})
.unwrap_or(DEFAULT_MQTT_BROKER.into()),
net::parse_or_default_broker(option_env!("BROKER")),
);
let generator = network.configure_streaming(
StreamFormat::AdcDacData,
SAMPLE_BUFFER_SIZE as u8,
);
let generator = network
.configure_streaming(StreamFormat::AdcDacData, BATCH_SIZE as u8);
// Spawn a settings update for default settings.
c.spawn.settings_update().unwrap();
@ -338,7 +322,7 @@ const APP: () = {
}
// Stream the data.
const N: usize = SAMPLE_BUFFER_SIZE * core::mem::size_of::<u16>();
const N: usize = BATCH_SIZE * core::mem::size_of::<u16>();
generator.add::<_, { N * 4 }>(|buf| {
for (data, buf) in adc_samples
.iter()

View File

@ -42,7 +42,6 @@ use stabilizer::{
adc::{Adc0Input, Adc1Input, AdcCode},
afe::Gain,
dac::{Dac0Output, Dac1Output, DacCode},
design_parameters::DEFAULT_MQTT_BROKER,
embedded_hal::digital::v2::InputPin,
hal,
input_stamper::InputStamper,
@ -51,6 +50,7 @@ use stabilizer::{
DigitalInput0, DigitalInput1, AFE0, AFE1,
},
net::{
self,
data_stream::{FrameGenerator, StreamFormat, StreamTarget},
miniconf::Miniconf,
serde::Deserialize,
@ -61,7 +61,7 @@ use stabilizer::{
// The logarithm of the number of samples in each batch process. This corresponds with 2^3 samples
// per batch = 8 samples
const SAMPLE_BUFFER_SIZE_LOG2: u8 = 3;
const BATCH_SIZE_SIZE_LOG2: u8 = 3;
// The logarithm of the number of 100MHz timer ticks between each sample. This corresponds with a
// sampling period of 2^7 = 128 ticks. At 100MHz, 10ns per tick, this corresponds to a sampling
@ -231,7 +231,7 @@ const APP: () = {
let (mut stabilizer, _pounder) = hardware::setup::setup(
c.core,
c.device,
1 << SAMPLE_BUFFER_SIZE_LOG2,
1 << BATCH_SIZE_SIZE_LOG2,
1 << ADC_SAMPLE_TICKS_LOG2,
);
@ -241,31 +241,17 @@ const APP: () = {
stabilizer.cycle_counter,
env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address,
option_env!("BROKER")
.and_then(|data| {
data.parse::<minimq::embedded_nal::IpAddr>().map_or_else(
|err| {
log::error!(
"{:?}: Failed to parse broker IP ({:?}) - Falling back to default",
err,
data
);
None
},
|ip| Some(ip),
)
})
.unwrap_or(DEFAULT_MQTT_BROKER.into()),
net::parse_or_default_broker(option_env!("BROKER")),
);
let generator = network.configure_streaming(
StreamFormat::AdcDacData,
1u8 << SAMPLE_BUFFER_SIZE_LOG2,
1u8 << BATCH_SIZE_SIZE_LOG2,
);
let settings = Settings::default();
let pll = RPLL::new(ADC_SAMPLE_TICKS_LOG2 + SAMPLE_BUFFER_SIZE_LOG2);
let pll = RPLL::new(ADC_SAMPLE_TICKS_LOG2 + BATCH_SIZE_SIZE_LOG2);
// Spawn a settings and telemetry update for default settings.
c.spawn.settings_update().unwrap();
@ -291,7 +277,7 @@ const APP: () = {
let signal_config = {
let frequency_tuning_word =
(1u64 << (32 - SAMPLE_BUFFER_SIZE_LOG2)) as u32;
(1u64 << (32 - BATCH_SIZE_SIZE_LOG2)) as u32;
signal_generator::Config {
// Same frequency as batch size.
@ -358,11 +344,11 @@ const APP: () = {
settings.pll_tc[0],
settings.pll_tc[1],
);
(pll_phase, (pll_frequency >> SAMPLE_BUFFER_SIZE_LOG2) as i32)
(pll_phase, (pll_frequency >> BATCH_SIZE_SIZE_LOG2) as i32)
}
LockinMode::Internal => {
// Reference phase and frequency are known.
(1i32 << 30, 1i32 << (32 - SAMPLE_BUFFER_SIZE_LOG2))
(1i32 << 30, 1i32 << (32 - BATCH_SIZE_SIZE_LOG2))
}
};
@ -417,7 +403,7 @@ const APP: () = {
// Stream the data.
const N: usize =
(1 << SAMPLE_BUFFER_SIZE_LOG2) * core::mem::size_of::<u16>();
(1 << BATCH_SIZE_SIZE_LOG2) * core::mem::size_of::<u16>();
generator.add::<_, { N * 4 }>(|buf| {
for (data, buf) in adc_samples
.iter()

View File

@ -44,6 +44,3 @@ pub const DDS_SYNC_CLK_DIV: u8 = 4;
pub const MAX_SAMPLE_BUFFER_SIZE: usize = 32;
pub type SampleBuffer = [u16; MAX_SAMPLE_BUFFER_SIZE];
/// The default MQTT broker IP address if unspecified.
pub const DEFAULT_MQTT_BROKER: [u8; 4] = [10, 34, 16, 10];

View File

@ -33,6 +33,9 @@ use smoltcp_nal::embedded_nal::SocketAddr;
pub type NetworkReference = shared::NetworkStackProxy<'static, NetworkStack>;
/// The default MQTT broker IP address if unspecified.
pub const DEFAULT_MQTT_BROKER: [u8; 4] = [10, 34, 16, 10];
#[derive(Copy, Clone, PartialEq)]
pub enum UpdateState {
NoChange,
@ -205,3 +208,30 @@ pub fn get_device_prefix(
prefix
}
/// Determine the broker IP address
///
/// # Note
/// If the broker IP is unspecified or unparseable, the default IP is returned.
///
/// # Args
/// * `input` - The optionally-specified command-line broker IP address as a string.
///
/// # Returns
/// The broker IP address.
pub fn parse_or_default_broker(input: Option<&str>) -> IpAddr {
input.and_then(|data| {
data.parse::<minimq::embedded_nal::IpAddr>().map_or_else(
|err| {
log::error!(
"{:?}: Failed to parse broker IP ({:?}) - Falling back to default",
err,
data
);
None
},
Some,
)
})
.unwrap_or_else(|| DEFAULT_MQTT_BROKER.into())
}