tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Accelerometer / Sensor.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include "Sensor.h"
17 #include <sensor.h>
18 #include <dpl/log/log.h>
19 #include <Commons/Exception.h>
20
21 namespace WrtDeviceApis {
22 namespace Accelerometer {
23
24 Sensor& Sensor::getInstance()
25 {
26     static Sensor instance;
27     return instance;
28 }
29
30 double Sensor::getX() const
31 {
32     sensor_data_t data;
33     memset(&data, 0, sizeof(sensor_data_t));
34     if ((sf_get_data(m_handle, ACCELEROMETER_BASE_DATA_SET, &data) < 0) ||
35         (data.values_num < 1))
36     {
37         Throw(Commons::PlatformException);
38     }
39     return data.values[0];
40 }
41
42 double Sensor::getY() const
43 {
44     sensor_data_t data;
45     memset(&data, 0, sizeof(sensor_data_t));
46     if ((sf_get_data(m_handle, ACCELEROMETER_BASE_DATA_SET, &data) < 0) ||
47         (data.values_num < 2))
48     {
49         Throw(Commons::PlatformException);
50     }
51     return data.values[1];
52 }
53
54 double Sensor::getZ() const
55 {
56     sensor_data_t data;
57     memset(&data, 0, sizeof(sensor_data_t));
58     if ((sf_get_data(m_handle, ACCELEROMETER_BASE_DATA_SET, &data) < 0) ||
59         (data.values_num < 3))
60     {
61         Throw(Commons::PlatformException);
62     }
63     return data.values[2];
64 }
65
66 Sensor::Sensor()
67 {
68     m_handle = sf_connect(ACCELEROMETER_SENSOR);
69     if (m_handle < 0) {
70         LogError("Could not connect with accelerometer sensor.");
71     } else if (sf_start(m_handle, 0) < 0) {
72         LogError("Could not start communication with sensor.");
73     }
74 }
75
76 Sensor::~Sensor()
77 {
78     if (m_handle >= 0) {
79         sf_stop(m_handle);
80         sf_disconnect(m_handle);
81     }
82 }
83
84 } // Accelerometer
85 } // WrtDeviceApis