Example #1
0
        /// <summary>
        /// Creates an interface to a microbit.
        /// </summary>
        /// <param name="desc"></param>
        /// <returns></returns>
        public static Microbit Create(MicrobitDesc desc)
        {
            Microbit microbit = null;

            try
            {
                microbit = new Microbit(desc);

                // Microbit is provisionally ready. May need to flash.
                return(microbit);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Failed to create microbit: " + ex.Message);

                // Cleanup the microbit.
                if (microbit != null)
                {
                    microbit.Dispose();
                }
            }
            // Failed to produce a microbit.
            return(null);
        }
Example #2
0
        }   // 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);
        }