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