[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / UncalibratedGyroscope.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 UncalibratedGyroscope sensor class is used for registering callbacks for the uncalibrated gyroscope and getting the uncalibrated gyroscope data.
23     /// </summary>
24     /// <since_tizen> 3 </since_tizen>
25     public sealed class UncalibratedGyroscope : Sensor
26     {
27         private static string UncalibratedGyroscopeKey = "http://tizen.org/feature/sensor.gyroscope.uncalibrated";
28
29         /// <summary>
30         /// Gets the X component of the acceleration.
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 acceleration.
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 acceleration.
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 BiasX component of the uncalibrated gyroscope data.
52         /// </summary>
53         /// <since_tizen> 3 </since_tizen>
54         /// <value> The X bias. </value>
55         public float BiasX { get; private set; } = 0;
56
57         /// <summary>
58         /// Gets the BiasY component of the uncalibrated gyroscope data.
59         /// </summary>
60         /// <since_tizen> 3 </since_tizen>
61         /// <value> The Y bias. </value>
62         public float BiasY { get; private set; } = 0;
63
64         /// <summary>
65         /// Gets the BiasZ component of the uncalibrated gyroscope data.
66         /// </summary>
67         /// <since_tizen> 3 </since_tizen>
68         /// <value> The Z bias. </value>
69         public float BiasZ { get; private set; } = 0;
70
71         /// <summary>
72         /// Returns true or false based on whether the uncalibrated gyroscope sensor is supported by the device.
73         /// </summary>
74         /// <since_tizen> 3 </since_tizen>
75         /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
76         public static bool IsSupported
77         {
78             get
79             {
80                 Log.Info(Globals.LogTag, "Checking if the UncalibratedGyroscope sensor is supported");
81                 return CheckIfSupported(SensorType.UncalibratedGyroscope, UncalibratedGyroscopeKey);
82             }
83         }
84
85         /// <summary>
86         /// Returns the number of the uncalibrated gyroscope sensors available on the device.
87         /// </summary>
88         /// <since_tizen> 3 </since_tizen>
89         /// <value> The count of the uncalibrated gyroscope sensors. </value>
90         public static int Count
91         {
92             get
93             {
94                 Log.Info(Globals.LogTag, "Getting the count of uncalibrated gyroscope sensors");
95                 return GetCount();
96             }
97         }
98
99         /// <summary>
100         /// Initializes a new instance of the <see cref="Tizen.Sensor.UncalibratedGyroscope"/> class.
101         /// </summary>
102         /// <since_tizen> 3 </since_tizen>
103         /// <feature>http://tizen.org/feature/sensor.gyroscope.uncalibrated</feature>
104         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
105         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
106         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
107         /// <param name='index'>
108         /// Index. Default value for this is 0. Index refers to a particular uncalibrated gyroscope sensor in case of multiple sensors.
109         /// </param>
110         public UncalibratedGyroscope(uint index = 0) : base(index)
111         {
112             Log.Info(Globals.LogTag, "Creating UncalibratedGyroscope object");
113         }
114
115         internal override SensorType GetSensorType()
116         {
117             return SensorType.UncalibratedGyroscope;
118         }
119
120         /// <summary>
121         /// An event handler for storing the callback functions for the event corresponding to the change in the uncalibrated gyroscope sensor data.
122         /// </summary>
123         /// <since_tizen> 3 </since_tizen>
124
125         public event EventHandler<UncalibratedGyroscopeDataUpdatedEventArgs> DataUpdated;
126
127         private static int GetCount()
128         {
129             IntPtr list;
130             int count;
131             int error = Interop.SensorManager.GetSensorList(SensorType.UncalibratedGyroscope, out list, out count);
132             if (error != (int)SensorError.None)
133             {
134                 Log.Error(Globals.LogTag, "Error getting sensor list for uncalibrated gyroscope");
135                 count = 0;
136             }
137             else
138                 Interop.Libc.Free(list);
139             return count;
140         }
141
142         private static Interop.SensorListener.SensorEventCallback _callback;
143
144         internal override void EventListenStart()
145         {
146             _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
147                 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
148
149                 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
150                 X = sensorData.values[0];
151                 Y = sensorData.values[1];
152                 Z = sensorData.values[2];
153                 BiasX = sensorData.values[3];
154                 BiasY = sensorData.values[4];
155                 BiasZ = sensorData.values[5];
156
157                 DataUpdated?.Invoke(this, new UncalibratedGyroscopeDataUpdatedEventArgs(sensorData.values));
158             };
159
160             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
161             if (error != (int)SensorError.None)
162             {
163                 Log.Error(Globals.LogTag, "Error setting event callback for uncalibrated gyroscope sensor");
164                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for uncalibrated gyroscope");
165             }
166         }
167
168         internal override void EventListenStop()
169         {
170             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
171             if (error != (int)SensorError.None)
172             {
173                 Log.Error(Globals.LogTag, "Error unsetting event callback for uncalibrated gyroscope sensor");
174                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for uncalibrated gyroscope");
175             }
176         }
177     }
178 }