[Sensor] Add new batched type sensor (#1522)
[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         /// <summary>
143         /// Read uncalibrated gyroscope data synchronously.
144         /// </summary>
145         internal override void ReadData()
146         {
147             Interop.SensorEventStruct sensorData;
148             int error = Interop.SensorListener.ReadData(ListenerHandle, out sensorData);
149             if (error != (int)SensorError.None)
150             {
151                 Log.Error(Globals.LogTag, "Error reading uncalibrated gyroscope data");
152                 throw SensorErrorFactory.CheckAndThrowException(error, "Reading uncalibrated gyroscope data failed");
153             }
154
155             Timestamp = sensorData.timestamp;
156             X = sensorData.values[0];
157             Y = sensorData.values[1];
158             Z = sensorData.values[2];
159             BiasX = sensorData.values[3];
160             BiasY = sensorData.values[4];
161             BiasZ = sensorData.values[5];
162         }
163
164         private static Interop.SensorListener.SensorEventsCallback _callback;
165
166         internal override void EventListenStart()
167         {
168             _callback = (IntPtr sensorHandle, IntPtr eventPtr, uint events_count, IntPtr data) => {
169                 updateBatchEvents(eventPtr, events_count);
170                 Interop.SensorEventStruct sensorData = latestEvent();
171
172                 Timestamp = sensorData.timestamp;
173                 X = sensorData.values[0];
174                 Y = sensorData.values[1];
175                 Z = sensorData.values[2];
176                 BiasX = sensorData.values[3];
177                 BiasY = sensorData.values[4];
178                 BiasZ = sensorData.values[5];
179
180                 DataUpdated?.Invoke(this, new UncalibratedGyroscopeDataUpdatedEventArgs(sensorData.values));
181             };
182
183             int error = Interop.SensorListener.SetEventsCallback(ListenerHandle, _callback, IntPtr.Zero);
184             if (error != (int)SensorError.None)
185             {
186                 Log.Error(Globals.LogTag, "Error setting event callback for uncalibrated gyroscope sensor");
187                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for uncalibrated gyroscope");
188             }
189         }
190
191         internal override void EventListenStop()
192         {
193             int error = Interop.SensorListener.UnsetEventsCallback(ListenerHandle);
194             if (error != (int)SensorError.None)
195             {
196                 Log.Error(Globals.LogTag, "Error unsetting event callback for uncalibrated gyroscope sensor");
197                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for uncalibrated gyroscope");
198             }
199         }
200     }
201 }