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);
147 /// Read gravity sensor data synchronously.
149 internal override void ReadData()
151 Interop.SensorEventStruct sensorData;
152 int error = Interop.SensorListener.ReadData(ListenerHandle, out sensorData);
153 if (error != (int)SensorError.None)
155 Log.Error(Globals.LogTag, "Error reading gravity sensor data");
156 throw SensorErrorFactory.CheckAndThrowException(error, "Reading gravity sensor data failed");
159 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
160 X = sensorData.values[0];
161 Y = sensorData.values[1];
162 Z = sensorData.values[2];
165 private static Interop.SensorListener.SensorEventCallback _callback;
167 internal override void EventListenStart()
169 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
170 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
172 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
173 X = sensorData.values[0];
174 Y = sensorData.values[1];
175 Z = sensorData.values[2];
177 DataUpdated?.Invoke(this, new GravitySensorDataUpdatedEventArgs(sensorData.values));
180 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
181 if (error != (int)SensorError.None)
183 Log.Error(Globals.LogTag, "Error setting event callback for gravity sensor");
184 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for gravity");
188 internal override void EventListenStop()
190 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
191 if (error != (int)SensorError.None)
193 Log.Error(Globals.LogTag, "Error unsetting event callback for gravity sensor");
194 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for gravity");
198 private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
200 private void AccuracyListenStart()
202 _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) => {
203 TimeSpan = new TimeSpan((Int64)timestamp);
204 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
207 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
208 if (error != (int)SensorError.None)
210 Log.Error(Globals.LogTag, "Error setting accuracy event callback for gravity sensor");
211 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for gravity");
215 private void AccuracyListenStop()
217 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
218 if (error != (int)SensorError.None)
220 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for gravity sensor");
221 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for gravity");