Esempio n. 1
0
        public static async Task <I2CMpuDevice> CreateMpuDeviceAsync(string deviceName)
        {
            Log.WriteLine("finding device {0}", deviceName != null ? deviceName : "(default)");

            Windows.Devices.Enumeration.DeviceInformation info = null;
            if (deviceName != null)
            {
                info = await FindAsync(I2cDevice.GetDeviceSelector(deviceName));
            }
            else
            {
                info = await FindAsync(I2cDevice.GetDeviceSelector());
            }
            Log.WriteLine("I2c device info {0} null", info == null ? "is" : "is not");

            var settings = new I2cConnectionSettings(DefaultMpuAddress);

            settings.BusSpeed = I2cBusSpeed.FastMode;
            var d = new I2CMpuDevice();

            d.Device = await AsAsync(I2cDevice.FromIdAsync(info.Id, settings));

            return(d);
        }
Esempio n. 2
0
        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...");
            AzureConnection connection = null;
            I2CMpuDevice    mpu        = null;
            // TODO: Options.List
            await Task.WhenAll(
                Task.Run(async() => {
                try {
                    if (!Options.Test)
                    {
                        Log.WriteLine("starting connection creation");
                        connection = await AzureConnection.CreateAzureConnectionAsync();
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine("I2c Main CreateAzureConnectionAsync exception {0}", e.ToString());
                }
            }),
                Task.Run(async() =>
            {
                try
                {
                    Log.WriteLine("creating mpu device {0}", Options.DeviceName != null ? Options.DeviceName : "(default)");
                    mpu = await I2CMpuDevice.CreateMpuDeviceAsync(Options.DeviceName);
                    mpu.InitAsync().Wait();
                    if (Options.Test)
                    {
                        Log.WriteLine("initiating test");
                        if (Options.Test)
                        {
                            mpu.Test(Options.TestTime.Value);
                        }
                        else
                        {
                            mpu.Test(TimeSpan.FromSeconds(15));
                        }
                        Environment.Exit(2);
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine("I2c exception {0}", e.ToString());
                    Environment.Exit(2);
                }
            }
                         )
                );

            try
            {
                EventHandler <string> ModuleLoadedHandler = async(Object sender, string moduleName) =>
                {
                    try
                    {
                        Log.WriteLine("module loaded.   resending state");
                        await connection.NotifyNewModuleOfCurrentStateAsync();
                    }
                    catch (Exception e)
                    {
                        Log.WriteLineError("failed to notify state {0}", e.ToString());
                        Environment.Exit(2);
                    }
                };
                if (connection != null)
                {
                    ((AzureModule)(connection.Module)).ModuleLoaded += ModuleLoadedHandler;
                }

                EdgeModuleSamples.Common.Device.MpuDevice.OrientationEventHandler OrientationChangedHandler = (device, change) =>
                {
                    connection.UpdateObject(new KeyValuePair <string, object>(Keys.Orientation, change.newOrientation));
                };
                mpu.OrientationChanged += OrientationChangedHandler;
                try
                {
                    Log.WriteLine("Initialization Complete. have connection and device.  ");

                    Task.WaitAll(Task.Run(async() =>
                    {
                        try
                        {
                            await mpu.BeginOrientationMonitoringAsync();
                        }
                        catch (Exception e)
                        {
                            Log.WriteLine("I2c wait spin exception {0}", e.ToString());
                        }
                    }));
                } finally
                {
                    mpu.OrientationChanged -= OrientationChangedHandler;
                    if (connection != null)
                    {
                        ((AzureModule)(connection.Module)).ModuleLoaded -= ModuleLoadedHandler;
                    }
                }
            } finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
                mpu.Dispose();
            }
            return(0);
        }