2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 namespace Tizen.Sensor
22 /// Pedometer Sensor Class. Used for registering callbacks for pedometer and getting pedometer data
24 public sealed class Pedometer : Sensor
26 private static string PedometerKey = "http://tizen.org/feature/sensor.pedometer";
29 /// Gets the step count
31 /// <since_tizen> 3 </since_tizen>
32 /// <value> Step count </value>
33 public uint StepCount { get; private set; } = 0;
36 /// Gets the walking step count
38 /// <since_tizen> 3 </since_tizen>
39 /// <value> Walk step count </value>
40 public uint WalkStepCount { get; private set; } = 0;
43 /// Gets the running step count
45 /// <since_tizen> 3 </since_tizen>
46 /// <value> Run step count </value>
47 public uint RunStepCount { get; private set; } = 0;
50 /// Gets the moving distance
52 /// <since_tizen> 3 </since_tizen>
53 /// <value> Moving distance </value>
54 public float MovingDistance { get; private set; } = 0;
57 /// Gets the calorie burned
59 /// <since_tizen> 3 </since_tizen>
60 /// <value> Calorie Burned </value>
61 public float CalorieBurned { get; private set; } = 0;
64 /// Gets the last speed
66 /// <since_tizen> 3 </since_tizen>
67 /// <value> Last speed </value>
68 public float LastSpeed { get; private set; } = 0;
71 /// Gets the last stepping frequency
73 /// <since_tizen> 3 </since_tizen>
74 /// <value> Last stepping frequency </value>
75 public float LastSteppingFrequency { get; private set; } = 0;
78 /// Gets the last step status
80 /// <since_tizen> 3 </since_tizen>
81 /// <value> Last step status </value>
82 public PedometerState LastStepStatus { get; private set; } = PedometerState.Unknown;
85 /// Returns true or false based on whether pedometer sensor is supported by device.
87 /// <since_tizen> 3 </since_tizen>
88 /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
89 public static bool IsSupported
93 Log.Info(Globals.LogTag, "Checking if the Pedometer sensor is supported");
94 return CheckIfSupported(SensorType.Pedometer, PedometerKey);
99 /// Returns the number of pedometer sensors available on the device.
101 /// <since_tizen> 3 </since_tizen>
102 /// <value> The count of pedometer sensors </value>
103 public static int Count
107 Log.Info(Globals.LogTag, "Getting the count of pedometer sensors");
113 /// Initializes a new instance of the <see cref="Tizen.Sensor.Pedometer"/> class.
115 /// <since_tizen> 3 </since_tizen>
116 /// <privilege>http://tizen.org/privilege/healthinfo</privilege>
117 /// <privlevel>public</privlevel>
118 /// <feature>http://tizen.org/feature/sensor.pedometer</feature>
119 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
120 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
121 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the sensor</exception>
122 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
123 /// <param name='index'>
124 /// Index. Default value for this is 0. Index refers to a particular pedometer sensor in case of multiple sensors
126 public Pedometer(uint index = 0) : base(index)
128 Log.Info(Globals.LogTag, "Creating Pedometer object");
131 internal override SensorType GetSensorType()
133 return SensorType.Pedometer;
137 /// Event Handler for storing the callback functions for event corresponding to change in pedometer sensor data.
139 /// <since_tizen> 3 </since_tizen>
141 public event EventHandler<PedometerDataUpdatedEventArgs> DataUpdated;
143 private static int GetCount()
147 int error = Interop.SensorManager.GetSensorList(SensorType.Pedometer, out list, out count);
148 if (error != (int)SensorError.None)
150 Log.Error(Globals.LogTag, "Error getting sensor list for pedometer");
154 Interop.Libc.Free(list);
158 private static Interop.SensorListener.SensorEventCallback _callback;
160 internal override void EventListenStart()
162 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
163 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
165 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
166 StepCount = (uint)sensorData.values[0];
167 WalkStepCount = (uint)sensorData.values[1];
168 RunStepCount = (uint)sensorData.values[2];
169 MovingDistance = sensorData.values[3];
170 CalorieBurned = sensorData.values[4];
171 LastSpeed = sensorData.values[5];
172 LastSteppingFrequency = sensorData.values[6];
173 LastStepStatus = (PedometerState)sensorData.values[7];
175 DataUpdated?.Invoke(this, new PedometerDataUpdatedEventArgs(sensorData.values));
178 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
179 if (error != (int)SensorError.None)
181 Log.Error(Globals.LogTag, "Error setting event callback for pedometer sensor");
182 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for pedometer");
186 internal override void EventListenStop()
188 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
189 if (error != (int)SensorError.None)
191 Log.Error(Globals.LogTag, "Error unsetting event callback for pedometer sensor");
192 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for pedometer");