A custom OS for the xteink x4 ebook reader
at main 63 lines 1.8 kB view raw
1// button definitions and ADC resistance ladder decoding 2// two ADC ladders (Row1 GPIO1, Row2 GPIO2) plus power button (GPIO3) 3 4#[derive(Debug, Clone, Copy, PartialEq, Eq)] 5pub enum Button { 6 Right, 7 Left, 8 Confirm, 9 Back, 10 VolUp, 11 VolDown, 12 Power, 13} 14 15impl Button { 16 pub const fn name(self) -> &'static str { 17 match self { 18 Button::Right => "Right", 19 Button::Left => "Left", 20 Button::Confirm => "Confirm", 21 Button::Back => "Back", 22 Button::VolUp => "Vol Up", 23 Button::VolDown => "Vol Down", 24 Button::Power => "Power", 25 } 26 } 27} 28 29impl core::fmt::Display for Button { 30 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 31 f.write_str(self.name()) 32 } 33} 34 35pub const DEFAULT_TOLERANCE: u16 = 150; 36 37// (center_mv, tolerance_mv, button) 38pub const ROW1_THRESHOLDS: &[(u16, u16, Button)] = &[ 39 (3, 50, Button::Right), 40 (1113, DEFAULT_TOLERANCE, Button::Left), 41 (1984, DEFAULT_TOLERANCE, Button::Confirm), 42 (2556, DEFAULT_TOLERANCE, Button::Back), 43]; 44 45pub const ROW2_THRESHOLDS: &[(u16, u16, Button)] = &[ 46 (3, 50, Button::VolDown), 47 (1659, DEFAULT_TOLERANCE, Button::VolUp), 48]; 49 50impl Button { 51 /// Decode a button from an ADC millivolt reading using a resistance 52 /// ladder threshold table. Returns `None` if no threshold matches. 53 pub fn from_ladder(mv: u16, thresholds: &[(u16, u16, Button)]) -> Option<Button> { 54 for &(center, tolerance, button) in thresholds { 55 let low = center.saturating_sub(tolerance); 56 let high = center.saturating_add(tolerance); 57 if mv >= low && mv <= high { 58 return Some(button); 59 } 60 } 61 None 62 } 63}