private StorageManager() { _usbStorages = new Hashtable(); _sdDetectPort = new InterruptPort(EMX.Pin.IO24, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth); _sdDetectPort.OnInterrupt += new NativeEventHandler(_sdDetectPort_OnInterrupt); try { MountCardStorage(); } catch (Exception) { //TODO: emulator throws exception return; } USBHostController.DeviceConnectedEvent += new USBH_DeviceConnectionEventHandler(USBHostController_DeviceConnectedEvent); USBHostController.DeviceDisconnectedEvent += new USBH_DeviceConnectionEventHandler(USBHostController_DeviceDisconnectedEvent); USBH_Device[] connectedDevices = USBHostController.GetDevices(); foreach (USBH_Device connected in connectedDevices) { USBHostController_DeviceConnectedEvent(connected); } }
private void ReaderThread() { int count; // Maximum data is wMaxPacketSize XBoxJoystickData = new byte[XBoxInputPipe.PipeEndpoint.wMaxPacketSize]; // Read every bInterval while (true) { Thread.Sleep(XBoxInputPipe.PipeEndpoint.bInterval); count = 0; lock (XBoxJoystickData) { try { count = XBoxInputPipe.TransferData(XBoxJoystickData, 0, XBoxJoystickData.Length); } catch { USBH_ERROR e = USBHostController.GetLastError(); #if DEBUG Debug.Print("ERROR" + e); #endif Array.Clear(XBoxJoystickData, 0, XBoxJoystickData.Length); count = 0; } } //if (count > 3)//this is useful to print the data to help in reverse engineering if (false) { #if DEBUG // Debug.Print("(dx, dy) = (" + (sbyte)(XBOX_joustickData[1]) + ", " + (sbyte)(XBOX_joustickData[2]) + ")"); int i = 0; Debug.Print("=" + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) + " " + ByteToHex(XBoxJoystickData[i++]) ); /////////// RT LT analog ///////////////////// if (XBoxJoystickData[4] > 200) { Debug.Print("LT"); } if (XBoxJoystickData[5] > 200) { Debug.Print("RT"); } /////// left hat analog ////////////////// //if (XBOX_joustickData[6] > 200) // Debug.Print("LH x"); //if (XBOX_joustickData[8] > 200) // Debug.Print("LH y"); #endif } } }
public static int GetCount(USBH_DeviceType type) { int n = 0; foreach (USBH_Device device in USBHostController.GetDevices()) { if (device.TYPE == type) { n++; } } return(n); }
/// <summary> /// Set the state of the LED on the Guide button. This is only supported on XBox 360 controllers, not classic XBox controllers. /// </summary> /// <param name="status">State for the button</param> public void SetLedState(Xbox360LedState status) { byte[] ledData = new byte[] { 0x01, 0x03, (byte)status }; try { XBoxOutputPipe.TransferData(ledData, 0, ledData.Length); } catch { USBH_ERROR e = USBHostController.GetLastError(); #if DEBUG Debug.Print("LED ERROR " + e); #endif } }
/// <summary> /// Set the rumble power. Note: You must have ~400mA of power for using rumble! /// </summary> /// <param name="left">Set the left (low frequency) power</param> /// <param name="right">Set the right (high frequency) power</param> public void SetRumblePower(byte left, byte right) { byte[] rumbleData = new byte[] { 0x00, 0x08, 0x00, left, right, 0x00, 0x00, 0x00 }; try { XBoxOutputPipe.TransferData(rumbleData, 0, rumbleData.Length); } catch { USBH_ERROR e = USBHostController.GetLastError(); #if DEBUG Debug.Print("RUMBLE ERROR " + e); #endif } }
public static int GetProtocol(USBH_RawDevice openedDevice) { var dataBytes = new byte[2]; try { openedDevice.SendSetupTransfer( (byte)(UsbRequestType.DeviceToHost | UsbRequestType.Vendor), (byte)AndroidAccessoryUsbCommands.GetProtocol, 0, 1, dataBytes, 0, dataBytes.Length ); } catch (Exception) { // SendSetupTransfer will just blow a generic Exception if it // fails. If this gets changed, catch the more specific // Exception instead. Right now the USB host code always // throws Exception when anything goes wrong, and then you use // USBHostController.GetLastError to see what happened. // // See http://www.tinyclr.com/forum/2/3367/ for details. if (USBHostController.GetLastError() == USBH_ERROR.NoError) { // Some other kind of Exception -- let it fly throw; } // Fake a nonsensical protocol version to signal to the caller // that it's not gonna happen. dataBytes should still be 0 // but just to make sure I'll reset it. dataBytes[0] = 0; dataBytes[1] = 0; } return((dataBytes[1] << 8) | dataBytes[0]); }
private static void SetupUSB() { Trace.TraceInformation("Setting up USB host..."); USBHostController.DeviceBadConnectionEvent += (device) => { Trace.TraceInformation("USB device bad connection"); }; USBHostController.DeviceDisconnectedEvent += (device) => { Trace.TraceInformation("USB device disconnected"); switch (device.TYPE) { case USBH_DeviceType.Serial_FTDI: //webhost.Stop(); automationController.Dispose(); break; } }; USBHostController.DeviceConnectedEvent += (device) => { Trace.TraceInformation("USB device connected"); switch (device.TYPE) { case USBH_DeviceType.Serial_FTDI: var usbSerial = new USBH_SerialUSB(device, 19200, Parity.None, 8, System.IO.Ports.StopBits.One); // PowerLinc USB Dual-Band #2413U if (device.VENDOR_ID == InsteonAutomationController.VendorID & device.PRODUCT_ID == InsteonAutomationController.ProductID) { Trace.TraceInformation("PowerLinc USB Dual-Band #2413U"); automationController = new InsteonAutomationController(settings, usbSerial); automationController.Initalize(); //webhost.Start(settings, automationController); //SetupTimers(); } break; case USBH_DeviceType.Unknown: // Aeon Labs Z-Stick2 if (device.VENDOR_ID == ZWaveAutomationController.VendorID & device.PRODUCT_ID == ZWaveAutomationController.ProductID) { Trace.TraceInformation("Aeon Labs Z-Stick2"); USBH_Device silabs = new USBH_Device(device.ID, device.INTERFACE_INDEX, USBH_DeviceType.Serial_SiLabs, device.VENDOR_ID, device.PRODUCT_ID, device.PORT_NUMBER); usbSerial = new USBH_SerialUSB(silabs, 19200, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One); usbSerial.Open(); automationController = new ZWaveAutomationController(settings, usbSerial); automationController.Initalize(); } break; } }; USBHostController.GetDevices(); }
/// <summary> /// /// </summary> private void JogReaderMonitor() { var jogWheelData = new byte[jogWheelPipe.PipeEndpoint.wMaxPacketSize]; for (; ;) { // Our sleep time is the device polling interval time Thread.Sleep(jogWheelPipe.PipeEndpoint.bInterval); // Read the data from the USB pipe lock (jogWheelData) { try { jogWheelPipe.TransferData(jogWheelData, 0, jogWheelData.Length); } catch { var e = USBHostController.GetLastError(); Debug.Print("ERROR" + e); Array.Clear(jogWheelData, 0, jogWheelData.Length); } } // Sets the last read settings before clearing the current one. shuttlePositionLast = shuttlePosition; jogPositionLast = jogPosition; // Set the current positions shuttlePosition = jogWheelData[0]; jogPosition = jogWheelData[1]; // Make sense of the counter-clockwise motion if (shuttlePosition > 200) { shuttlePosition = shuttlePosition - 256; } // With these two bytes we can read the 15 buttons on the device. buttonsLast = buttons; buttons = new BitArray(Utility.ExtractRangeFromArray(jogWheelData, 3, 2)); // Compare the last positions and call an event if needed for (var i = 0; i < buttons.Count; i++) { if (buttons[i] && buttonsLast[i] == false) { RaiseEvent(JogButtonDown, i); } if (buttons[i] == false && buttonsLast[i]) { RaiseEvent(JogButtonUp, i); } } if (jogPosition != jogPositionLast) { RaiseEvent(JogPositionChanged, jogPosition); } if (shuttlePosition != shuttlePositionLast) { RaiseEvent(ShuttlePositionChanged, shuttlePosition); } } // ReSharper disable FunctionNeverReturns }
public static int GetCount() { return(USBHostController.GetDevices().Length); }