internal PedometerReading(int steps, TimeSpan duration, PedometerStepKind kind, DateTimeOffset timestamp) { _steps = steps; _duration = duration; _kind = kind; _timestamp = timestamp; }
/// <summary> /// Invoked when 'GetCurrentButton' is clicked. /// 'ReadingChanged' will not be fired when there is no activity on the pedometer /// and hence can't be reliably used to get the current step count. This handler makes /// use of pedometer history on the system to get the current step count of the parameter /// </summary> async private void GetCurrentButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { DateTime dt = DateTime.FromFileTimeUtc(0); int totalStepCount = 0; int lastTotalCount = 0; rootPage.NotifyUser("Retrieving history to get current step counts", NotifyType.StatusMessage); // Disable the button while we get the history GetCurrentButton.IsEnabled = false; DateTimeOffset fromBeginning = new DateTimeOffset(dt); IReadOnlyList <PedometerReading> historyReadings = await Pedometer.GetSystemHistoryAsync(fromBeginning); // History always returns chronological list of step counts for all PedometerStepKinds // And each record represents cumulative step counts for that step kind. // So we will use the last set of records - that gives us the cumulative step count for // each kind and ignore rest of the records PedometerStepKind stepKind = PedometerStepKind.Unknown; DateTimeOffset lastReadingTimestamp; bool resetTotal = false; foreach (PedometerReading reading in historyReadings) { if (stepKind == PedometerStepKind.Running) { // reset the total after reading the 'PedometerStepKind.Running' count resetTotal = true; } totalStepCount += reading.CumulativeSteps; if (resetTotal) { lastReadingTimestamp = reading.Timestamp; lastTotalCount = totalStepCount; stepKind = PedometerStepKind.Unknown; totalStepCount = 0; resetTotal = false; } else { stepKind++; } } ScenarioOutput_TotalStepCount.Text = lastTotalCount.ToString(); DateTimeFormatter timestampFormatter = new DateTimeFormatter("shortdate longtime"); ScenarioOutput_Timestamp.Text = timestampFormatter.Format(lastReadingTimestamp); rootPage.NotifyUser("Hit the 'Get steps count' Button", NotifyType.StatusMessage); GetCurrentButton.IsEnabled = true; }
internal PedometerReading( int cumulativeSteps, TimeSpan cumulativeStepsDuration, PedometerStepKind stepKind, DateTimeOffset timestamp) { CumulativeSteps = cumulativeSteps; CumulativeStepsDuration = cumulativeStepsDuration; StepKind = stepKind; Timestamp = timestamp; }
public void UpdateText(SensorData sensorData) { try { int index = sensorData._reading.Count - 1; if (sensorData._count == Sensor.currentId) { UpdateProperty(sensorData._deviceId, sensorData._deviceName, sensorData._reportInterval, sensorData._minReportInterval, sensorData._reportLatency, sensorData._category, sensorData._persistentUniqueId, sensorData._manufacturer, sensorData._model, sensorData._connectionType); } if (StackPanelSensor.Visibility == Visibility.Visible) { if (sensorData._sensorType == Sensor.ACCELEROMETER || sensorData._sensorType == Sensor.ACCELEROMETERLINEAR || sensorData._sensorType == Sensor.ACCELEROMETERGRAVITY) { double margin = 80; double x = Math.Min(1, sensorData._reading[index].value[0]); double y = Math.Min(1, sensorData._reading[index].value[1]); double square = x * x + y * y; if (square > 1) { x /= Math.Sqrt(square); y /= Math.Sqrt(square); } DisplayInformation displayInformation = DisplayInformation.GetForCurrentView(); if (displayInformation.NativeOrientation == DisplayOrientations.Landscape) { switch (displayInformation.CurrentOrientation) { case DisplayOrientations.Landscape: EllipseAccelerometer.Margin = new Thickness() { Left = margin * x, Bottom = margin * y }; break; case DisplayOrientations.Portrait: EllipseAccelerometer.Margin = new Thickness() { Left = margin * y, Bottom = -margin * x }; break; case DisplayOrientations.LandscapeFlipped: EllipseAccelerometer.Margin = new Thickness() { Left = -margin * x, Bottom = -margin * y }; break; case DisplayOrientations.PortraitFlipped: EllipseAccelerometer.Margin = new Thickness() { Left = -margin * y, Bottom = margin * x }; break; } } else if (displayInformation.NativeOrientation == DisplayOrientations.Portrait) { switch (displayInformation.CurrentOrientation) { case DisplayOrientations.Landscape: EllipseAccelerometer.Margin = new Thickness() { Left = -margin * y, Bottom = margin * x }; break; case DisplayOrientations.Portrait: EllipseAccelerometer.Margin = new Thickness() { Left = margin * x, Bottom = margin * y }; break; case DisplayOrientations.LandscapeFlipped: EllipseAccelerometer.Margin = new Thickness() { Left = margin * y, Bottom = -margin * x }; break; case DisplayOrientations.PortraitFlipped: EllipseAccelerometer.Margin = new Thickness() { Left = -margin * x, Bottom = -margin * y }; break; } } } for (int i = 0; i < sensorData._reading[index].value.Length; i++) { TextBlockProperty[i].Text = sensorData._property[i]; TextBlockValue[i].Text = String.Format(" {0,5:0.00}", sensorData._reading[index].value[i]); TextBlockMinValue[i].Text = String.Format(" {0,5:0.0}", sensorData._minValue[i]); TextBlockMaxValue[i].Text = String.Format(" {0,5:0.0}", sensorData._maxValue[i]); if (sensorData._property[i].StartsWith("MagneticNorth")) { RotateTransform rotateCompass = new RotateTransform(); ImageCompass.RenderTransform = rotateCompass; rotateCompass.Angle = (-1) * Convert.ToDouble(sensorData._reading[index].value[i]); rotateCompass.CenterX = ImageCompass.ActualWidth / 2; rotateCompass.CenterY = ImageCompass.ActualHeight / 2; } else if (sensorData._property[i].StartsWith("AngularVelocityX")) { RotateTransform rotateGyrometerX = new RotateTransform() { CenterX = ImageGyrometerX.ActualWidth / 2, CenterY = ImageGyrometerX.ActualHeight / 2 }; ImageGyrometerX.RenderTransform = rotateGyrometerX; rotateGyrometerX.Angle = Math.Max(-135, Math.Min(135, Convert.ToDouble(sensorData._reading[index].value[i]))); } else if (sensorData._property[i].StartsWith("AngularVelocityY")) { RotateTransform rotateGyrometerY = new RotateTransform(); ImageGyrometerY.RenderTransform = rotateGyrometerY; rotateGyrometerY.Angle = Math.Max(-135, Math.Min(135, Convert.ToDouble(sensorData._reading[index].value[i]))); rotateGyrometerY.CenterX = ImageGyrometerY.ActualWidth / 2; rotateGyrometerY.CenterY = ImageGyrometerY.ActualHeight / 2; } else if (sensorData._property[i].StartsWith("AngularVelocityZ")) { RotateTransform rotateGyrometerZ = new RotateTransform(); ImageGyrometerZ.RenderTransform = rotateGyrometerZ; rotateGyrometerZ.Angle = Math.Max(-135, Math.Min(135, Convert.ToDouble(sensorData._reading[index].value[i]))); rotateGyrometerZ.CenterX = ImageGyrometerZ.ActualWidth / 2; rotateGyrometerZ.CenterY = ImageGyrometerZ.ActualHeight / 2; } else if (sensorData._property[i].StartsWith("Pitch")) { RotateTransform rotate = new RotateTransform() { CenterX = ImageInclinometerPitch.ActualWidth / 2, CenterY = ImageInclinometerPitch.ActualHeight / 2 }; ImageInclinometerPitch.RenderTransform = rotate; rotate.Angle = sensorData._reading[index].value[i]; } else if (sensorData._property[i].StartsWith("Roll")) { RotateTransform rotate = new RotateTransform() { CenterX = ImageInclinometerRoll.ActualWidth / 2, CenterY = ImageInclinometerRoll.ActualHeight / 2 }; ImageInclinometerRoll.RenderTransform = rotate; rotate.Angle = sensorData._reading[index].value[i]; } else if (sensorData._property[i] == "Yaw (°)") { RotateTransform rotate = new RotateTransform() { CenterX = ImageInclinometerYaw.ActualWidth / 2, CenterY = ImageInclinometerYaw.ActualHeight / 2 }; ImageInclinometerYaw.RenderTransform = rotate; rotate.Angle = -sensorData._reading[index].value[i]; } else if (sensorData._property[i] == "Illuminance (lux)") { TextBlockSensor.Text = "💡"; if (sensorData._reading[index].value[i] < 1) { TextBlockSensor.Opacity = 0.1; } else { TextBlockSensor.Opacity = Math.Min(0.1 + Math.Log(sensorData._reading[index].value[i], 2) / 10, 1); } } else if (sensorData._property[i] == "CumulativeSteps") { int value = Convert.ToInt32(sensorData._reading[index].value[i]) / 100; _plotCanvas.SetRange((value + 1) * 100, value * 100); } else if (sensorData._property[i] == "HeadingAccuracy" || sensorData._property[i] == "YawAccuracy") { MagnetometerAccuracy magnetometerAccuracy = (MagnetometerAccuracy)sensorData._reading[index].value[i]; TextBlockValue[i].Text = String.Format(" {0}", magnetometerAccuracy); } else if (sensorData._property[i] == "IsDetected") { TextBlockSensor.Text = (sensorData._reading[index].value[i] > 0.5 ? "📲" : "📱"); } else if (sensorData._property[i] == "StepKind") { PedometerStepKind pedometerStepKind = (PedometerStepKind)sensorData._reading[index].value[i]; TextBlockValue[i].Text = String.Format(" {0}", pedometerStepKind); TextBlockSensor.Text = DictionaryStepKind[pedometerStepKind]; } else if (sensorData._sensorType == Sensor.SIMPLEORIENTATIONSENSOR) { SimpleOrientation simpleOrientation = (SimpleOrientation)sensorData._reading[index].value[i]; TextBlockValue[i].Text = String.Format(" {0}", simpleOrientation).Replace("DegreesCounterclockwise", "°↺"); TextBlockMinValue[i].Text = ""; TextBlockMaxValue[i].Text = ""; } else if (sensorData._sensorType == Sensor.ACTIVITYSENSOR) { if (sensorData._reading[index].value[i] == Sensor.ACTIVITYNONE) { TextBlockValue[i].Text = "None"; } else if (sensorData._reading[index].value[i] == Sensor.ACTIVITYNOTSUPPORTED) { TextBlockValue[i].Text = "Not Supported"; } else { ActivitySensorReadingConfidence activitySensorReadingConfidence = (ActivitySensorReadingConfidence)sensorData._reading[index].value[i]; TextBlockValue[i].Text = String.Format(" {0}", activitySensorReadingConfidence); TextBlockSensor.Text = DictionaryActivity[(ActivityType)i]; } } else if (sensorData._sensorType == Sensor.LIGHTSENSOR) { if (sensorData._reading[index].value[i] == -1) { TextBlockValue[i].Text = "N/A"; TextBlockMinValue[i].Text = "N/A"; TextBlockMaxValue[i].Text = "N/A"; } } } } } catch { } }
/// <summary> /// Invoked when 'GetCurrentButton' is clicked. /// 'ReadingChanged' will not be fired when there is no activity on the pedometer /// and hence can't be reliably used to get the current step count. This handler makes /// use of pedometer history on the system to get the current step count of the parameter /// </summary> async private void GetCurrentButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { // Determine if we can access pedometers var deviceAccessInfo = DeviceAccessInformation.CreateFromDeviceClassId(PedometerClassId); if (deviceAccessInfo.CurrentStatus == DeviceAccessStatus.Allowed) { // Determine if a pedometer is present // This can also be done using Windows::Devices::Enumeration::DeviceInformation::FindAllAsync var sensor = await Pedometer.GetDefaultAsync(); if (sensor != null) { DateTime dt = DateTime.FromFileTimeUtc(0); int totalStepCount = 0; int lastTotalCount = 0; rootPage.NotifyUser("Retrieving history to get current step counts", NotifyType.StatusMessage); // Disable the button while we get the history GetCurrentButton.IsEnabled = false; DateTimeOffset fromBeginning = new DateTimeOffset(dt); try { var historyReadings = await Pedometer.GetSystemHistoryAsync(fromBeginning); // History always returns chronological list of step counts for all PedometerStepKinds // And each record represents cumulative step counts for that step kind. // So we will use the last set of records - that gives us the cumulative step count for // each kind and ignore rest of the records PedometerStepKind stepKind = PedometerStepKind.Unknown; DateTimeOffset lastReadingTimestamp; bool resetTotal = false; foreach (PedometerReading reading in historyReadings) { if (stepKind == PedometerStepKind.Running) { // reset the total after reading the 'PedometerStepKind.Running' count resetTotal = true; } totalStepCount += reading.CumulativeSteps; if (resetTotal) { lastReadingTimestamp = reading.Timestamp; lastTotalCount = totalStepCount; stepKind = PedometerStepKind.Unknown; totalStepCount = 0; resetTotal = false; } else { stepKind++; } } ScenarioOutput_TotalStepCount.Text = lastTotalCount.ToString(); DateTimeFormatter timestampFormatter = new DateTimeFormatter("shortdate longtime"); ScenarioOutput_Timestamp.Text = timestampFormatter.Format(lastReadingTimestamp); rootPage.NotifyUser("Hit the 'Get steps count' Button", NotifyType.StatusMessage); } catch (UnauthorizedAccessException) { rootPage.NotifyUser("User has denied access to activity history", NotifyType.ErrorMessage); } // Re-enable button GetCurrentButton.IsEnabled = true; } else { rootPage.NotifyUser("No pedometers found", NotifyType.ErrorMessage); } } else { rootPage.NotifyUser("Access to pedometers is denied", NotifyType.ErrorMessage); } }