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 /// The Pedometer Sensor class is used for registering callbacks for the pedometer and getting the pedometer data.
24 /// <since_tizen> 3 </since_tizen>
25 public sealed class Pedometer : Sensor
27 private static string PedometerKey = "http://tizen.org/feature/sensor.pedometer";
30 /// Gets the step count.
32 /// <since_tizen> 3 </since_tizen>
33 /// <value> The step count. </value>
34 public uint StepCount { get; private set; } = 0;
37 /// Gets the walking step count.
39 /// <since_tizen> 3 </since_tizen>
40 /// <value> The walk step count. </value>
41 public uint WalkStepCount { get; private set; } = 0;
44 /// Gets the running step count.
46 /// <since_tizen> 3 </since_tizen>
47 /// <value> The run step count. </value>
48 public uint RunStepCount { get; private set; } = 0;
51 /// Gets the moving distance.
53 /// <since_tizen> 3 </since_tizen>
54 /// <value> The moving distance. </value>
55 public float MovingDistance { get; private set; } = 0;
58 /// Gets the calorie burned.
60 /// <since_tizen> 3 </since_tizen>
61 /// <value> The calorie burned. </value>
62 public float CalorieBurned { get; private set; } = 0;
65 /// Gets the last speed.
67 /// <since_tizen> 3 </since_tizen>
68 /// <value> The last speed. </value>
69 public float LastSpeed { get; private set; } = 0;
72 /// Gets the last stepping frequency.
74 /// <since_tizen> 3 </since_tizen>
75 /// <value> The last stepping frequency. </value>
76 public float LastSteppingFrequency { get; private set; } = 0;
79 /// Gets the last step status.
81 /// <since_tizen> 3 </since_tizen>
82 /// <value> The last step status. </value>
83 public PedometerState LastStepStatus { get; private set; } = PedometerState.Unknown;
86 /// Returns true or false based on whether the pedometer sensor is supported by the device.
88 /// <since_tizen> 3 </since_tizen>
89 /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
90 public static bool IsSupported
94 Log.Info(Globals.LogTag, "Checking if the Pedometer sensor is supported");
95 return CheckIfSupported(SensorType.Pedometer, PedometerKey);
100 /// Returns the number of pedometer sensors available on the device.
102 /// <since_tizen> 3 </since_tizen>
103 /// <value> The count of pedometer sensors. </value>
104 public static int Count
108 Log.Info(Globals.LogTag, "Getting the count of pedometer sensors");
114 /// Initializes a new instance of the <see cref="Tizen.Sensor.Pedometer"/> class.
116 /// <since_tizen> 3 </since_tizen>
117 /// <privilege>http://tizen.org/privilege/healthinfo</privilege>
118 /// <privlevel>public</privlevel>
119 /// <feature>http://tizen.org/feature/sensor.pedometer</feature>
120 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
121 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
122 /// <exception cref="UnauthorizedAccessException">Thrown when the application has no privilege to use the sensor.</exception>
123 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
124 /// <param name='index'>
125 /// Index. Default value for this is 0. Index refers to a particular pedometer sensor in case of multiple sensors.
127 public Pedometer(uint index = 0) : base(index)
129 Log.Info(Globals.LogTag, "Creating Pedometer object");
132 internal override SensorType GetSensorType()
134 return SensorType.Pedometer;
138 /// An event handler for storing the callback functions for the event corresponding to the change in the pedometer sensor data.
140 /// <since_tizen> 3 </since_tizen>
142 public event EventHandler<PedometerDataUpdatedEventArgs> DataUpdated;
144 private static int GetCount()
148 int error = Interop.SensorManager.GetSensorList(SensorType.Pedometer, out list, out count);
149 if (error != (int)SensorError.None)
151 Log.Error(Globals.LogTag, "Error getting sensor list for pedometer");
155 Interop.Libc.Free(list);
160 /// Read pedometer sensor data synchronously.
162 internal override void ReadData()
164 Interop.SensorEventStruct pedoSensorData;
165 int error = Interop.SensorListener.ReadData(ListenerHandle, out pedoSensorData);
166 if (error != (int)SensorError.None)
168 Log.Error(Globals.LogTag, "Error reading pedometer sensor data");
169 throw SensorErrorFactory.CheckAndThrowException(error, "Reading pedometer sensor data failed");
172 StepCount = (uint)pedoSensorData.values[0];
173 WalkStepCount = (uint)pedoSensorData.values[1];
174 RunStepCount = (uint)pedoSensorData.values[2];
175 MovingDistance = pedoSensorData.values[3];
176 CalorieBurned = pedoSensorData.values[4];
177 LastSpeed = pedoSensorData.values[5];
178 LastSteppingFrequency = pedoSensorData.values[6];
179 LastStepStatus = (PedometerState)pedoSensorData.values[7];
182 private static Interop.SensorListener.SensorEventsCallback _callback;
184 internal override void EventListenStart()
186 _callback = (IntPtr sensorHandle, IntPtr eventPtr, uint events_count, IntPtr data) => {
187 updateBatchEvents(eventPtr, events_count);
188 Interop.SensorEventStruct sensorData = latestEvent();
190 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
191 StepCount = (uint)sensorData.values[0];
192 WalkStepCount = (uint)sensorData.values[1];
193 RunStepCount = (uint)sensorData.values[2];
194 MovingDistance = sensorData.values[3];
195 CalorieBurned = sensorData.values[4];
196 LastSpeed = sensorData.values[5];
197 LastSteppingFrequency = sensorData.values[6];
198 LastStepStatus = (PedometerState)sensorData.values[7];
200 DataUpdated?.Invoke(this, new PedometerDataUpdatedEventArgs(sensorData.values));
203 int error = Interop.SensorListener.SetEventsCallback(ListenerHandle, _callback, IntPtr.Zero);
204 if (error != (int)SensorError.None)
206 Log.Error(Globals.LogTag, "Error setting event callback for pedometer sensor");
207 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for pedometer");
211 internal override void EventListenStop()
213 int error = Interop.SensorListener.UnsetEventsCallback(ListenerHandle);
214 if (error != (int)SensorError.None)
216 Log.Error(Globals.LogTag, "Error unsetting event callback for pedometer sensor");
217 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for pedometer");