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 public float X { get; private set; } = float.MinValue;
35 /// Gets the Y component of the gravity.
37 public float Y { get; private set; } = float.MinValue;
40 /// Gets the Z component of the gravity.
42 public float Z { get; private set; } = float.MinValue;
45 /// Returns true or false based on whether gravity sensor is supported by device.
47 public static bool IsSupported
51 Log.Info(Globals.LogTag, "Checking if the GravitySensor is supported");
52 return CheckIfSupported(SensorType.GravitySensor, GravitySensorKey);
57 /// Returns the number of gravity sensors available on the device.
59 public static int Count
63 Log.Info(Globals.LogTag, "Getting the count of gravity sensors");
69 /// Initializes a new instance of the <see cref="Tizen.Sensor.GravitySensor"/> class.
71 /// <feature>http://tizen.org/feature/sensor.gravity</feature>
72 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
73 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
74 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
75 /// <param name='index'>
76 /// Index. Default value for this is 0. Index refers to a particular gravity sensor in case of multiple sensors
78 public GravitySensor (uint index = 0) : base(index)
80 Log.Info(Globals.LogTag, "Creating GravitySensor object");
83 internal override SensorType GetSensorType()
85 return SensorType.GravitySensor;
89 /// Event Handler for storing the callback functions for event corresponding to change in gravity sensor data.
92 public event EventHandler<GravitySensorDataUpdatedEventArgs> DataUpdated;
95 /// Event handler for accuracy changed events.
97 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
101 if (_accuracyChanged == null)
103 AccuracyListenStart();
105 _accuracyChanged += value;
109 _accuracyChanged -= value;
110 if (_accuracyChanged == null)
112 AccuracyListenStop();
117 private static int GetCount()
121 int error = Interop.SensorManager.GetSensorList(SensorType.GravitySensor, out list, out count);
122 if (error != (int)SensorError.None)
124 Log.Error(Globals.LogTag, "Error getting sensor list for gravity");
128 Interop.Libc.Free(list);
132 private static Interop.SensorListener.SensorEventCallback _callback;
134 internal override void EventListenStart()
136 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
137 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
139 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
140 X = sensorData.values[0];
141 Y = sensorData.values[1];
142 Z = sensorData.values[2];
144 DataUpdated?.Invoke(this, new GravitySensorDataUpdatedEventArgs(sensorData.values));
147 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
148 if (error != (int)SensorError.None)
150 Log.Error(Globals.LogTag, "Error setting event callback for gravity sensor");
151 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for gravity");
155 internal override void EventListenStop()
157 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
158 if (error != (int)SensorError.None)
160 Log.Error(Globals.LogTag, "Error unsetting event callback for gravity sensor");
161 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for gravity");
165 private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
167 private void AccuracyListenStart()
169 _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) => {
170 TimeSpan = new TimeSpan((Int64)timestamp);
171 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
174 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
175 if (error != (int)SensorError.None)
177 Log.Error(Globals.LogTag, "Error setting accuracy event callback for gravity sensor");
178 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for gravity");
182 private void AccuracyListenStop()
184 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
185 if (error != (int)SensorError.None)
187 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for gravity sensor");
188 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for gravity");