public static async Task <PWMDevice> CreatePWMDeviceAsync(string deviceName, int pinNumber) { Log.WriteLine("finding device {0}", deviceName != null ? deviceName : "(default)"); Windows.Devices.Enumeration.DeviceInformation info = null; if (deviceName != null) { info = await FindAsync(PwmController.GetDeviceSelector(deviceName)); } else { info = await FindAsync(PwmController.GetDeviceSelector()); } Log.WriteLine("PWM device info {0} null", info == null ? "is" : "is not"); var d = new PWMDevice(); d.Device = await AsAsync(PwmController.FromIdAsync(info.Id)); if (d.Device != null) { Log.WriteLine("Pin Count {0}", d.Device.PinCount); } d.Pin = d.Device.OpenPin(pinNumber); return(d); }
static async Task <int> MainAsync(string[] args) { Log.WriteLine("Starting async..."); var Options = new AppOptions(); Options.Parse(args); Log.Enabled = !Options.Quiet; Log.Verbose = Options.Verbose; Log.WriteLine("arg parse complete..."); if (Options.List) { await PWMDevice.ListPWMDevicesAsync(); } // speed as int pct 0-100 Dictionary <string, int> FruitSpeed = new Dictionary <string, int>() { { "apple", 50 }, { "pear", 50 }, { "pen", 0 }, { "grapes", 100 }, { "other", 0 } }; AzureConnection connection = null; PWMDevice pwm = null; await Task.WhenAll( Task.Run(async() => { try { if (!Options.Test) { Log.WriteLine("starting connection creation"); connection = await AzureConnection.CreateAzureConnectionAsync(); } } catch (Exception e) { Log.WriteLine("PWM Main CreateAzureConnectionAsync exception {0}", e.ToString()); } }), Task.Run(async() => { try { // TODO: fix appoptions to allow pin number to be specified. pwm = await PWMDevice.CreatePWMDeviceAsync(Options.DeviceName, 0); if (Options.Test) { Log.WriteLine("initiating pin test"); var ts = TimeSpan.FromSeconds(15); var tc = 80; if (Options.TestTime.HasValue) { ts = Options.TestTime.Value; } if (Options.TestCount.HasValue) { tc = Options.TestCount.Value; } pwm.Test(ts, tc); } } catch (Exception e) { Log.WriteLine("PWM InitOutputPins exception {0}", e.ToString()); } } ) ); AzureModule m = null; EventHandler <ConfigurationType> ConfigurationChangedHandler = async(object sender, ConfigurationType newConfiguration) => { var module = (AzureModule)sender; Log.WriteLine("updating pwm pin config with {0}", newConfiguration.ToString()); //await pwm.UpdatePinConfigurationAsync(newConfiguration.GpioPins); await Task.CompletedTask; }; EventHandler <string> FruitChangedHandler = (object sender, string fruit) => { Log.WriteLine("fruit changed to {0}", fruit.ToLower()); var module = (AzureModule)sender; pwm.SetSpeed(FruitSpeed[fruit.ToLower()]); }; if (!Options.Test) { m = (AzureModule)connection.Module; m.ConfigurationChanged += ConfigurationChangedHandler; m.FruitChanged += FruitChangedHandler; await Task.Run(async() => { try { //Log.WriteLine("initializing pwm pin config with {0}", m.Configuration.GpioPins); //await pwm.UpdatePinConfigurationAsync(m.Configuration.GpioPins); await Task.CompletedTask; } catch (Exception e) { Log.WriteLine("GPIO UpdatePinConfig Lambda exception {0}", e.ToString()); } }); await connection.NotifyModuleLoadAsync(); } try { Log.WriteLine("Initialization Complete. have {0}", Options.Test ? "device" : "connection and device"); Task.WaitAll(Task.Run(() => { try { for (; ;) { Log.WriteLine("{0} wait spin", Environment.TickCount); //pwm.LogInputPins(); Thread.Sleep(TimeSpan.FromSeconds(30)); } } catch (Exception e) { Log.WriteLine("PWM wait spin exception {0}", e.ToString()); } })); } finally { if (!Options.Test) { m.ConfigurationChanged -= ConfigurationChangedHandler; m.FruitChanged -= FruitChangedHandler; } if (connection != null) { connection.Dispose(); } pwm.Dispose(); } return(0); }