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 GyroscopeRotationVectorSensor class is used for registering callbacks for the gyroscope rotation vector sensor and getting the gyroscope rotation vector data.
24 /// <since_tizen> 3 </since_tizen>
25 public sealed class GyroscopeRotationVectorSensor : Sensor
27 private const string GyroscopeRVKey = "http://tizen.org/feature/sensor.gyroscope_rotation_vector";
30 /// Gets the X component of the gyroscope rotation vector.
32 /// <since_tizen> 3 </since_tizen>
33 /// <value> X </value>
34 public float X { get; private set; } = float.MinValue;
37 /// Gets the Y component of the gyroscope rotation vector.
39 /// <since_tizen> 3 </since_tizen>
40 /// <value> Y </value>
41 public float Y { get; private set; } = float.MinValue;
44 /// Gets the Z component of the gyroscope rotation vector.
46 /// <since_tizen> 3 </since_tizen>
47 /// <value> Z </value>
48 public float Z { get; private set; } = float.MinValue;
51 /// Gets the W component of the gyroscope rotation vector.
53 /// <since_tizen> 3 </since_tizen>
54 /// <value> W </value>
55 public float W { get; private set; } = float.MinValue;
58 /// Gets the accuracy of the gyroscope rotation vector data.
60 /// <since_tizen> 3 </since_tizen>
61 /// <value> Accuracy </value>
62 public SensorDataAccuracy Accuracy { get; private set; }
65 /// Returns true or false based on whether the gyroscope rotation vector sensor is supported by the device.
67 /// <since_tizen> 3 </since_tizen>
68 /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
69 public static bool IsSupported
73 Log.Info(Globals.LogTag, "Checking if the GyroscopeRotationVectorSensor is supported");
74 return CheckIfSupported(SensorType.GyroscopeRotationVectorSensor, GyroscopeRVKey);
79 /// Returns the number of the gyroscope rotation vector sensors available on the device.
81 /// <since_tizen> 3 </since_tizen>
82 /// <value> The count of accelerometer rotation vector sensors. </value>
83 public static int Count
87 Log.Info(Globals.LogTag, "Getting the count of gyroscope rotation vector sensors");
93 /// Initializes a new instance of the <see cref="Tizen.Sensor.GyroscopeRotationVectorSensor"/> class.
95 /// <since_tizen> 3 </since_tizen>
96 /// <feature>http://tizen.org/feature/sensor.gyroscope_rotation_vector</feature>
97 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
98 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
99 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
100 /// <param name='index'>
101 /// Index. Default value for this is 0. Index refers to a particular gyroscope rotation vector sensor in case of multiple sensors.
103 public GyroscopeRotationVectorSensor(uint index = 0) : base(index)
105 Log.Info(Globals.LogTag, "Creating GyroscopeRotationVectorSensor object");
108 internal override SensorType GetSensorType()
110 return SensorType.GyroscopeRotationVectorSensor;
114 /// An event handler for storing the callback functions for the event corresponding to the change in the gyroscope rotation vector sensor data.
116 /// <since_tizen> 3 </since_tizen>
118 public event EventHandler<GyroscopeRotationVectorSensorDataUpdatedEventArgs> DataUpdated;
120 private static int GetCount()
124 int error = Interop.SensorManager.GetSensorList(SensorType.GyroscopeRotationVectorSensor, out list, out count);
125 if (error != (int)SensorError.None)
127 Log.Error(Globals.LogTag, "Error getting sensor list for gyroscope rotation vector");
131 Interop.Libc.Free(list);
136 /// Read gyroscope rotation vector sensor data synchronously.
138 internal override void ReadData()
140 Interop.SensorEventStruct sensorData;
141 int error = Interop.SensorListener.ReadData(ListenerHandle, out sensorData);
142 if (error != (int)SensorError.None)
144 Log.Error(Globals.LogTag, "Error reading gyroscope rotation vector sensor data");
145 throw SensorErrorFactory.CheckAndThrowException(error, "Reading gyroscope rotation vector sensor data failed");
148 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
149 X = sensorData.values[0];
150 Y = sensorData.values[1];
151 Z = sensorData.values[2];
152 W = sensorData.values[3];
153 Accuracy = sensorData.accuracy;
156 private static Interop.SensorListener.SensorEventsCallback _callback;
158 internal override void EventListenStart()
160 _callback = (IntPtr sensorHandle, IntPtr eventPtr, uint events_count, IntPtr data) => {
161 updateBatchEvents(eventPtr, events_count);
162 Interop.SensorEventStruct sensorData = latestEvent();
164 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
165 X = sensorData.values[0];
166 Y = sensorData.values[1];
167 Z = sensorData.values[2];
168 W = sensorData.values[3];
169 Accuracy = sensorData.accuracy;
171 DataUpdated?.Invoke(this, new GyroscopeRotationVectorSensorDataUpdatedEventArgs(sensorData.values, sensorData.accuracy));
174 int error = Interop.SensorListener.SetEventsCallback(ListenerHandle, _callback, IntPtr.Zero);
175 if (error != (int)SensorError.None)
177 Log.Error(Globals.LogTag, "Error setting event callback for gyroscope rotation vector sensor");
178 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for gyroscope rotation vector");
182 internal override void EventListenStop()
184 int error = Interop.SensorListener.UnsetEventsCallback(ListenerHandle);
185 if (error != (int)SensorError.None)
187 Log.Error(Globals.LogTag, "Error unsetting event callback for gyroscope rotation vector sensor");
188 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for gyroscope rotation vector");