launchkey_sdk/launchkey/surface/
encoders.rs

1use crate::bidirectional_enum_mappings_with_mode;
2use strum::IntoEnumIterator;
3use strum_macros::EnumIter;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter)]
6pub enum Encoder {
7    Encoder1,
8    Encoder2,
9    Encoder3,
10    Encoder4,
11    Encoder5,
12    Encoder6,
13    Encoder7,
14    Encoder8,
15}
16
17bidirectional_enum_mappings_with_mode!(Encoder, u8, EncoderCCIndex,
18    {
19        Absolute => {
20            Encoder1 => 0x15,
21            Encoder2 => 0x16,
22            Encoder3 => 0x17,
23            Encoder4 => 0x18,
24            Encoder5 => 0x19,
25            Encoder6 => 0x1A,
26            Encoder7 => 0x1B,
27            Encoder8 => 0x1C,
28        },
29        Relative => {
30            Encoder1 => 0x55,
31            Encoder2 => 0x56,
32            Encoder3 => 0x57,
33            Encoder4 => 0x58,
34            Encoder5 => 0x59,
35            Encoder6 => 0x5A,
36            Encoder7 => 0x5B,
37            Encoder8 => 0x5C,
38        }
39    }
40);
41
42impl Encoder {
43    /// Get the correct Control Change index based on whether the encoder is in absolute or relative mode
44    pub fn to_index(self, is_absolute: bool) -> u8 {
45        if is_absolute {
46            self.to_value_mode(EncoderCCIndex::Absolute)
47        } else {
48            self.to_value_mode(EncoderCCIndex::Relative)
49        }
50    }
51
52    /// Get all possible variants of `Encoder`.
53    pub fn all() -> impl Iterator<Item = Self> {
54        Self::iter()
55    }
56
57    /// Get the count of all variants
58    pub fn count() -> usize {
59        Self::all().count()
60    }
61
62    /// Safely get a variant by its index
63    pub fn from_index(index: usize) -> Option<Self> {
64        Self::all().nth(index)
65    }
66}