2020-09-21 17:31:34 +08:00
|
|
|
use nom::IResult;
|
2020-09-23 12:34:13 +08:00
|
|
|
use nom::combinator::{value, map, map_res, opt, all_consuming};
|
|
|
|
use nom::sequence::{terminated, preceded, pair};
|
|
|
|
use nom::bytes::complete::{tag, tag_no_case, take_while};
|
2020-09-21 17:31:34 +08:00
|
|
|
use nom::character::complete::digit1;
|
|
|
|
use nom::character::is_space;
|
2020-09-23 12:34:13 +08:00
|
|
|
use nom::branch::{permutation, alt};
|
2020-09-21 17:31:34 +08:00
|
|
|
use nom::number::complete::{float, double};
|
2020-09-29 17:02:15 +08:00
|
|
|
use heapless::{ Vec, String, consts::* };
|
2020-09-28 14:15:37 +08:00
|
|
|
use ryu;
|
2020-09-17 17:02:01 +08:00
|
|
|
use embedded_hal::blocking::spi::Transfer;
|
|
|
|
use core::convert::TryInto;
|
2020-09-24 17:32:53 +08:00
|
|
|
use crate::urukul::ClockSource as UrukulClockSource;
|
|
|
|
use crate::urukul::Urukul;
|
|
|
|
use crate::urukul::Error;
|
2020-12-18 17:42:26 +08:00
|
|
|
use crate::flash_store::{ FlashStore, update_flash };
|
|
|
|
use crate::flash::Flash;
|
|
|
|
use crate::config::{ UrukulConfig, ChannelConfig };
|
2020-09-17 17:02:01 +08:00
|
|
|
|
2020-09-21 17:31:34 +08:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum MqttTopic {
|
2020-09-23 09:42:14 +08:00
|
|
|
Reset,
|
2020-09-21 17:31:34 +08:00
|
|
|
Switch(u8),
|
|
|
|
Attenuation(u8),
|
2020-09-22 13:40:46 +08:00
|
|
|
Clock,
|
|
|
|
ClockSource,
|
|
|
|
ClockFrequency,
|
|
|
|
ClockDivision,
|
|
|
|
SystemClock(u8),
|
2020-09-21 17:31:34 +08:00
|
|
|
Singletone(u8, u8),
|
|
|
|
SingletoneFrequency(u8, u8),
|
|
|
|
SingletoneAmplitude(u8, u8),
|
|
|
|
SingletonePhase(u8, u8),
|
|
|
|
Profile,
|
2020-12-18 17:42:26 +08:00
|
|
|
Save,
|
|
|
|
Load,
|
2020-09-21 17:31:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prossible change: Make this enum public to all comm protocol (if any)
|
|
|
|
// Such that Urukul accepts the enum directly
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum MqttCommand {
|
2020-09-30 15:57:33 +08:00
|
|
|
ProcessError(&'static str),
|
2020-09-23 09:42:14 +08:00
|
|
|
Reset,
|
2020-09-17 17:02:01 +08:00
|
|
|
Switch(u8, bool),
|
|
|
|
Attenuation(u8, f32),
|
2020-09-22 13:40:46 +08:00
|
|
|
Clock(UrukulClockSource, f64, u8),
|
|
|
|
ClockSource(UrukulClockSource),
|
|
|
|
ClockFrequency(f64),
|
|
|
|
ClockDivision(u8),
|
|
|
|
SystemClock(u8, f64),
|
2020-09-21 17:31:34 +08:00
|
|
|
Singletone(u8, u8, f64, f64, f64),
|
|
|
|
SingletoneFrequency(u8, u8, f64),
|
|
|
|
SingletoneAmplitude(u8, u8, f64),
|
|
|
|
SingletonePhase(u8, u8, f64),
|
2020-12-18 17:42:26 +08:00
|
|
|
Profile(u8),
|
|
|
|
Save,
|
|
|
|
Load,
|
2020-09-17 17:02:01 +08:00
|
|
|
}
|
|
|
|
|
2020-09-29 17:02:15 +08:00
|
|
|
pub struct MqttMux<'s, SPI> {
|
2020-09-28 14:15:37 +08:00
|
|
|
urukul: Urukul<SPI>,
|
|
|
|
yet_to_respond: Option<MqttCommand>,
|
2020-12-18 17:42:26 +08:00
|
|
|
flash_controller: Flash,
|
|
|
|
flash_store: FlashStore,
|
2020-09-29 17:02:15 +08:00
|
|
|
name: &'s str,
|
2020-09-28 14:15:37 +08:00
|
|
|
float_buffer: ryu::Buffer,
|
2020-09-17 17:02:01 +08:00
|
|
|
}
|
|
|
|
|
2020-12-18 17:42:26 +08:00
|
|
|
const CHANNELS: [&str; 4] = [ "ch0", "ch1", "ch2", "ch3" ];
|
|
|
|
static mut SERDE_BUFFER: [u8; 64] = [0; 64];
|
|
|
|
|
2020-09-29 17:02:15 +08:00
|
|
|
impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer<u8, Error = E> {
|
2020-12-18 17:42:26 +08:00
|
|
|
pub fn new(urukul: Urukul<SPI>, flash_controller: Flash, flash_store: FlashStore, name: &'s str) -> Self {
|
|
|
|
Self {
|
2020-09-28 14:15:37 +08:00
|
|
|
urukul: urukul,
|
|
|
|
yet_to_respond: None,
|
2020-12-18 17:42:26 +08:00
|
|
|
flash_controller,
|
|
|
|
flash_store,
|
2020-09-29 17:02:15 +08:00
|
|
|
name: name,
|
2020-09-28 14:15:37 +08:00
|
|
|
float_buffer: ryu::Buffer::new(),
|
2020-09-17 17:02:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 17:42:26 +08:00
|
|
|
fn load_device(&mut self) -> Result<(), Error<E>> {
|
|
|
|
self.urukul.reset()?;
|
|
|
|
let profile = match self.flash_store
|
|
|
|
.read_value::<UrukulConfig>("urukul")
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
Some(urukul_config) => {
|
|
|
|
self.urukul.set_clock(
|
|
|
|
urukul_config.clk_src,
|
|
|
|
urukul_config.clk_freq,
|
|
|
|
urukul_config.clk_div
|
|
|
|
)?;
|
|
|
|
self.urukul.set_profile(urukul_config.profile)?;
|
|
|
|
urukul_config.profile
|
|
|
|
},
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (channel, channel_tag) in CHANNELS.iter().enumerate() {
|
|
|
|
match self.flash_store
|
|
|
|
.read_value::<ChannelConfig>(channel_tag)
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
Some(channel_config) => {
|
|
|
|
self.urukul.set_channel_switch(
|
|
|
|
channel as u32,
|
|
|
|
channel_config.sw
|
|
|
|
)?;
|
|
|
|
self.urukul.set_channel_attenuation(
|
|
|
|
channel as u8,
|
|
|
|
channel_config.att
|
|
|
|
)?;
|
|
|
|
self.urukul.set_channel_sys_clk(
|
|
|
|
channel as u8,
|
|
|
|
channel_config.sys_clk
|
|
|
|
)?;
|
|
|
|
self.urukul.set_channel_single_tone_profile(
|
|
|
|
channel as u8,
|
|
|
|
profile,
|
|
|
|
channel_config.freq,
|
|
|
|
channel_config.phase,
|
|
|
|
channel_config.asf,
|
|
|
|
)?;
|
|
|
|
},
|
|
|
|
None => ()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_device(&mut self) -> Result<(), Error<E>> {
|
|
|
|
let urukul_config = UrukulConfig {
|
|
|
|
clk_src: {
|
|
|
|
self.urukul.get_clock_source()?
|
|
|
|
},
|
|
|
|
clk_freq: {
|
|
|
|
self.urukul.get_clock_frequency()
|
|
|
|
},
|
|
|
|
clk_div: {
|
|
|
|
self.urukul.get_clock_division()?
|
|
|
|
},
|
|
|
|
profile: {
|
|
|
|
self.urukul.get_profile()?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe { self.flash_store.write_value("urukul", &urukul_config, &mut SERDE_BUFFER).unwrap(); }
|
|
|
|
|
|
|
|
for channel in 0..4 {
|
|
|
|
let (freq, phase, asf) = self.urukul.get_channel_single_tone_profile(
|
|
|
|
channel as u8,
|
|
|
|
urukul_config.profile
|
|
|
|
)?;
|
|
|
|
let channel_config = ChannelConfig {
|
|
|
|
sw: {
|
|
|
|
self.urukul.get_channel_switch_status(channel as u32)?
|
|
|
|
},
|
|
|
|
att: {
|
|
|
|
self.urukul.get_channel_attenuation(channel as u8)?
|
|
|
|
},
|
|
|
|
sys_clk: {
|
|
|
|
self.urukul.get_channel_sys_clk(channel as u8)?
|
|
|
|
},
|
|
|
|
freq,
|
|
|
|
phase,
|
|
|
|
asf
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
self.flash_store.write_value(CHANNELS[channel], &channel_config, &mut SERDE_BUFFER).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update_flash(&mut self.flash_controller, &mut self.flash_store).unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-09-28 14:15:37 +08:00
|
|
|
// Instead of using a return type, the result of processing the command is stored internally
|
|
|
|
// Invoke process_mqtt_egress to get a response after invoking ingress handler
|
|
|
|
pub fn process_mqtt_ingress(&mut self, topic: &str, message: &[u8]) {
|
|
|
|
let topic = match self.parse_topic(topic) {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(_) => {
|
2020-09-30 15:57:33 +08:00
|
|
|
self.yet_to_respond = Some(MqttCommand::ProcessError("Cannot prase MQTT topic"));
|
2020-09-28 14:15:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let command = match self.parse_message(topic, message) {
|
|
|
|
Ok((_, cmd)) => cmd,
|
|
|
|
Err(_) => {
|
2020-09-30 15:57:33 +08:00
|
|
|
self.yet_to_respond = Some(MqttCommand::ProcessError("Cannot parse MQTT message"));
|
2020-09-28 14:15:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.yet_to_respond = match self.execute(command.clone()) {
|
2020-09-30 15:57:33 +08:00
|
|
|
Err(_) => Some(MqttCommand::ProcessError("Cannot execute MQTT command")),
|
2020-09-28 14:15:37 +08:00
|
|
|
Ok(()) => Some(command)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be sure to call egress function after each ingress.
|
|
|
|
// Otherwise, response will be lost if successive valid MQTT messages were captured
|
|
|
|
// without calling egress in between
|
2020-09-29 17:02:15 +08:00
|
|
|
pub fn process_mqtt_egress(&mut self) -> Result<Vec<(String<U128>, String<U64>), U4>, Error<E>> {
|
2020-09-28 14:15:37 +08:00
|
|
|
// Remove previously executed command, and process it afterwards
|
|
|
|
let prev_cmd = self.yet_to_respond.clone();
|
|
|
|
self.yet_to_respond = None;
|
2020-09-29 17:02:15 +08:00
|
|
|
let mut vec = Vec::new();
|
2020-09-28 14:15:37 +08:00
|
|
|
|
|
|
|
match prev_cmd {
|
|
|
|
Some(cmd) => match cmd {
|
2020-09-30 15:57:33 +08:00
|
|
|
MqttCommand::ProcessError(e_str) => {
|
2020-09-29 17:02:15 +08:00
|
|
|
vec.push((
|
|
|
|
{
|
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Error")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string
|
|
|
|
},
|
2020-09-30 15:57:33 +08:00
|
|
|
String::from(e_str)
|
2020-09-29 17:02:15 +08:00
|
|
|
)).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
MqttCommand::Reset => {
|
|
|
|
vec.push((
|
2020-09-28 14:15:37 +08:00
|
|
|
{
|
2020-09-29 17:02:15 +08:00
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Reset")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string
|
|
|
|
},
|
2020-09-29 17:02:43 +08:00
|
|
|
String::from(
|
|
|
|
match self.urukul.test() {
|
|
|
|
Ok(0) => "Reset successful.",
|
|
|
|
_ => "Reset error!",
|
|
|
|
}
|
|
|
|
)
|
2020-09-29 17:02:15 +08:00
|
|
|
)).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
2020-09-29 17:02:43 +08:00
|
|
|
}
|
2020-09-29 17:02:15 +08:00
|
|
|
|
|
|
|
MqttCommand::Switch(ch, _) => {
|
|
|
|
vec.push((
|
2020-09-28 14:15:37 +08:00
|
|
|
{
|
2020-09-29 17:02:15 +08:00
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Channel")
|
2020-09-28 14:15:37 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
2020-09-29 17:02:15 +08:00
|
|
|
topic_string.push(char::from_digit(ch.into(), 10).unwrap())
|
2020-09-28 14:15:37 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
2020-09-29 17:02:15 +08:00
|
|
|
topic_string.push_str("/Switch")
|
2020-09-28 14:15:37 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
2020-09-29 17:02:15 +08:00
|
|
|
topic_string
|
2020-09-28 14:15:37 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
String::from(
|
|
|
|
if self.urukul.get_channel_switch_status(ch.into())? {
|
|
|
|
"on"
|
|
|
|
} else {
|
|
|
|
"off"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-09-29 17:02:15 +08:00
|
|
|
)).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
MqttCommand::Attenuation(ch, _) => {
|
|
|
|
vec.push((
|
2020-09-28 14:15:37 +08:00
|
|
|
{
|
2020-09-29 17:02:15 +08:00
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Channel")
|
2020-09-28 14:15:37 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
2020-09-29 17:02:15 +08:00
|
|
|
topic_string.push(char::from_digit(ch.into(), 10).unwrap())
|
2020-09-28 14:15:37 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
2020-09-29 17:02:15 +08:00
|
|
|
topic_string.push_str("/Attenuation")
|
2020-09-28 14:15:37 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
2020-09-29 17:02:15 +08:00
|
|
|
topic_string
|
2020-09-28 14:15:37 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
String::from(
|
|
|
|
self.float_buffer.format_finite(
|
|
|
|
self.urukul.get_channel_attenuation(ch)?
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2020-09-29 17:02:43 +08:00
|
|
|
)).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
MqttCommand::Clock(_, _, _) => {
|
|
|
|
vec.push(
|
|
|
|
self.get_clock_source_message()?
|
|
|
|
).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
vec.push(
|
|
|
|
self.get_clock_frequency_message()?
|
|
|
|
).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
vec.push(
|
|
|
|
self.get_clock_division_message()?
|
|
|
|
).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
MqttCommand::ClockSource(_) => {
|
|
|
|
vec.push(
|
|
|
|
self.get_clock_source_message()?
|
|
|
|
).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
MqttCommand::ClockFrequency(_) => {
|
|
|
|
vec.push(
|
|
|
|
self.get_clock_frequency_message()?
|
|
|
|
).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
MqttCommand::ClockDivision(_) => {
|
|
|
|
vec.push(
|
|
|
|
self.get_clock_division_message()?
|
|
|
|
).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
MqttCommand::SystemClock(ch, _) => {
|
|
|
|
vec.push((
|
2020-09-28 14:15:37 +08:00
|
|
|
{
|
2020-09-29 17:02:15 +08:00
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Channel")
|
2020-09-28 14:15:37 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
2020-09-29 17:02:15 +08:00
|
|
|
topic_string.push(char::from_digit(ch.into(), 10).unwrap())
|
2020-09-28 14:15:37 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
2020-09-29 17:02:15 +08:00
|
|
|
topic_string.push_str("/SystemClock")
|
2020-09-28 14:15:37 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
2020-09-29 17:02:15 +08:00
|
|
|
topic_string
|
2020-09-28 14:15:37 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
let mut message_str = String::from(
|
|
|
|
self.float_buffer.format_finite(
|
|
|
|
self.urukul.get_channel_sys_clk(ch)?
|
|
|
|
)
|
|
|
|
);
|
|
|
|
message_str.push_str(" Hz")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
message_str
|
|
|
|
}
|
2020-09-29 17:02:43 +08:00
|
|
|
)).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
MqttCommand::Singletone(ch, pr, _, _, _) |
|
|
|
|
MqttCommand::SingletoneFrequency(ch, pr, _) |
|
|
|
|
MqttCommand::SingletoneAmplitude(ch, pr, _) |
|
|
|
|
MqttCommand::SingletonePhase(ch, pr, _) => {
|
|
|
|
let (f_out, phase, ampl) = self.urukul.get_channel_single_tone_profile(ch, pr)?;
|
2020-09-30 16:43:39 +08:00
|
|
|
vec.push(self.get_single_tone_frequency_message(ch, f_out)?)
|
2020-09-29 17:02:43 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
2020-09-30 16:43:39 +08:00
|
|
|
vec.push(self.get_single_tone_phase_message(ch, phase)?)
|
2020-09-29 17:02:43 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
2020-09-30 16:43:39 +08:00
|
|
|
vec.push(self.get_single_tone_amplitude_message(ch, ampl)?)
|
2020-09-29 17:02:43 +08:00
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
MqttCommand::Profile(_) => {
|
|
|
|
vec.push((
|
|
|
|
{
|
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Profile")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string
|
|
|
|
},
|
2020-09-28 14:15:37 +08:00
|
|
|
{
|
|
|
|
let mut message_str = String::new();
|
|
|
|
let prof = self.urukul.get_profile()?;
|
|
|
|
message_str.push(char::from_digit(prof.into(), 10).unwrap())
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
message_str
|
|
|
|
}
|
2020-09-29 17:02:43 +08:00
|
|
|
)).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
2020-12-18 17:42:26 +08:00
|
|
|
|
|
|
|
MqttCommand::Save => {
|
|
|
|
vec.push((
|
|
|
|
{
|
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Save")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string
|
|
|
|
},
|
|
|
|
String::from("Saved device.")
|
|
|
|
)).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
MqttCommand::Load => {
|
|
|
|
vec.push((
|
|
|
|
{
|
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Load")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string
|
|
|
|
},
|
|
|
|
String::from("Loaded from flash.")
|
|
|
|
)).map_err(|_| Error::VectorOutOfSpace)?;
|
|
|
|
Ok(vec)
|
|
|
|
}
|
2020-09-28 14:15:37 +08:00
|
|
|
},
|
2020-09-29 17:02:43 +08:00
|
|
|
None => Ok(vec),
|
2020-09-28 14:15:37 +08:00
|
|
|
}
|
2020-09-17 17:02:01 +08:00
|
|
|
}
|
2020-09-23 10:41:54 +08:00
|
|
|
|
2020-09-23 09:42:14 +08:00
|
|
|
fn parse_topic<'a>(&mut self, topic: &'a str) -> Result<MqttTopic, Error<E>> {
|
|
|
|
let mut assigned_channel = false;
|
|
|
|
let mut assigned_profile = false;
|
|
|
|
let mut channel :u8 = 0;
|
|
|
|
let mut profile :u8 = 0;
|
|
|
|
|
2020-09-29 17:02:15 +08:00
|
|
|
// Verify that the topic starts with <name>/Control/ or /<name>/Control/
|
|
|
|
let mut header = {
|
|
|
|
let mut topic_builder_with_slash: String<U128> = String::from("/");
|
|
|
|
topic_builder_with_slash.push_str(self.name)
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_builder_with_slash.push_str("/Control/")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
let topic_builder: &str = topic_builder_with_slash.as_str()
|
|
|
|
.strip_prefix("/")
|
|
|
|
.ok_or(Error::StringOutOfSpace)?;
|
|
|
|
topic.strip_prefix(topic_builder_with_slash.as_str())
|
|
|
|
.or_else(|| topic.strip_prefix(topic_builder))
|
|
|
|
.ok_or(Error::MqttCommandError)?
|
|
|
|
};
|
2020-09-23 09:42:14 +08:00
|
|
|
|
|
|
|
loop {
|
|
|
|
match header {
|
|
|
|
// The topic has a channel subtopic
|
|
|
|
_ if header.starts_with("Channel") => {
|
|
|
|
// MQTT command should only mention channel once appropriately
|
|
|
|
// Channel must be referred before profile,
|
|
|
|
// as a channel is broader than a profile
|
|
|
|
if assigned_channel || assigned_profile {
|
|
|
|
return Err(Error::MqttCommandError);
|
|
|
|
}
|
|
|
|
// Remove the "Channel" part of the subtopic
|
|
|
|
header = header.strip_prefix("Channel")
|
|
|
|
.ok_or(Error::MqttCommandError)?;
|
|
|
|
// Remove the channel number at the end of the subtopic
|
|
|
|
// But store the channel as a char, so it can be removed easily
|
|
|
|
let numeric_char :char = header.chars()
|
|
|
|
.next()
|
|
|
|
.ok_or(Error::MqttCommandError)?;
|
|
|
|
// Record the channel number
|
|
|
|
channel = numeric_char.to_digit(10)
|
|
|
|
.ok_or(Error::MqttCommandError)?
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
assigned_channel = true;
|
|
|
|
header = header.strip_prefix(numeric_char)
|
|
|
|
.ok_or(Error::MqttCommandError)?;
|
|
|
|
// Remove forward slash ("/")
|
|
|
|
header = header.strip_prefix("/")
|
|
|
|
.ok_or(Error::MqttCommandError)?;
|
|
|
|
},
|
|
|
|
|
|
|
|
_ if (header.starts_with("Profile") && assigned_channel) => {
|
|
|
|
// MQTT command should only mention profile once appropriately
|
|
|
|
if assigned_profile {
|
|
|
|
return Err(Error::MqttCommandError);
|
|
|
|
}
|
|
|
|
// Remove the "Profile" part of the subtopic
|
|
|
|
header = header.strip_prefix("Profile")
|
|
|
|
.ok_or(Error::MqttCommandError)?;
|
|
|
|
// Remove the profile number at the end of the subtopic
|
|
|
|
// But store the profile as a char, so it can be removed easily
|
|
|
|
let numeric_char :char = header.chars()
|
|
|
|
.next()
|
|
|
|
.ok_or(Error::MqttCommandError)?;
|
|
|
|
// Record the channel number
|
|
|
|
profile = numeric_char.to_digit(10)
|
|
|
|
.ok_or(Error::MqttCommandError)?
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
assigned_profile = true;
|
|
|
|
header = header.strip_prefix(numeric_char)
|
|
|
|
.ok_or(Error::MqttCommandError)?;
|
|
|
|
// Remove forward slash ("/")
|
|
|
|
header = header.strip_prefix("/")
|
|
|
|
.ok_or(Error::MqttCommandError)?;
|
|
|
|
},
|
|
|
|
|
|
|
|
"Reset" => {
|
|
|
|
if assigned_channel || assigned_profile {
|
|
|
|
return Err(Error::MqttCommandError);
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::Reset);
|
|
|
|
},
|
|
|
|
|
|
|
|
"Switch" => {
|
|
|
|
// Switch is a channel specific topic
|
|
|
|
if !(assigned_channel && !assigned_profile) {
|
|
|
|
return Err(Error::MqttCommandError);
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::Switch(channel));
|
|
|
|
},
|
|
|
|
|
|
|
|
"Attenuation" => {
|
|
|
|
// Attenuation is a channel specific topic
|
|
|
|
if !(assigned_channel && !assigned_profile) {
|
|
|
|
return Err(Error::MqttCommandError);
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::Attenuation(channel));
|
|
|
|
},
|
|
|
|
|
|
|
|
"Clock" => {
|
|
|
|
if assigned_channel || assigned_profile {
|
|
|
|
return Err(Error::MqttCommandError);
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::Clock);
|
|
|
|
},
|
|
|
|
|
|
|
|
"Clock/Source" => {
|
|
|
|
// Clock/Source refers to the Urukul clock source
|
|
|
|
// It should be common for all channels and profiles
|
|
|
|
if assigned_channel || assigned_profile {
|
|
|
|
return Err(Error::MqttCommandError);
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::ClockSource);
|
|
|
|
},
|
|
|
|
|
|
|
|
"Clock/Frequency" => {
|
|
|
|
// Clock/Frequency refers to the Urukul clock frequency
|
|
|
|
// It should be common for all channels and profiles
|
|
|
|
if assigned_channel || assigned_profile {
|
|
|
|
return Err(Error::MqttCommandError);
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::ClockFrequency);
|
|
|
|
},
|
|
|
|
|
|
|
|
"Clock/Division" => {
|
|
|
|
// Clock/Division refers to the Urukul clock division
|
|
|
|
// It should be common for all channels and profiles
|
|
|
|
if assigned_channel || assigned_profile {
|
|
|
|
return Err(Error::MqttCommandError);
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::ClockDivision);
|
|
|
|
},
|
|
|
|
|
|
|
|
"SystemClock" => {
|
|
|
|
if !(assigned_channel && !assigned_profile) {
|
|
|
|
return Err(Error::MqttCommandError);
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::SystemClock(channel));
|
|
|
|
}
|
|
|
|
|
|
|
|
"Singletone" => {
|
|
|
|
if !(assigned_channel && assigned_profile) {
|
|
|
|
return Err(Error::MqttCommandError)
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::Singletone(channel, profile));
|
|
|
|
}
|
|
|
|
|
|
|
|
"Singletone/Frequency" => {
|
|
|
|
if !(assigned_channel && assigned_profile) {
|
|
|
|
return Err(Error::MqttCommandError)
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::SingletoneFrequency(channel, profile));
|
|
|
|
}
|
|
|
|
|
|
|
|
"Singletone/Amplitude" => {
|
|
|
|
if !(assigned_channel && assigned_profile) {
|
|
|
|
return Err(Error::MqttCommandError)
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::SingletoneAmplitude(channel, profile));
|
|
|
|
}
|
|
|
|
|
|
|
|
"Singletone/Phase" => {
|
|
|
|
if !(assigned_channel && assigned_profile) {
|
|
|
|
return Err(Error::MqttCommandError)
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::SingletonePhase(channel, profile));
|
|
|
|
}
|
|
|
|
|
|
|
|
"Profile" => {
|
|
|
|
if assigned_channel || assigned_profile {
|
|
|
|
return Err(Error::MqttCommandError)
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::Profile);
|
|
|
|
}
|
|
|
|
|
2020-12-18 17:42:26 +08:00
|
|
|
"Save" => {
|
|
|
|
if assigned_channel || assigned_profile {
|
|
|
|
return Err(Error::MqttCommandError)
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::Save);
|
|
|
|
}
|
|
|
|
|
|
|
|
"Load" => {
|
|
|
|
if assigned_channel || assigned_profile {
|
|
|
|
return Err(Error::MqttCommandError)
|
|
|
|
}
|
|
|
|
return Ok(MqttTopic::Load);
|
|
|
|
}
|
|
|
|
|
2020-09-23 09:42:14 +08:00
|
|
|
_ => return Err(Error::MqttCommandError),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 17:31:34 +08:00
|
|
|
|
|
|
|
fn parse_message<'a>(&mut self, topic: MqttTopic, message: &'a [u8]) -> IResult<&'a [u8], MqttCommand> {
|
|
|
|
match topic {
|
2020-09-23 09:42:14 +08:00
|
|
|
MqttTopic::Reset => Ok((message, MqttCommand::Reset)),
|
2020-09-21 17:31:34 +08:00
|
|
|
MqttTopic::Switch(ch) => switch_message(ch, message),
|
|
|
|
MqttTopic::Attenuation(ch) => attenuation_message(ch, message),
|
2020-09-22 13:40:46 +08:00
|
|
|
MqttTopic::Clock => clock_message(message),
|
|
|
|
MqttTopic::ClockSource => clock_source_message(message),
|
|
|
|
MqttTopic::ClockFrequency => clock_frequency_message(message),
|
|
|
|
MqttTopic::ClockDivision => clock_division_message(message),
|
|
|
|
MqttTopic::SystemClock(ch) => system_clock_message(ch, message),
|
2020-09-21 17:31:34 +08:00
|
|
|
MqttTopic::Singletone(ch, prof) => singletone_message(ch, prof, message),
|
|
|
|
MqttTopic::SingletoneFrequency(ch, prof) => singletone_frequency_message(ch, prof, message),
|
|
|
|
MqttTopic::SingletoneAmplitude(ch, prof) => singletone_amplitude_message(ch, prof, message),
|
|
|
|
MqttTopic::SingletonePhase(ch, prof) => singletone_phase_message(ch, prof, message),
|
|
|
|
MqttTopic::Profile => profile_message(message),
|
2020-12-18 17:42:26 +08:00
|
|
|
MqttTopic::Save => Ok((message, MqttCommand::Save)),
|
|
|
|
MqttTopic::Load => Ok((message, MqttCommand::Load)),
|
2020-09-17 17:02:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:31:34 +08:00
|
|
|
fn execute(&mut self, command: MqttCommand) -> Result<(), Error<E>> {
|
|
|
|
match command {
|
2020-09-30 15:57:33 +08:00
|
|
|
MqttCommand::ProcessError(_) => Ok(()),
|
2020-09-23 09:42:14 +08:00
|
|
|
MqttCommand::Reset => self.urukul.reset(),
|
2020-09-21 17:31:34 +08:00
|
|
|
MqttCommand::Switch(ch, state) => self.urukul.set_channel_switch(ch.into(), state),
|
|
|
|
MqttCommand::Attenuation(ch, ampl) => self.urukul.set_channel_attenuation(ch, ampl),
|
2020-09-22 13:40:46 +08:00
|
|
|
MqttCommand::Clock(src, freq, div) => self.urukul.set_clock(src, freq, div),
|
|
|
|
MqttCommand::ClockSource(src) => self.urukul.set_clock_source(src),
|
|
|
|
MqttCommand::ClockFrequency(freq) => self.urukul.set_clock_frequency(freq),
|
|
|
|
MqttCommand::ClockDivision(div) => self.urukul.set_clock_division(div),
|
|
|
|
MqttCommand::SystemClock(ch, freq) => self.urukul.set_channel_sys_clk(ch, freq),
|
2020-09-21 17:31:34 +08:00
|
|
|
MqttCommand::Singletone(ch, prof, freq, ampl, deg) => self.urukul.set_channel_single_tone_profile(ch, prof, freq, ampl, deg),
|
|
|
|
MqttCommand::SingletoneFrequency(ch, prof, freq) => self.urukul.set_channel_single_tone_profile_frequency(ch, prof, freq),
|
|
|
|
MqttCommand::SingletoneAmplitude(ch, prof, ampl) => self.urukul.set_channel_single_tone_profile_amplitude(ch, prof, ampl),
|
|
|
|
MqttCommand::SingletonePhase(ch, prof, deg) => self.urukul.set_channel_single_tone_profile_phase(ch, prof, deg),
|
|
|
|
MqttCommand::Profile(prof) => self.urukul.set_profile(prof),
|
2020-12-18 17:42:26 +08:00
|
|
|
MqttCommand::Save => self.save_device(),
|
|
|
|
MqttCommand::Load => self.load_device(),
|
2020-09-17 17:02:01 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-25 10:24:18 +08:00
|
|
|
|
2020-09-29 17:02:43 +08:00
|
|
|
fn get_clock_source_message(&mut self) -> Result<(String<U128>, String<U64>), Error<E>> {
|
|
|
|
Ok((
|
|
|
|
{
|
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Clock/Source")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string
|
|
|
|
},
|
|
|
|
self.urukul.get_clock_source().map(
|
|
|
|
|src| match src {
|
|
|
|
UrukulClockSource::OSC => String::from("OSC"),
|
|
|
|
UrukulClockSource::MMCX => String::from("MMCX"),
|
|
|
|
UrukulClockSource::SMA => String::from("SMA")
|
|
|
|
}
|
|
|
|
)?
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_clock_frequency_message(&mut self) -> Result<(String<U128>, String<U64>), Error<E>> {
|
|
|
|
Ok((
|
|
|
|
{
|
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Clock/Frequency")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let mut freq_str = String::from(
|
|
|
|
self.float_buffer.format_finite(
|
|
|
|
self.urukul.get_clock_frequency()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
freq_str.push_str(" Hz").map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
freq_str
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_clock_division_message(&mut self) -> Result<(String<U128>, String<U64>), Error<E>> {
|
|
|
|
Ok((
|
|
|
|
{
|
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Clock/Division")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string
|
|
|
|
},
|
|
|
|
{
|
|
|
|
self.urukul.get_clock_division().map(
|
|
|
|
|src| match src {
|
|
|
|
1 => String::from("1"),
|
|
|
|
2 => String::from("2"),
|
|
|
|
4 => String::from("4"),
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
)?
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:43:39 +08:00
|
|
|
fn get_single_tone_frequency_message(&mut self, ch: u8, f_out: f64) -> Result<(String<U128>, String<U64>), Error<E>> {
|
2020-09-29 17:02:43 +08:00
|
|
|
Ok((
|
|
|
|
{
|
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Channel")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string.push(char::from_digit(ch.into(), 10).unwrap())
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string.push_str("/Singletone/Frequency")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let mut message_str = String::from(
|
|
|
|
self.float_buffer.format_finite(f_out)
|
|
|
|
);
|
|
|
|
message_str.push_str(" Hz")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
message_str
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:43:39 +08:00
|
|
|
fn get_single_tone_amplitude_message(&mut self, ch: u8, ampl: f64) -> Result<(String<U128>, String<U64>), Error<E>> {
|
2020-09-29 17:02:43 +08:00
|
|
|
Ok((
|
|
|
|
{
|
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Channel")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string.push(char::from_digit(ch.into(), 10).unwrap())
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string.push_str("/Singletone/Amplitude")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let message_str = String::from(
|
|
|
|
self.float_buffer.format_finite(ampl)
|
|
|
|
);
|
|
|
|
message_str
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:43:39 +08:00
|
|
|
fn get_single_tone_phase_message(&mut self, ch: u8, phase: f64) -> Result<(String<U128>, String<U64>), Error<E>> {
|
2020-09-29 17:02:43 +08:00
|
|
|
Ok((
|
|
|
|
{
|
|
|
|
let mut topic_string = String::from(self.name);
|
|
|
|
topic_string.push_str("/Feedback/Channel")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string.push(char::from_digit(ch.into(), 10).unwrap())
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string.push_str("/Singletone/Phase")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
topic_string
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let mut message_str = String::from(
|
|
|
|
self.float_buffer.format_finite(phase)
|
|
|
|
);
|
|
|
|
message_str.push_str(" deg")
|
|
|
|
.map_err(|_| Error::StringOutOfSpace)?;
|
|
|
|
message_str
|
|
|
|
}
|
|
|
|
))
|
2020-09-25 10:24:18 +08:00
|
|
|
}
|
2020-09-21 17:31:34 +08:00
|
|
|
}
|
|
|
|
|
2020-09-23 12:34:13 +08:00
|
|
|
// Read message parameter separator (optional comma and whitespace)
|
2020-09-21 17:31:34 +08:00
|
|
|
fn message_separator(message: &[u8]) -> IResult<&[u8], ()> {
|
2020-09-23 12:34:13 +08:00
|
|
|
preceded(
|
|
|
|
opt(
|
|
|
|
tag(",")
|
|
|
|
),
|
|
|
|
whitespace
|
2020-09-21 17:31:34 +08:00
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read whitespace
|
|
|
|
fn whitespace(message: &[u8]) -> IResult<&[u8], ()> {
|
|
|
|
value((), take_while(is_space))(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reader for uom instances
|
|
|
|
fn read_frequency(message: &[u8]) -> IResult<&[u8], f64> {
|
|
|
|
map(
|
|
|
|
pair(
|
|
|
|
double,
|
|
|
|
opt(
|
|
|
|
preceded(
|
|
|
|
whitespace,
|
|
|
|
alt((
|
|
|
|
value(1.0, tag_no_case("hz")),
|
|
|
|
value(1_000.0, tag_no_case("khz")),
|
|
|
|
value(1_000_000.0, tag_no_case("mhz")),
|
|
|
|
value(1_000_000_000.0, tag_no_case("ghz"))
|
|
|
|
))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
|(freq, unit): (f64, Option<f64>)| {
|
|
|
|
freq * unit.map_or(1.0, |mul| mul)
|
|
|
|
}
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Switch Command Message
|
|
|
|
fn switch_message(channel: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
alt((
|
|
|
|
value(true, tag("on")),
|
|
|
|
value(false, tag("off"))
|
|
|
|
)),
|
|
|
|
|switch| MqttCommand::Switch(channel, switch)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Attenuation Command Message
|
|
|
|
fn attenuation_message(channel: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
terminated(
|
|
|
|
float,
|
|
|
|
opt(
|
|
|
|
preceded(
|
|
|
|
whitespace,
|
|
|
|
tag_no_case("db")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
|att: f32| MqttCommand::Attenuation(channel, att)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:40:46 +08:00
|
|
|
// Parser for Clock Source Command Message
|
|
|
|
fn clock_source_message(message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
alt((
|
|
|
|
value(MqttCommand::ClockSource(UrukulClockSource::OSC), tag_no_case("OSC")),
|
|
|
|
value(MqttCommand::ClockSource(UrukulClockSource::MMCX), tag_no_case("MMCX")),
|
|
|
|
value(MqttCommand::ClockSource(UrukulClockSource::SMA), tag_no_case("SMA"))
|
|
|
|
))
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Clock Frequency Command Message
|
|
|
|
fn clock_frequency_message(message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
read_frequency,
|
|
|
|
|freq: f64| MqttCommand::ClockFrequency(freq)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Clock Division Command Message
|
|
|
|
fn clock_division_message(message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
digit1,
|
|
|
|
|div: &[u8]| MqttCommand::ClockDivision(
|
|
|
|
u8::from_str_radix(
|
|
|
|
core::str::from_utf8(div).unwrap(),
|
|
|
|
10
|
|
|
|
).unwrap()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for one-command master clock setup message
|
|
|
|
fn clock_message(message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
2020-09-23 12:34:13 +08:00
|
|
|
permutation((
|
|
|
|
preceded(
|
|
|
|
tag_no_case("source:"),
|
2020-09-22 13:40:46 +08:00
|
|
|
preceded(
|
|
|
|
whitespace,
|
2020-09-23 12:34:13 +08:00
|
|
|
terminated(
|
|
|
|
alt((
|
|
|
|
value(UrukulClockSource::OSC, tag_no_case("OSC")),
|
|
|
|
value(UrukulClockSource::MMCX, tag_no_case("MMCX")),
|
|
|
|
value(UrukulClockSource::SMA, tag_no_case("SMA"))
|
|
|
|
)),
|
|
|
|
message_separator
|
2020-09-22 13:40:46 +08:00
|
|
|
)
|
2020-09-23 12:34:13 +08:00
|
|
|
)
|
|
|
|
),
|
|
|
|
preceded(
|
|
|
|
tag_no_case("frequency:"),
|
2020-09-22 13:40:46 +08:00
|
|
|
preceded(
|
|
|
|
whitespace,
|
2020-09-23 12:34:13 +08:00
|
|
|
terminated(
|
|
|
|
read_frequency,
|
|
|
|
message_separator
|
2020-09-22 13:40:46 +08:00
|
|
|
)
|
2020-09-23 12:34:13 +08:00
|
|
|
)
|
|
|
|
),
|
|
|
|
preceded(
|
|
|
|
tag_no_case("division:"),
|
2020-09-22 13:40:46 +08:00
|
|
|
preceded(
|
|
|
|
whitespace,
|
2020-09-23 12:34:13 +08:00
|
|
|
terminated(
|
|
|
|
map_res(
|
|
|
|
digit1,
|
|
|
|
|div: &[u8]| u8::from_str_radix(core::str::from_utf8(div).unwrap(), 10)
|
|
|
|
),
|
|
|
|
message_separator
|
2020-09-22 13:40:46 +08:00
|
|
|
)
|
|
|
|
)
|
2020-09-23 12:34:13 +08:00
|
|
|
)
|
|
|
|
)),
|
2020-09-22 13:40:46 +08:00
|
|
|
|(src, freq, div): (UrukulClockSource, f64, u8)| MqttCommand::Clock(src, freq, div)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message parser for f_sys_clk of any channels
|
|
|
|
fn system_clock_message(channel: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
read_frequency,
|
|
|
|
|freq: f64| MqttCommand::SystemClock(channel, freq)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:31:34 +08:00
|
|
|
// Parser for Singletone frequency Command Message
|
|
|
|
fn singletone_frequency_message(channel: u8, profile: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
read_frequency,
|
|
|
|
|freq: f64| MqttCommand::SingletoneFrequency(channel, profile, freq)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Singletone AMplitude Command Message
|
|
|
|
fn singletone_amplitude_message(channel: u8, profile: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
double,
|
|
|
|
|ampl: f64| MqttCommand::SingletoneAmplitude(channel, profile, ampl)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Phase Command Message
|
|
|
|
fn singletone_phase_message(channel: u8, profile: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
terminated(
|
|
|
|
double,
|
|
|
|
opt(
|
|
|
|
preceded(
|
|
|
|
whitespace,
|
|
|
|
tag_no_case("deg")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
|deg: f64| MqttCommand::SingletonePhase(channel, profile, deg)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for one-command singletone profile Command
|
|
|
|
fn singletone_message(channel: u8, profile: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
2020-09-23 12:34:13 +08:00
|
|
|
permutation((
|
2020-09-21 17:31:34 +08:00
|
|
|
preceded(
|
2020-09-23 12:34:13 +08:00
|
|
|
tag_no_case("frequency:"),
|
2020-09-22 13:40:46 +08:00
|
|
|
preceded(
|
|
|
|
whitespace,
|
2020-09-23 12:34:13 +08:00
|
|
|
terminated(
|
|
|
|
read_frequency,
|
|
|
|
message_separator
|
2020-09-22 13:40:46 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
preceded(
|
2020-09-23 12:34:13 +08:00
|
|
|
tag_no_case("phase:"),
|
2020-09-22 13:40:46 +08:00
|
|
|
preceded(
|
|
|
|
whitespace,
|
2020-09-23 12:34:13 +08:00
|
|
|
terminated(
|
|
|
|
double,
|
2020-09-22 13:40:46 +08:00
|
|
|
preceded(
|
2020-09-23 12:34:13 +08:00
|
|
|
opt(
|
|
|
|
preceded(
|
|
|
|
whitespace,
|
|
|
|
tag_no_case("deg")
|
|
|
|
)
|
|
|
|
),
|
|
|
|
message_separator
|
2020-09-22 13:40:46 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2020-09-21 17:31:34 +08:00
|
|
|
),
|
|
|
|
preceded(
|
2020-09-23 12:34:13 +08:00
|
|
|
tag_no_case("amplitude:"),
|
2020-09-22 13:40:46 +08:00
|
|
|
preceded(
|
|
|
|
whitespace,
|
2020-09-23 12:34:13 +08:00
|
|
|
terminated(
|
|
|
|
double,
|
|
|
|
message_separator
|
2020-09-21 17:31:34 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)),
|
2020-09-23 12:34:13 +08:00
|
|
|
|(freq, phase, ampl): (f64, f64, f64)| MqttCommand::Singletone(channel, profile, freq, phase, ampl)
|
2020-09-21 17:31:34 +08:00
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn profile_message(message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
digit1,
|
|
|
|
|num: &[u8]| {
|
|
|
|
MqttCommand::Profile(
|
|
|
|
u8::from_str_radix(core::str::from_utf8(num).unwrap(), 10).unwrap()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|