6a2d68e82ffb7dd66a7c8f679785479a8ab111ec
[platform/core/csapi/sensor.git] / Tizen.Sensor / Tizen.Sensor / Plugins / OrientationSensor.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). Pitchou
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
8
9 using System;
10
11 namespace Tizen.Sensor
12 {
13     /// <summary>
14     /// OrientationSensor Class. Used for registering callbacks for orientation sensor and getting orientation data
15     /// /// </summary>
16     public class OrientationSensor : Sensor
17     {
18         private static string OrientationSensorKey = "http://tizen.org/feature/sensor.tiltmeter";
19
20         private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
21         /// <summary>
22         /// Gets the Azimuth component of the orientation.
23         /// </summary>
24         public float Azimuth { get; private set; }
25
26         /// <summary>
27         /// Gets the Pitch component of the orientation.
28         /// </summary>
29         public float Pitch { get; private set; }
30
31         /// <summary>
32         /// Gets the Roll component of the orientation.
33         /// </summary>
34         public float Roll { get; private set; }
35
36         /// <summary>
37         /// Returns true or false based on whether orientation sensor is supported by device.
38         /// </summary>
39         public static bool IsSupported
40         {
41             get
42             {
43                 Log.Info(Globals.LogTag, "Checking if the OrientationSensor is supported");
44                 return CheckIfSupported(SensorType.OrientationSensor, OrientationSensorKey);
45             }
46         }
47
48         /// <summary>
49         /// Returns the number of orientation sensors available on the device.
50         /// </summary>
51         public static int Count
52         {
53             get
54             {
55                 Log.Info(Globals.LogTag, "Getting the count of orientation sensors");
56                 return GetCount();
57             }
58         }
59
60         /// <summary>
61         /// Initializes a new instance of the <see cref="Tizen.Sensor.OrientationSensor"/> class.
62         /// </summary>
63         /// <param name='index'>
64         /// Index. Default value for this is 0. Index refers to a particular orientation sensor in case of multiple sensors
65         /// </param>
66         public OrientationSensor(int index = 0) : base(index)
67         {
68             Log.Info(Globals.LogTag, "Creating OrientationSensor object");
69         }
70
71         internal override SensorType GetSensorType()
72         {
73             return SensorType.OrientationSensor;
74         }
75
76         /// <summary>
77         /// Event Handler for storing the callback functions for event corresponding to change in orientation sensor data.
78         /// </summary>
79
80         public event EventHandler<OrientationSensorDataUpdatedEventArgs> DataUpdated;
81
82         /// <summary>
83         /// Event handler for accuracy changed events.
84         /// </summary>
85         public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
86         {
87             add
88             {
89                 if (_accuracyChanged == null)
90                 {
91                     AccuracyListenStart();
92                 }
93                 _accuracyChanged += value;
94             }
95             remove
96             {
97                 _accuracyChanged -= value;
98                 if (_accuracyChanged == null)
99                 {
100                     AccuracyListenStop();
101                 }
102             }
103         }
104
105         private static int GetCount()
106         {
107             IntPtr list;
108             int count;
109             int error = Interop.SensorManager.GetSensorList(SensorType.OrientationSensor, out list, out count);
110             if (error != (int)SensorError.None)
111             {
112                 Log.Error(Globals.LogTag, "Error getting sensor list for orientation");
113                 count = 0;
114             }
115             else
116                 Interop.Libc.Free(list);
117             return count;
118         }
119
120         protected override void EventListenStart()
121         {
122             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
123             if (error != (int)SensorError.None)
124             {
125                 Log.Error(Globals.LogTag, "Error setting event callback for orientation sensor");
126                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for orientation");
127             }
128         }
129
130         protected override void EventListenStop()
131         {
132             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
133             if (error != (int)SensorError.None)
134             {
135                 Log.Error(Globals.LogTag, "Error unsetting event callback for orientation sensor");
136                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for orientation");
137             }
138         }
139
140         private void AccuracyListenStart()
141         {
142             int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, Interval, AccuracyEventCallback, IntPtr.Zero);
143             if (error != (int)SensorError.None)
144             {
145                 Log.Error(Globals.LogTag, "Error setting accuracy event callback for orientation sensor");
146                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy accuracy event callback for orientation");
147             }
148         }
149
150         private void AccuracyListenStop()
151         {
152             int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
153             if (error != (int)SensorError.None)
154             {
155                 Log.Error(Globals.LogTag, "Error unsetting event callback for orientation sensor");
156                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for orientation");
157             }
158         }
159
160         private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
161         {
162             Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
163             TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
164             Azimuth = sensorData.values[0];
165             Pitch = sensorData.values[1];
166             Roll = sensorData.values[2];
167
168             DataUpdated?.Invoke(this, new OrientationSensorDataUpdatedEventArgs(sensorData.values));
169         }
170
171         private void AccuracyEventCallback(IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data)
172         {
173             TimeSpan = new TimeSpan((Int64)timestamp);
174             _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
175         }
176     }
177 }