Release 4.0.0-preview1-00052
[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     /// GyroscopeRotationVectorSensor Class. Used for registering callbacks for gyroscope rotation vector sensor and getting gyroscope rotation vector data
23     /// </summary>
24     public sealed class GyroscopeRotationVectorSensor : Sensor
25     {
26         private const string GyroscopeRVKey = "http://tizen.org/feature/sensor.gyroscope_rotation_vector";
27
28         /// <summary>
29         /// Gets the X component of the gyroscope rotation vector.
30         /// </summary>
31         /// <since_tizen> 3 </since_tizen>
32         /// <value> X </value>
33         public float X { get; private set; } = float.MinValue;
34
35         /// <summary>
36         /// Gets the Y component of the gyroscope rotation vector.
37         /// </summary>
38         /// <since_tizen> 3 </since_tizen>
39         /// <value> Y </value>
40         public float Y { get; private set; } = float.MinValue;
41
42         /// <summary>
43         /// Gets the Z component of the gyroscope rotation vector.
44         /// </summary>
45         /// <since_tizen> 3 </since_tizen>
46         /// <value> Z </value>
47         public float Z { get; private set; } = float.MinValue;
48
49         /// <summary>
50         /// Gets the W component of the gyroscope rotation vector.
51         /// </summary>
52         /// <since_tizen> 3 </since_tizen>
53         /// <value> W </value>
54         public float W { get; private set; } = float.MinValue;
55
56         /// <summary>
57         /// Gets the Accuracy of the gyroscope rotation vector data.
58         /// </summary>
59         /// <since_tizen> 3 </since_tizen>
60         /// <value> Accuracy </value>
61         public SensorDataAccuracy Accuracy { get; private set; }
62
63         /// <summary>
64         /// Returns true or false based on whether gyroscope rotation vector sensor is supported by device.
65         /// </summary>
66         /// <since_tizen> 3 </since_tizen>
67         /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
68         public static bool IsSupported
69         {
70             get
71             {
72                 Log.Info(Globals.LogTag, "Checking if the GyroscopeRotationVectorSensor is supported");
73                 return CheckIfSupported(SensorType.GyroscopeRotationVectorSensor, GyroscopeRVKey);
74             }
75         }
76
77         /// <summary>
78         /// Returns the number of gyroscope rotation vector sensors available on the device.
79         /// </summary>
80         /// <since_tizen> 3 </since_tizen>
81         /// <value> The count of accelerometer rotation vector sensors </value>
82         public static int Count
83         {
84             get
85             {
86                 Log.Info(Globals.LogTag, "Getting the count of gyroscope rotation vector sensors");
87                 return GetCount();
88             }
89         }
90
91         /// <summary>
92         /// Initializes a new instance of the <see cref="Tizen.Sensor.GyroscopeRotationVectorSensor"/> class.
93         /// </summary>
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
101         /// </param>
102         public GyroscopeRotationVectorSensor(uint index = 0) : base(index)
103         {
104             Log.Info(Globals.LogTag, "Creating GyroscopeRotationVectorSensor object");
105         }
106
107         internal override SensorType GetSensorType()
108         {
109             return SensorType.GyroscopeRotationVectorSensor;
110         }
111
112         /// <summary>
113         /// Event Handler for storing the callback functions for event corresponding to change in gyroscope rotation vector sensor data.
114         /// </summary>
115         /// <since_tizen> 3 </since_tizen>
116
117         public event EventHandler<GyroscopeRotationVectorSensorDataUpdatedEventArgs> DataUpdated;
118
119         private static int GetCount()
120         {
121             IntPtr list;
122             int count;
123             int error = Interop.SensorManager.GetSensorList(SensorType.GyroscopeRotationVectorSensor, out list, out count);
124             if (error != (int)SensorError.None)
125             {
126                 Log.Error(Globals.LogTag, "Error getting sensor list for gyroscope rotation vector");
127                 count = 0;
128             }
129             else
130                 Interop.Libc.Free(list);
131             return count;
132         }
133
134         private static Interop.SensorListener.SensorEventCallback _callback;
135
136         internal override void EventListenStart()
137         {
138             _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
139                 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
140
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;
147
148                 DataUpdated?.Invoke(this, new GyroscopeRotationVectorSensorDataUpdatedEventArgs(sensorData.values, sensorData.accuracy));
149             };
150
151             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
152             if (error != (int)SensorError.None)
153             {
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");
156             }
157         }
158
159         internal override void EventListenStop()
160         {
161             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
162             if (error != (int)SensorError.None)
163             {
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");
166             }
167         }
168     }
169 }