4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <sf_common.h>
23 #include <accel_sensor.h>
24 #include <sensor_plugin_loader.h>
30 #define GRAVITY 9.80665
33 #define RAW_DATA_TO_G_UNIT(X) (((float)(X))/((float)G_TO_MG))
34 #define RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(X) (GRAVITY * (RAW_DATA_TO_G_UNIT(X)))
36 #define SENSOR_NAME "ACCELEROMETER_SENSOR"
38 accel_sensor::accel_sensor()
40 , m_interval(POLL_1HZ_MS)
42 m_name = string(SENSOR_NAME);
44 vector<unsigned int> supported_events = {
45 ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME,
48 for_each(supported_events.begin(), supported_events.end(),
49 bind1st(mem_fun(&sensor_base::register_supported_event), this));
51 physical_sensor::set_poller(accel_sensor::working, this);
54 accel_sensor::~accel_sensor()
56 INFO("accel_sensor is destroyed!\n");
59 bool accel_sensor::init()
61 m_sensor_hal = sensor_plugin_loader::get_instance().get_sensor_hal(ACCELEROMETER_SENSOR);
64 ERR("cannot load sensor_hal[%s]", sensor_base::get_name());
68 sensor_properties_s properties;
70 if (m_sensor_hal->get_properties(properties) == false) {
71 ERR("sensor->get_properties() is failed!\n");
75 m_raw_data_unit = properties.resolution / GRAVITY * G_TO_MG;
77 INFO("m_raw_data_unit accel : [%f]\n", m_raw_data_unit);
79 INFO("%s is created!\n", sensor_base::get_name());
83 sensor_type_t accel_sensor::get_type(void)
85 return ACCELEROMETER_SENSOR;
88 bool accel_sensor::working(void *inst)
90 accel_sensor *sensor = (accel_sensor*)inst;
91 return sensor->process_event();
94 bool accel_sensor::process_event(void)
96 sensor_event_t base_event;
98 if (!m_sensor_hal->is_data_ready(true))
101 m_sensor_hal->get_sensor_data(base_event.data);
104 AUTOLOCK(m_client_info_mutex);
106 if (get_client_cnt(ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME)) {
107 base_event.sensor_id = get_id();
108 base_event.event_type = ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME;
109 raw_to_base(base_event.data);
116 bool accel_sensor::on_start(void)
118 if (!m_sensor_hal->enable()) {
119 ERR("m_sensor_hal start fail\n");
126 bool accel_sensor::on_stop(void)
128 if (!m_sensor_hal->disable()) {
129 ERR("m_sensor_hal stop fail\n");
136 bool accel_sensor::get_properties(sensor_properties_s &properties)
138 return m_sensor_hal->get_properties(properties);
141 int accel_sensor::get_sensor_data(unsigned int type, sensor_data_t &data)
143 if (m_sensor_hal->get_sensor_data(data) < 0) {
144 ERR("Failed to get sensor data");
148 if (type == ACCELEROMETER_BASE_DATA_SET) {
151 ERR("Does not support type: 0x%x", type);
158 bool accel_sensor::set_interval(unsigned long interval)
162 m_interval = interval;
164 INFO("Polling interval is set to %dms", interval);
166 return m_sensor_hal->set_interval(interval);
169 void accel_sensor::raw_to_base(sensor_data_t &data)
171 data.value_count = 3;
172 data.values[0] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data.values[0] * m_raw_data_unit);
173 data.values[1] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data.values[1] * m_raw_data_unit);
174 data.values[2] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data.values[2] * m_raw_data_unit);
177 extern "C" void *create(void)
182 inst = new accel_sensor();
184 ERR("accel_sensor class create fail , errno : %d , errstr : %s\n", err, strerror(err));
191 extern "C" void destroy(void *inst)
193 delete (accel_sensor*)inst;;