-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use super::{DecodeError, Property}; | ||
use num_enum::IntoPrimitive; | ||
use std::fmt::{self, Display, Formatter}; | ||
|
||
#[derive(Copy, Clone, Debug, Eq, IntoPrimitive, PartialEq)] | ||
#[repr(u8)] | ||
pub enum ButtonEventType { | ||
Press = 0x01, | ||
DoublePress = 0x02, | ||
TriplePress = 0x03, | ||
LongPress = 0x04, | ||
LongDoublePress = 0x05, | ||
LongTriplePress = 0x06, | ||
} | ||
|
||
impl ButtonEventType { | ||
pub(crate) fn from_bytes(bytes: &[u8]) -> Result<Option<Self>, DecodeError> { | ||
match bytes { | ||
[0x00] => Ok(None), | ||
[0x01] => Ok(Some(Self::Press)), | ||
[0x02] => Ok(Some(Self::DoublePress)), | ||
[0x03] => Ok(Some(Self::TriplePress)), | ||
[0x04] => Ok(Some(Self::LongPress)), | ||
[0x05] => Ok(Some(Self::LongDoublePress)), | ||
[0x06] => Ok(Some(Self::LongTriplePress)), | ||
[value] => Err(DecodeError::InvalidEventType(*value)), | ||
[] => Err(DecodeError::PrematureEnd), | ||
_ => Err(DecodeError::ExtraData(bytes.to_owned())), | ||
} | ||
} | ||
|
||
pub fn as_str(&self) -> &'static str { | ||
match self { | ||
Self::Press => "press", | ||
Self::DoublePress => "double press", | ||
Self::TriplePress => "triple press", | ||
Self::LongPress => "long press", | ||
Self::LongDoublePress => "long double press", | ||
Self::LongTriplePress => "long triple press", | ||
} | ||
} | ||
} | ||
|
||
impl Display for ButtonEventType { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
f.write_str(self.as_str()) | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub enum DimmerEventType { | ||
RotateLeft(u8), | ||
RotateRight(u8), | ||
} | ||
|
||
impl DimmerEventType { | ||
pub(crate) fn from_bytes(bytes: &[u8]) -> Result<Option<Self>, DecodeError> { | ||
match bytes { | ||
[0x00] => Ok(None), | ||
[0x01, steps] => Ok(Some(Self::RotateLeft(*steps))), | ||
[0x02, steps] => Ok(Some(Self::RotateRight(*steps))), | ||
[value] => Err(DecodeError::InvalidEventType(*value)), | ||
[] => Err(DecodeError::PrematureEnd), | ||
_ => Err(DecodeError::ExtraData(bytes.to_owned())), | ||
} | ||
} | ||
} | ||
|
||
impl Display for DimmerEventType { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
match self { | ||
Self::RotateLeft(steps) => write!(f, "rotate left {} steps", steps), | ||
Self::RotateRight(steps) => write!(f, "rotate right {} steps", steps), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub enum Event { | ||
Button(Option<ButtonEventType>), | ||
Dimmer(Option<DimmerEventType>), | ||
} | ||
|
||
impl Event { | ||
pub fn property(&self) -> Property { | ||
match self { | ||
Self::Button(_) => Property::ButtonEvent, | ||
Self::Dimmer(_) => Property::DimmerEvent, | ||
} | ||
} | ||
} |