/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; namespace Tizen.Sensor { /// /// Pedometer Sensor Class. Used for registering callbacks for pedometer and getting pedometer data /// public sealed class Pedometer : Sensor { private static string PedometerKey = "http://tizen.org/feature/sensor.pedometer"; /// /// Gets the step count /// /// 3 /// Step count public uint StepCount { get; private set; } = 0; /// /// Gets the walking step count /// /// 3 /// Walk step count public uint WalkStepCount { get; private set; } = 0; /// /// Gets the running step count /// /// 3 /// Run step count public uint RunStepCount { get; private set; } = 0; /// /// Gets the moving distance /// /// 3 /// Moving distance public float MovingDistance { get; private set; } = 0; /// /// Gets the calorie burned /// /// 3 /// Calorie Burned public float CalorieBurned { get; private set; } = 0; /// /// Gets the last speed /// /// 3 /// Last speed public float LastSpeed { get; private set; } = 0; /// /// Gets the last stepping frequency /// /// 3 /// Last stepping frequency public float LastSteppingFrequency { get; private set; } = 0; /// /// Gets the last step status /// /// 3 /// Last step status public PedometerState LastStepStatus { get; private set; } = PedometerState.Unknown; /// /// Returns true or false based on whether pedometer sensor is supported by device. /// /// 3 /// true if supported; otherwise, false. public static bool IsSupported { get { Log.Info(Globals.LogTag, "Checking if the Pedometer sensor is supported"); return CheckIfSupported(SensorType.Pedometer, PedometerKey); } } /// /// Returns the number of pedometer sensors available on the device. /// /// 3 /// The count of pedometer sensors public static int Count { get { Log.Info(Globals.LogTag, "Getting the count of pedometer sensors"); return GetCount(); } } /// /// Initializes a new instance of the class. /// /// 3 /// http://tizen.org/privilege/healthinfo /// public /// http://tizen.org/feature/sensor.pedometer /// Thrown when an invalid argument is used /// Thrown when the sensor is not supported /// Thrown when the app has no privilege to use the sensor /// Thrown when the operation is invalid for the current state /// /// Index. Default value for this is 0. Index refers to a particular pedometer sensor in case of multiple sensors /// public Pedometer(uint index = 0) : base(index) { Log.Info(Globals.LogTag, "Creating Pedometer object"); } internal override SensorType GetSensorType() { return SensorType.Pedometer; } /// /// Event Handler for storing the callback functions for event corresponding to change in pedometer sensor data. /// /// 3 public event EventHandler DataUpdated; private static int GetCount() { IntPtr list; int count; int error = Interop.SensorManager.GetSensorList(SensorType.Pedometer, out list, out count); if (error != (int)SensorError.None) { Log.Error(Globals.LogTag, "Error getting sensor list for pedometer"); count = 0; } else Interop.Libc.Free(list); return count; } private static Interop.SensorListener.SensorEventCallback _callback; internal override void EventListenStart() { _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => { Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr); TimeSpan = new TimeSpan((Int64)sensorData.timestamp); StepCount = (uint)sensorData.values[0]; WalkStepCount = (uint)sensorData.values[1]; RunStepCount = (uint)sensorData.values[2]; MovingDistance = sensorData.values[3]; CalorieBurned = sensorData.values[4]; LastSpeed = sensorData.values[5]; LastSteppingFrequency = sensorData.values[6]; LastStepStatus = (PedometerState)sensorData.values[7]; DataUpdated?.Invoke(this, new PedometerDataUpdatedEventArgs(sensorData.values)); }; int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero); if (error != (int)SensorError.None) { Log.Error(Globals.LogTag, "Error setting event callback for pedometer sensor"); throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for pedometer"); } } internal override void EventListenStop() { int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle); if (error != (int)SensorError.None) { Log.Error(Globals.LogTag, "Error unsetting event callback for pedometer sensor"); throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for pedometer"); } } } }