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 /// GravitySensor Class. Used for registering callbacks for gravity sensor and getting gravity data
24 public sealed class GravitySensor : Sensor
26 private const string GravitySensorKey = "http://tizen.org/feature/sensor.gravity";
28 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
30 /// Gets the X component of the gravity.
32 /// <since_tizen> 3 </since_tizen>
33 /// <value> X </value>
34 public float X { get; private set; } = float.MinValue;
37 /// Gets the Y component of the gravity.
39 /// <since_tizen> 3 </since_tizen>
40 /// <value> Y </value>
41 public float Y { get; private set; } = float.MinValue;
44 /// Gets the Z component of the gravity.
46 /// <since_tizen> 3 </since_tizen>
47 /// <value> Z </value>
48 public float Z { get; private set; } = float.MinValue;
51 /// Returns true or false based on whether gravity sensor is supported by device.
53 /// <since_tizen> 3 </since_tizen>
54 /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
55 public static bool IsSupported
59 Log.Info(Globals.LogTag, "Checking if the GravitySensor is supported");
60 return CheckIfSupported(SensorType.GravitySensor, GravitySensorKey);
65 /// Returns the number of gravity sensors available on the device.
67 /// <since_tizen> 3 </since_tizen>
68 /// <value> The count of gravity sensors </value>
69 public static int Count
73 Log.Info(Globals.LogTag, "Getting the count of gravity sensors");
79 /// Initializes a new instance of the <see cref="Tizen.Sensor.GravitySensor"/> class.
81 /// <since_tizen> 3 </since_tizen>
82 /// <feature>http://tizen.org/feature/sensor.gravity</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 gravity sensor in case of multiple sensors
89 public GravitySensor (uint index = 0) : base(index)
91 Log.Info(Globals.LogTag, "Creating GravitySensor object");
94 internal override SensorType GetSensorType()
96 return SensorType.GravitySensor;
100 /// Event Handler for storing the callback functions for event corresponding to change in gravity sensor data.
102 /// <since_tizen> 3 </since_tizen>
104 public event EventHandler<GravitySensorDataUpdatedEventArgs> DataUpdated;
107 /// Event handler for accuracy changed events.
109 /// <since_tizen> 3 </since_tizen>
110 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
114 if (_accuracyChanged == null)
116 AccuracyListenStart();
118 _accuracyChanged += value;
122 _accuracyChanged -= value;
123 if (_accuracyChanged == null)
125 AccuracyListenStop();
130 private static int GetCount()
134 int error = Interop.SensorManager.GetSensorList(SensorType.GravitySensor, out list, out count);
135 if (error != (int)SensorError.None)
137 Log.Error(Globals.LogTag, "Error getting sensor list for gravity");
141 Interop.Libc.Free(list);
145 private static Interop.SensorListener.SensorEventCallback _callback;
147 internal override void EventListenStart()
149 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
150 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
152 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
153 X = sensorData.values[0];
154 Y = sensorData.values[1];
155 Z = sensorData.values[2];
157 DataUpdated?.Invoke(this, new GravitySensorDataUpdatedEventArgs(sensorData.values));
160 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
161 if (error != (int)SensorError.None)
163 Log.Error(Globals.LogTag, "Error setting event callback for gravity sensor");
164 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for gravity");
168 internal override void EventListenStop()
170 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
171 if (error != (int)SensorError.None)
173 Log.Error(Globals.LogTag, "Error unsetting event callback for gravity sensor");
174 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for gravity");
178 private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
180 private void AccuracyListenStart()
182 _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) => {
183 TimeSpan = new TimeSpan((Int64)timestamp);
184 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
187 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
188 if (error != (int)SensorError.None)
190 Log.Error(Globals.LogTag, "Error setting accuracy event callback for gravity sensor");
191 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for gravity");
195 private void AccuracyListenStop()
197 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
198 if (error != (int)SensorError.None)
200 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for gravity sensor");
201 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for gravity");