diff --git a/src/command/bc125at/backlight.rs b/src/command/bc125at/backlight.rs new file mode 100644 index 0000000..51f41fa --- /dev/null +++ b/src/command/bc125at/backlight.rs @@ -0,0 +1,15 @@ +use crate::{ + OkResponse, + command::{command, macros::enum_param_response}, +}; + +enum_param_response!(Backlight { + AlwaysOn => b"AO", + AlwaysOff => b"AF", + Keypress => b"KY", + Squelch => b"SQ", + KeypressOrSquelch => b"KS", +}: BacklightError("invalid backlight setting")); + +command!(b"BLT": GetBacklight => Backlight); +command!(b"BLT": SetBacklight(Backlight) => OkResponse); diff --git a/src/command/bc125at/mod.rs b/src/command/bc125at/mod.rs index 0da677d..ff078c9 100644 --- a/src/command/bc125at/mod.rs +++ b/src/command/bc125at/mod.rs @@ -1,3 +1,4 @@ +mod backlight; mod battery_charge_time; mod firmware_version; mod lcd_contrast; @@ -5,6 +6,7 @@ mod program_mode; mod squelch; mod volume; +pub use backlight::{Backlight, BacklightError, GetBacklight, SetBacklight}; pub use battery_charge_time::{ BatteryChargeTime, BatteryChargeTimeError, GetBatteryInfo, SetBatteryInfo, }; diff --git a/src/command/macros/enum_param_response.rs b/src/command/macros/enum_param_response.rs new file mode 100644 index 0000000..cf2be6d --- /dev/null +++ b/src/command/macros/enum_param_response.rs @@ -0,0 +1,61 @@ +macro_rules! enum_param_response { + ($name:ident { $($variant:ident => $val:literal),+ $(,)? } : $error:ident($msg:literal)) => { + #[derive( + ::core::clone::Clone, + ::core::marker::Copy, + ::core::fmt::Debug, + ::core::cmp::PartialEq, + ::core::cmp::Eq, + )] + pub enum $name { + $($variant),+ + } + + impl crate::command::Params for $name { + fn count(&self) -> usize { + 1 + } + + fn max_size(&self) -> usize { + 2 + } + + fn serialize_to(&self, mut buffer: crate::command::ParamBuffer) { + let txt = match self { + $(Self::$variant => $val,)+ + }; + buffer.serialize_param(txt); + } + } + + #[derive( + ::core::clone::Clone, + ::core::marker::Copy, + ::core::fmt::Debug, + ::core::cmp::PartialEq, + ::core::cmp::Eq, + ::thiserror::Error, + )] + #[error($msg)] + pub struct $error; + + impl crate::command::Response for $name { + type Error = $error; + + fn deserialize(raw_values: &[::tokio_util::bytes::Bytes]) -> ::core::result::Result { + let val = match raw_values[0].as_ref() { + $($val => Self::$variant,)+ + _ => return ::core::result::Result::Err($error), + }; + Ok(val) + } + + + fn expected_field_count() -> usize { + 1 + } + } + }; +} + +pub(crate) use enum_param_response; diff --git a/src/command/macros/mod.rs b/src/command/macros/mod.rs index 85403e1..045411d 100644 --- a/src/command/macros/mod.rs +++ b/src/command/macros/mod.rs @@ -1,9 +1,11 @@ mod command; +mod enum_param_response; mod range_param; mod range_response; mod string_response; pub(crate) use command::command; +pub(crate) use enum_param_response::enum_param_response; pub(crate) use range_param::range_param; pub(crate) use range_response::range_response; pub(crate) use string_response::string_response;