} // end of GetDeviceDesc() /// <summary> /// Detect attached microbits. /// </summary> /// <param name="createDevices">Whether or not to open interfaces to attached microbits.</param> /// <returns>The number of attached microbits.</returns> public static int RefreshDevices(bool createDevices = true) { // If specified on the command line, do not scan for microbits. if (Program2.CmdLine.Exists("NoMicrobit")) { return(0); } int prevDeviceCount = Microbits.Count; ReleaseDevices(); #if false // Print the Caption and DeviceID of all connected PNP devices. This // is helpful when you need to discover what the exact device id is // for a particular device you want to be able to detect. { using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity")) using (var collection = searcher.Get()) { foreach (var device in collection) { string Caption = (string)device.GetPropertyValue("Caption"); string DeviceID = (string)device.GetPropertyValue("DeviceID"); System.Diagnostics.Debug.WriteLine(Caption + " ---- " + DeviceID); } } } #endif List <MicrobitDesc> microbitDescs = new List <MicrobitDesc>(); // Detect microbits. microbitDescs = GetDeviceDesc(); // Open interfaces to devices. if (createDevices) { if (DriverInstalled) { if (microbitDescs != null && microbitDescs.Count > 0) { int microbitIndex = (int)GamePadSensor.PlayerId.One; foreach (MicrobitDesc desc in microbitDescs) { Microbit microbit = Microbit.Create(desc); if (microbit != null) { Microbits.TryAdd(microbitIndex++, microbit); } } } } else { // Do nothing here. The main thread loop will notice that DirverInstalled is false // and should put up the needed dialog there. We can't do it here since the dialog // can't be shown on a background thread. } } // If none were found, check if user tried the command line. if (microbitDescs.Count == 0 && Program2.MicrobitCmdLine != null) { if (Program2.MicrobitCmdLine.Length == 7) { MicrobitDesc desc = new MicrobitDesc(); desc.COM = Program2.MicrobitCmdLine.Substring(0, 4); desc.Drive = Program2.MicrobitCmdLine.Substring(5); microbitDescs.Add(desc); } } int deviceCount = createDevices ? Microbits.Count : microbitDescs.Count; // If any microbits were detected, then permanently enable visibility of the microbit programming tiles. if (!XmlOptionsData.ShowMicrobitTiles && deviceCount > 0) { XmlOptionsData.ShowMicrobitTiles = true; Instrumentation.RecordEvent(Instrumentation.EventId.MicrobitTilesEnabled, ""); } // Track the number of microbits attached at one time. if (prevDeviceCount < deviceCount) { Instrumentation.SetCounter(Instrumentation.CounterId.MicrobitCount, deviceCount); } return(deviceCount); }