Exemple #1
0
        public byte GetBatteryLevel(ulong pMacAdress)
        {
            IMetaWearBoard board = _mwBoardsManager.GetBoard(pMacAdress);

            if ((board != null) && !board.InMetaBootMode)
            {
                return(board.ReadBatteryLevelAsync().RunSynchronously <byte>());
            }
            return(0);
        }
Exemple #2
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;
                    }
                }
            }
        }