1 // Copyright 2016 by Samsung Electronics, Inc.,
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
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 /// Accelerometer Sensor Class. Used for registering callbacks for accelerometer and getting accelerometer data
16 public class Accelerometer : Sensor
18 private static string AccelerometerKey = "http://tizen.org/feature/sensor.accelerometer";
20 /// Gets the X component of the acceleration.
22 public float X { get; private set; }
25 /// Gets the Y component of the acceleration.
27 public float Y { get; private set; }
30 /// Gets the Z component of the acceleration.
32 public float Z { get; private set; }
35 /// Returns true or false based on whether accelerometer sensor is supported by device.
37 public static bool IsSupported
41 Log.Info(Globals.LogTag, "Checking if the Accelerometer sensor is supported");
42 return CheckIfSupported(SensorType.Accelerometer, AccelerometerKey);
47 /// Returns the number of accelerometer sensors available on the device.
49 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
50 public static int Count
54 Log.Info(Globals.LogTag, "Getting the count of accelerometer sensors");
60 /// Initializes a new instance of the <see cref="Tizen.Sensor.Accelerometer"/> class.
62 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
63 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
64 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
65 /// <param name='index'>
66 /// Index. Default value for this is 0. Index refers to a particular accelerometer sensor in case of multiple sensors
68 public Accelerometer(int index = 0) : base(index)
70 Log.Info(Globals.LogTag, "Creating Accelerometer object");
73 internal override SensorType GetSensorType()
75 return SensorType.Accelerometer;
79 /// Event Handler for storing the callback functions for event corresponding to change in accelerometer sensor data.
81 public event EventHandler<AccelerometerDataUpdatedEventArgs> DataUpdated;
83 private static int GetCount()
87 int error = Interop.SensorManager.GetSensorList(SensorType.Accelerometer, out list, out count);
88 if (error != (int)SensorError.None)
90 Log.Error(Globals.LogTag, "Error getting sensor list for accelerometer");
94 Interop.Libc.Free(list);
98 protected override void EventListenStart()
100 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
101 if (error != (int)SensorError.None)
103 Log.Error(Globals.LogTag, "Error setting event callback for accelerometer sensor");
104 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for accelerometer");
108 protected override void EventListenStop()
110 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
111 if (error != (int)SensorError.None)
113 Log.Error(Globals.LogTag, "Error unsetting event callback for accelerometer sensor");
114 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for accelerometer");
118 private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
120 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
121 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
122 X = sensorData.values[0];
123 Y = sensorData.values[1];
124 Z = sensorData.values[2];
126 DataUpdated?.Invoke(this, new AccelerometerDataUpdatedEventArgs(sensorData.values));