ae0ad4e80deff24eee4e80196c3c75b70cf436f5
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / GyroscopeRotationVectorSensor.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18
19 namespace Tizen.Sensor
20 {
21     /// <summary>
22     /// The GyroscopeRotationVectorSensor class is used for registering callbacks for the gyroscope rotation vector sensor and getting the gyroscope rotation vector data.
23     /// </summary>
24     /// <since_tizen> 3 </since_tizen>
25     public sealed class GyroscopeRotationVectorSensor : Sensor
26     {
27         private const string GyroscopeRVKey = "http://tizen.org/feature/sensor.gyroscope_rotation_vector";
28
29         /// <summary>
30         /// Gets the X component of the gyroscope rotation vector.
31         /// </summary>
32         /// <since_tizen> 3 </since_tizen>
33         /// <value> X </value>
34         public float X { get; private set; } = float.MinValue;
35
36         /// <summary>
37         /// Gets the Y component of the gyroscope rotation vector.
38         /// </summary>
39         /// <since_tizen> 3 </since_tizen>
40         /// <value> Y </value>
41         public float Y { get; private set; } = float.MinValue;
42
43         /// <summary>
44         /// Gets the Z component of the gyroscope rotation vector.
45         /// </summary>
46         /// <since_tizen> 3 </since_tizen>
47         /// <value> Z </value>
48         public float Z { get; private set; } = float.MinValue;
49
50         /// <summary>
51         /// Gets the W component of the gyroscope rotation vector.
52         /// </summary>
53         /// <since_tizen> 3 </since_tizen>
54         /// <value> W </value>
55         public float W { get; private set; } = float.MinValue;
56
57         /// <summary>
58         /// Gets the accuracy of the gyroscope rotation vector data.
59         /// </summary>
60         /// <since_tizen> 3 </since_tizen>
61         /// <value> Accuracy </value>
62         public SensorDataAccuracy Accuracy { get; private set; }
63
64         /// <summary>
65         /// Returns true or false based on whether the gyroscope rotation vector sensor is supported by the device.
66         /// </summary>
67         /// <since_tizen> 3 </since_tizen>
68         /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
69         public static bool IsSupported
70         {
71             get
72             {
73                 Log.Info(Globals.LogTag, "Checking if the GyroscopeRotationVectorSensor is supported");
74                 return CheckIfSupported(SensorType.GyroscopeRotationVectorSensor, GyroscopeRVKey);
75             }
76         }
77
78         /// <summary>
79         /// Returns the number of the gyroscope rotation vector sensors available on the device.
80         /// </summary>
81         /// <since_tizen> 3 </since_tizen>
82         /// <value> The count of accelerometer rotation vector sensors. </value>
83         public static int Count
84         {
85             get
86             {
87                 Log.Info(Globals.LogTag, "Getting the count of gyroscope rotation vector sensors");
88                 return GetCount();
89             }
90         }
91
92         /// <summary>
93         /// Initializes a new instance of the <see cref="Tizen.Sensor.GyroscopeRotationVectorSensor"/> class.
94         /// </summary>
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.
102         /// </param>
103         public GyroscopeRotationVectorSensor(uint index = 0) : base(index)
104         {
105             Log.Info(Globals.LogTag, "Creating GyroscopeRotationVectorSensor object");
106         }
107
108         internal override SensorType GetSensorType()
109         {
110             return SensorType.GyroscopeRotationVectorSensor;
111         }
112
113         /// <summary>
114         /// An event handler for storing the callback functions for the event corresponding to the change in the gyroscope rotation vector sensor data.
115         /// </summary>
116         /// <since_tizen> 3 </since_tizen>
117
118         public event EventHandler<GyroscopeRotationVectorSensorDataUpdatedEventArgs> DataUpdated;
119
120         private static int GetCount()
121         {
122             IntPtr list;
123             int count;
124             int error = Interop.SensorManager.GetSensorList(SensorType.GyroscopeRotationVectorSensor, out list, out count);
125             if (error != (int)SensorError.None)
126             {
127                 Log.Error(Globals.LogTag, "Error getting sensor list for gyroscope rotation vector");
128                 count = 0;
129             }
130             else
131                 Interop.Libc.Free(list);
132             return count;
133         }
134
135         private static Interop.SensorListener.SensorEventCallback _callback;
136
137         internal override void EventListenStart()
138         {
139             _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
140                 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
141
142                 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
143                 X = sensorData.values[0];
144                 Y = sensorData.values[1];
145                 Z = sensorData.values[2];
146                 W = sensorData.values[3];
147                 Accuracy = sensorData.accuracy;
148
149                 DataUpdated?.Invoke(this, new GyroscopeRotationVectorSensorDataUpdatedEventArgs(sensorData.values, sensorData.accuracy));
150             };
151
152             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
153             if (error != (int)SensorError.None)
154             {
155                 Log.Error(Globals.LogTag, "Error setting event callback for gyroscope rotation vector sensor");
156                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for gyroscope rotation vector");
157             }
158         }
159
160         internal override void EventListenStop()
161         {
162             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
163             if (error != (int)SensorError.None)
164             {
165                 Log.Error(Globals.LogTag, "Error unsetting event callback for gyroscope rotation vector sensor");
166                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for gyroscope rotation vector");
167             }
168         }
169     }
170 }