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