Skip to content

Commit 8ed31a1

Browse files
authored
descriptor: add From<u8> impl for key enums (twitchyliquid64#56)
Adds `From<u8>` impls for `KeyboardUsage`, `MediaKey`, and `SystemControlKey`. Adds `Reserved` variants to handle reserved and invalid values.
1 parent afb5041 commit 8ed31a1

File tree

1 file changed

+308
-2
lines changed

1 file changed

+308
-2
lines changed

src/descriptor.rs

Lines changed: 308 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,237 @@ pub enum KeyboardUsage {
547547
KeyboardRightAlt = 0xE6,
548548
/// Keyboard RightGUI (Footnote 11) (Footnote 34)
549549
KeyboardRightGUI = 0xE7,
550+
/// Reserved keyboard values (used for all reserved / invalid values)
551+
Reserved = 0xE8,
550552
// 0xE8-0xFF: Reserved
551553
}
552554

555+
impl From<u8> for KeyboardUsage {
556+
fn from(k: u8) -> Self {
557+
match k {
558+
0x01 => Self::KeyboardErrorRollOver,
559+
0x02 => Self::KeyboardPOSTFail,
560+
0x03 => Self::KeyboardErrorUndefined,
561+
0x04 => Self::KeyboardAa,
562+
0x05 => Self::KeyboardBb,
563+
0x06 => Self::KeyboardCc,
564+
0x07 => Self::KeyboardDd,
565+
0x08 => Self::KeyboardEe,
566+
0x09 => Self::KeyboardFf,
567+
0x0A => Self::KeyboardGg,
568+
0x0B => Self::KeyboardHh,
569+
0x0C => Self::KeyboardIi,
570+
0x0D => Self::KeyboardJj,
571+
0x0E => Self::KeyboardKk,
572+
0x0F => Self::KeyboardLl,
573+
0x10 => Self::KeyboardMm,
574+
0x11 => Self::KeyboardNn,
575+
0x12 => Self::KeyboardOo,
576+
0x13 => Self::KeyboardPp,
577+
0x14 => Self::KeyboardQq,
578+
0x15 => Self::KeyboardRr,
579+
0x16 => Self::KeyboardSs,
580+
0x17 => Self::KeyboardTt,
581+
0x18 => Self::KeyboardUu,
582+
0x19 => Self::KeyboardVv,
583+
0x1A => Self::KeyboardWw,
584+
0x1B => Self::KeyboardXx,
585+
0x1C => Self::KeyboardYy,
586+
0x1D => Self::KeyboardZz,
587+
0x1E => Self::Keyboard1Exclamation,
588+
0x1F => Self::Keyboard2At,
589+
0x20 => Self::Keyboard3Hash,
590+
0x21 => Self::Keyboard4Dollar,
591+
0x22 => Self::Keyboard5Percent,
592+
0x23 => Self::Keyboard6Caret,
593+
0x24 => Self::Keyboard7Ampersand,
594+
0x25 => Self::Keyboard8Asterisk,
595+
0x26 => Self::Keyboard9OpenParens,
596+
0x27 => Self::Keyboard0CloseParens,
597+
0x28 => Self::KeyboardEnter,
598+
0x29 => Self::KeyboardEscape,
599+
0x2A => Self::KeyboardBackspace,
600+
0x2B => Self::KeyboardTab,
601+
0x2C => Self::KeyboardSpacebar,
602+
0x2D => Self::KeyboardDashUnderscore,
603+
0x2E => Self::KeyboardEqualPlus,
604+
0x2F => Self::KeyboardOpenBracketBrace,
605+
0x30 => Self::KeyboardCloseBracketBrace,
606+
0x31 => Self::KeyboardBackslashBar,
607+
0x32 => Self::KeyboardNonUSHash,
608+
0x33 => Self::KeyboardSemiColon,
609+
0x34 => Self::KeyboardSingleDoubleQuote,
610+
0x35 => Self::KeyboardBacktickTilde,
611+
0x36 => Self::KeyboardCommaLess,
612+
0x37 => Self::KeyboardPeriodGreater,
613+
0x38 => Self::KeyboardSlashQuestion,
614+
0x39 => Self::KeyboardCapsLock,
615+
0x3A => Self::KeyboardF1,
616+
0x3B => Self::KeyboardF2,
617+
0x3C => Self::KeyboardF3,
618+
0x3D => Self::KeyboardF4,
619+
0x3E => Self::KeyboardF5,
620+
0x3F => Self::KeyboardF6,
621+
0x40 => Self::KeyboardF7,
622+
0x41 => Self::KeyboardF8,
623+
0x42 => Self::KeyboardF9,
624+
0x43 => Self::KeyboardF10,
625+
0x44 => Self::KeyboardF11,
626+
0x45 => Self::KeyboardF12,
627+
0x46 => Self::KeyboardPrintScreen,
628+
0x47 => Self::KeyboardScrollLock,
629+
0x48 => Self::KeyboardPause,
630+
0x49 => Self::KeyboardInsert,
631+
0x4A => Self::KeyboardHome,
632+
0x4B => Self::KeyboardPageUp,
633+
0x4C => Self::KeyboardDelete,
634+
0x4D => Self::KeyboardEnd,
635+
0x4E => Self::KeyboardPageDown,
636+
0x4F => Self::KeyboardRightArrow,
637+
0x50 => Self::KeyboardLeftArrow,
638+
0x51 => Self::KeyboardDownArrow,
639+
0x52 => Self::KeyboardUpArrow,
640+
0x53 => Self::KeypadNumLock,
641+
0x54 => Self::KeypadDivide,
642+
0x55 => Self::KeypadMultiply,
643+
0x56 => Self::KeypadMinus,
644+
0x57 => Self::KeypadPlus,
645+
0x58 => Self::KeypadEnter,
646+
0x59 => Self::Keypad1End,
647+
0x5A => Self::Keypad2DownArrow,
648+
0x5B => Self::Keypad3PageDown,
649+
0x5C => Self::Keypad4LeftArrow,
650+
0x5D => Self::Keypad5,
651+
0x5E => Self::Keypad6RightArrow,
652+
0x5F => Self::Keypad7Home,
653+
0x60 => Self::Keypad8UpArrow,
654+
0x61 => Self::Keypad9PageUp,
655+
0x62 => Self::Keypad0Insert,
656+
0x63 => Self::KeypadPeriodDelete,
657+
0x64 => Self::KeyboardNonUSSlash,
658+
0x65 => Self::KeyboardApplication,
659+
0x66 => Self::KeyboardPower,
660+
0x67 => Self::KeypadEqual,
661+
0x68 => Self::KeyboardF13,
662+
0x69 => Self::KeyboardF14,
663+
0x6A => Self::KeyboardF15,
664+
0x6B => Self::KeyboardF16,
665+
0x6C => Self::KeyboardF17,
666+
0x6D => Self::KeyboardF18,
667+
0x6E => Self::KeyboardF19,
668+
0x6F => Self::KeyboardF20,
669+
0x70 => Self::KeyboardF21,
670+
0x71 => Self::KeyboardF22,
671+
0x72 => Self::KeyboardF23,
672+
0x73 => Self::KeyboardF24,
673+
0x74 => Self::KeyboardExecute,
674+
0x75 => Self::KeyboardHelp,
675+
0x76 => Self::KeyboardMenu,
676+
0x77 => Self::KeyboardSelect,
677+
0x78 => Self::KeyboardStop,
678+
0x79 => Self::KeyboardAgain,
679+
0x7A => Self::KeyboardUndo,
680+
0x7B => Self::KeyboardCut,
681+
0x7C => Self::KeyboardCopy,
682+
0x7D => Self::KeyboardPaste,
683+
0x7E => Self::KeyboardFind,
684+
0x7F => Self::KeyboardMute,
685+
0x80 => Self::KeyboardVolumeUp,
686+
0x81 => Self::KeyboardVolumeDown,
687+
0x82 => Self::KeyboardLockingCapsLock,
688+
0x83 => Self::KeyboardLockingNumLock,
689+
0x84 => Self::KeyboardLockingScrollLock,
690+
0x85 => Self::KeypadComma,
691+
0x86 => Self::KeypadEqualSign,
692+
0x87 => Self::KeyboardInternational1,
693+
0x88 => Self::KeyboardInternational2,
694+
0x89 => Self::KeyboardInternational3,
695+
0x8A => Self::KeyboardInternational4,
696+
0x8B => Self::KeyboardInternational5,
697+
0x8C => Self::KeyboardInternational6,
698+
0x8D => Self::KeyboardInternational7,
699+
0x8E => Self::KeyboardInternational8,
700+
0x8F => Self::KeyboardInternational9,
701+
0x90 => Self::KeyboardLANG1,
702+
0x91 => Self::KeyboardLANG2,
703+
0x92 => Self::KeyboardLANG3,
704+
0x93 => Self::KeyboardLANG4,
705+
0x94 => Self::KeyboardLANG5,
706+
0x95 => Self::KeyboardLANG6,
707+
0x96 => Self::KeyboardLANG7,
708+
0x97 => Self::KeyboardLANG8,
709+
0x98 => Self::KeyboardLANG9,
710+
0x99 => Self::KeyboardAlternateErase,
711+
0x9A => Self::KeyboardSysReqAttention,
712+
0x9B => Self::KeyboardCancel,
713+
0x9C => Self::KeyboardClear,
714+
0x9D => Self::KeyboardPrior,
715+
0x9E => Self::KeyboardReturn,
716+
0x9F => Self::KeyboardSeparator,
717+
0xA0 => Self::KeyboardOut,
718+
0xA1 => Self::KeyboardOper,
719+
0xA2 => Self::KeyboardClearAgain,
720+
0xA3 => Self::KeyboardCrSelProps,
721+
0xA4 => Self::KeyboardExSel,
722+
0xB0 => Self::Keypad00,
723+
0xB1 => Self::Keypad000,
724+
0xB2 => Self::ThousandsSeparator,
725+
0xB3 => Self::DecimalSeparator,
726+
0xB4 => Self::CurrencyUnit,
727+
0xB5 => Self::CurrencySubunit,
728+
0xB6 => Self::KeypadOpenParens,
729+
0xB7 => Self::KeypadCloseParens,
730+
0xB8 => Self::KeypadOpenBrace,
731+
0xB9 => Self::KeypadCloseBrace,
732+
0xBA => Self::KeypadTab,
733+
0xBB => Self::KeypadBackspace,
734+
0xBC => Self::KeypadA,
735+
0xBD => Self::KeypadB,
736+
0xBE => Self::KeypadC,
737+
0xBF => Self::KeypadD,
738+
0xC0 => Self::KeypadE,
739+
0xC1 => Self::KeypadF,
740+
0xC2 => Self::KeypadBitwiseXor,
741+
0xC3 => Self::KeypadLogicalXor,
742+
0xC4 => Self::KeypadModulo,
743+
0xC5 => Self::KeypadLeftShift,
744+
0xC6 => Self::KeypadRightShift,
745+
0xC7 => Self::KeypadBitwiseAnd,
746+
0xC8 => Self::KeypadLogicalAnd,
747+
0xC9 => Self::KeypadBitwiseOr,
748+
0xCA => Self::KeypadLogicalOr,
749+
0xCB => Self::KeypadColon,
750+
0xCC => Self::KeypadHash,
751+
0xCD => Self::KeypadSpace,
752+
0xCE => Self::KeypadAt,
753+
0xCF => Self::KeypadExclamation,
754+
0xD0 => Self::KeypadMemoryStore,
755+
0xD1 => Self::KeypadMemoryRecall,
756+
0xD2 => Self::KeypadMemoryClear,
757+
0xD3 => Self::KeypadMemoryAdd,
758+
0xD4 => Self::KeypadMemorySubtract,
759+
0xD5 => Self::KeypadMemoryMultiply,
760+
0xD6 => Self::KeypadMemoryDivide,
761+
0xD7 => Self::KeypadPositiveNegative,
762+
0xD8 => Self::KeypadClear,
763+
0xD9 => Self::KeypadClearEntry,
764+
0xDA => Self::KeypadBinary,
765+
0xDB => Self::KeypadOctal,
766+
0xDC => Self::KeypadDecimal,
767+
0xDD => Self::KeypadHexadecimal,
768+
0xE0 => Self::KeyboardLeftControl,
769+
0xE1 => Self::KeyboardLeftShift,
770+
0xE2 => Self::KeyboardLeftAlt,
771+
0xE3 => Self::KeyboardLeftGUI,
772+
0xE4 => Self::KeyboardRightControl,
773+
0xE5 => Self::KeyboardRightShift,
774+
0xE6 => Self::KeyboardRightAlt,
775+
0xE7 => Self::KeyboardRightGUI,
776+
_ => Self::Reserved,
777+
}
778+
}
779+
}
780+
553781
/// MediaKeyboardReport describes a report and descriptor that can be used to
554782
/// send consumer control commands to the host.
555783
///
@@ -573,7 +801,7 @@ pub struct MediaKeyboardReport {
573801
/// Media player usage ids that can be used in MediaKeyboardReport
574802
#[non_exhaustive]
575803
#[repr(u16)]
576-
#[derive(Debug)]
804+
#[derive(Clone, Copy, Debug, PartialEq)]
577805
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
578806
pub enum MediaKey {
579807
Zero = 0x00,
@@ -589,6 +817,7 @@ pub enum MediaKey {
589817
Mute = 0xE2,
590818
VolumeIncrement = 0xE9,
591819
VolumeDecrement = 0xEA,
820+
Reserved = 0xEB,
592821
}
593822

594823
impl From<MediaKey> for u16 {
@@ -597,6 +826,33 @@ impl From<MediaKey> for u16 {
597826
}
598827
}
599828

829+
impl From<u8> for MediaKey {
830+
fn from(k: u8) -> Self {
831+
match k {
832+
0x00 => Self::Zero,
833+
0xB0 => Self::Play,
834+
0xB1 => Self::Pause,
835+
0xB2 => Self::Record,
836+
0xB5 => Self::NextTrack,
837+
0xB6 => Self::PrevTrack,
838+
0xB7 => Self::Stop,
839+
0xB9 => Self::RandomPlay,
840+
0xBC => Self::Repeat,
841+
0xCD => Self::PlayPause,
842+
0xE2 => Self::Mute,
843+
0xE9 => Self::VolumeIncrement,
844+
0xEA => Self::VolumeDecrement,
845+
_ => Self::Reserved,
846+
}
847+
}
848+
}
849+
850+
impl From<u16> for MediaKey {
851+
fn from(k: u16) -> Self {
852+
(k as u8).into()
853+
}
854+
}
855+
600856
/// SystemControlReport describes a report and descriptor that can be used to
601857
/// send system control commands to the host.
602858
///
@@ -622,7 +878,7 @@ pub struct SystemControlReport {
622878
/// System control usage ids to use with SystemControlReport
623879
#[non_exhaustive]
624880
#[repr(u8)]
625-
#[derive(Debug)]
881+
#[derive(Clone, Copy, Debug, PartialEq)]
626882
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
627883
pub enum SystemControlKey {
628884
PowerDown = 0x81,
@@ -665,10 +921,60 @@ pub enum SystemControlKey {
665921
DisplayToggleInternalExternal = 0xB5,
666922
DisplaySwapPrimarySecondary = 0xB6,
667923
DisplayLcdAutoscale = 0xB7,
924+
// Use this reserved value to represent all reserved keys / invalid values
925+
Reserved = 0xB8,
668926
}
669927

670928
impl From<SystemControlKey> for u8 {
671929
fn from(sck: SystemControlKey) -> u8 {
672930
sck as u8
673931
}
674932
}
933+
934+
impl From<u8> for SystemControlKey {
935+
fn from(k: u8) -> Self {
936+
match k {
937+
0x81 => Self::PowerDown,
938+
0x82 => Self::Sleep,
939+
0x83 => Self::WakeUp,
940+
0x84 => Self::ContextMenu,
941+
0x85 => Self::MainMenu,
942+
0x86 => Self::AppMenu,
943+
0x87 => Self::MenuHelp,
944+
0x88 => Self::MenuExit,
945+
0x89 => Self::MenuSelect,
946+
0x8A => Self::MenuRight,
947+
0x8B => Self::MenuLeft,
948+
0x8C => Self::MenuUp,
949+
0x8D => Self::MenuDown,
950+
0x8E => Self::ColdRestart,
951+
0x8F => Self::WarmRestart,
952+
0x90 => Self::DpadUp,
953+
0x91 => Self::DpadDown,
954+
0x92 => Self::DpadRight,
955+
0x93 => Self::DpadLeft,
956+
0x97 => Self::SystemFunctionShift,
957+
0x98 => Self::SystemFunctionShiftLock,
958+
0x9A => Self::SystemDismissNotification,
959+
0x9B => Self::SystemDoNotDisturb,
960+
0xA0 => Self::Dock,
961+
0xA1 => Self::Undock,
962+
0xA2 => Self::Setup,
963+
0xA3 => Self::Break,
964+
0xA4 => Self::DebuggerBreak,
965+
0xA5 => Self::ApplicationBreak,
966+
0xA6 => Self::ApplicationDebuggerBreak,
967+
0xA7 => Self::SpeakerMute,
968+
0xA8 => Self::Hibernate,
969+
0xB0 => Self::DisplayInvert,
970+
0xB1 => Self::DisplayInternal,
971+
0xB2 => Self::DisplayExternal,
972+
0xB3 => Self::DisplayBoth,
973+
0xB4 => Self::DisplayDual,
974+
0xB5 => Self::DisplayToggleInternalExternal,
975+
0xB6 => Self::DisplaySwapPrimarySecondary,
976+
0xB7 => Self::DisplayLcdAutoscale,
977+
_ => Self::Reserved,
978+
}
979+
}
980+
}

0 commit comments

Comments
 (0)