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 /// OrientationSensor Class. Used for registering callbacks for orientation sensor and getting orientation data
24 public sealed class OrientationSensor : Sensor
26 private static string OrientationSensorKey = "http://tizen.org/feature/sensor.tiltmeter";
28 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
30 /// Gets the Azimuth component of the orientation.
32 /// <since_tizen> 3 </since_tizen>
33 /// <value> Azimuth </value>
34 public float Azimuth { get; private set; } = float.MinValue;
37 /// Gets the Pitch component of the orientation.
39 /// <since_tizen> 3 </since_tizen>
40 /// <value> Pitch </value>
41 public float Pitch { get; private set; } = float.MinValue;
44 /// Gets the Roll component of the orientation.
46 /// <since_tizen> 3 </since_tizen>
47 /// <value> Roll </value>
48 public float Roll { get; private set; } = float.MinValue;
51 /// Returns true or false based on whether orientation 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 OrientationSensor is supported");
60 return CheckIfSupported(SensorType.OrientationSensor, OrientationSensorKey);
65 /// Returns the number of orientation sensors available on the device.
67 /// <since_tizen> 3 </since_tizen>
68 /// <value> The count of orientation sensors </value>
69 public static int Count
73 Log.Info(Globals.LogTag, "Getting the count of orientation sensors");
79 /// Initializes a new instance of the <see cref="Tizen.Sensor.OrientationSensor"/> class.
81 /// <since_tizen> 3 </since_tizen>
82 /// <feature>http://tizen.org/feature/sensor.tiltmeter</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 orientation sensor in case of multiple sensors
89 public OrientationSensor(uint index = 0) : base(index)
91 Log.Info(Globals.LogTag, "Creating OrientationSensor object");
94 internal override SensorType GetSensorType()
96 return SensorType.OrientationSensor;
100 /// Event Handler for storing the callback functions for event corresponding to change in orientation sensor data.
102 /// <since_tizen> 3 </since_tizen>
104 public event EventHandler<OrientationSensorDataUpdatedEventArgs> 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.OrientationSensor, out list, out count);
135 if (error != (int)SensorError.None)
137 Log.Error(Globals.LogTag, "Error getting sensor list for orientation");
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 Azimuth = sensorData.values[0];
154 Pitch = sensorData.values[1];
155 Roll = sensorData.values[2];
157 DataUpdated?.Invoke(this, new OrientationSensorDataUpdatedEventArgs(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 orientation sensor");
164 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for orientation");
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 orientation sensor");
174 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for orientation");
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 orientation sensor");
191 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy accuracy event callback for orientation");
195 private void AccuracyListenStop()
197 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
198 if (error != (int)SensorError.None)
200 Log.Error(Globals.LogTag, "Error unsetting event callback for orientation sensor");
201 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for orientation");