public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length != PACKET_SIZE) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], packet[i] != 0x00); } float x = ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length)); float y = ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 8)); state.SetAnalog("stick_x", x); state.SetAnalog("stick_y", y); SignalTool.SetMouseProperties(x, y, state); return(state.Build()); }
public static ControllerState ReadFromPacket_SNES(byte[] packet) { if (packet.Length < BUTTONS_SNES.Length) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS_SNES.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS_SNES[i])) { continue; } state.SetButton(BUTTONS_SNES[i], packet[i] != 0x00); } if (state != null && packet.Length == 32 && packet[15] != 0x00) { float y = (float)(SignalTool.ReadByte(packet, 17, 7, 0x1) * ((packet[16] & 0x1) != 0 ? 1 : -1)) / 127; float x = (float)(SignalTool.ReadByte(packet, 25, 7, 0x1) * ((packet[24] & 0x1) != 0 ? -1 : 1)) / 127; SignalTool.SetMouseProperties(x, y, state); } return(state.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length != PACKET_SIZE) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], packet[i] != 0x00); } state.SetAnalog("lstick_x", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length))); state.SetAnalog("lstick_y", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 8))); state.SetAnalog("cstick_x", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 16))); state.SetAnalog("cstick_y", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 24))); state.SetAnalog("trig_l", ReadTrigger(SignalTool.ReadByte(packet, BUTTONS.Length + 32))); state.SetAnalog("trig_r", ReadTrigger(SignalTool.ReadByte(packet, BUTTONS.Length + 40))); return(state.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < PACKET_SIZE) { return(null); } byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE]; for (int i = 0; i < 16; ++i) { polishedPacket[i] = (byte)((packet[i] == 0x31) ? 1 : 0); } for (int i = 0; i < 2; ++i) { packet[16 + i] = 0; for (byte j = 0; j < 8; ++j) { polishedPacket[16 + i] |= (byte)((packet[16 + (i * 8 + j)] == 0x30 ? 0 : 1) << j); } } short[] sticks = new short[4]; for (int i = 0; i < 4; ++i) { sticks[i] = 0; for (byte j = 0; j < 16; ++j) { sticks[i] |= (short)((packet[32 + (i * 16 + j)] == 0x30 ? 0 : 1) << j); } } ControllerStateBuilder outState = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } outState.SetButton(BUTTONS[i], polishedPacket[i] != 0x00); } outState.SetAnalog("trig_l", ReadTrigger(polishedPacket[16])); outState.SetAnalog("trig_r", ReadTrigger(polishedPacket[17])); outState.SetAnalog("rstick_x", ReadStick(sticks[2])); outState.SetAnalog("rstick_y", ReadStick(sticks[3])); outState.SetAnalog("lstick_x", ReadStick(sticks[0])); outState.SetAnalog("lstick_y", ReadStick(sticks[1])); return(outState.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < 1) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); if (packet[0] == 0) { if (packet.Length < PACKET_SIZE) { return(null); } for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], packet[i] != 0x00); } } else { if (packet.Length < MOUSE_PACKET_SIZE) { return(null); } for (int i = 0; i < MOUSE_BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(MOUSE_BUTTONS[i])) { continue; } state.SetButton(MOUSE_BUTTONS[i], packet[i] != 0x00); } bool ySign = packet[11] != 0; byte yVal = SignalTool.ReadByte(packet, 14, 7); bool xSign = packet[21] != 0; byte xVal = SignalTool.ReadByte(packet, 24, 7); float x = ReadMouse(xSign, xVal); float y = ReadMouse(ySign, yVal); SignalTool.SetMouseProperties(x, y, state); } return(state.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < PACKET_SIZE) { return(null); } byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE]; for (int i = 0; i < 24; ++i) { polishedPacket[i] = (byte)((packet[i] == 0x31) ? 1 : 0); } for (int i = 0; i < 16; ++i) { polishedPacket[24 + i] = 0; for (byte j = 0; j < 8; ++j) { polishedPacket[24 + i] |= (byte)((packet[24 + (i * 8 + j)] == 0x30 ? 0 : 1) << j); } } ControllerStateBuilder outState = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } outState.SetButton(BUTTONS[i], polishedPacket[i] != 0x00); } outState.SetAnalog("rstick_x", ReadStick(polishedPacket[26])); outState.SetAnalog("rstick_y", ReadStick(polishedPacket[27])); outState.SetAnalog("lstick_x", ReadStick(polishedPacket[24])); outState.SetAnalog("lstick_y", ReadStick(polishedPacket[25])); outState.SetAnalog("analog_up", ReadAnalogButton(polishedPacket[28])); outState.SetAnalog("analog_right", ReadAnalogButton(polishedPacket[29])); outState.SetAnalog("analog_down", ReadAnalogButton(polishedPacket[30])); outState.SetAnalog("analog_left", ReadAnalogButton(polishedPacket[31])); outState.SetAnalog("l_trig", ReadAnalogButton(polishedPacket[32])); outState.SetAnalog("r_trig", ReadAnalogButton(polishedPacket[33])); outState.SetAnalog("analog_l1", ReadAnalogButton(polishedPacket[34])); outState.SetAnalog("analog_r1", ReadAnalogButton(polishedPacket[35])); outState.SetAnalog("analog_triangle", ReadAnalogButton(polishedPacket[36])); outState.SetAnalog("analog_circle", ReadAnalogButton(polishedPacket[37])); outState.SetAnalog("analog_x", ReadAnalogButton(polishedPacket[38])); outState.SetAnalog("analog_square", ReadAnalogButton(polishedPacket[39])); return(outState.Build()); }
static public ControllerStateEventArgs ReadFromPacket(byte[] packet) { if (packet.Length < PACKET_SIZE) { return(null); } byte[] reconstructedPacket = new byte[34]; int j = 0; for (int i = 0; i < 34; ++i) { reconstructedPacket[i] = (byte)((packet[j] >> 4) | packet[j + 1]); j += 2; } byte[] polishedPacket = new byte[256]; int checksum = 0; for (int i = 0; i < 32; ++i) { checksum += reconstructedPacket[i]; for (int k = 0; k < 8; ++k) { polishedPacket[(i * 8) + k] = (byte)((reconstructedPacket[i] & (1 << k)) != 0 ? 1 : 0); } } var state = new ControllerStateBuilder(); short sentChecksum = (short)((reconstructedPacket[33] << 8) | reconstructedPacket[32]); if (checksum != sentChecksum) { state.SetButton("ChecksumFail", true); return(state.Build()); } for (int i = 0; i < KEYS.Length; ++i) { if (string.IsNullOrEmpty(KEYS[i])) { continue; } state.SetButton(KEYS[i], polishedPacket[i] != 0x00); state.SetButton("ChecksumFail", false); } return(state.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length != PACKET_SIZE) { return(null); } byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE]; polishedPacket[0] = (byte)((packet[0] >> 4) | packet[1]); polishedPacket[1] = (byte)(packet[9] == 0 ? 0 : 1); polishedPacket[2] = (byte)(packet[17] == 0 ? 0 : 1); for (int i = 18; i < 29; ++i) { polishedPacket[i - 15] = (byte)(packet[i] == 0 ? 0 : 1); } for (byte i = 0; i < 7; ++i) { polishedPacket[14] |= (byte)((packet[i + 2] == 0 ? 0 : 1) << i); } for (byte i = 0; i < 7; ++i) { polishedPacket[15] |= (byte)((packet[i + 10] == 0 ? 0 : 1) << i); } ControllerStateBuilder state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], polishedPacket[i] == 0x00); } float x = 0; float y = 0; if (packet[29] == 1) // This is the "Has Data" bit. Reset the mouse on cached results. { y = ReadMouse(polishedPacket[14]); x = ReadMouse(polishedPacket[15]); } SignalTool.SetMouseProperties(x, y, state); return(state.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length != PACKET_SIZE && packet.Length != MOUSE_PACKET_SIZE) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); if (packet.Length == PACKET_SIZE) { for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], packet[i] != 0x00); } state.SetButton("1", packet[5] != 0); state.SetButton("2", packet[6] != 0); } else if (packet.Length == MOUSE_PACKET_SIZE) { for (int i = 0; i < MOUSE_BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(MOUSE_BUTTONS[i])) { continue; } state.SetButton(MOUSE_BUTTONS[i], packet[i] != 0x00); } bool xSign = packet[4] != 0; bool ySign = packet[5] != 0; bool xOver = packet[6] != 0; bool yOver = packet[7] != 0; byte xVal = SignalTool.ReadByteBackwards(packet, 8); byte yVal = SignalTool.ReadByteBackwards(packet, 16); float x = ReadMouse(xSign, xOver, xVal); float y = ReadMouse(ySign, yOver, yVal); SignalTool.SetMouseProperties(x, y, state); } return(state.Build()); }
static public ControllerState ReadFromPacket(byte[] packet) { var state = new ControllerStateBuilder(); switch (packet.Length) { // Standard 64 bit packet size case PACKET_SIZE: for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], packet[i] != 0x00); } state.SetAnalog("lstick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length))); state.SetAnalog("lstick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 8))); state.SetAnalog("cstick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length + 16))); state.SetAnalog("cstick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 24))); state.SetAnalog("trig_l", readTrigger(SignalTool.readByte(packet, BUTTONS.Length + 32))); state.SetAnalog("trig_r", readTrigger(SignalTool.readByte(packet, BUTTONS.Length + 40))); break; // Packets are written as bytes when writing from the NicoHood API, so we're looking for a packet size of 8 (interpreted as bytes) case NICOHOOD_PACKET_SIZE: for (int i = 0; i < 16; i++) { if (string.IsNullOrEmpty(NICOHOOD_BUTTONS[i])) { continue; } int bitPacket = (packet[i / 8] >> (i % 8)) & 0x1; state.SetButton(NICOHOOD_BUTTONS[i], bitPacket != 0x00); } state.SetAnalog("lstick_x", readStick(packet[2])); state.SetAnalog("lstick_y", readStick(packet[3])); state.SetAnalog("cstick_x", readStick(packet[4])); state.SetAnalog("cstick_y", readStick(packet[5])); state.SetAnalog("trig_l", readTrigger(packet[6])); state.SetAnalog("trig_r", readTrigger(packet[7])); break; default: return(null); } return(state.Build()); }
static ControllerState readPacketButtons(byte[] packet, string[] buttons) { if (packet.Length < buttons.Length) { return(null); } var state = new ControllerStateBuilder(); for (int i = 0; i < buttons.Length; ++i) { if (string.IsNullOrEmpty(buttons[i])) { continue; } state.SetButton(buttons[i], packet[i] != 0x00); } return(state.Build()); }
public static ControllerState ReadFromPacket_Jaguar(byte[] packet) { if (packet.Length < 4) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); state.SetButton("pause", (packet[0] & 0b00000100) == 0x00); state.SetButton("a", (packet[0] & 0b00001000) == 0x00); state.SetButton("right", (packet[0] & 0b00010000) == 0x00); state.SetButton("left", (packet[0] & 0b00100000) == 0x00); state.SetButton("down", (packet[0] & 0b01000000) == 0x00); state.SetButton("up", (packet[0] & 0b10000000) == 0x00); state.SetButton("b", (packet[1] & 0b00001000) == 0x00); state.SetButton("1", (packet[1] & 0b00010000) == 0x00); state.SetButton("4", (packet[1] & 0b00100000) == 0x00); state.SetButton("l", (packet[1] & 0b00100000) == 0x00); state.SetButton("7", (packet[1] & 0b01000000) == 0x00); state.SetButton("x", (packet[1] & 0b01000000) == 0x00); state.SetButton("star", (packet[1] & 0b10000000) == 0x00); state.SetButton("c", (packet[2] & 0b00001000) == 0x00); state.SetButton("2", (packet[2] & 0b00010000) == 0x00); state.SetButton("5", (packet[2] & 0b00100000) == 0x00); state.SetButton("8", (packet[2] & 0b01000000) == 0x00); state.SetButton("y", (packet[2] & 0b01000000) == 0x00); state.SetButton("0", (packet[2] & 0b10000000) == 0x00); state.SetButton("option", (packet[3] & 0b00001000) == 0x00); state.SetButton("3", (packet[3] & 0b00010000) == 0x00); state.SetButton("6", (packet[3] & 0b00100000) == 0x00); state.SetButton("r", (packet[3] & 0b00100000) == 0x00); state.SetButton("9", (packet[3] & 0b01000000) == 0x00); state.SetButton("z", (packet[3] & 0b01000000) == 0x00); state.SetButton("pound", (packet[3] & 0b10000000) == 0x00); return(state.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < PACKET_SIZE) { return(null); } if (packet[4] != 5) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); state.SetAnalog("paddle", ReadPaddle(packet[2])); state.SetAnalog("paddle2", SecondPaddle); state.SetButton("fire2", SecondButton); state.SetButton("fire", (packet[1] != 0x00)); return(state.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < PACKET_SIZE) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], packet[i] != 0x00); } return(state.Build()); }
private static void SetTablet(int X, int Y, ControllerStateBuilder state) { if (X < minX) { minX = X; } else if (X > maxX) { maxX = X; } if (Y < minY) { minY = Y; } else if (Y > maxY) { maxY = Y; } state.SetAnalog("touchpad_x", ScaleInteger(X, minX, maxX, 0, 1024) / 1024.0f, X); state.SetAnalog("touchpad_y", ScaleInteger(Y, minY, maxY, 0, 16384) / 16384.0f, Y); }
static public ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < PACKET_SIZE) { return(null); } var state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], packet[i] != 0x00); } state.SetAnalog("stick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length))); state.SetAnalog("stick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 8))); return(state.Build()); }
public static ControllerState ReadFromPacket_Atari5200_1(byte[] packet) { if (packet.Length != BUTTONS_ATARI5200.Length) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS_ATARI5200.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS_ATARI5200[i])) { continue; } state.SetButton(BUTTONS_ATARI5200[i], packet[i] == 0x00); } state.SetAnalog("x", (((packet[17] >> 4) | (packet[18])) - 128.0f) / -128.0f); state.SetAnalog("y", atari5200_y); return(state.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < PACKET_SIZE) { return(null); } byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE]; polishedPacket[0] = 0; byte j = 8; for (byte i = 0; i < 8; ++i) { j--; polishedPacket[0] |= (byte)((packet[i] == 0 ? 0 : 1) << j); } if (polishedPacket[0] != 0x02 && polishedPacket[0] != 0x16 && polishedPacket[0] != 0xFF) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); if (polishedPacket[0] != 0xFF) { for (byte i = 0; i < 16; ++i) { polishedPacket[i + 1] = packet[i + 8] == 0 ? (byte)1 : (byte)0; } byte numExtraBytes = 0; if (polishedPacket[0] == 0x16) { numExtraBytes = 4; } for (int i = 0; i < numExtraBytes; ++i) { polishedPacket[17 + i] = 0; j = 8; for (byte k = 0; k < 8; ++k) { j--; polishedPacket[17 + i] |= (byte)((packet[24 + (i * 8 + k)] == 0 ? 0 : 1) << j); } } if (polishedPacket.Length < POLISHED_PACKET_SIZE) { return(null); } for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], polishedPacket[i] != 0x00); } if (polishedPacket[0] == 0x16) { state.SetAnalog("lstick_x", ReadStick(polishedPacket[17])); state.SetAnalog("lstick_y", ReadStick(polishedPacket[18])); state.SetAnalog("trig_r", ReadTrigger(polishedPacket[19])); state.SetAnalog("trig_l", ReadTrigger(polishedPacket[20])); } else { state.SetAnalog("lstick_x", ReadStick(128)); state.SetAnalog("lstick_y", ReadStick(128)); state.SetAnalog("trig_r", ReadTrigger(0)); state.SetAnalog("trig_l", ReadTrigger(0)); } } else { for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], false); } for (int i = 0; i < MOUSE_BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(MOUSE_BUTTONS[i])) { continue; } state.SetButton(MOUSE_BUTTONS[i], packet[i + 8] != 0x00); } state.SetAnalog("lstick_x", ReadStick(128)); state.SetAnalog("lstick_y", ReadStick(128)); state.SetAnalog("trig_r", ReadTrigger(0)); state.SetAnalog("trig_l", ReadTrigger(0)); byte xVal = 0; j = 8; for (byte k = 0; k < 8; ++k) { j--; xVal |= (byte)((packet[16 + k] == 0 ? 0 : 1) << j); } byte yVal = 0; j = 8; for (byte k = 0; k < 8; ++k) { j--; yVal |= (byte)((packet[24 + k] == 0 ? 0 : 1) << j); } float x = ReadMouse(packet[11] != 0, packet[9] != 0, xVal); float y = ReadMouse(packet[10] != 0, packet[8] != 0, yVal); SignalTool.SetMouseProperties(x, y, state); } return(state.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { ControllerStateBuilder state = null; if (packet.Length == 13) { return(Classic.ReadFromPacket(packet)); } if (packet.Length == BUTTONS_CD32.Length) { state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS_CD32.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS_CD32[i])) { continue; } state.SetButton(BUTTONS_CD32[i], (packet[i] & 0b10000000) == 0x00); } state.SetButton("up", (packet[8] & 0b00000001) == 0); state.SetButton("down", (packet[0] & 0b00000100) == 0); state.SetButton("left", (packet[0] & 0b00001000) == 0); state.SetButton("right", (packet[0] & 0b00010000) == 0); } else if (packet.Length == BUTTONS_CDTV_REMOTE.Length) { int checksum = (packet[33] >> 4) | packet[34]; int checkedCheckSum = 0; for (int i = 0; i < 33; ++i) { checkedCheckSum += packet[i] == 0 ? 0 : 1; } if (checksum == checkedCheckSum) { state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS_CDTV_REMOTE.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS_CDTV_REMOTE[i])) { continue; } state.SetButton(BUTTONS_CDTV_REMOTE[i], packet[i] != 0x00); } float x = 0; float y = 0; if (packet[0] != 0x00) { x = -0.25f; } else if (packet[2] != 0x00) { x = 0.25f; } if (packet[1] != 0x00) { y = 0.25f; } else if (packet[3] != 0x00) { y = -0.25f; } SignalTool.SetMouseProperties(x, y, state, .25f); } } else if (packet.Length == BUTTONS_CDTV_JOYSTICK.Length && packet[0] == 0) { int checksum = (packet[24] >> 4) | packet[25]; int checkedCheckSum = 0; for (int i = 0; i < 24; ++i) { checkedCheckSum += packet[i] == 0 ? 0 : 1; } if (checksum == checkedCheckSum) { state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS_CDTV_JOYSTICK.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS_CDTV_JOYSTICK[i])) { continue; } state.SetButton(BUTTONS_CDTV_JOYSTICK[i], packet[i] != 0x00); } SignalTool.FakeAnalogStick(packet[6], packet[5], packet[4], packet[3], state, "x", "y"); SignalTool.FakeAnalogStick(packet[12], packet[11], packet[10], packet[9], state, "Joy2x", "Joy2y"); } } else if (packet.Length == 26 && packet[0] == 1) { int checksum = (packet[24] >> 4) | packet[25]; int checkedCheckSum = 0; for (int i = 0; i < 24; ++i) { checkedCheckSum += packet[i] == 0 ? 0 : 1; } if (checksum == checkedCheckSum) { state = new ControllerStateBuilder(); state.SetButton("left_button", packet[2] == 0x00); state.SetButton("right_button", packet[1] == 0x00); sbyte xVal = (sbyte)SignalTool.ReadByte(packet, 3); sbyte yVal = (sbyte)SignalTool.ReadByte(packet, 11); SignalTool.SetMouseProperties(xVal / -128.0f, yVal / 128.0f, state); } } else if (packet.Length == 19) { state = new ControllerStateBuilder(); state.SetButton("left_button", packet[0] != 0x00); state.SetButton("right_button", packet[2] != 0x00); sbyte xVal = (sbyte)SignalTool.ReadByteBackwards(packet, 3); sbyte yVal = (sbyte)SignalTool.ReadByteBackwards(packet, 11); SignalTool.SetMouseProperties(xVal / -128.0f, yVal / 128.0f, state); } else if (packet.Length == 36) { byte[] reconstructedPacket = new byte[18]; int j = 0; for (int i = 0; i < 18; ++i) { reconstructedPacket[i] = (byte)((packet[j] >> 4) | packet[j + 1]); j += 2; } byte[] polishedPacket = new byte[128]; int checksum = 0; for (int i = 0; i < 16; ++i) { checksum += reconstructedPacket[i]; for (int k = 0; k < 8; ++k) { polishedPacket[(i * 8) + k] = (byte)((reconstructedPacket[i] & (1 << k)) != 0 ? 1 : 0); } } short sentChecksum = (short)((reconstructedPacket[17] << 8) | reconstructedPacket[16]); if (checksum == sentChecksum) { state = new ControllerStateBuilder(); for (int i = 0; i < 128; ++i) { string scanCode = i.ToString("X").ToUpper();; state.SetButton(scanCode, polishedPacket[i] != 0x00); } } } return(state?.Build()); }
public static ControllerState ReadFromPacket_FMTowns(byte[] packet) { if (packet.Length != 9 && packet.Length != 70) { return(null); } if (packet.Length == 9) { byte[] polishedPacket = new byte[BUTTONS_FMTOWNS.Length]; if (packet[0] != 0 && packet[1] != 0) { packet[0] = packet[1] = 0; polishedPacket[9] = 1; } if (packet[2] != 0 && packet[3] != 0) { packet[2] = packet[3] = 0; polishedPacket[10] = 1; } for (int i = 0; i < packet.Length; ++i) { polishedPacket[i] = packet[i]; } return(ReadPacketButtons(polishedPacket, BUTTONS_FMTOWNS)); } else { int j = 0; byte[] reconstructedPacket = new byte[34]; for (int i = 0; i < 34; ++i) { reconstructedPacket[i] = (byte)((packet[j] >> 4) | packet[j + 1]); j += 2; } byte[] polishedPacket = new byte[256]; for (int i = 0; i < 32; ++i) { for (int k = 0; k < 8; ++k) { polishedPacket[(i * 8) + k] = (byte)((reconstructedPacket[i] & (1 << k)) != 0 ? 1 : 0); } } ControllerStateBuilder state = new ControllerStateBuilder(); for (int i = 0; i < SCANCODES_FMTOWNS.Length; ++i) { if (string.IsNullOrEmpty(SCANCODES_FMTOWNS[i])) { continue; } state.SetButton(SCANCODES_FMTOWNS[i], polishedPacket[i] != 0x00); } SignalTool.SetMouseProperties(((sbyte)reconstructedPacket[33]) / -128.0f, ((sbyte)reconstructedPacket[32]) / 128.0f, state); state.SetButton("left", packet[68] == 1); state.SetButton("right", packet[69] == 1); return(state.Build()); } }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < PACKET_SIZE) { return(null); } byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE]; for (int i = 40; i < 56; ++i) { polishedPacket[i - 40] = (byte)((packet[i] == 0x31) ? 1 : 0); } for (int i = 0; i < 2; ++i) { polishedPacket[16 + i] = 0; for (byte j = 0; j < 8; ++j) { polishedPacket[16 + i] |= (byte)((packet[24 + (i * 8 + j)] == 0x30 ? 0 : 1) << j); } } if (polishedPacket[0] == 1 && polishedPacket[1] == 1 && polishedPacket[2] == 1 && polishedPacket[3] == 1) { ControllerStateBuilder outState = new ControllerStateBuilder(); for (int i = 0; i < THREE_BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(THREE_BUTTONS[i])) { continue; } outState.SetButton(THREE_BUTTONS[i], polishedPacket[i] != 0x00); } outState.SetButton("left", polishedPacket[16] < 0x7f); outState.SetButton("right", polishedPacket[16] > 0x7f); outState.SetButton("up", polishedPacket[17] < 0x7f); outState.SetButton("down", polishedPacket[17] > 0x7f); return(outState.Build()); } else if (polishedPacket[0] == 0 && polishedPacket[1] == 0 && polishedPacket[2] == 0 && polishedPacket[3] == 0) { ControllerStateBuilder outState = new ControllerStateBuilder(); for (int i = 0; i < SIX_BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(SIX_BUTTONS[i])) { continue; } outState.SetButton(SIX_BUTTONS[i], polishedPacket[i] != 0x00); } outState.SetButton("left", polishedPacket[16] < 0x80); outState.SetButton("right", polishedPacket[16] > 0x80); outState.SetButton("up", polishedPacket[17] < 0x80); outState.SetButton("down", polishedPacket[17] > 0x80); return(outState.Build()); } return(null); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < PACKET_SIZE) { return(null); } byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE]; polishedPacket[0] = 0; for (byte i = 0; i < 8; ++i) { polishedPacket[0] |= (byte)((packet[i] == 0 ? 0 : 1) << i); } for (byte i = 0; i < 16; ++i) { polishedPacket[i + 1] = packet[i + 8]; } int nextNumBytes = 0; if (polishedPacket[0] == 0x73 || polishedPacket[0] == 0x79) { nextNumBytes = 4; } else if (polishedPacket[0] == 0x12) { nextNumBytes = 2; } for (int i = 0; i < nextNumBytes; ++i) { polishedPacket[17 + i] = 0; for (byte j = 0; j < 8; ++j) { polishedPacket[17 + i] |= (byte)((packet[24 + (i * 8 + j)] == 0 ? 0 : 1) << j); } } if (polishedPacket[0] == 0x79) { for (int i = 0; i < 12; ++i) { polishedPacket[21 + i] = 0; for (byte j = 0; j < 8; ++j) { polishedPacket[21 + i] |= (byte)((packet[56 + (i * 8 + j)] == 0 ? 0 : 1) << j); } } } if (polishedPacket.Length < POLISHED_PACKET_SIZE) { return(null); } if (polishedPacket[0] != 0x41 && polishedPacket[0] != 0x73 && polishedPacket[0] != 0x79 && polishedPacket[0] != 0x12) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], polishedPacket[i] != 0x00); } if (polishedPacket[0] == 0x73 || polishedPacket[0] == 0x79) { state.SetAnalog("rstick_x", ReadStick(polishedPacket[17])); state.SetAnalog("rstick_y", ReadStick(polishedPacket[18])); state.SetAnalog("lstick_x", ReadStick(polishedPacket[19])); state.SetAnalog("lstick_y", ReadStick(polishedPacket[20])); } else { state.SetAnalog("rstick_x", 0); state.SetAnalog("rstick_y", 0); state.SetAnalog("lstick_x", 0); state.SetAnalog("lstick_y", 0); } if (polishedPacket[0] == 0x79) { state.SetAnalog("analog_right", polishedPacket[6] != 0x00 ? ReadAnalogButton(polishedPacket[21]) : 0.0f); state.SetAnalog("analog_left", polishedPacket[8] != 0x00 ? ReadAnalogButton(polishedPacket[22]) : 0.0f); state.SetAnalog("analog_up", polishedPacket[5] != 0x00 ? ReadAnalogButton(polishedPacket[23]) : 0.0f); state.SetAnalog("analog_down", polishedPacket[7] != 0x00 ? ReadAnalogButton(polishedPacket[24]) : 0.0f); state.SetAnalog("analog_triangle", polishedPacket[13] != 0x00 ? ReadAnalogButton(polishedPacket[25]) : 0.0f); state.SetAnalog("analog_circle", polishedPacket[14] != 0x00 ? ReadAnalogButton(polishedPacket[26]) : 0.0f); state.SetAnalog("analog_x", polishedPacket[15] != 0x00 ? ReadAnalogButton(polishedPacket[27]) : 0.0f); state.SetAnalog("analog_square", polishedPacket[16] != 0x00 ? ReadAnalogButton(polishedPacket[28]) : 0.0f); state.SetAnalog("analog_l1", polishedPacket[11] != 0x00 ? ReadAnalogButton(polishedPacket[29]) : 0.0f); state.SetAnalog("analog_r1", polishedPacket[12] != 0x00 ? ReadAnalogButton(polishedPacket[30]) : 0.0f); state.SetAnalog("analog_l2", polishedPacket[9] != 0x00 ? ReadAnalogButton(polishedPacket[31]) : 0.0f); state.SetAnalog("analog_r2", polishedPacket[10] != 0x00 ? ReadAnalogButton(polishedPacket[32]) : 0.0f); } else { state.SetAnalog("analog_right", (float)(polishedPacket[6] != 0x00 ? 1.0 : 0.0)); state.SetAnalog("analog_left", (float)(polishedPacket[8] != 0x00 ? 1.0 : 0.0)); state.SetAnalog("analog_up", (float)(polishedPacket[5] != 0x00 ? 1.0 : 0.0)); state.SetAnalog("analog_down", (float)(polishedPacket[7] != 0x00 ? 1.0 : 0.0)); state.SetAnalog("analog_triangle", (float)(polishedPacket[13] != 0x00 ? 1.0 : 0.0)); state.SetAnalog("analog_circle", (float)(polishedPacket[14] != 0x00 ? 1.0 : 0.0)); state.SetAnalog("analog_x", (float)(polishedPacket[15] != 0x00 ? 1.0 : 0.0)); state.SetAnalog("analog_square", (float)(polishedPacket[16] != 0x00 ? 1.0 : 0.0)); state.SetAnalog("analog_l1", (float)(polishedPacket[11] != 0x00 ? 1.0 : 0.0)); state.SetAnalog("analog_r1", (float)(polishedPacket[12] != 0x00 ? 1.0 : 0.0)); state.SetAnalog("analog_l2", (float)(polishedPacket[9] != 0x00 ? 1.0 : 0.0)); state.SetAnalog("analog_r2", (float)(polishedPacket[10] != 0x00 ? 1.0 : 0.0)); } if (polishedPacket[0] == 0x12) { float x = ReadMouse(polishedPacket[10] == 0x00, polishedPacket[17]); float y = ReadMouse(polishedPacket[9] == 0x00, polishedPacket[18]); SignalTool.SetMouseProperties(x, y, state); } else { SignalTool.SetMouseProperties(0, 0, state); } return(state.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < 16) { return(null); } int axes = 0; for (byte j = 0; j < 8; ++j) { axes |= (packet[j] == 0x30 ? 0 : 1) << j; } int buttons = 0; for (byte j = 0; j < 8; ++j) { buttons |= (packet[8 + j] == 0x30 ? 0 : 1) << j; } int packetSize = 16 + (axes * 32) + buttons + 1; if (packet.Length != packetSize) { return(null); } byte[] buttonValues = new byte[buttons]; int[] axesValues = new int[axes]; for (int i = 0; i < buttons; ++i) { buttonValues[i] = (byte)((packet[16 + i] == 0x31) ? 1 : 0); } for (int i = 0; i < axes; ++i) { axesValues[i] = 0; for (byte j = 0; j < 32; ++j) { axesValues[i] |= (packet[16 + buttons + i * 32 + j] == 0x30 ? 0 : 1) << j; } } ControllerStateBuilder outState = new ControllerStateBuilder(); for (int i = 0; i < buttonValues.Length; ++i) { outState.SetButton("b" + i.ToString(), buttonValues[i] != 0x00); } for (int i = 0; i < axesValues.Length; ++i) { if (i < AXES_NAMES.Length) { outState.SetAnalog(AXES_NAMES[i], axesValues[i] / (float)short.MaxValue); } outState.SetAnalog("a" + i.ToString(), axesValues[i] / (float)short.MaxValue); } if (axes >= 2) { if (axesValues[axes - 2] < 0) { outState.SetButton("left", true); outState.SetButton("right", false); } else if (axesValues[axes - 2] > 0) { outState.SetButton("right", true); outState.SetButton("left", false); } else { outState.SetButton("left", false); outState.SetButton("right", false); } if (axesValues[axes - 1] < 0) { outState.SetButton("up", true); outState.SetButton("down", false); } else if (axesValues[axes - 1] > 0) { outState.SetButton("down", true); outState.SetButton("up", false); } else { outState.SetButton("up", false); outState.SetButton("down", false); } } else { outState.SetButton("up", false); outState.SetButton("down", false); outState.SetButton("left", false); outState.SetButton("right", false); } return(outState.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < FRAME_HEADER_SIZE) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); int j = 0; byte numWords = 0; for (int i = 0; i < 4; ++i) { numWords |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); numWords |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } // Skip sender and receiver address j += 16; byte dcCommand = 0; for (int i = 0; i < 4; ++i) { dcCommand |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); dcCommand |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } if (dcCommand == 8 && numWords >= 1) { uint controllerType = 0; for (int i = 0; i < 2; i++) { for (int k = 0; k < 4; ++k) { controllerType |= (uint)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (k * 2) + (i * 8))); controllerType |= (uint)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (k * 2) + (i * 8))); j += 2; } } j += 16; if (controllerType == 1 && numWords == 3) { byte ltrigger = 0; for (int i = 0; i < 4; ++i) { ltrigger |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); ltrigger |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } byte rtrigger = 0; for (int i = 0; i < 4; ++i) { rtrigger |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); rtrigger |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } int k = 0; for (int i = 0; i < BUTTONS.Length / 2; ++i) { if (!string.IsNullOrEmpty(BUTTONS[k])) { state.SetButton(BUTTONS[k], (packet[j] & 0x2) == 0x0); } if (!string.IsNullOrEmpty(BUTTONS[k + 1])) { state.SetButton(BUTTONS[k + 1], (packet[j + 1] & 0x1) == 0x0); } k += 2; j += 2; } byte joyy2 = 0; for (int i = 0; i < 4; ++i) { joyy2 |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); joyy2 |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } byte joyx2 = 0; for (int i = 0; i < 4; ++i) { joyx2 |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); joyx2 |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } byte joyy = 0; for (int i = 0; i < 4; ++i) { joyy |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); joyy |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } byte joyx = 0; for (int i = 0; i < 4; ++i) { joyx |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); joyx |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } state.SetAnalog("stick_x", ReadStick(joyx)); state.SetAnalog("stick_y", ReadStick(joyy)); state.SetAnalog("stick_x2", ReadStick(joyx2)); state.SetAnalog("stick_y2", ReadStick(joyy2)); state.SetAnalog("trig_r", ReadTrigger(rtrigger)); state.SetAnalog("trig_l", ReadTrigger(ltrigger)); } else if (controllerType == 0x200 && numWords == 6) { j += 24; int k = 0; for (int i = 0; i < MOUSE_BUTTONS.Length / 2; ++i) { if (!string.IsNullOrEmpty(MOUSE_BUTTONS[k])) { state.SetButton(MOUSE_BUTTONS[k], (packet[j] & 0x2) == 0x0); } if (!string.IsNullOrEmpty(MOUSE_BUTTONS[k + 1])) { state.SetButton(MOUSE_BUTTONS[k + 1], (packet[j + 1] & 0x1) == 0x0); } k += 2; j += 2; } ushort axis1 = 0; for (int i = 1; i >= 0; --i) { for (k = 0; k < 4; ++k) { axis1 |= (ushort)(((packet[j] & 0x02) != 0 ? 1 : 0) << ((7 - k * 2) + (i * 8))); axis1 |= (ushort)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (k * 2) + (i * 8))); j += 2; } } ushort axis2 = 0; for (int i = 1; i >= 0; --i) { for (k = 0; k < 4; ++k) { axis2 |= (ushort)(((packet[j] & 0x02) != 0 ? 1 : 0) << ((7 - k * 2) + (i * 8))); axis2 |= (ushort)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (k * 2) + (i * 8))); j += 2; } } j += 16; ushort axis3 = 0; for (int i = 1; i >= 0; --i) { for (k = 0; k < 4; ++k) { axis3 |= (ushort)(((packet[j] & 0x02) != 0 ? 1 : 0) << ((7 - k * 2) + (i * 8))); axis3 |= (ushort)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (k * 2) + (i * 8))); j += 2; } } float x = (axis2 - 512) / 512.0f; float y = -1 * (axis1 - 512) / 512.0f; SignalTool.SetMouseProperties(x, y, state); state.SetButton("scroll_up", axis3 < 512); state.SetButton("scroll_down", axis3 > 512); } return(state.Build()); } return(null); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < PACKET_SIZE) { return(null); } byte[] cleanedData = new byte[12]; for (int i = 0; i < 24; i = i + 2) { cleanedData[i / 2] = (byte)((packet[i]) | ((packet[i + 1]) >> 4)); } ControllerStateBuilder state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], cleanedData[i] != 0x00); } // Set double 1 buttons state.SetButton("wired-1a", cleanedData[4] != 0x00); state.SetButton("wireless-1a", cleanedData[10] != 0x00); // Handle 3 overriding other pushes if (cleanedData[4] != 0x00 && cleanedData[5] != 0x00) { state.SetButton("wired-3of3", true); state.SetButton("wired-1of3", false); state.SetButton("wired-1aof3", false); state.SetButton("wired-2of3", false); } else { state.SetButton("wired-3of3", false); state.SetButton("wired-1of3", cleanedData[4] != 0x00); state.SetButton("wired-1aof3", cleanedData[4] != 0x00); state.SetButton("wired-2of3", cleanedData[5] != 0x00); } state.SetButton("wireless-1a", cleanedData[10] != 0x00); state.SetAnalog("wired-analog_right", ReadAnalogButton(cleanedData[3])); state.SetAnalog("wired-analog_left", ReadAnalogButton(cleanedData[2])); state.SetAnalog("wired-analog_up", ReadAnalogButton(cleanedData[0])); state.SetAnalog("wired-analog_down", ReadAnalogButton(cleanedData[1])); state.SetAnalog("wireless-analog_right", ReadAnalogButton(cleanedData[9])); state.SetAnalog("wireless-analog_left", ReadAnalogButton(cleanedData[8])); state.SetAnalog("wireless-analog_up", ReadAnalogButton(cleanedData[6])); state.SetAnalog("wireless-analog_down", ReadAnalogButton(cleanedData[7])); float x = 0; float y = 0; if (cleanedData[2] > 0) { x = -1 * ReadAnalogButton(cleanedData[2]); } else if (cleanedData[3] > 0) { x = ReadAnalogButton(cleanedData[3]); } if (cleanedData[0] > 0) { y = ReadAnalogButton(cleanedData[0]); } else if (cleanedData[1] > 0) { y = -1 * ReadAnalogButton(cleanedData[1]); } state.SetAnalog("stick_x", x); state.SetAnalog("stick_y", y); SignalTool.SetMouseProperties(x, y, state); return(state.Build()); }
public static ControllerStateEventArgs ReadFromPacket(byte[] packet) { if (packet.Length == PACKET_SIZE) { byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE]; polishedPacket[0] = (byte)((packet[0] >> 4) | packet[1]); polishedPacket[1] = (byte)(packet[9] == 0 ? 0 : 1); polishedPacket[2] = (byte)(packet[17] == 0 ? 0 : 1); for (int i = 18; i < 29; ++i) { polishedPacket[i - 15] = (byte)(packet[i] == 0 ? 0 : 1); } for (byte i = 0; i < 7; ++i) { polishedPacket[14] |= (byte)((packet[i + 2] == 0 ? 0 : 1) << i); } for (byte i = 0; i < 7; ++i) { polishedPacket[15] |= (byte)((packet[i + 10] == 0 ? 0 : 1) << i); } for (int i = 29; i < 61; i += 2) { polishedPacket[(i / 2) + 2] = (byte)((packet[i] >> 4) | packet[i + 1]); } ControllerStateBuilder state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], polishedPacket[i] == 0x00); } float y = ReadMouse(polishedPacket[14]); float x = ReadMouse(polishedPacket[15]); SignalTool.SetMouseProperties(x, y, polishedPacket[14], polishedPacket[15], state); for (int i = 0; i < KEYBOARD_CODES.Length; ++i) { if (KEYBOARD_CODES[i] != null) { state.SetButton(KEYBOARD_CODES[i], (polishedPacket[(i / 8) + 16] & (1 << (i % 8))) != 0); } } return(state.Build()); } else if (packet.Length == JOYSTICK_PACKET_SIZE) { ControllerStateBuilder state = new ControllerStateBuilder(); byte x = 0, y = 0; for (byte i = 0; i < 8; ++i) { x |= (byte)((packet[i + 2] == 0 ? 0 : 1) << i); } for (byte i = 0; i < 8; ++i) { y |= (byte)((packet[i + 10] == 0 ? 0 : 1) << i); } for (int i = 0; i < JOYSTICK_BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(JOYSTICK_BUTTONS[i])) { continue; } state.SetButton(JOYSTICK_BUTTONS[i], packet[i + 18] == 0x00); } state.SetAnalog("x", ReadJoystick(x), x); state.SetAnalog("y", ReadJoystick(y), y); return(state.Build()); } else if (packet.Length == TABLET_PACKET_SIZE) { ControllerStateBuilder state = new ControllerStateBuilder(); if (packet[34] != 0) { byte x = 0, y = 0, x1 = 0, y1 = 0; for (byte i = 0; i < 8; ++i) { x |= (byte)((packet[i + 2] == 0 ? 0 : 1) << i); } for (byte i = 0; i < 8; ++i) { x1 |= (byte)((packet[i + 10] == 0 ? 0 : 1) << i); } for (byte i = 0; i < 8; ++i) { y |= (byte)((packet[i + 18] == 0 ? 0 : 1) << i); } for (byte i = 0; i < 8; ++i) { y1 |= (byte)((packet[i + 26] == 0 ? 0 : 1) << i); } SetTablet(((x & 0b00011111) * 32) + (x1 >> 3), ((y & 0b00011111) * 128) + (y1 >> 1), state); state.SetButton("Button", (y & 0b10000000) != 0); } return(state.Build()); } else { return(null); } }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < PACKET_SIZE) { return(null); } byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE]; for (int i = 0; i < 20; ++i) { polishedPacket[i] = (byte)((packet[PACKET_HEADER + i] == 0x31) ? 1 : 0); } for (int i = 0; i < 14; ++i) { polishedPacket[20 + i] = 0; for (byte j = 0; j < 8; ++j) { polishedPacket[20 + i] |= (byte)((packet[PACKET_HEADER + 20 + (i * 8 + j)] == 0x30 ? 0 : 1) << j); } } ControllerStateBuilder outState = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } outState.SetButton(BUTTONS[i], polishedPacket[i] != 0x00); } outState.SetAnalog("rstick_x", ReadStick(polishedPacket[22])); outState.SetAnalog("rstick_y", ReadStick(polishedPacket[23])); outState.SetAnalog("lstick_x", ReadStick(polishedPacket[20])); outState.SetAnalog("lstick_y", ReadStick(polishedPacket[21])); outState.SetAnalog("l_trig", ReadStick(polishedPacket[24])); outState.SetAnalog("r_trig", ReadStick(polishedPacket[25])); int touchpad_x1 = (polishedPacket[27] << 8) | polishedPacket[26]; int touchpad_y1 = (polishedPacket[29] << 8) | polishedPacket[28]; int touchpad_x2 = (polishedPacket[31] << 8) | polishedPacket[30]; int touchpad_y2 = (polishedPacket[33] << 8) | polishedPacket[32]; if (polishedPacket[18] == 1) // touch { if (polishedPacket[17] == 1) // click { outState.SetAnalog("touchpad_x3", ReadTouchPad(touchpad_x1, 1920)); outState.SetAnalog("touchpad_y3", ReadTouchPad(touchpad_y1, 943)); } else { outState.SetAnalog("touchpad_x1", ReadTouchPad(touchpad_x1, 1920)); outState.SetAnalog("touchpad_y1", ReadTouchPad(touchpad_y1, 943)); } } if (polishedPacket[19] == 1) // touch { if (polishedPacket[17] == 1) // click { outState.SetAnalog("touchpad_x4", ReadTouchPad(touchpad_x2, 1920)); outState.SetAnalog("touchpad_y4", ReadTouchPad(touchpad_y2, 943)); } else { outState.SetAnalog("touchpad_x2", ReadTouchPad(touchpad_x2, 1920)); outState.SetAnalog("touchpad_y2", ReadTouchPad(touchpad_y2, 943)); } } return(outState.Build()); }
public static ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < PRO_PACKET_SIZE) { return(null); } byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE]; if (packet.Length == PRO_PACKET_SIZE) { for (int i = 0; i < 24; ++i) { polishedPacket[i] = (byte)((packet[i] == 0x31) ? 1 : 0); } for (int i = 0; i < 4; ++i) { polishedPacket[24 + i] = 0; for (byte j = 0; j < 8; ++j) { polishedPacket[24 + i] |= (byte)((packet[24 + (i * 8 + j)] == 0x30 ? 0 : 1) << j); } } ControllerStateBuilder outState = new ControllerStateBuilder(); for (int i = 0; i < PRO_BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(PRO_BUTTONS[i])) { continue; } outState.SetButton(PRO_BUTTONS[i], polishedPacket[i] != 0x00); } outState.SetAnalog("rstick_x", ReadStick(polishedPacket[26])); outState.SetAnalog("rstick_y", ReadStick(polishedPacket[27])); outState.SetAnalog("lstick_x", ReadStick(polishedPacket[24])); outState.SetAnalog("lstick_y", ReadStick(polishedPacket[25])); return(outState.Build()); } else if (packet.Length == POKKEN_PACKET_SIZE) { for (int i = 0; i < 16; ++i) { polishedPacket[i] = (byte)((packet[i] == 0x31) ? 1 : 0); } polishedPacket[16] = 0; for (byte j = 0; j < 4; ++j) { polishedPacket[16] |= (byte)((packet[16 + j] == 0x30 ? 0 : 1) << j); } for (int i = 0; i < 4; ++i) { polishedPacket[17 + i] = 0; for (byte j = 0; j < 8; ++j) { polishedPacket[17 + i] |= (byte)((packet[24 + (i * 8 + j)] == 0x30 ? 0 : 1) << j); } } ControllerStateBuilder outState = new ControllerStateBuilder(); for (int i = 0; i < POKKEN_BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(POKKEN_BUTTONS[i])) { continue; } outState.SetButton(POKKEN_BUTTONS[i], polishedPacket[i] != 0x00); } switch (polishedPacket[16]) { case 0: outState.SetButton("up", true); outState.SetButton("down", false); outState.SetButton("left", false); outState.SetButton("right", false); break; case 1: outState.SetButton("up", true); outState.SetButton("right", true); outState.SetButton("down", false); outState.SetButton("left", false); break; case 2: outState.SetButton("right", true); outState.SetButton("down", false); outState.SetButton("left", false); outState.SetButton("up", false); break; case 3: outState.SetButton("right", true); outState.SetButton("down", true); outState.SetButton("up", false); outState.SetButton("left", false); break; case 4: outState.SetButton("down", true); outState.SetButton("up", false); outState.SetButton("left", false); outState.SetButton("right", false); break; case 5: outState.SetButton("left", true); outState.SetButton("down", true); outState.SetButton("right", false); outState.SetButton("up", false); break; case 6: outState.SetButton("right", false); outState.SetButton("down", false); outState.SetButton("up", false); outState.SetButton("left", true); break; case 7: outState.SetButton("up", true); outState.SetButton("left", true); outState.SetButton("right", false); outState.SetButton("down", false); break; default: outState.SetButton("up", false); outState.SetButton("left", false); outState.SetButton("right", false); outState.SetButton("down", false); break; } outState.SetAnalog("lstick_x", ReadPokkenStick(polishedPacket[17], false)); outState.SetAnalog("lstick_y", ReadPokkenStick(polishedPacket[18], true)); outState.SetAnalog("rstick_x", ReadPokkenStick(polishedPacket[19], false)); outState.SetAnalog("rstick_y", ReadPokkenStick(polishedPacket[20], true)); return(outState.Build()); } return(null); }