PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/customusbdeviceaccess/cs/utilities.cs

https://gitlab.com/Rockyspade/Windows-universal-samples
C# | 105 lines | 64 code | 12 blank | 29 comment | 22 complexity | f337cc4ac512292e7d9fc690e0313317 MD5 | raw file
  1. //*********************************************************
  2. //
  3. // Copyright (c) Microsoft. All rights reserved.
  4. // This code is licensed under the MIT License (MIT).
  5. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  6. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  7. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  8. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  9. //
  10. //*********************************************************
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using Windows.Devices.Usb;
  17. using Windows.UI.Xaml;
  18. using SDKTemplate;
  19. namespace CustomUsbDeviceAccess
  20. {
  21. class Utilities
  22. {
  23. /// <summary>
  24. /// Displays the compatible scenarios and hides the non-compatible ones.
  25. /// If there are no supported devices, the scenarioContainer will be hidden and an error message
  26. /// will be displayed.
  27. /// </summary>
  28. /// <param name="scenarios">The key is the device type that the value, scenario, supports.</param>
  29. /// <param name="scenarioContainer">The container that encompasses all the scenarios that are specific to devices</param>
  30. public static void SetUpDeviceScenarios(Dictionary<DeviceType, UIElement> scenarios, UIElement scenarioContainer)
  31. {
  32. UIElement supportedScenario = null;
  33. if (EventHandlerForDevice.Current.IsDeviceConnected)
  34. {
  35. foreach (KeyValuePair<DeviceType, UIElement> deviceScenario in scenarios)
  36. {
  37. // Enable the scenario if it's generic or the device type matches
  38. if ((deviceScenario.Key == DeviceType.All)
  39. || (deviceScenario.Key == Utilities.GetDeviceType(EventHandlerForDevice.Current.Device)))
  40. {
  41. // Make the scenario visible in case other devices use the same scenario and collapsed it.
  42. deviceScenario.Value.Visibility = Visibility.Visible;
  43. supportedScenario = deviceScenario.Value;
  44. }
  45. else if (deviceScenario.Value != supportedScenario)
  46. {
  47. // Don't hide the scenario if it is supported by the current device and is shared by other devices
  48. deviceScenario.Value.Visibility = Visibility.Collapsed;
  49. }
  50. }
  51. }
  52. if (supportedScenario == null)
  53. {
  54. // Hide the container so that common elements shared across scenarios are also hidden
  55. scenarioContainer.Visibility = Visibility.Collapsed;
  56. NotifyDeviceNotConnected();
  57. }
  58. }
  59. /// <summary>
  60. /// Device type of the device provided device.
  61. /// </summary>
  62. /// <param name="device"></param>
  63. /// <returns>The DeviceType of the device or DeviceType.None if there are no devices connected or is not recognized</returns>
  64. public static DeviceType GetDeviceType(UsbDevice device)
  65. {
  66. if (device != null)
  67. {
  68. if (device.DeviceDescriptor.VendorId == OsrFx2.DeviceVid
  69. && device.DeviceDescriptor.ProductId == OsrFx2.DevicePid)
  70. {
  71. return DeviceType.OsrFx2;
  72. }
  73. else if (device.DeviceDescriptor.VendorId == SuperMutt.DeviceVid
  74. && device.DeviceDescriptor.ProductId == SuperMutt.DevicePid)
  75. {
  76. return DeviceType.SuperMutt;
  77. }
  78. }
  79. return DeviceType.None;
  80. }
  81. public static Boolean IsSuperMuttDevice(UsbDevice device)
  82. {
  83. return (GetDeviceType(device) == DeviceType.SuperMutt);
  84. }
  85. /// <summary>
  86. /// Prints an error message stating that device is not connected
  87. /// </summary>
  88. public static void NotifyDeviceNotConnected()
  89. {
  90. MainPage.Current.NotifyUser("Device is not connected, please select a plugged in device to try the scenario again", NotifyType.ErrorMessage);
  91. }
  92. }
  93. }