2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 namespace Tizen.Sensor
22 /// LinearAccelerationSensor Class. Used for registering callbacks for linear acceleration sensor and getting linear acceleration data
24 public sealed class LinearAccelerationSensor : Sensor
26 private const string LinearAccelerationSensorKey = "http://tizen.org/feature/sensor.linear_acceleration";
28 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
30 /// Gets the X component of the linear acceleration.
32 public float X { get; private set; } = float.MinValue;
35 /// Gets the Y component of the linear acceleration.
37 public float Y { get; private set; } = float.MinValue;
40 /// Gets the Z component of the linear acceleration.
42 public float Z { get; private set; } = float.MinValue;
45 /// Returns true or false based on whether linear acceleration sensor is supported by device.
47 public static bool IsSupported
51 Log.Info(Globals.LogTag, "Checking if the LinearAccelerationSensor is supported");
52 return CheckIfSupported(SensorType.LinearAccelerationSensor, LinearAccelerationSensorKey);
57 /// Returns the number of linear acceleration sensors available on the device.
59 public static int Count
63 Log.Info(Globals.LogTag, "Getting the count of linear acceleration sensors");
69 /// Initializes a new instance of the <see cref="Tizen.Sensor.LinearAccelerationSensor"/> class.
71 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
72 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
73 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
74 /// <param name='index'>
75 /// Index. Default value for this is 0. Index refers to a particular linear acceleration sensor in case of multiple sensors
77 public LinearAccelerationSensor(uint index = 0) : base(index)
79 Log.Info(Globals.LogTag, "Creating LinearAccelerationSensor object");
82 internal override SensorType GetSensorType()
84 return SensorType.LinearAccelerationSensor;
88 /// Event Handler for storing the callback functions for event corresponding to change in linear acceleration sensor data.
91 public event EventHandler<LinearAccelerationSensorDataUpdatedEventArgs> DataUpdated;
94 /// Event handler for accuracy changed events.
96 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
100 if (_accuracyChanged == null)
102 AccuracyListenStart();
104 _accuracyChanged += value;
108 _accuracyChanged -= value;
109 if (_accuracyChanged == null)
111 AccuracyListenStop();
116 private static int GetCount()
120 int error = Interop.SensorManager.GetSensorList(SensorType.LinearAccelerationSensor, out list, out count);
121 if (error != (int)SensorError.None)
123 Log.Error(Globals.LogTag, "Error getting sensor list for linear acceleration sensor");
127 Interop.Libc.Free(list);
131 protected internal override void EventListenStart()
133 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
134 if (error != (int)SensorError.None)
136 Log.Error(Globals.LogTag, "Error setting event callback for linear acceleration sensor");
137 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for linear acceleration sensor");
141 protected internal override void EventListenStop()
143 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
144 if (error != (int)SensorError.None)
146 Log.Error(Globals.LogTag, "Error unsetting event callback for linear acceleration sensor");
147 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for linear acceleration");
151 private void AccuracyListenStart()
153 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, AccuracyEventCallback, IntPtr.Zero);
154 if (error != (int)SensorError.None)
156 Log.Error(Globals.LogTag, "Error setting accuracy event callback for linear acceleration sensor");
157 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for linear acceleration sensor");
161 private void AccuracyListenStop()
163 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
164 if (error != (int)SensorError.None)
166 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for linear acceleration sensor");
167 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for linear acceleration sensor");
171 private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
173 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
174 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
175 X = sensorData.values[0];
176 Y = sensorData.values[1];
177 Z = sensorData.values[2];
179 DataUpdated?.Invoke(this, new LinearAccelerationSensorDataUpdatedEventArgs(sensorData.values));
182 private void AccuracyEventCallback(IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data)
184 TimeSpan = new TimeSpan((Int64)timestamp);
185 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));