static bool SendMouseReport(UsbStream stream, bool button1, bool button2, bool button3, byte Xmovement, byte Ymovement) { byte[] report = new byte[3]; report[0] = (byte)((button1 ? 1 : 0) | (button2 ? 2 : 0) | (button3 ? 4 : 0)); report[1] = Xmovement; report[2] = Ymovement; stream.Write(report, 0, 3); return true; }
// Sends the given WP_Packet (header) and public static void SendPacket(UsbStream stream, WP_Packet packet, byte[] data) { int index; // Create a buffer whose size is large enough to contain both the WP_Packet (header) // and data so that they may be sent as a single packet. byte[] buffer = new byte[WP_Packet.packetSize + data.Length]; // Marshal the signature string into the buffer for (index = 0; index < packet.m_signature.Length && index < WP_Packet.m_crcHeader_offset; index++) { buffer[index] = (byte)packet.m_signature[index]; } while (index++ < WP_Packet.m_crcHeader_offset) buffer[index] = 0; // Fill to end with zeros // Marshal the WP_Packet (header) into the buffer. Note that the CRC values start as zero // so that the CRC of the WP_Packet (header) may be computed the same for host and target. WriteUINT32(buffer, WP_Packet.m_crcHeader_offset, 0); WriteUINT32(buffer, WP_Packet.m_crcData_offset, 0); WriteUINT32(buffer, WP_Packet.m_cmd_offset, (UInt32)packet.m_cmd); WriteUINT16(buffer, WP_Packet.m_seq_offset, packet.m_seq); WriteUINT16(buffer, WP_Packet.m_seqReply_offset, packet.m_seqReply); WriteUINT32(buffer, WP_Packet.m_flags_offset, packet.m_flags); WriteUINT32(buffer, WP_Packet.m_size_offset, (UInt32)data.Length); // Copy the data to the buffer for (int i = 0; i < data.Length; i++) buffer[WP_Packet.packetSize + i] = data[i]; // Calculate the CRC of the data if (data.Length != 0) { packet.m_crcData = SUPPORT_ComputeCRC(data, 0, data.Length, 0); WriteUINT32(buffer, WP_Packet.m_crcData_offset, packet.m_crcData); } // Calculate the CRC of the packet header packet.m_crcHeader = SUPPORT_ComputeCRC(buffer, 0, WP_Packet.packetSize, 0); WriteUINT32(buffer, WP_Packet.m_crcHeader_offset, packet.m_crcHeader); // Write out the complete packet to the USB stream stream.Write(buffer, 0, buffer.Length); }
/// <summary> /// /// </summary> /// <param name="port"></param> /// <param name="mouse"></param> static void MouseLoop(UsbController port, UsbStream mouse) { // Mouse report storage. byte[] report = new byte[5]; // Offsets into mouse report storage. const byte BUTTONS = 0; const byte X_MOVEMENT = 1; const byte Y_MOVEMENT = 2; // Size of mouse movement for each report (every 10 mS). const byte mouseStep = 3; bool fAddMouse = true; // While the Done button is not pressed... while (buttons.Done.Read()) { // Perform this operation once every 10 milliseconds (actually a // bit more than 10 milliseconds). We've asked the host to // query for mouse info at least every 10 milliseconds, but it // actually queries every 8 milliseconds - it's OK not to // respond to every query. Thread.Sleep(10); report[X_MOVEMENT] = 0; // Zero X movement report[Y_MOVEMENT] = 0; // Zero Y movement // Add X,Y movement to the mouse report. if (!buttons.Left.Read()) report[X_MOVEMENT] -= mouseStep; if (!buttons.Right.Read()) report[X_MOVEMENT] += mouseStep; if (!buttons.Up.Read()) report[Y_MOVEMENT] -= mouseStep; if (!buttons.Down.Read()) report[Y_MOVEMENT] += mouseStep; if (!buttons.Toggle.Read()) { if (mouse != null) { mouse.Dispose(); mouse = null; } fAddMouse = !fAddMouse; ConfigureUsbPort(port, fAddMouse); if (fAddMouse) { mouse = port.CreateUsbStream(3, UsbStream.NullEndpoint); } } // Add the button state to the mouse report. report[BUTTONS] = (byte)((!buttons.LeftMouseButton.Read() ? 1 : 0) | (!buttons.RightMouseButton.Read() ? 2 : 0)); if (mouse != null) { // Send the mouse report to the host. mouse.Write(report, 0, 3); } } // Wait for the Done button to be released. while (!buttons.Done.Read()) ; }