namespace RioJoy.Core.Mapping;
///
/// Translates raw RIO events into an iRIO table address. The 72 digital
/// buttons map to 0x00–0x47 directly; keypad 0 is offset by 0x50
/// and keypad 1 by 0x60 (port of KeypadEvent, riovjoy2.cpp#L1185);
/// see docs/PROTOCOL.md §5.
///
public static class RioAddress
{
/// Number of digital button inputs (addresses 0x00–0x47).
public const int ButtonCount = 72;
/// Address offset added to keypad-0 key indices.
public const int Keypad0Base = 0x50;
/// Address offset added to keypad-1 key indices.
public const int Keypad1Base = 0x60;
/// Highest valid address (keypad 1, index 0x0F).
public const int MaxAddress = Keypad1Base + 0x0F; // 0x6F
/// Size of the iRIO table (addresses 0x00..0x6F inclusive).
public const int TableSize = MaxAddress + 1; // 112
/// Address for a digital button event ( 0x00–0x47).
public static int FromButton(byte index)
{
if (index >= ButtonCount)
throw new ArgumentOutOfRangeException(
nameof(index), $"Button index must be 0..{ButtonCount - 1}.");
return index;
}
///
/// Address for a keypad key event: 0 or 1,
/// 0x00–0x0F. Mirrors the legacy offsets (+0x50 / +0x60).
///
public static int FromKeypad(byte pad, byte index)
{
if (index > 0x0F)
throw new ArgumentOutOfRangeException(nameof(index), "Keypad index must be 0..0x0F.");
return pad switch
{
0 => Keypad0Base + index,
1 => Keypad1Base + index,
_ => throw new ArgumentOutOfRangeException(nameof(pad), "Keypad must be 0 or 1."),
};
}
}