Skip to content

Instantly share code, notes, and snippets.

@saibatizoku
Last active August 11, 2017 23:37
Show Gist options
  • Save saibatizoku/320e0067f82cac87f5d2bf2c26dfd2f2 to your computer and use it in GitHub Desktop.
Save saibatizoku/320e0067f82cac87f5d2bf2c26dfd2f2 to your computer and use it in GitHub Desktop.
initial parser for RTD EZO commands
/// Public Commands API
#[derive(Debug, PartialEq)]
pub enum ApiCommand {
Baud(BpsRate),
Calibrate(f64),
CalClear,
CalStatus,
Export,
ExportInfo,
Import(String),
DataLoggerSet(u32),
DataLoggerOff,
DataLoggerStatus,
Factory,
Find,
I2c(u16),
Info,
LedOff,
LedOn,
LedStatus,
MemoryClear,
MemoryRecall,
MemoryLast,
PlockOff,
PlockOn,
PlockStatus,
Reading,
ScaleCelsius,
ScaleKelvin,
ScaleFahrenheit,
ScaleStatus,
Status,
Sleep,
_Error,
}
impl ApiCommand {
pub fn parse(cmd: &str) -> Result<ApiCommand> {
match parsers::parse_Command(cmd) {
Ok(c) => Ok(c),
_ => Err(ErrorKind::CommandParse.into()),
}
}
pub fn run(&self, dev: &mut LinuxI2CDevice) -> Result<Option<String>> {
let _cmd = match *self {
ApiCommand::Baud(ref b) => Baud(b.clone()).run(dev)?,
ApiCommand::Calibrate(t) => CalibrationTemperature(t).run(dev)?,
ApiCommand::CalClear => CalibrationClear.run(dev)?,
ApiCommand::CalStatus => {
let response = CalibrationState.run(dev)?;
return Ok(Some(format!("{}", response)));
},
ApiCommand::DataLoggerSet(n) => DataloggerPeriod(n).run(dev)?,
ApiCommand::DataLoggerOff => DataloggerDisable.run(dev)?,
ApiCommand::DataLoggerStatus => {
let response = DataloggerInterval.run(dev)?;
return Ok(Some(format!("{}", response)));
},
ApiCommand::Export => {
let response = Export.run(dev)?;
return Ok(Some(format!("{}", response)));
},
ApiCommand::ExportInfo => {
let response = ExportInfo.run(dev)?;
return Ok(Some(format!("{}", response)));
},
ApiCommand::Import(ref s) => Import(s.clone()).run(dev)?,
ApiCommand::Factory => Factory.run(dev)?,
ApiCommand::Find => Find.run(dev)?,
ApiCommand::Info => {
let response = DeviceInformation.run(dev)?;
return Ok(Some(format!("{}", response)));
},
ApiCommand::I2c(addr) => DeviceAddress(addr).run(dev)?,
ApiCommand::LedOff => LedOff.run(dev)?,
ApiCommand::LedOn => LedOn.run(dev)?,
ApiCommand::LedStatus => {
let response = LedState.run(dev)?;
return Ok(Some(format!("{}", response)));
},
ApiCommand::PlockOff => ProtocolLockDisable.run(dev)?,
ApiCommand::PlockOn => ProtocolLockEnable.run(dev)?,
ApiCommand::PlockStatus => {
let response = ProtocolLockState.run(dev)?;
return Ok(Some(format!("{}", response)));
},
ApiCommand::Sleep => Sleep.run(dev)?,
ApiCommand::_Error | _ => return Err(ErrorKind::CommandParse.into()),
};
Ok(None)
}
}
use std::str::FromStr;
use command::ApiCommand;
use ezo_common::BpsRate;
grammar;
/// Parse all EZO commands
pub Command: ApiCommand = {
BaudCmd,
CalCmd,
DatalogCmd,
ExportCmd,
ExportInfoCmd,
ImportCmd,
InfoCmd,
I2cCmd,
LedCmd,
MemoryCmd,
PlockCmd,
ReadCmd,
ScaleCmd,
StatusCmd,
SleepCmd,
};
pub NumBaud: BpsRate = {
"300" => BpsRate::Bps300,
"1200" => BpsRate::Bps1200,
"2400" => BpsRate::Bps2400,
"9600" => BpsRate::Bps9600,
"19200" => BpsRate::Bps19200,
"38400" => BpsRate::Bps38400,
"57600" => BpsRate::Bps57600,
"115200" => BpsRate::Bps115200,
};
BaudCmd: ApiCommand = {
"BAUD," <NumBaud> => ApiCommand::Baud(<>),
};
CalCmd: ApiCommand = {
"CAL,CLEAR" => ApiCommand::CalClear,
"CAL,?" => ApiCommand::CalStatus,
"CAL," <FloatNum> => ApiCommand::Calibrate(<>),
};
ExportCmd: ApiCommand = "EXPORT" => ApiCommand::Export;
ExportInfoCmd: ApiCommand = "EXPORT,?" => ApiCommand::ExportInfo;
ImportCmd: ApiCommand = "IMPORT," <ImportExportStr>=> ApiCommand::Import(<>.to_string());
DatalogCmd: ApiCommand = {
"D,0" => ApiCommand::DataLoggerOff,
"D,?" => ApiCommand::DataLoggerStatus,
"D," <TwoDigitMin> => ApiCommand::DataLoggerSet(u32::from_str(<>).unwrap()),
};
FactoryCmd: ApiCommand = "FACTORY" => ApiCommand::Factory;
InfoCmd: ApiCommand = "I" => ApiCommand::Info;
I2cCmd: ApiCommand = "I2C," <TwoDigitMin>=> ApiCommand::I2c(u16::from_str(<>).unwrap());
LedCmd: ApiCommand = {
"L,0" => ApiCommand::LedOff,
"L,1" => ApiCommand::LedOn,
"L,?" => ApiCommand::LedStatus,
};
MemoryCmd: ApiCommand = {
"M" => ApiCommand::MemoryRecall,
"M,?" => ApiCommand::MemoryLast,
"M,CLEAR" => ApiCommand::MemoryClear,
};
PlockCmd: ApiCommand = {
"PLOCK,0" => ApiCommand::PlockOff,
"PLOCK,1" => ApiCommand::PlockOn,
"PLOCK,?" => ApiCommand::PlockStatus,
};
ReadCmd: ApiCommand = "R" => ApiCommand::Reading;
ScaleCmd: ApiCommand = {
"S,?" => ApiCommand::ScaleStatus,
"S,C" => ApiCommand::ScaleCelsius,
"S,K" => ApiCommand::ScaleKelvin,
"S,F" => ApiCommand::ScaleFahrenheit,
};
StatusCmd: ApiCommand = "STATUS" => ApiCommand::Status;
SleepCmd: ApiCommand = "SLEEP" => ApiCommand::Sleep;
FloatNum: f64 = <s:FloatStr> => f64::from_str(s).unwrap();
IntNum: u16 = <s:IntStr> => u16::from_str(s).unwrap();
match {
r"(?i)baud," => "BAUD,",
r"(?i)cal,clear" => "CAL,CLEAR",
r"(?i)cal,\?" => "CAL,?",
r"(?i)cal," => "CAL,",
r"(?i)export,\?" => "EXPORT,?",
r"(?i)export" => "EXPORT",
r"(?i)import," => "IMPORT,",
r"(?i)d,0" => "D,0",
r"(?i)d,\?" => "D,?",
r"(?i)d," => "D,",
r"(?i)factory" => "FACTORY",
r"(?i)f" => "F",
r"(?i)i" => "I",
r"(?i)i2c," => "I2C,",
r"(?i)l,0" => "L,0",
r"(?i)l,1" => "L,1",
r"(?i)l,\?" => "L,?",
r"(?i)m,clear" => "M,CLEAR",
r"(?i)m,\?" => "M,?",
r"(?i)m" => "M",
r"(?i)plock,0" => "PLOCK,0",
r"(?i)plock,1" => "PLOCK,1",
r"(?i)plock,\?" => "PLOCK,?",
r"(?i)r" => "R",
r"(?i)s,\?" => "S,?",
r"(?i)s,c" => "S,C",
r"(?i)s,k" => "S,K",
r"(?i)s,f" => "S,F",
r"(?i)status" => "STATUS",
r"(?i)sleep" => "SLEEP",
} else {
"300",
"1200",
"2400",
"9600",
"19200",
"38400",
"57600",
"115200",
r"[[0-9]&&^0][0-9]*" => TwoDigitMin,
} else {
r"[0-9]+" => IntStr,
} else {
r"[+-]?([0-9]+\.[0-9]+|[0-9]+(\.[0-9]+)?)" => FloatStr,
} else {
r"[0-9a-fA-F]{0,12}" => ImportExportStr,
_
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment