5cce25199c01dec2225607ed9b538c4c2010cd37
[platform/core/csapi/sensor.git] / Tizen.Sensor / Tizen.Sensor / Plugins / Accelerometer.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"). 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.
8
9 using System;
10
11 namespace Tizen.Sensor
12 {
13     /// <summary>
14     /// Accelerometer Sensor Class. Used for registering callbacks for accelerometer and getting accelerometer data
15     /// </summary>
16     public class Accelerometer : Sensor
17     {
18         private static string AccelerometerKey = "http://tizen.org/feature/sensor.accelerometer";
19         /// <summary>
20         /// Gets the X component of the acceleration.
21         /// </summary>
22         public float X { get; private set; }
23
24         /// <summary>
25         /// Gets the Y component of the acceleration.
26         /// </summary>
27         public float Y { get; private set; }
28
29         /// <summary>
30         /// Gets the Z component of the acceleration.
31         /// </summary>
32         public float Z { get; private set; }
33
34         /// <summary>
35         /// Returns true or false based on whether accelerometer sensor is supported by device.
36         /// </summary>
37         public static bool IsSupported
38         {
39             get
40             {
41                 Log.Info(Globals.LogTag, "Checking if the Accelerometer sensor is supported");
42                 return CheckIfSupported(SensorType.Accelerometer, AccelerometerKey);
43             }
44         }
45
46         /// <summary>
47         /// Returns the number of accelerometer sensors available on the device.
48         /// </summary>
49         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
50         public static int Count
51         {
52             get
53             {
54                 Log.Info(Globals.LogTag, "Getting the count of accelerometer sensors");
55                 return GetCount();
56             }
57         }
58
59         /// <summary>
60         /// Initializes a new instance of the <see cref="Tizen.Sensor.Accelerometer"/> class.
61         /// </summary>
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
67         /// </param>
68         public Accelerometer(int index = 0) : base(index)
69         {
70             Log.Info(Globals.LogTag, "Creating Accelerometer object");
71         }
72
73         internal override SensorType GetSensorType()
74         {
75             return SensorType.Accelerometer;
76         }
77
78         /// <summary>
79         /// Event Handler for storing the callback functions for event corresponding to change in accelerometer sensor data.
80         /// </summary>
81         public event EventHandler<AccelerometerDataUpdatedEventArgs> DataUpdated;
82
83         private static int GetCount()
84         {
85             IntPtr list;
86             int count;
87             int error = Interop.SensorManager.GetSensorList(SensorType.Accelerometer, out list, out count);
88             if (error != (int)SensorError.None)
89             {
90                 Log.Error(Globals.LogTag, "Error getting sensor list for accelerometer");
91                 count = 0;
92             }
93             else
94                 Interop.Libc.Free(list);
95             return count;
96         }
97
98         protected override void EventListenStart()
99         {
100             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
101             if (error != (int)SensorError.None)
102             {
103                 Log.Error(Globals.LogTag, "Error setting event callback for accelerometer sensor");
104                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for accelerometer");
105             }
106         }
107
108         protected override void EventListenStop()
109         {
110             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
111             if (error != (int)SensorError.None)
112             {
113                 Log.Error(Globals.LogTag, "Error unsetting event callback for accelerometer sensor");
114                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for accelerometer");
115             }
116         }
117
118         private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
119         {
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];
125
126             DataUpdated?.Invoke(this, new AccelerometerDataUpdatedEventArgs(sensorData.values));
127         }
128     }
129 }