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 GravitySensor class is used for registering callbacks for the gravity sensor and getting the gravity data.
24 /// <since_tizen> 3 </since_tizen>
25 public sealed class GravitySensor : Sensor
27 private const string GravitySensorKey = "http://tizen.org/feature/sensor.gravity";
29 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
31 /// Gets the X component of the gravity.
33 /// <since_tizen> 3 </since_tizen>
34 /// <value> X </value>
35 public float X { get; private set; } = float.MinValue;
38 /// Gets the Y component of the gravity.
40 /// <since_tizen> 3 </since_tizen>
41 /// <value> Y </value>
42 public float Y { get; private set; } = float.MinValue;
45 /// Gets the Z component of the gravity.
47 /// <since_tizen> 3 </since_tizen>
48 /// <value> Z </value>
49 public float Z { get; private set; } = float.MinValue;
52 /// Returns true or false based on whether the gravity sensor is supported by the device.
54 /// <since_tizen> 3 </since_tizen>
55 /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
56 public static bool IsSupported
60 Log.Info(Globals.LogTag, "Checking if the GravitySensor is supported");
61 return CheckIfSupported(SensorType.GravitySensor, GravitySensorKey);
66 /// Returns the number of gravity sensors available on the device.
68 /// <since_tizen> 3 </since_tizen>
69 /// <value> The count of gravity sensors. </value>
70 public static int Count
74 Log.Info(Globals.LogTag, "Getting the count of gravity sensors");
80 /// Initializes a new instance of the <see cref="Tizen.Sensor.GravitySensor"/> class.
82 /// <since_tizen> 3 </since_tizen>
83 /// <feature>http://tizen.org/feature/sensor.gravity</feature>
84 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
85 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
86 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
87 /// <param name='index'>
88 /// Index. Default value for this is 0. Index refers to a particular gravity sensor in case of multiple sensors.
90 public GravitySensor (uint index = 0) : base(index)
92 Log.Info(Globals.LogTag, "Creating GravitySensor object");
95 internal override SensorType GetSensorType()
97 return SensorType.GravitySensor;
101 /// An event handler for storing the callback functions for the event corresponding to the change in the gravity sensor data.
103 /// <since_tizen> 3 </since_tizen>
105 public event EventHandler<GravitySensorDataUpdatedEventArgs> DataUpdated;
108 /// An event handler for accuracy changed events.
110 /// <since_tizen> 3 </since_tizen>
111 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
115 if (_accuracyChanged == null)
117 AccuracyListenStart();
119 _accuracyChanged += value;
123 _accuracyChanged -= value;
124 if (_accuracyChanged == null)
126 AccuracyListenStop();
131 private static int GetCount()
135 int error = Interop.SensorManager.GetSensorList(SensorType.GravitySensor, out list, out count);
136 if (error != (int)SensorError.None)
138 Log.Error(Globals.LogTag, "Error getting sensor list for gravity");
142 Interop.Libc.Free(list);
146 private static Interop.SensorListener.SensorEventCallback _callback;
148 internal override void EventListenStart()
150 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
151 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
153 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
154 X = sensorData.values[0];
155 Y = sensorData.values[1];
156 Z = sensorData.values[2];
158 DataUpdated?.Invoke(this, new GravitySensorDataUpdatedEventArgs(sensorData.values));
161 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
162 if (error != (int)SensorError.None)
164 Log.Error(Globals.LogTag, "Error setting event callback for gravity sensor");
165 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for gravity");
169 internal override void EventListenStop()
171 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
172 if (error != (int)SensorError.None)
174 Log.Error(Globals.LogTag, "Error unsetting event callback for gravity sensor");
175 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for gravity");
179 private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
181 private void AccuracyListenStart()
183 _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) => {
184 TimeSpan = new TimeSpan((Int64)timestamp);
185 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
188 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
189 if (error != (int)SensorError.None)
191 Log.Error(Globals.LogTag, "Error setting accuracy event callback for gravity sensor");
192 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for gravity");
196 private void AccuracyListenStop()
198 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
199 if (error != (int)SensorError.None)
201 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for gravity sensor");
202 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for gravity");