Exemplo n.º 1
0
        private async void _OnBLEDeviceFound(BluetoothLEAdvertisementReceivedEventArgs pBLEAdvertisement)
        {
            // Treats BLE devices one after the other to avoid issues with async tasks.
            _bleScanner.StopScanning();

            if (!_connectedMWBoards.Keys.Contains(pBLEAdvertisement.BluetoothAddress))
            {
                BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(pBLEAdvertisement.BluetoothAddress);

                if (bleDevice != null)
                {
                    IMetaWearBoard mwBoard = MbientLab.MetaWear.Win10.Application.GetMetaWearBoard(bleDevice);
                    //mwBoard.TimeForResponse = 2000;
                    await mwBoard.InitializeAsync();

                    if (mwBoard.IsConnected)
                    {
                        // board is connected.
                        Console.WriteLine(string.Format("[MetaWearBoardsManager] MetaWear board {0} {1}.",
                                                        Global.MacToString(bleDevice.BluetoothAddress), bleDevice.ConnectionStatus));
                        // Add board.
                        _connectedMWBoards.Add(bleDevice.BluetoothAddress, new Tuple <IMetaWearBoard, BluetoothLEDevice>(mwBoard, bleDevice));
                        bleDevice.ConnectionStatusChanged += _OnConnectionStatusChanged;
                    }
                }
            }

            // Check for restarting Scan.
            _CheckForScanning();
        }
Exemplo n.º 2
0
        static async Task Main(string[] args)
        {
            IMetaWearBoard board   = null;
            bool           succeed = false;
            int            retries = 5;
            //while (!succeed && retries > 0)
            {
                //try
                {
                    board = MbientLab.MetaWear.NetStandard.Application.GetMetaWearBoard(args[0]);
                    //var board = BLEMetawear.Application.GetMetaWearBoard(args[0]);
                    board.TimeForResponse         = 100000;
                    board.OnUnexpectedDisconnect += OnDisconneted;
                    await board.InitializeAsync();

                    succeed = true;
                }
                //catch
                {
                    retries--;
                }
            }

            ILed led = null;
            ISensorFusionBosch sensor = null;

            if (board.IsConnected)
            {
                led = board.GetModule <ILed>();
                led.EditPattern(MbientLab.MetaWear.Peripheral.Led.Color.Green, MbientLab.MetaWear.Peripheral.Led.Pattern.Solid);
                led.Play();

                sensor = board.GetModule <ISensorFusionBosch>();
                sensor.Configure();

                var rout = await sensor.EulerAngles.AddRouteAsync(source =>
                {
                    try
                    {
                        source.Stream(data =>
                        {
                            var value       = data.Value <EulerAngles>();
                            var AngularRead = (int)value.Roll;
                            var OrbitalRead = (int)value.Pitch;
                            //Console.Clear();
                            Console.Write($"\rroll: {value.Roll} pitch:{value.Pitch} Yaw:{value.Yaw}      ");



                            //Rotate(-value.Pitch, 0 , -value.Yaw);
                        });
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                        //LogException(LoggerCategory, ex, "Could not initialize IMU stream callback");
                        throw;
                    }
                });

                sensor.EulerAngles.Start();
                sensor.Start();
            }
            ConsoleKeyInfo key = new ConsoleKeyInfo();

            while (key.Key != ConsoleKey.Q)
            {
                key = Console.ReadKey();
            }
            if (board.IsConnected)
            {
                if (led != null)
                {
                    led.Stop(true);
                }
                if (sensor != null)
                {
                    sensor.EulerAngles.Stop();
                    sensor.Stop();
                }

                await board.DisconnectAsync();

                //board.TearDown();
            }
        }
Exemplo n.º 3
0
        private async void OnConnect(object sender, RoutedEventArgs e)
        {
            var item = lst.SelectedItem as BLEDevice;

            if (item != null)
            {
                if (m_Metaware == null)
                {
                    await MbientLab.MetaWear.NetStandard.Application.ClearDeviceCacheAsync(item.Mac);

                    m_Metaware = MbientLab.MetaWear.NetStandard.Application.GetMetaWearBoard(item.Mac);
                    m_Metaware.TimeForResponse = 100000;
                }
                if (m_Metaware.IsConnected)
                {
                    await Disconnect();
                }
                else
                {
                    try
                    {
                        int retries = 5;
                        do
                        {
                            try
                            {
                                btnConnect.Content   = "Connecting...";
                                btnConnect.IsEnabled = false;
                                m_Metaware.OnUnexpectedDisconnect += OnDisconneted;
                                await m_Metaware.InitializeAsync();

                                retries = -1;
                            }
                            catch
                            {
                                retries--;
                            }
                        } while (retries > 0);
                        btnConnect.IsEnabled = true;
                        if (m_Metaware.IsConnected)
                        {
                            var batteryLevel = await m_Metaware.ReadBatteryLevelAsync();

                            var led = m_Metaware.GetModule <ILed>();
                            led.EditPattern(MbientLab.MetaWear.Peripheral.Led.Color.Green, MbientLab.MetaWear.Peripheral.Led.Pattern.Solid);
                            led.Play();
                            item.BatteryLevel  = batteryLevel;
                            BatteryText.Text   = $" Battery:{batteryLevel}";
                            btnConnect.Content = "Disconnect";
                            m_Connected        = true;

                            //m_Metaware.GetModule<IMagnetometerBmm150>();
                            //m_Gyro = m_Metaware.GetModule<IGyroBmi160>();
                            //m_Gyro.Configure(OutputDataRate._100Hz);
                            //m_Accelerometer = m_Metaware.GetModule<IAccelerometer>();
                            //m_Accelerometer.Configure(odr: 100f, range: 8f);
                            m_SensorFusion = m_Metaware.GetModule <ISensorFusionBosch>();
                            m_SensorFusion.Configure();

                            var rout = await m_SensorFusion.EulerAngles.AddRouteAsync(source =>
                            {
                                try
                                {
                                    source.Stream(async data =>
                                    {
                                        await Dispatcher.InvokeAsync(() =>
                                        {
                                            var value      = data.Value <EulerAngles>();
                                            AngleText.Text = value.ToString();
                                            Rotate(-value.Roll, -value.Pitch, -value.Yaw);
                                            //Rotate(-value.Pitch, 0 , -value.Yaw);
                                        });
                                    });
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                }
                            });

                            if (!rout.Valid)
                            {
                                MessageBox.Show("rout invalid");
                            }
                            await m_SensorFusion.Gravity.AddRouteAsync(source =>
                            {
                                source.Stream(async data =>
                                {
                                    await Dispatcher.InvokeAsync(() =>
                                    {
                                        var value        = data.Value <Acceleration>();
                                        GravityText.Text = value.ToString();
                                        //Rotate(-value.Roll, -value.Pitch, -value.Yaw);
                                        //Rotate(-value.Pitch, 0 , -value.Yaw);
                                    });
                                });
                            });

                            //var calib = await m_SensorFusion.ReadCalibrationStateAsync();
                            m_SensorFusion.Gravity.Start();
                            m_SensorFusion.EulerAngles.Start();
                            m_SensorFusion.Start();


                            //Scanner.Stop();
                        }
                        else
                        {
                            throw new InvalidOperationException("Could not connect");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        btnConnect.Content = "Connect";
                        m_Connected        = false;
                    }
                }
            }
        }