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 /// GyroscopeRotationVectorSensor Class. Used for registering callbacks for gyroscope rotation vector sensor and getting gyroscope rotation vector data
24 public sealed class GyroscopeRotationVectorSensor : Sensor
26 private const string GyroscopeRVKey = "http://tizen.org/feature/sensor.gyroscope_rotation_vector";
29 /// Gets the X component of the gyroscope rotation vector.
31 /// <since_tizen> 3 </since_tizen>
32 /// <value> X </value>
33 public float X { get; private set; } = float.MinValue;
36 /// Gets the Y component of the gyroscope rotation vector.
38 /// <since_tizen> 3 </since_tizen>
39 /// <value> Y </value>
40 public float Y { get; private set; } = float.MinValue;
43 /// Gets the Z component of the gyroscope rotation vector.
45 /// <since_tizen> 3 </since_tizen>
46 /// <value> Z </value>
47 public float Z { get; private set; } = float.MinValue;
50 /// Gets the W component of the gyroscope rotation vector.
52 /// <since_tizen> 3 </since_tizen>
53 /// <value> W </value>
54 public float W { get; private set; } = float.MinValue;
57 /// Gets the Accuracy of the gyroscope rotation vector data.
59 /// <since_tizen> 3 </since_tizen>
60 /// <value> Accuracy </value>
61 public SensorDataAccuracy Accuracy { get; private set; }
64 /// Returns true or false based on whether gyroscope rotation vector sensor is supported by device.
66 /// <since_tizen> 3 </since_tizen>
67 /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
68 public static bool IsSupported
72 Log.Info(Globals.LogTag, "Checking if the GyroscopeRotationVectorSensor is supported");
73 return CheckIfSupported(SensorType.GyroscopeRotationVectorSensor, GyroscopeRVKey);
78 /// Returns the number of gyroscope rotation vector sensors available on the device.
80 /// <since_tizen> 3 </since_tizen>
81 /// <value> The count of accelerometer rotation vector sensors </value>
82 public static int Count
86 Log.Info(Globals.LogTag, "Getting the count of gyroscope rotation vector sensors");
92 /// Initializes a new instance of the <see cref="Tizen.Sensor.GyroscopeRotationVectorSensor"/> class.
94 /// <since_tizen> 3 </since_tizen>
95 /// <feature>http://tizen.org/feature/sensor.gyroscope_rotation_vector</feature>
96 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
97 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
98 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
99 /// <param name='index'>
100 /// Index. Default value for this is 0. Index refers to a particular gyroscope rotation vector sensor in case of multiple sensors
102 public GyroscopeRotationVectorSensor(uint index = 0) : base(index)
104 Log.Info(Globals.LogTag, "Creating GyroscopeRotationVectorSensor object");
107 internal override SensorType GetSensorType()
109 return SensorType.GyroscopeRotationVectorSensor;
113 /// Event Handler for storing the callback functions for event corresponding to change in gyroscope rotation vector sensor data.
115 /// <since_tizen> 3 </since_tizen>
117 public event EventHandler<GyroscopeRotationVectorSensorDataUpdatedEventArgs> DataUpdated;
119 private static int GetCount()
123 int error = Interop.SensorManager.GetSensorList(SensorType.GyroscopeRotationVectorSensor, out list, out count);
124 if (error != (int)SensorError.None)
126 Log.Error(Globals.LogTag, "Error getting sensor list for gyroscope rotation vector");
130 Interop.Libc.Free(list);
134 private static Interop.SensorListener.SensorEventCallback _callback;
136 internal override void EventListenStart()
138 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
139 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
141 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
142 X = sensorData.values[0];
143 Y = sensorData.values[1];
144 Z = sensorData.values[2];
145 W = sensorData.values[3];
146 Accuracy = sensorData.accuracy;
148 DataUpdated?.Invoke(this, new GyroscopeRotationVectorSensorDataUpdatedEventArgs(sensorData.values, sensorData.accuracy));
151 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
152 if (error != (int)SensorError.None)
154 Log.Error(Globals.LogTag, "Error setting event callback for gyroscope rotation vector sensor");
155 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for gyroscope rotation vector");
159 internal override void EventListenStop()
161 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
162 if (error != (int)SensorError.None)
164 Log.Error(Globals.LogTag, "Error unsetting event callback for gyroscope rotation vector sensor");
165 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for gyroscope rotation vector");