948e8637a4297109a63e8ea80dca7d4a65a83f65
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / UncalibratedMagnetometer.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 UncalibratedMagnetometer sensor class is used for registering callbacks for the uncalibrated magnetometer and getting the uncalibrated magnetometer data.
23     /// </summary>
24     /// <since_tizen> 3 </since_tizen>
25     public sealed class UncalibratedMagnetometer : Sensor
26     {
27         private static string UncalibratedMagnetometerKey = "http://tizen.org/feature/sensor.magnetometer.uncalibrated";
28
29         private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
30         /// <summary>
31         /// Gets the X component of the acceleration.
32         /// </summary>
33         /// <since_tizen> 3 </since_tizen>
34         /// <value> X </value>
35         public float X { get; private set; } = float.MinValue;
36
37         /// <summary>
38         /// Gets the Y component of the acceleration.
39         /// </summary>
40         /// <since_tizen> 3 </since_tizen>
41         /// <value> Y </value>
42         public float Y { get; private set; } = float.MinValue;
43
44         /// <summary>
45         /// Gets the Z component of the acceleration.
46         /// </summary>
47         /// <since_tizen> 3 </since_tizen>
48         /// <value> Z </value>
49         public float Z { get; private set; } = float.MinValue;
50
51         /// <summary>
52         /// Gets the BiasX component of the uncalibrated magnetometer data.
53         /// </summary>
54         /// <since_tizen> 3 </since_tizen>
55         /// <value> The X bias. </value>
56         public float BiasX { get; private set; } = 0;
57
58         /// <summary>
59         /// Gets the BiasY component of the uncalibrated magnetometer data.
60         /// </summary>
61         /// <since_tizen> 3 </since_tizen>
62         /// <value> The Y bias. </value>
63         public float BiasY { get; private set; } = 0;
64
65         /// <summary>
66         /// Gets the BiasZ component of the uncalibrated magnetometer data.
67         /// </summary>
68         /// <since_tizen> 3 </since_tizen>
69         /// <value> The Z bias. </value>
70         public float BiasZ { get; private set; } = 0;
71
72         /// <summary>
73         /// Returns true or false based on whether the uncalibrated magnetometer sensor is supported by the device.
74         /// </summary>
75         /// <since_tizen> 3 </since_tizen>
76         /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
77         public static bool IsSupported
78         {
79             get
80             {
81                 Log.Info(Globals.LogTag, "Checking if the UncalibratedMagnetometer sensor is supported");
82                 return CheckIfSupported(SensorType.UncalibratedMagnetometer, UncalibratedMagnetometerKey);
83             }
84         }
85
86         /// <summary>
87         /// Returns the number of uncalibrated magnetometer sensors available on the device.
88         /// </summary>
89         /// <since_tizen> 3 </since_tizen>
90         /// <value> The count of uncalibrated magnetometer sensors. </value>
91         public static int Count
92         {
93             get
94             {
95                 Log.Info(Globals.LogTag, "Getting the count of uncalibrated magnetometer sensors");
96                 return GetCount();
97             }
98         }
99
100         /// <summary>
101         /// Initializes a new instance of the <see cref="Tizen.Sensor.UncalibratedMagnetometer"/> class.
102         /// </summary>
103         /// <since_tizen> 3 </since_tizen>
104         /// <feature>http://tizen.org/feature/sensor.magnetometer.uncalibrated</feature>
105         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
106         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
107         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
108         /// <param name='index'>
109         /// Index. Default value for this is 0. Index refers to a particular uncalibrated magnetometer sensor in case of multiple sensors.
110         /// </param>
111         public UncalibratedMagnetometer(uint index = 0) : base(index)
112         {
113             Log.Info(Globals.LogTag, "Creating UncalibratedMagnetometer object");
114         }
115
116         internal override SensorType GetSensorType()
117         {
118             return SensorType.UncalibratedMagnetometer;
119         }
120
121         /// <summary>
122         /// An event handler for storing the callback functions for the event corresponding to the change in the uncalibrated magnetometer sensor data.
123         /// </summary>
124         /// <since_tizen> 3 </since_tizen>
125
126         public event EventHandler<UncalibratedMagnetometerDataUpdatedEventArgs> DataUpdated;
127
128         /// <summary>
129         /// An event handler for accuracy changed events.
130         /// </summary>
131         /// <since_tizen> 3 </since_tizen>
132         public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
133         {
134             add
135             {
136                 if (_accuracyChanged == null)
137                 {
138                     AccuracyListenStart();
139                 }
140                 _accuracyChanged += value;
141             }
142             remove
143             {
144                 _accuracyChanged -= value;
145                 if (_accuracyChanged == null)
146                 {
147                     AccuracyListenStop();
148                 }
149             }
150         }
151
152         private static int GetCount()
153         {
154             IntPtr list;
155             int count;
156             int error = Interop.SensorManager.GetSensorList(SensorType.UncalibratedMagnetometer, out list, out count);
157             if (error != (int)SensorError.None)
158             {
159                 Log.Error(Globals.LogTag, "Error getting sensor list for uncalibrated magnetometer");
160                 count = 0;
161             }
162             else
163                 Interop.Libc.Free(list);
164             return count;
165         }
166
167         private static Interop.SensorListener.SensorEventCallback _callback;
168
169         internal override void EventListenStart()
170         {
171             _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
172                 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
173
174                 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
175                 X = sensorData.values[0];
176                 Y = sensorData.values[1];
177                 Z = sensorData.values[2];
178                 BiasX = sensorData.values[3];
179                 BiasY = sensorData.values[4];
180                 BiasZ = sensorData.values[5];
181
182                 DataUpdated?.Invoke(this, new UncalibratedMagnetometerDataUpdatedEventArgs(sensorData.values));
183             };
184
185             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
186             if (error != (int)SensorError.None)
187             {
188                 Log.Error(Globals.LogTag, "Error setting event callback for uncalibrated magnetometer sensor");
189                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for uncalibrated magnetometer");
190             }
191         }
192
193         internal override void EventListenStop()
194         {
195             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
196             if (error != (int)SensorError.None)
197             {
198                 Log.Error(Globals.LogTag, "Error unsetting event callback for uncalibrated magnetometer sensor");
199                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for uncalibrated magnetometer");
200             }
201         }
202
203         private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
204
205         private void AccuracyListenStart()
206         {
207             _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) => {
208                 TimeSpan = new TimeSpan((Int64)timestamp);
209                 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
210             };
211
212             int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
213             if (error != (int)SensorError.None)
214             {
215                 Log.Error(Globals.LogTag, "Error setting accuracy event callback for uncalibrated magnetometer");
216                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for uncalibrated magnetometer");
217             }
218         }
219
220         private void AccuracyListenStop()
221         {
222             int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
223             if (error != (int)SensorError.None)
224             {
225                 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for uncalibrated magnetometer");
226                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for uncalibrated magnetometer");
227             }
228         }
229     }
230 }