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 /// RotationVectorSensor Class. Used for registering callbacks for rotation vector sensor and getting rotation vector data
24 public sealed class RotationVectorSensor : Sensor
26 private static string RotationVectorKey = "http://tizen.org/feature/sensor.rotation_vector";
28 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
30 /// Gets the X component of the rotation vector.
32 public float X { get; private set; } = float.MinValue;
35 /// Gets the Y component of the rotation vector.
37 public float Y { get; private set; } = float.MinValue;
40 /// Gets the Z component of the rotation vector.
42 public float Z { get; private set; } = float.MinValue;
45 /// Gets the W component of the rotation vector.
47 public float W { get; private set; } = float.MinValue;
50 /// Gets the Accuracy of the rotation vector data.
52 public SensorDataAccuracy Accuracy { get; private set; } = SensorDataAccuracy.Undefined;
55 /// Returns true or false based on whether rotation vector sensor is supported by device.
57 public static bool IsSupported
61 Log.Info(Globals.LogTag, "Checking if the RotationVectorSensor is supported");
62 return CheckIfSupported(SensorType.RotationVectorSensor, RotationVectorKey);
67 /// Returns the number of rotation vector sensors available on the device.
69 public static int Count
73 Log.Info(Globals.LogTag, "Getting the count of rotation vector sensors");
79 /// Initializes a new instance of the <see cref="Tizen.Sensor.RotationVectorSensor"/> class.
81 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
82 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
83 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
84 /// <param name='index'>
85 /// Index. Default value for this is 0. Index refers to a particular rotation vector sensor in case of multiple sensors
87 public RotationVectorSensor(uint index = 0) : base(index)
89 Log.Info(Globals.LogTag, "Creating RotationVectorSensor object");
92 internal override SensorType GetSensorType()
94 return SensorType.RotationVectorSensor;
98 /// Event Handler for storing the callback functions for event corresponding to change in rotation vector sensor data.
101 public event EventHandler<RotationVectorSensorDataUpdatedEventArgs> DataUpdated;
104 /// Event handler for accuracy changed events.
106 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
110 if (_accuracyChanged == null)
112 AccuracyListenStart();
114 _accuracyChanged += value;
118 _accuracyChanged -= value;
119 if (_accuracyChanged == null)
121 AccuracyListenStop();
126 private static int GetCount()
130 int error = Interop.SensorManager.GetSensorList(SensorType.RotationVectorSensor, out list, out count);
131 if (error != (int)SensorError.None)
133 Log.Error(Globals.LogTag, "Error getting sensor list for rotation vector");
137 Interop.Libc.Free(list);
141 protected internal override void EventListenStart()
143 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
144 if (error != (int)SensorError.None)
146 Log.Error(Globals.LogTag, "Error setting event callback for rotation vector sensor");
147 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for rotation vector");
151 protected internal override void EventListenStop()
153 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
154 if (error != (int)SensorError.None)
156 Log.Error(Globals.LogTag, "Error unsetting event callback for rotation vector sensor");
157 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for rotation vector");
161 private void AccuracyListenStart()
163 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, Interval, AccuracyEventCallback, IntPtr.Zero);
164 if (error != (int)SensorError.None)
166 Log.Error(Globals.LogTag, "Error setting accuracy event callback for rotation vector sensor");
167 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for rotation vector");
171 private void AccuracyListenStop()
173 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
174 if (error != (int)SensorError.None)
176 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for rotation vector sensor");
177 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for rotation vector");
181 private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
183 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
184 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
185 X = sensorData.values[0];
186 Y = sensorData.values[1];
187 Z = sensorData.values[2];
188 Accuracy = sensorData.accuracy;
190 DataUpdated?.Invoke(this, new RotationVectorSensorDataUpdatedEventArgs(sensorData.values, sensorData.accuracy));
193 private void AccuracyEventCallback(IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data)
195 TimeSpan = new TimeSpan((Int64)timestamp);
197 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));