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>
22 #include <light_sensor.h>
23 #include <sensor_plugin_loader.h>
29 #define SENSOR_NAME "LIGHT_SENSOR"
31 #define INITIAL_VALUE -1
32 const int light_sensor::m_light_level[] = {0, 1, 165, 288, 497, 869, 1532, 2692, 4692, 8280, 21428, 65535, 137852};
34 light_sensor::light_sensor()
36 , m_level(INITIAL_VALUE)
38 m_name = string(SENSOR_NAME);
40 vector<unsigned int> supported_events = {
41 LIGHT_EVENT_CHANGE_LEVEL,
42 LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME,
43 LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME,
46 for_each(supported_events.begin(), supported_events.end(),
47 bind1st(mem_fun(&sensor_base::register_supported_event), this));
49 physical_sensor::set_poller(light_sensor::working, this);
52 light_sensor::~light_sensor()
54 INFO("light_sensor is destroyed!");
57 bool light_sensor::init()
59 m_sensor_hal = sensor_plugin_loader::get_instance().get_sensor_hal(LIGHT_SENSOR);
62 ERR("cannot load sensor_hal[%s]", sensor_base::get_name());
66 INFO("%s is created!", sensor_base::get_name());
70 sensor_type_t light_sensor::get_type(void)
75 bool light_sensor::working(void *inst)
77 light_sensor *sensor = (light_sensor *)inst;
78 return sensor->process_event();
81 bool light_sensor::process_event(void)
86 if (!m_sensor_hal->is_data_ready(true))
89 m_sensor_hal->get_sensor_data(event.data);
90 level = (int) adc_to_light_level((int)event.data.values[0]);
92 AUTOLOCK(m_client_info_mutex);
94 if (get_client_cnt(LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME)) {
95 event.event_type = LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME;
99 if (get_client_cnt(LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME)) {
100 event.event_type = LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME;
101 raw_to_level(event.data);
105 if (m_level != level) {
108 if (get_client_cnt(LIGHT_EVENT_CHANGE_LEVEL)) {
109 event.event_type = LIGHT_EVENT_CHANGE_LEVEL;
110 raw_to_level(event.data);
118 int light_sensor::adc_to_light_level(int adc)
120 int level_cnt = sizeof(m_light_level) / sizeof(m_light_level[0]) - 1;
122 for (int i = 0; i < level_cnt; ++i) {
123 if (adc >= m_light_level[i] && adc < m_light_level[i + 1])
130 bool light_sensor::on_start(void)
134 if (!m_sensor_hal->enable()) {
135 ERR("m_sensor_hal start fail");
142 bool light_sensor::on_stop(void)
146 if (!m_sensor_hal->disable()) {
147 ERR("m_sensor_hal stop fail");
154 bool light_sensor::get_properties(const unsigned int type, sensor_properties_s &properties)
156 m_sensor_hal->get_properties(properties);
158 if (type == LIGHT_LUX_DATA_SET)
161 if (type == LIGHT_BASE_DATA_SET) {
162 properties.min_range = 0;
163 properties.max_range = sizeof(m_light_level) / sizeof(m_light_level[0]) - 1;
164 properties.resolution = 1;
171 int light_sensor::get_sensor_data(const unsigned int type, sensor_data_t &data)
174 ret = m_sensor_hal->get_sensor_data(data);
179 if (type == LIGHT_LUX_DATA_SET)
182 if (type == LIGHT_BASE_DATA_SET) {
190 bool light_sensor::set_interval(unsigned long interval)
194 INFO("Polling interval is set to %dms", interval);
195 return m_sensor_hal->set_interval(interval);
198 void light_sensor::raw_to_level(sensor_data_t &data)
200 data.values[0] = (int) adc_to_light_level((int)data.values[0]);
204 extern "C" void *create(void)
209 inst = new light_sensor();
211 ERR("Failed to create light_sensor class, errno : %d, errstr : %s", err, strerror(err));
218 extern "C" void destroy(void *inst)
220 delete (light_sensor *)inst;