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