/// <summary> /// This callback only increments the total number of interrupts received and prints it /// /// This method is called whenever the device sends an interrupt on the pipe we registered this callback on. /// </summary> /// <param name="sender">The interrupt pipe that raised the event (the one that received the interrupt)</param> /// <param name="eventArgs">Contains the data, in an IBuffer, that was received through the interrupts</param> private async void OnGeneralInterruptEvent(UsbInterruptInPipe sender, UsbInterruptInEventArgs eventArgs) { // If we navigated away from this page, we don't need to process this event // This also prevents output from spilling into another page if (!navigatedAway) { numInterruptsReceived++; // The data from the interrupt IBuffer buffer = eventArgs.InterruptData; totalNumberBytesReceived += buffer.Length; // Create a DispatchedHandler for the because we are interracting with the UI directly and the // thread that this function is running on may not be the UI thread; if a non-UI thread modifies // the UI, an exception is thrown await rootPage.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, new DispatchedHandler(() => { // If we navigated away from this page, do not print anything. The dispatch may be handled after // we move to a different page. if (!navigatedAway) { MainPage.Current.NotifyUser( "Total number of interrupt events received: " + numInterruptsReceived.ToString() + "\nTotal number of bytes received: " + totalNumberBytesReceived.ToString(), NotifyType.StatusMessage); } })); } }
public override async Task InitializeAsync() { if (Disposed) { throw new Exception(DeviceDisposedErrorMessage); } await GetDeviceAsync(DeviceId); if (ConnectedDevice != null) { var usbInterface = ConnectedDevice.Configuration.UsbInterfaces.FirstOrDefault(); if (usbInterface == null) { ConnectedDevice.Dispose(); throw new Exception("There was no Usb Interface found for the device."); } var interruptPipe = usbInterface.InterruptInPipes.FirstOrDefault(); if (interruptPipe == null) { throw new Exception("There was no interrupt pipe found on the interface"); } interruptPipe.DataReceived += InterruptPipe_DataReceived; //TODO: Fill in the DeviceDefinition... // TODO: It should be possible to select a different configurations, interface, and pipes _DefaultConfigurationInterface = ConnectedDevice.Configuration.UsbInterfaces.FirstOrDefault(); //TODO: Clean up this messaging and move down to a base class across platforms if (_DefaultConfigurationInterface == null) { throw new Exception("Could not get the default interface configuration for the USB device"); } _DefaultOutPipe = _DefaultConfigurationInterface.InterruptOutPipes.FirstOrDefault(); if (_DefaultOutPipe == null) { throw new Exception("Could not get the default out pipe for the default USB interface"); } _DefaultInPipe = _DefaultConfigurationInterface.InterruptInPipes.FirstOrDefault(); if (_DefaultOutPipe == null) { throw new Exception("Could not get the default in pipe for the default USB interface"); } } else { throw new Exception($"Could not connect to device with Device Id {DeviceId}. Check that the package manifest has been configured to allow this device."); } }
/// <summary> /// Main Interrupt Handler /// </summary> /// <param name="sender"></param> /// <param name="eventArgs"></param> private void OnStatusChangeEventOnce(UsbInterruptInPipe sender, UsbInterruptInEventArgs eventArgs) { IBuffer buffer = eventArgs.InterruptData; if (buffer.Length > 0) { DataReader reader = DataReader.FromBuffer(buffer); usbState.OnStateUpdate(reader, buffer.Length); ReadResultWait.Set(); } }
/// <summary> /// This callback only increments the total number of interrupts received and prints it /// /// This method is called whenever the device sends an interrupt on the pipe we registered this callback on. /// </summary> /// <param name="sender">The interrupt pipe that raised the event (the one that received the interrupt)</param> /// <param name="eventArgs">Contains the data, in an IBuffer, that was received through the interrupts</param> private async void OnGeneralInterruptEvent(UsbInterruptInPipe sender, UsbInterruptInEventArgs eventArgs) { // If we navigated away from this page, we don't need to process this event // This also prevents output from spilling into another page if (!navigatedAway) { numInterruptsReceived++; // The data from the interrupt IBuffer buffer = eventArgs.InterruptData; totalNumberBytesReceived += buffer.Length; // Create a DispatchedHandler for the because we are interracting with the UI directly and the // thread that this function is running on may not be the UI thread; if a non-UI thread modifies // the UI, an exception is thrown await rootPage.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, new DispatchedHandler(() => { // If we navigated away from this page, do not print anything. The dispatch may be handled after // we move to a different page. if (!navigatedAway) { MainPage.Current.NotifyUser( "Total number of interrupt events received: " + numInterruptsReceived.ToString() + "\nTotal number of bytes received: " + totalNumberBytesReceived.ToString(), NotifyType.StatusMessage); } })); } }
/// <summary> /// This method is called whenever the device sends an interrupt on the pipe we registered this callback on. /// /// We will read a byte from the buffer that the device sent to us and then look at each bit to determine state of /// each switch. AFter determining the state of each switch, we will print out a table with each row representing a /// switch and its state. /// </summary> /// <param name="sender">The interrupt pipe that raised the event (the one that received the interrupt)</param> /// <param name="eventArgs">Contains the data, in an IBuffer, that was received through the interrupts</param> private async void OnOsrFx2SwitchStateChangeEvent(UsbInterruptInPipe sender, UsbInterruptInEventArgs eventArgs) { // If we navigated away from this page, we don't need to process this event // This also prevents output from spilling into another page if (!navigatedAway) { numInterruptsReceived++; // The OSRFX2 gives us 1 byte, each bit representing the state of a switch const Byte numberOfSwitches = 8; var switchStateList = new List<Boolean>(numberOfSwitches); IBuffer buffer = eventArgs.InterruptData; if (buffer.Length > 0) { totalNumberBytesReceived += buffer.Length; DataReader reader = DataReader.FromBuffer(buffer); byte switchStates = reader.ReadByte(); // Loop through each bit of what the device sent us and determine the state of each switch for (int switchIndex = 0; switchIndex < numberOfSwitches; switchIndex++) { int result = (1 << switchIndex) & switchStates; if (result != 0) { switchStateList.Add(true); } else { switchStateList.Add(false); } } // Create a DispatchedHandler for the because we are printing the table to the UI directly and the // thread that this function is running on may not be the UI thread; if a non-UI thread modifies // the UI, an exception is thrown await rootPage.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, new DispatchedHandler(() => { // If we navigated away from this page, do not print anything. The dispatch may be handled after // we move to a different page. if (!navigatedAway) { rootPage.NotifyUser( "Total number of interrupt events received: " + numInterruptsReceived.ToString() + "\nTotal number of bytes received: " + totalNumberBytesReceived.ToString(), NotifyType.StatusMessage); // Print the switch state table UpdateSwitchStateTable(switchStateList); } })); } else { await rootPage.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, new DispatchedHandler(() => { // If we navigated away from this page, do not print anything. The dispatch may be handled after // we move to a different page. if (!navigatedAway) { rootPage.NotifyUser("Received 0 bytes from interrupt", NotifyType.ErrorMessage); } })); } } }
/// <summary> /// This method is called whenever the device sends an interrupt on the pipe we registered this callback on. /// /// We will read a byte from the buffer that the device sent to us and then look at each bit to determine state of /// each switch. AFter determining the state of each switch, we will print out a table with each row representing a /// switch and its state. /// </summary> /// <param name="sender">The interrupt pipe that raised the event (the one that received the interrupt)</param> /// <param name="eventArgs">Contains the data, in an IBuffer, that was received through the interrupts</param> private async void OnOsrFx2SwitchStateChangeEvent(UsbInterruptInPipe sender, UsbInterruptInEventArgs eventArgs) { // If we navigated away from this page, we don't need to process this event // This also prevents output from spilling into another page if (!navigatedAway) { numInterruptsReceived++; // The OSRFX2 gives us 1 byte, each bit representing the state of a switch const Byte numberOfSwitches = 8; var switchStateList = new List <Boolean>(numberOfSwitches); IBuffer buffer = eventArgs.InterruptData; if (buffer.Length > 0) { totalNumberBytesReceived += buffer.Length; DataReader reader = DataReader.FromBuffer(buffer); byte switchStates = reader.ReadByte(); // Loop through each bit of what the device sent us and determine the state of each switch for (int switchIndex = 0; switchIndex < numberOfSwitches; switchIndex++) { int result = (1 << switchIndex) & switchStates; if (result != 0) { switchStateList.Add(true); } else { switchStateList.Add(false); } } // Create a DispatchedHandler for the because we are printing the table to the UI directly and the // thread that this function is running on may not be the UI thread; if a non-UI thread modifies // the UI, an exception is thrown await rootPage.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, new DispatchedHandler(() => { // If we navigated away from this page, do not print anything. The dispatch may be handled after // we move to a different page. if (!navigatedAway) { rootPage.NotifyUser( "Total number of interrupt events received: " + numInterruptsReceived.ToString() + "\nTotal number of bytes received: " + totalNumberBytesReceived.ToString(), NotifyType.StatusMessage); // Print the switch state table UpdateSwitchStateTable(switchStateList); } })); } else { await rootPage.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, new DispatchedHandler(() => { // If we navigated away from this page, do not print anything. The dispatch may be handled after // we move to a different page. if (!navigatedAway) { rootPage.NotifyUser("Received 0 bytes from interrupt", NotifyType.ErrorMessage); } })); } } }
private void InterruptPipe_DataReceived(UsbInterruptInPipe sender, UsbInterruptInEventArgs args) { HandleDataReceived(args.InterruptData.ToArray()); }
public UsbInterruptInPipeEvents(UsbInterruptInPipe This) { this.This = This; }