2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Contact: junkyu Han <junkyu.han@samsung.com>
6 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
23 #include "communication_center.h"
24 #include "communication_by_udp.h"
29 #define AVERAGE_COUNT 11
30 #define BASE_VALUE 100
32 static void __finalize(void);
33 static void __start_sensing(void);
34 static void __stop_sensing(void);
35 static void __initialize_average_value_info();
36 static void __sensor_value_changed_cb(sensor_h sensor, sensor_event_s *event, void *data);
38 typedef struct _average_value_info {
47 static struct __accel_info_s {
48 resource_info *sensor_info;
49 average_value_info avr_val;
55 resource_info *resource_accelerometer_init_sensor(resource_info *sensor_info)
57 bool is_supported = false;
60 if (sensor_info == NULL) {
61 _E("There's no allocated memory for Accelerometer Sensor");
65 ret = sensor_is_supported(SENSOR_ACCELEROMETER, &is_supported);
66 if (ret != SENSOR_ERROR_NONE) {
67 _E("Failed to check sensor support");
72 _E("This Device Do Not Support Accelerometer Sensor");
75 _D("Accelerometer is available");
77 ret = sensor_get_default_sensor(SENSOR_ACCELEROMETER, &(sensor_info->sensor));
78 if (ret != SENSOR_ERROR_NONE) {
79 _E("Failed to get sensor handler");
83 ret = sensor_create_listener(sensor_info->sensor, &(sensor_info->listener));
84 if (ret != SENSOR_ERROR_NONE) {
85 _E("Failed to create sensor listener");
89 ret = sensor_listener_set_event_cb(sensor_info->listener, 1, __sensor_value_changed_cb, NULL);
90 if (ret != SENSOR_ERROR_NONE) {
91 _E("Failed to set sensor listener event");
95 sensor_info->finalize = __finalize;
96 sensor_info->start_sensing = __start_sensing;
97 sensor_info->stop_sensing = __stop_sensing;
99 accel_info.sensor_info = sensor_info;
104 if (sensor_info->listener) {
105 ret = sensor_destroy_listener(sensor_info->listener);
106 if (ret != SENSOR_ERROR_NONE)
107 _E("Failed to destroy sensor listener");
110 resource_info_free(sensor_info);
115 static void __finalize(void)
119 ret = sensor_listener_unset_event_cb(accel_info.sensor_info->listener);
120 if (ret != SENSOR_ERROR_NONE) {
121 _E("Failed to unset sensor listener event");
125 ret = sensor_listener_stop(accel_info.sensor_info->listener);
126 if (ret != SENSOR_ERROR_NONE) {
127 _E("Failed to stop sensor listener event");
131 ret = sensor_destroy_listener(accel_info.sensor_info->listener);
132 if (ret != SENSOR_ERROR_NONE) {
133 _E("Failed to destroy sensor listener event");
137 resource_info_free(accel_info.sensor_info);
138 __initialize_average_value_info();
141 static void __sensor_value_changed_cb(sensor_h sensor, sensor_event_s *event, void *data)
143 _D("Time: [%llu], X: [%f], Y: [%f]", event->timestamp, event->values[0], event->values[1]);
145 accel_info.avr_val.servo_sum -= accel_info.avr_val.servo[accel_info.avr_val.front];
146 accel_info.avr_val.speed_sum -= accel_info.avr_val.speed[accel_info.avr_val.front];
148 accel_info.avr_val.servo[accel_info.avr_val.front] = event->values[0] * -BASE_VALUE;
149 accel_info.avr_val.speed[accel_info.avr_val.front] = event->values[1] * -BASE_VALUE;
151 accel_info.avr_val.servo_sum += accel_info.avr_val.servo[accel_info.avr_val.front];
152 accel_info.avr_val.speed_sum += accel_info.avr_val.speed[accel_info.avr_val.front];
154 accel_info.avr_val.front++;
156 if (accel_info.avr_val.front >= AVERAGE_COUNT)
157 accel_info.avr_val.ready_to_send = 1;
159 if (accel_info.avr_val.front % AVERAGE_COUNT == 0) {
160 accel_info.avr_val.front = 1;
163 if (!accel_info.avr_val.ready_to_send)
166 commu_data_s temp = { NULL, NULL };
168 temp.servo = accel_info.avr_val.servo_sum / 10;
169 temp.speed = accel_info.avr_val.speed_sum / 10;
171 _D("AVERAGE --- Time: [%llu], Servo: [%d], Speed: [%d]", event->timestamp, temp.servo, temp.speed);
173 view_update_view_with_data((void *)&temp);
174 communication_center_send_data((void *)&temp);
177 static void __start_sensing(void)
181 ret = sensor_listener_start(accel_info.sensor_info->listener);
182 if (ret != SENSOR_ERROR_NONE) {
183 _E("Failed to start sensor listener");
188 static void __initialize_average_value_info(void)
190 memset(&(accel_info.avr_val), 0, sizeof(accel_info.avr_val));
193 static void __stop_sensing(void)
197 ret = sensor_listener_stop(accel_info.sensor_info->listener);
198 if (ret != SENSOR_ERROR_NONE) {
199 _E("Failed to stop sensor listener event");
203 commu_data_s temp = { NULL, NULL };
208 view_update_view_with_data((void *)&temp);
209 communication_center_send_data((void *)&temp);
210 __initialize_average_value_info();