diff --git a/common/src/buttons.rs b/common/src/buttons.rs index 96dea03..ae6bd9f 100644 --- a/common/src/buttons.rs +++ b/common/src/buttons.rs @@ -81,13 +81,23 @@ where } fn calculate_press_state(&self) -> u8 { - self.current_states.iter().rev().fold(0u8, |acc, state| { - // state keeps track of HIGH, but we | on low - if *state { - acc << 1 - } else { - (acc << 1) | 1 - } - }) + if self.current_states.len() <= 8 { + // For 8 or fewer inputs, assume we're priority-encoded and build a byte with bitflags + self.current_states.iter().rev().fold(0u8, |acc, state| { + // state keeps track of HIGH, but we | on low + if *state { + acc << 1 + } else { + (acc << 1) | 1 + } + }) + } else { + // For more than 8 inputs, assume buttons are 1-to-1 and send the first found press + self.current_states + .iter() + .enumerate() + .find_map(|(i, state)| if *state { None } else { Some(i as u8) }) + .unwrap_or(0) + } } }