/// <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); } })); } }
//TODO: Put unit tests around locking here somehow #region Events private void UsbInterruptInPipe_DataReceived( UsbInterruptInPipe sender, UsbInterruptInEventArgs args) { var bytes = args?.InterruptData?.ToArray(); if (bytes != null) { _dataReceiver.DataReceived(new TransferResult(bytes, args.InterruptData.Length)); } }
/// <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); } })); } } }
//TODO: Put unit tests around locking here somehow #region Events private async void UsbInterruptInPipe_DataReceived(UsbInterruptInPipe sender, UsbInterruptInEventArgs args) { try { await _DataReceivedLock.WaitAsync(); var bytes = args.InterruptData.ToArray(); _Chunks.Add(bytes); if (bytes != null) { Logger?.Log($"{bytes.Length} read on interrupt pipe {UsbInterruptInPipe.EndpointDescriptor.EndpointNumber}", nameof(UWPUsbInterfaceInterruptReadEndpoint), null, LogLevel.Information); } if (_ReadChunkTaskCompletionSource != null && _ReadChunkTaskCompletionSource.Task.Status != TaskStatus.RanToCompletion) { //In this case there should be no chunks. TODO: Put some unit tests around this. //The read method wil be waiting on this var result = _Chunks[0]; _Chunks.RemoveAt(0); _ReadChunkTaskCompletionSource.SetResult(result); Logger?.Log($"Completion source result set", nameof(UWPUsbInterfaceInterruptReadEndpoint), null, LogLevel.Information); return; } } finally { _DataReceivedLock.Release(); } }
private void InterruptPipe_DataReceived(UsbInterruptInPipe sender, UsbInterruptInEventArgs args) { HandleDataReceived(args.InterruptData.ToArray()); }