1 // Copyright 2016 by Samsung Electronics, Inc.,
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.
11 namespace Tizen.Sensor
14 /// OrientationSensor Class. Used for registering callbacks for orientation sensor and getting orientation data
16 public class OrientationSensor : Sensor
18 private static string OrientationSensorKey = "http://tizen.org/feature/sensor.tiltmeter";
20 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
22 /// Gets the Azimuth component of the orientation.
24 public float Azimuth { get; private set; }
27 /// Gets the Pitch component of the orientation.
29 public float Pitch { get; private set; }
32 /// Gets the Roll component of the orientation.
34 public float Roll { get; private set; }
37 /// Returns true or false based on whether orientation sensor is supported by device.
39 public static bool IsSupported
43 Log.Info(Globals.LogTag, "Checking if the OrientationSensor is supported");
44 return CheckIfSupported(SensorType.OrientationSensor, OrientationSensorKey);
49 /// Returns the number of orientation sensors available on the device.
51 public static int Count
55 Log.Info(Globals.LogTag, "Getting the count of orientation sensors");
61 /// Initializes a new instance of the <see cref="Tizen.Sensor.OrientationSensor"/> class.
63 /// <param name='index'>
64 /// Index. Default value for this is 0. Index refers to a particular orientation sensor in case of multiple sensors
66 public OrientationSensor(int index = 0) : base(index)
68 Log.Info(Globals.LogTag, "Creating OrientationSensor object");
71 internal override SensorType GetSensorType()
73 return SensorType.OrientationSensor;
77 /// Event Handler for storing the callback functions for event corresponding to change in orientation sensor data.
80 public event EventHandler<OrientationSensorDataUpdatedEventArgs> DataUpdated;
83 /// Event handler for accuracy changed events.
85 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
89 if (_accuracyChanged == null)
91 AccuracyListenStart();
93 _accuracyChanged += value;
97 _accuracyChanged -= value;
98 if (_accuracyChanged == null)
100 AccuracyListenStop();
105 private static int GetCount()
109 int error = Interop.SensorManager.GetSensorList(SensorType.OrientationSensor, out list, out count);
110 if (error != (int)SensorError.None)
112 Log.Error(Globals.LogTag, "Error getting sensor list for orientation");
116 Interop.Libc.Free(list);
120 protected override void EventListenStart()
122 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
123 if (error != (int)SensorError.None)
125 Log.Error(Globals.LogTag, "Error setting event callback for orientation sensor");
126 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for orientation");
130 protected override void EventListenStop()
132 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
133 if (error != (int)SensorError.None)
135 Log.Error(Globals.LogTag, "Error unsetting event callback for orientation sensor");
136 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for orientation");
140 private void AccuracyListenStart()
142 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, Interval, AccuracyEventCallback, IntPtr.Zero);
143 if (error != (int)SensorError.None)
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");
150 private void AccuracyListenStop()
152 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
153 if (error != (int)SensorError.None)
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");
160 private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
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];
168 DataUpdated?.Invoke(this, new OrientationSensorDataUpdatedEventArgs(sensorData.values));
171 private void AccuracyEventCallback(IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data)
173 TimeSpan = new TimeSpan((Int64)timestamp);
174 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));