csapi-sensor: remove interval parameter of accuracy listeners
[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         public float X { get; private set; } = float.MinValue;
33
34         /// <summary>
35         /// Gets the Y component of the acceleration.
36         /// </summary>
37         public float Y { get; private set; } = float.MinValue;
38
39         /// <summary>
40         /// Gets the Z component of the acceleration.
41         /// </summary>
42         public float Z { get; private set; } = float.MinValue;
43
44         /// <summary>
45         /// Gets the BiasX component of the uncalibrated magnetometer data.
46         /// </summary>
47         public float BiasX { get; private set; } = 0;
48
49         /// <summary>
50         /// Gets the BiasY component of the uncalibrated magnetometer data.
51         /// </summary>
52         public float BiasY { get; private set; } = 0;
53
54         /// <summary>
55         /// Gets the BiasZ component of the uncalibrated magnetometer data.
56         /// </summary>
57         public float BiasZ { get; private set; } = 0;
58
59         /// <summary>
60         /// Returns true or false based on whether uncalibrated magnetometer sensor is supported by device.
61         /// </summary>
62         public static bool IsSupported
63         {
64             get
65             {
66                 Log.Info(Globals.LogTag, "Checking if the UncalibratedMagnetometer sensor is supported");
67                 return CheckIfSupported(SensorType.UncalibratedMagnetometer, UncalibratedMagnetometerKey);
68             }
69         }
70
71         /// <summary>
72         /// Returns the number of uncalibrated magnetometer sensors available on the device.
73         /// </summary>
74         public static int Count
75         {
76             get
77             {
78                 Log.Info(Globals.LogTag, "Getting the count of uncalibrated magnetometer sensors");
79                 return GetCount();
80             }
81         }
82
83         /// <summary>
84         /// Initializes a new instance of the <see cref="Tizen.Sensor.UncalibratedMagnetometer"/> class.
85         /// </summary>
86         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
87         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
88         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
89         /// <param name='index'>
90         /// Index. Default value for this is 0. Index refers to a particular uncalibrated magnetometer sensor in case of multiple sensors
91         /// </param>
92         public UncalibratedMagnetometer(uint index = 0) : base(index)
93         {
94             Log.Info(Globals.LogTag, "Creating UncalibratedMagnetometer object");
95         }
96
97         internal override SensorType GetSensorType()
98         {
99             return SensorType.UncalibratedMagnetometer;
100         }
101
102         /// <summary>
103         /// Event Handler for storing the callback functions for event corresponding to change in uncalibrated magnetometer sensor data.
104         /// </summary>
105
106         public event EventHandler<UncalibratedMagnetometerDataUpdatedEventArgs> DataUpdated;
107
108         /// <summary>
109         /// Event handler for accuracy changed events.
110         /// </summary>
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.UncalibratedMagnetometer, out list, out count);
136             if (error != (int)SensorError.None)
137             {
138                 Log.Error(Globals.LogTag, "Error getting sensor list for uncalibrated magnetometer");
139                 count = 0;
140             }
141             else
142                 Interop.Libc.Free(list);
143             return count;
144         }
145
146         protected internal override void EventListenStart()
147         {
148             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
149             if (error != (int)SensorError.None)
150             {
151                 Log.Error(Globals.LogTag, "Error setting event callback for uncalibrated magnetometer sensor");
152                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for uncalibrated magnetometer");
153             }
154         }
155
156         protected internal override void EventListenStop()
157         {
158             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
159             if (error != (int)SensorError.None)
160             {
161                 Log.Error(Globals.LogTag, "Error unsetting event callback for uncalibrated magnetometer sensor");
162                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for uncalibrated magnetometer");
163             }
164         }
165
166         private void AccuracyListenStart()
167         {
168             int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, AccuracyEventCallback, IntPtr.Zero);
169             if (error != (int)SensorError.None)
170             {
171                 Log.Error(Globals.LogTag, "Error setting accuracy event callback for uncalibrated magnetometer");
172                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for uncalibrated magnetometer");
173             }
174         }
175
176         private void AccuracyListenStop()
177         {
178             int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
179             if (error != (int)SensorError.None)
180             {
181                 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for uncalibrated magnetometer");
182                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for uncalibrated magnetometer");
183             }
184         }
185
186         private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
187         {
188             Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
189             TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
190             X = sensorData.values[0];
191             Y = sensorData.values[1];
192             Z = sensorData.values[2];
193             BiasX = sensorData.values[3];
194             BiasY = sensorData.values[4];
195             BiasZ = sensorData.values[5];
196
197             DataUpdated?.Invoke(this, new UncalibratedMagnetometerDataUpdatedEventArgs(sensorData.values));
198         }
199
200         private void AccuracyEventCallback(IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data)
201         {
202             TimeSpan = new TimeSpan((Int64)timestamp);
203             _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
204         }
205     }
206 }