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 Accelerometer Sensor class is used for registering callbacks for the accelerometer and getting the accelerometer data.
24 /// <since_tizen> 3 </since_tizen>
25 public sealed class Accelerometer : Sensor
27 private static string AccelerometerKey = "http://tizen.org/feature/sensor.accelerometer";
29 /// Gets the X component of the acceleration.
31 /// <since_tizen> 3 </since_tizen>
32 /// <value> X </value>
33 public float X { get; private set; } = float.MinValue;
36 /// Gets the Y component of the acceleration.
38 /// <since_tizen> 3 </since_tizen>
39 /// <value> Y </value>
40 public float Y { get; private set; } = float.MinValue;
43 /// Gets the Z component of the acceleration.
45 /// <since_tizen> 3 </since_tizen>
46 /// <value> Z </value>
47 public float Z { get; private set; } = float.MinValue;
50 /// Returns true or false based on whether accelerometer sensor is supported by the device.
52 /// <since_tizen> 3 </since_tizen>
53 /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
54 public static bool IsSupported
58 Log.Info(Globals.LogTag, "Checking if the Accelerometer sensor is supported");
59 return CheckIfSupported(SensorType.Accelerometer, AccelerometerKey);
64 /// Returns the number of accelerometer sensors available on the device.
66 /// <since_tizen> 3 </since_tizen>
67 /// <value> The count of accelerometer sensors. </value>
68 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
69 public static int Count
73 Log.Info(Globals.LogTag, "Getting the count of accelerometer sensors");
79 /// Initializes a new instance of the <see cref="Tizen.Sensor.Accelerometer"/> class.
81 /// <since_tizen> 3 </since_tizen>
82 /// <feature>http://tizen.org/feature/sensor.accelerometer</feature>
83 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
84 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
85 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
86 /// <param name='index'>
87 /// Index. Default value for this is 0. Index refers to a particular accelerometer sensor in case of multiple sensors.
89 public Accelerometer(uint index = 0) : base(index)
91 Log.Info(Globals.LogTag, "Creating Accelerometer object");
94 internal override SensorType GetSensorType()
96 return SensorType.Accelerometer;
100 /// An event handler for storing the callback functions for the event corresponding to the change in the accelerometer sensor data.
102 /// <since_tizen> 3 </since_tizen>
103 public event EventHandler<AccelerometerDataUpdatedEventArgs> DataUpdated;
105 private static int GetCount()
109 int error = Interop.SensorManager.GetSensorList(SensorType.Accelerometer, out list, out count);
110 if (error != (int)SensorError.None)
112 Log.Error(Globals.LogTag, "Error getting sensor list for accelerometer");
116 Interop.Libc.Free(list);
120 private static Interop.SensorListener.SensorEventCallback _callback;
122 internal override void EventListenStart()
124 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
125 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
127 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
128 X = sensorData.values[0];
129 Y = sensorData.values[1];
130 Z = sensorData.values[2];
132 DataUpdated?.Invoke(this, new AccelerometerDataUpdatedEventArgs(sensorData.values));
135 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
136 if (error != (int)SensorError.None)
138 Log.Error(Globals.LogTag, "Error setting event callback for accelerometer sensor");
139 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for accelerometer");
143 internal override void EventListenStop()
145 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
146 if (error != (int)SensorError.None)
148 Log.Error(Globals.LogTag, "Error unsetting event callback for accelerometer sensor");
149 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for accelerometer");