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 static void __finalize(void);
30 static void __start_sensing(void);
31 static void __stop_sensing(void);
32 static void __initialize_accel_info(void);
33 static void __sensor_value_changed_cb(sensor_h sensor, sensor_event_s *event, void *data);
35 typedef struct _average_value_info {
44 static struct __accel_info_s {
45 resource_info *sensor_info;
46 average_value_info avr_val;
52 resource_info *resource_accelerometer_init_sensor(resource_info *sensor_info)
54 bool is_supported = false;
57 if (sensor_info == NULL) {
58 _E("There's no allocated memory for Accelerometer Sensor");
62 ret = sensor_is_supported(SENSOR_ACCELEROMETER, &is_supported);
63 if (ret != SENSOR_ERROR_NONE) {
64 _E("Failed to check sensor support");
69 _E("This Device Do Not Support Accelerometer Sensor");
72 _D("Accelerometer is available");
74 ret = sensor_get_default_sensor(SENSOR_ACCELEROMETER, &(sensor_info->sensor));
75 if (ret != SENSOR_ERROR_NONE) {
76 _E("Failed to get sensor handler");
80 ret = sensor_create_listener(sensor_info->sensor, &(sensor_info->listener));
81 if (ret != SENSOR_ERROR_NONE) {
82 _E("Failed to create sensor listener");
86 ret = sensor_listener_set_event_cb(sensor_info->listener, 1, __sensor_value_changed_cb, NULL);
87 if (ret != SENSOR_ERROR_NONE) {
88 _E("Failed to set sensor listener event");
92 sensor_info->finalize = __finalize;
93 sensor_info->start_sensing = __start_sensing;
94 sensor_info->stop_sensing = __stop_sensing;
96 accel_info.sensor_info = sensor_info;
101 if (sensor_info->listener) {
102 ret = sensor_destroy_listener(sensor_info->listener);
103 if (ret != SENSOR_ERROR_NONE)
104 _E("Failed to destroy sensor listener");
107 resource_info_free(sensor_info);
112 static void __finalize(void)
116 ret = sensor_listener_unset_event_cb(accel_info.sensor_info->listener);
117 if (ret != SENSOR_ERROR_NONE) {
118 _E("Failed to unset sensor listener event");
122 ret = sensor_listener_stop(accel_info.sensor_info->listener);
123 if (ret != SENSOR_ERROR_NONE) {
124 _E("Failed to stop sensor listener event");
128 ret = sensor_destroy_listener(accel_info.sensor_info->listener);
129 if (ret != SENSOR_ERROR_NONE) {
130 _E("Failed to destroy sensor listener event");
134 resource_info_free(accel_info.sensor_info);
135 __initialize_average_value_info();
138 static void __sensor_value_changed_cb(sensor_h sensor, sensor_event_s *event, void *data)
140 _D("Time: [%llu], X: [%f], Y: [%f]", event->timestamp, event->values[0], event->values[1]);
142 accel_info.avr_val.servo_sum -= accel_info.avr_val.servo[accel_info.avr_val.front];
143 accel_info.avr_val.speed_sum -= accel_info.avr_val.speed[accel_info.avr_val.front];
145 accel_info.avr_val.servo[accel_info.avr_val.front] = event->values[0] * -100;
146 accel_info.avr_val.speed[accel_info.avr_val.front] = event->values[1] * -100;
148 accel_info.avr_val.servo_sum += accel_info.avr_val.servo[accel_info.avr_val.front];
149 accel_info.avr_val.speed_sum += accel_info.avr_val.speed[accel_info.avr_val.front];
151 accel_info.avr_val.front++;
153 if (accel_info.avr_val.front >= 11)
154 accel_info.avr_val.ready_to_send = 1;
156 if (accel_info.avr_val.front % 11 == 0) {
157 accel_info.avr_val.front = 1;
160 if (!accel_info.avr_val.ready_to_send)
163 commu_data_s temp = { NULL, NULL };
165 temp.servo = accel_info.avr_val.servo_sum / 10;
166 temp.speed = accel_info.avr_val.speed_sum / 10;
168 _D("AVERAGE --- Time: [%llu], Servo: [%d], Speed: [%d]", event->timestamp, temp.servo, temp.speed);
170 view_update_view_with_data((void *)&temp);
171 communication_center_send_data((void *)&temp);
174 static void __start_sensing(void)
178 ret = sensor_listener_start(accel_info.sensor_info->listener);
179 if (ret != SENSOR_ERROR_NONE) {
180 _E("Failed to start sensor listener");
185 static void __initialize_average_value_info(void)
187 memset(&(accel_info.avr_val), 0, sizeof(accel_info.avr_val));
190 static void __stop_sensing(void)
194 ret = sensor_listener_stop(accel_info.sensor_info->listener);
195 if (ret != SENSOR_ERROR_NONE) {
196 _E("Failed to stop sensor listener event");
200 commu_data_s temp = { NULL, NULL };
205 view_update_view_with_data((void *)&temp);
206 communication_center_send_data((void *)&temp);
207 __initialize_average_value_info();