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