Beispiel #1
0
        static async void pwmlist()
        {
            UpBridge.Up   upb        = new UpBridge.Up();
            PwmController controller = await PwmController.GetDefaultAsync();

            Console.WriteLine(controller.PinCount);
        }
Beispiel #2
0
        static async void pwmlist()
        {
            try
            {
                UpBridge.Up   upb        = new UpBridge.Up();
                PwmController controller = await PwmController.GetDefaultAsync();

                Console.WriteLine(controller.PinCount);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Beispiel #3
0
        private async void BtnLGBOn_Click(object sender, RoutedEventArgs e)
        {
            var gpio = GpioController.GetDefault();
            var pwm  = await PwmController.GetDefaultAsync();

            var pinR = gpio.OpenPin(26);
            var pinG = gpio.OpenPin(6);
            var pinB = gpio.OpenPin(13);

            pinR.SetDriveMode(GpioPinDriveMode.Output);
            pinG.SetDriveMode(GpioPinDriveMode.Output);
            pinB.SetDriveMode(GpioPinDriveMode.Output);

            pinR.Write(GpioPinValue.High);
            pinG.Write(GpioPinValue.High);
            pinB.Write(GpioPinValue.High);
        }
Beispiel #4
0
        public static async Task <PinManager> CreateAsync()
        {
            if (!LightningProvider.IsLightningEnabled)
            {
                throw new NotSupportedException("Controller needs Ligtning provider");
            }

            var gpioController = await GpioController.GetDefaultAsync();

            var pwmController = await PwmController.GetDefaultAsync();

            var pwmProvider = LightningPwmProvider.GetPwmProvider();

            var list       = (await PwmController.GetControllersAsync(pwmProvider));
            var length     = list.Count;
            var controller = list[1];

            controller.SetDesiredFrequency(50);
            return(new PinManager(gpioController, controller));
        }
Beispiel #5
0
        static async Task MainAsync()
        {
            string input = "";
            bool   exit  = input.Equals("exit");

            UpBridge.Up upb = new UpBridge.Up();
            Console.WriteLine("UWP console PWM test:");
            PwmController controller = await PwmController.GetDefaultAsync();

            pin1.pin = 0;
            //print Board INFO
            Console.WriteLine(upb.BoardGetManufacture() + "\n"
                              + "Board Name:  " + upb.BoardGetName() + "\n"
                              + "BIOS Ver:    " + upb.BoardGetBIOSVersion() + "\n"
                              + "Firmware Ver:" + upb.BoardGetFirmwareVersion().ToString("X") + "\n");
            PwmPin pin = controller.OpenPin(0);

            Console.WriteLine(Usage);
            while (exit == false)
            {
                Console.Write(pin1.pin.ToString() + ">");
                input = Console.ReadLine();
                string[] inputnum = input.Split(' ');
                switch (inputnum[0].ToLower())
                {
                case "list":
                    pwmlist();
                    break;

                case "set":
                    if (inputnum.Length == 2)
                    {
                        int pin_convert;
                        if (int.TryParse(inputnum[1], out pin_convert))
                        {
                            if (pin_convert == pin1.pin)
                            {
                                Console.WriteLine("This Pin is currently setting");
                            }
                            else
                            {
                                pin1.pin = pin_convert;
                                try
                                {
                                    controller = await PwmController.GetDefaultAsync();

                                    pin = controller.OpenPin(pin1.pin);
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e.Message);
                                }
                                Console.WriteLine("You select pin   " + pin1.pin + "   to set");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Please input : set {int}");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Please input : set {int}");
                    }
                    break;

                case "get":
                    if (inputnum.Length == 1)
                    {
                        Console.WriteLine("Pin  " + pin1.pin + "\n");
                        Console.WriteLine("Max Frequency    :   " + controller.MaxFrequency + "\n");
                        Console.WriteLine("Min Frequency    :   " + controller.MinFrequency + "\n");
                        Console.WriteLine("Actual Frequency :   " + controller.ActualFrequency + "\n");
                        Console.WriteLine("Duty Cycle       :   " + pin.GetActiveDutyCyclePercentage() + "\n");
                        Console.WriteLine("Pin Status       :   " + pin.IsStarted + "\n");
                    }
                    else
                    {
                        Console.WriteLine("Please input : get");
                    }
                    break;

                case "frequency":
                    if (inputnum.Length == 2)
                    {
                        try
                        {
                            if (double.TryParse(inputnum[1], out pin1.pin_ActualFrequency))
                            {
                                Console.WriteLine("Frequency set : " + pin1.pin_ActualFrequency);
                                controller.SetDesiredFrequency(pin1.pin_ActualFrequency);
                                pin.Start();
                            }
                            else
                            {
                                Console.WriteLine("Please input : frequency {double}");
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Please input : frequency {double}");
                    }
                    break;

                case "duty":
                    if (inputnum.Length == 2)
                    {
                        try
                        {
                            if (double.TryParse(inputnum[1], out pin1.pin_DutyCycle))
                            {
                                Console.WriteLine("duty set : " + pin1.pin_DutyCycle);
                                pin.SetActiveDutyCyclePercentage(pin1.pin_DutyCycle);
                                pin.Start();
                            }
                            else
                            {
                                Console.WriteLine("Please input : duty {double}");
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Please input : duty {double}");
                    }
                    break;

                case "exit":
                    exit = inputnum[0].Equals("exit");
                    pin.Dispose();
                    break;

                case "help":
                default:
                    Console.WriteLine(Usage);
                    break;
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Initializes the Remote Arduino Provider and stores the handles to the 4 controllers. Only runs once.
        /// </summary>
        private static async Task InitAsync()
        {
            await semaphoreSlim.WaitAsync();

            try
            {
                if (!initialized)
                {
                    // Change this to true for Bluetooth and false for USB
                    bool UseBluetooth       = false;
                    bool configurationFound = false;
                    DeviceInformation connectedDeviceInformation = null;

                    if (UseBluetooth)
                    {
                        // This assumes that there is only one BT SPP device on the computer. If there are multiple devices
                        // the you will need to identify the device from the list returned from FindAllAsync() and use
                        // that one as the device to pass to the arduino provider
                        string spp_aqs = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort);
                        DeviceInformationCollection deviceList = await DeviceInformation.FindAllAsync(spp_aqs);

                        if (deviceList.Count > 0)
                        {
                            DeviceInformation device = deviceList[0];

                            // Remember to ensure that the Firmata Sketch has the baud rate set to the same default as the BT device
                            ArduinoProviders.ArduinoProvider.Configuration = new ArduinoProviders.ArduinoConnectionConfiguration(device, 57600);
                            configurationFound = true;
                        }
                        else
                        {
                            Debug.WriteLine("No bluetooth connected Arduino found");
                        }
                    }
                    else
                    {
                        Selector selector = new Selector();
                        IEnumerable <ArduinoDeviceListEntry> list = await selector.GetDeviceList();

                        if (list.Count() > 0)
                        {
                            var listArray = list.ToArray();

                            connectedDeviceInformation = listArray[0].DeviceInformation;
                            ArduinoProviders.ArduinoProvider.Configuration = new ArduinoProviders.ArduinoConnectionConfiguration(connectedDeviceInformation, 57600);
                            configurationFound = true;
                        }
                        else
                        {
                            Debug.WriteLine("No USB Connected Ardino found");
                        }
                    }

                    // we can't use a null check on Configuration as it will auto allocate and be in an invalid state
                    if (configurationFound)
                    {
                        // Check our firmata and make sure we are good, if not we will flash the device with our required MakeCode specific firmata
                        int  retry = 2;
                        bool FirmwareUploadRequired = false;

                        while (retry-- > 0)
                        {
                            FirmwareUploadRequired = false;
                            try
                            {
                                if (firmataProvider == null)
                                {
                                    firmataProvider = new ArduinoProviders.ArduinoFirmataProvider();
                                }
                                bool firmataInitOk = false;
                                try
                                {
                                    firmataInitOk = await firmataProvider.InitializeAsync();
                                }
                                catch (Exception e)
                                {
                                    Debug.WriteLine(e.Message);
                                }

                                if (firmataInitOk == true)
                                {
                                    // there is a firmata version on the device now.  Check if it is what we need.
                                    int MajorVersion = firmataProvider.FirmataInstance.getFirmwareVersionMajor();
                                    int MinorVersion = firmataProvider.FirmataInstance.getFirmwareVersionMinor();

                                    String FirmwareName = firmataProvider.FirmataInstance.getFirmwareName();
                                    Debug.WriteLine($"Arduino with Firmata found Version {MajorVersion}.{MinorVersion} {FirmwareName}");
                                    if (String.Compare(FirmwareName, "MakeCodeFirmata.ino", StringComparison.OrdinalIgnoreCase) != 0)
                                    {
                                        // Not the makecode firmware so need to upload it
                                        FirmwareUploadRequired = true;
                                    }
                                    else
                                    {
                                        // Makecode firmware, but is it the required version?
                                        // TODO: Version check
                                    }
                                }
                                else
                                {
                                    // No firmata on the device
                                    FirmwareUploadRequired = true;
                                }
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine("Failed to get Firmata Instance: " + ex.Message);
                                FirmwareUploadRequired = true;
                            }

                            if (FirmwareUploadRequired)
                            {
                                Debug.WriteLine("Firmata update required");
                                // Need to close our current connection if any
                                if (firmataProvider != null)
                                {
                                    firmataProvider.Dispose();
                                    firmataProvider = null;

                                    ArduinoProvider.Close();
                                }

                                Arduino arduino = Arduino.GetArduino(connectedDeviceInformation.Id);

                                await arduino.Connect();

                                var programmer = arduino.GetProgrammer();

                                await programmer.Program("ms-appx:///BlockCode/MakeCodeFirmata/MakeCodeFirmata.ino.standard.hex", 28672);

                                // Now let the arduino go so we can reopen it in non progamming mode.
                                arduino.Dispose();

                                // And we will let it cycle again to verify
                                Debug.WriteLine("Arduino Firmata updated ");
                                return;
                            }
                            else
                            {
                                // Have a good firmware so store it and setup our callback
                                uwpFirmata = firmataProvider.FirmataInstance;
                                uwpFirmata.SysexMessageReceived += UwpFirmata_SysexMessageReceived;
                                retry = 0;
                            }
                        }

                        if (uwpFirmata == null)
                        {
                            // not going to be able to go on.
                            Debug.WriteLine("Arduino Firmata Initalization failed");
                            return;
                        }

                        // Now that we have a good firmata we can do the other connections
                        Windows.Devices.LowLevelDevicesController.DefaultProvider = new ArduinoProviders.ArduinoProvider();

                        gpioController = await GpioController.GetDefaultAsync();

                        pwmController = await PwmController.GetDefaultAsync();

                        adcController = await AdcController.GetDefaultAsync();

                        i2cController = await I2cController.GetDefaultAsync();

                        if (
                            (gpioController != null) &&
                            (pwmController != null) &&
                            (adcController != null) &&
                            (i2cController != null) &&
                            (uwpFirmata != null)
                            )
                        {
                            // We got good initialzation
                            initialized = true;
                        }
                    }
                    else
                    {
                        // Something went wrong with init
                        Debug.WriteLine("Arduino Initalization failed");
                    }
                }

                else
                {
                    // Duplicate initalization, we can just ignore this
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Arduino Initalization failed: " + e.Message);
            }
            finally
            {
                // When the task is ready, release the semaphore. It is vital to ALWAYS release the semaphore when we are ready,
                // or else we will end up with a Semaphore that is forever locked.
                // This is why it is important to do the Release within a try...finally clause;
                // program execution may crash or take a different path, this way you are guaranteed execution
                semaphoreSlim.Release();
            }
        }