示例#1
0
        public static async Task <UARTDevice> CreateUARTDeviceAsync(string deviceName)
        {
            Log.WriteLine("finding device {0}", deviceName != null ? deviceName : "(default)");

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

            var d = new UARTDevice();

            d.Device = await AsAsync(SerialDevice.FromIdAsync(info.Id));

            d.Device.DataBits     = 8;
            d.Device.Handshake    = SerialHandshake.None;
            d.Device.BaudRate     = 9600;
            d.Device.WriteTimeout = TimeSpan.FromMilliseconds(1000);
            d.Device.ReadTimeout  = TimeSpan.FromMilliseconds(1000);
            d.Device.StopBits     = SerialStopBitCount.One;
            d.Device.Parity       = SerialParity.None;
            Log.WriteLine("UART Settings complete");
            d._write = new DataWriter(d.Device.OutputStream);
            Log.WriteLine("have datawriter");
            return(d);
        }
示例#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...");
            // TODO: Options.List
            Dictionary <string, RGBColor> FruitColors = new Dictionary <string, RGBColor>()
            {
                { "apple", Colors.Red },
                { "pear", Colors.Yellow },
                { "pen", Colors.Green },
                { "grapes", Colors.Blue },
                { "other", Colors.Black }
            };
            AzureConnection connection = null;
            UARTDevice      uart       = 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("Main CreateAzureConnectionAsync exception {0}", e.ToString());
                }
            }),
                Task.Run(async() =>
            {
                try
                {
                    Log.WriteLine("creating UART device {0}", Options.DeviceName != null ? Options.DeviceName : "(default)");
                    uart = await UARTDevice.CreateUARTDeviceAsync(Options.DeviceName);
                    await uart.InitAsync();
                    Log.WriteLine("uart initialzed");
                    if (Options.Test)
                    {
                        Log.WriteLine("initiating test");
                        if (Options.TestMessage != null && Options.TestMessage.Length > 1)
                        {
                            await uart.TestAsync(Options.TestMessage);
                        }
                        else
                        {
                            await uart.TestAsync("Test");
                        }
                        Environment.Exit(2);
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine("UART exception {0}", e.ToString());
                }
            }
                         )
                );

            try
            {
                string      currentFruit       = null;
                Orientation currentOrientation = Orientation.Unknown;
                AzureModule m = (AzureModule)connection.Module;
                EventHandler <ConfigurationType> ConfigurationChangedHandler = async(object sender, ConfigurationType newConfiguration) =>
                {
                    var module = (AzureModule)sender;
                    Log.WriteLine("updating UART_lcd config with {0}", newConfiguration.ToString());
                    //await gpio.UpdatePinConfigurationAsync(newConfiguration.GpioPins);
                    await Task.CompletedTask;
                };
                m.ConfigurationChanged += ConfigurationChangedHandler;
                try
                {
                    EventHandler <string> FruitChangedHandler = async(object sender, string fruit) =>
                    {
                        Log.WriteLine("fruit changed sent {0}", fruit.ToLower());
                        Log.WriteLine("current fruit {0}", currentFruit);
                        if (fruit.ToLower() != currentFruit)
                        {
                            currentFruit = fruit.ToLower();
                            Log.WriteLine("setting fruit to {0}", fruit.ToLower());
                            LCDMessage msg;
                            msg.bgcolor = currentOrientation == Orientation.UpsideDown ? Colors.White : FruitColors[currentFruit.ToLower()];
                            msg.clear   = true;
                            msg.msg     = currentFruit;
                            uart.QueueMessage(msg);
                        }
                        else
                        {
                            Log.WriteLine("fruit already correct -- skipping");
                            await Task.CompletedTask;
                        }
                    };
                    m.FruitChanged += FruitChangedHandler;
                    try
                    {
                        EventHandler <Orientation> OrientationChangedHandler = async(object sender, Orientation o) =>
                        {
                            Log.WriteLine("orientation changed sent {0}", o.ToString());
                            Log.WriteLine("current orientation {0}", currentOrientation);
                            if (o != currentOrientation)
                            {
                                currentOrientation = o;
                                Log.WriteLine("setting orientation to {0}", o.ToString());
                                LCDMessage msg;

                                msg.bgcolor = o == Orientation.UpsideDown ? Colors.White : FruitColors[currentFruit != null ? currentFruit.ToLower() : "other"];
                                msg.clear   = false;
                                msg.msg     = null;
                                uart.QueueMessage(msg);
                            }
                            else
                            {
                                Log.WriteLine("fruit already correct -- skipping");
                                await Task.CompletedTask;
                            }
                        };
                        m.OrientationChanged += OrientationChangedHandler;
                        try
                        {
                            await uart.SetBackgroundAsync(Colors.White);

                            await uart.WriteStringAsync("Loaded");

                            await connection.NotifyModuleLoadAsync();

                            Log.WriteLine("Initialization Complete. have connection and device.  ");

                            Task.WaitAll(Task.Run(() =>
                            {
                                try
                                {
                                    // TODO: implement ctl-c handler and cancellation token
                                    for (; ;)
                                    {
                                        Log.WriteLine("{0} wait spin", Environment.TickCount);
                                        Thread.Sleep(TimeSpan.FromSeconds(30));
                                    }
                                }
                                catch (Exception e)
                                {
                                    Log.WriteLine("wait spin exception {0}", e.ToString());
                                }
                            }));
                        }
                        finally
                        {
                            m.OrientationChanged -= OrientationChangedHandler;
                        }
                    }
                    finally
                    {
                        m.FruitChanged -= FruitChangedHandler;
                    }
                }
                finally
                {
                    m.ConfigurationChanged -= ConfigurationChangedHandler;
                }
            } finally
            {
                uart.Dispose();
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
            return(0);
        }