From 47655267960bc72d0b3be5fc495a991dbb982fb2 Mon Sep 17 00:00:00 2001 From: Vibhor Gaur Date: Mon, 22 Dec 2014 18:04:12 +0530 Subject: [PATCH] Adding polling based support for testing of light sensor Change-Id: Iab85d66ad648a2ecd5669938b448728ddfe898c7 --- packaging/sensord.spec | 1 + test/CMakeLists.txt | 4 ++ test/src/light.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 test/src/light.c diff --git a/packaging/sensord.spec b/packaging/sensord.spec index 1eaccdf..2e1fff7 100755 --- a/packaging/sensord.spec +++ b/packaging/sensord.spec @@ -145,6 +145,7 @@ systemctl daemon-reload /usr/bin/proxi /usr/bin/pressure /usr/bin/temperature +/usr/bin/light %license LICENSE.APLv2 %{_datadir}/license/test diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5c89818..3c103cc 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -36,6 +36,7 @@ add_executable(gyro src/gyro.c) add_executable(proxi src/proxi.c) add_executable(pressure src/pressure.c) add_executable(temperature src/temperature.c) +add_executable(light src/light.c) SET_TARGET_PROPERTIES(accelerometer PROPERTIES LINKER_LANGUAGE C) SET_TARGET_PROPERTIES(geomagnetic PROPERTIES LINKER_LANGUAGE C) @@ -46,6 +47,7 @@ SET_TARGET_PROPERTIES(gyro PROPERTIES LINKER_LANGUAGE C) SET_TARGET_PROPERTIES(proxi PROPERTIES LINKER_LANGUAGE C) SET_TARGET_PROPERTIES(pressure PROPERTIES LINKER_LANGUAGE C) SET_TARGET_PROPERTIES(temperature PROPERTIES LINKER_LANGUAGE C) +SET_TARGET_PROPERTIES(light PROPERTIES LINKER_LANGUAGE C) target_link_libraries(accelerometer glib-2.0 dlog sensor) target_link_libraries(geomagnetic glib-2.0 dlog sensor) @@ -56,6 +58,7 @@ target_link_libraries(gyro glib-2.0 dlog sensor) target_link_libraries(proxi glib-2.0 dlog sensor) target_link_libraries(pressure glib-2.0 dlog sensor) target_link_libraries(temperature glib-2.0 dlog sensor) +target_link_libraries(light glib-2.0 dlog sensor) INSTALL(TARGETS accelerometer DESTINATION /usr/bin/) INSTALL(TARGETS geomagnetic DESTINATION /usr/bin/) @@ -66,4 +69,5 @@ INSTALL(TARGETS gyro DESTINATION /usr/bin/) INSTALL(TARGETS proxi DESTINATION /usr/bin/) INSTALL(TARGETS pressure DESTINATION /usr/bin/) INSTALL(TARGETS temperature DESTINATION /usr/bin/) +INSTALL(TARGETS light DESTINATION /usr/bin/) diff --git a/test/src/light.c b/test/src/light.c new file mode 100644 index 0000000..454d5c2 --- /dev/null +++ b/test/src/light.c @@ -0,0 +1,140 @@ +/* + * sensord + * + * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include +#include +#include +#include +#include +#include +#include +#include + +static GMainLoop *mainloop; + +void callback(unsigned int event_type, sensor_event_data_t *event, void *user_data) +{ + sensor_data_t *data = (sensor_data_t *)event->event_data; + printf("Light :[%lld] [%6.6f]\n", data->timestamp, data->values[0]); +} + +void printformat() +{ + printf("Usage : ./light (optional) (optional)\n\n"); + + printf("mode:"); + printf("[-p]\n"); + printf("p is for polling based,default mode is event driven\n"); + + printf("event:"); + printf("[RAW_DATA_REPORT_ON_TIME]\n"); + + printf("interval:\n"); + printf("The time interval should be entered based on the sampling frequency supported by light driver on the device in ms.If no value for sensor is entered default value by the driver will be used.\n"); +} + +int main(int argc,char **argv) +{ + int result, handle, start_handle, stop_handle; + unsigned int event; + mainloop = g_main_loop_new(NULL, FALSE); + event = LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME; + event_condition_t *event_condition = (event_condition_t*) malloc(sizeof(event_condition_t)); + event_condition->cond_op = CONDITION_EQUAL; + + sensor_type_t type = LIGHT_SENSOR; + + if (argc != 2 && argc != 3 && argc!=4) { + printformat(); + free(event_condition); + return 0; + } + + else if (argc>=3 && strcmp(argv[1], "-p") == 0 && strcmp(argv[2], "RAW_DATA_REPORT_ON_TIME") == 0) { + printf("Polling based\n"); + + handle = sf_connect(type); + result = sf_start(handle, 1); + + if (result < 0) { + printf("Can't start light SENSOR\n"); + printf("Error\n\n\n\n"); + return -1; + } + + sensor_data_t data; + + while(1) { + result = sf_get_data(handle, LIGHT_LUX_DATA_SET , &data); + printf("Light : [%6.6f]\n", data.values[0]); + usleep(100000); + } + + result = sf_disconnect(handle); + if (result < 0) { + printf("Can't disconnect light sensor\n"); + printf("Error\n\n\n\n"); + return -1; + } + } + + else if (strcmp(argv[1], "RAW_DATA_REPORT_ON_TIME") == 0) { + printf("Event based\n"); + + event_condition->cond_value1 = 100; + if (argc == 3) + event_condition->cond_value1 = atof(argv[2]); + + handle = sf_connect(type); + result = sf_register_event(handle, event, event_condition, callback, NULL); + + if (result < 0) + printf("Can't register light\n"); + + start_handle = sf_start(handle,0); + + if (start_handle < 0) { + printf("Error\n\n\n\n"); + sf_unregister_event(handle, event); + sf_disconnect(handle); + return -1; + } + + g_main_loop_run(mainloop); + g_main_loop_unref(mainloop); + + sf_unregister_event(handle, event); + + stop_handle = sf_stop(handle); + + if (stop_handle < 0) { + printf("Error\n\n"); + return -1; + } + + sf_disconnect(handle); + free(event_condition); + } + + else { + printformat(); + } + + return 0; +} + -- 2.7.4