37 lines
1.4 KiB
Rust

use anyhow::anyhow;
use prometeu_core::model::ButtonId;
/// Mapping of physical button names to their virtual Button ID.
///
/// These constants match the `ButtonId` enum in `prometeu-core`.
pub const BTN_UP: u32 = ButtonId::Up as u32;
pub const BTN_DOWN: u32 = ButtonId::Down as u32;
pub const BTN_LEFT: u32 = ButtonId::Left as u32;
pub const BTN_RIGHT: u32 = ButtonId::Right as u32;
pub const BTN_A: u32 = ButtonId::A as u32;
pub const BTN_B: u32 = ButtonId::B as u32;
pub const BTN_X: u32 = ButtonId::X as u32;
pub const BTN_Y: u32 = ButtonId::Y as u32;
pub const BTN_L: u32 = ButtonId::L as u32;
pub const BTN_R: u32 = ButtonId::R as u32;
pub const BTN_START: u32 = ButtonId::Start as u32;
pub const BTN_SELECT: u32 = ButtonId::Select as u32;
/// Translates a string identifier (e.g., "up") into a numeric Button ID.
pub fn map_btn_name(btn_name: &str) -> anyhow::Result<u32> {
match btn_name.to_lowercase().as_str() {
"up" => Ok(BTN_UP),
"down" => Ok(BTN_DOWN),
"left" => Ok(BTN_LEFT),
"right" => Ok(BTN_RIGHT),
"a" => Ok(BTN_A),
"b" => Ok(BTN_B),
"x" => Ok(BTN_X),
"y" => Ok(BTN_Y),
"l" => Ok(BTN_L),
"r" => Ok(BTN_R),
"start" => Ok(BTN_START),
"select" => Ok(BTN_SELECT),
_ => Err(anyhow!("Unsupported button: {}. Expected one of: up, down, left, right, a, b, x, y, l, r, start, select.", btn_name)),
}
}