Common: Support >8 inputs

This commit is contained in:
Aode (lion) 2022-03-01 10:36:54 -06:00
parent 912ab75a31
commit 6badd6e386

View file

@ -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)
}
}
}