Send average value of current 10 data
[apps/native/gear-racing-controller.git] / src / resource_accelerometer.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: junkyu Han <junkyu.han@samsung.com>
5  *
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
9  *
10  * http://floralicense.org/license/
11  *
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.
17  */
18
19 #include <sensor.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 #include "communication_center.h"
24 #include "communication_by_udp.h"
25 #include "resource.h"
26 #include "view.h"
27 #include "log.h"
28
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);
34
35 typedef struct _average_value_info {
36         int servo[11];
37         int speed[11];
38         int servo_sum;
39         int speed_sum;
40         int front;
41         int ready_to_send;
42 } average_value_info;
43
44 static struct __accel_info_s {
45         resource_info *sensor_info;
46         average_value_info avr_val;
47 } accel_info = {
48         .sensor_info = NULL,
49         .avr_val = { 0, },
50 };
51
52 resource_info *resource_accelerometer_init_sensor(resource_info *sensor_info)
53 {
54         bool is_supported = false;
55         int ret = 0;
56
57         if (sensor_info == NULL) {
58                 _E("There's no allocated memory for Accelerometer Sensor");
59                 return NULL;
60         }
61
62         ret = sensor_is_supported(SENSOR_ACCELEROMETER, &is_supported);
63         if (ret != SENSOR_ERROR_NONE) {
64                 _E("Failed to check sensor support");
65                 goto ERROR;
66         }
67
68         if (!is_supported) {
69                 _E("This Device Do Not Support Accelerometer Sensor");
70                 goto ERROR;
71         }
72         _D("Accelerometer is available");
73
74         ret = sensor_get_default_sensor(SENSOR_ACCELEROMETER, &(sensor_info->sensor));
75         if (ret != SENSOR_ERROR_NONE) {
76                 _E("Failed to get sensor handler");
77                 goto ERROR;
78         }
79
80         ret = sensor_create_listener(sensor_info->sensor, &(sensor_info->listener));
81         if (ret != SENSOR_ERROR_NONE) {
82                 _E("Failed to create sensor listener");
83                 goto ERROR;
84         }
85
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");
89                 goto ERROR;
90         }
91
92         sensor_info->finalize = __finalize;
93         sensor_info->start_sensing = __start_sensing;
94         sensor_info->stop_sensing = __stop_sensing;
95
96         accel_info.sensor_info = sensor_info;
97
98         return sensor_info;
99
100 ERROR:
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");
105         }
106
107         resource_info_free(sensor_info);
108
109         return NULL;
110 }
111
112 static void __finalize(void)
113 {
114         int ret = 0;
115
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");
119                 return;
120         }
121
122         ret = sensor_listener_stop(accel_info.sensor_info->listener);
123         if (ret != SENSOR_ERROR_NONE) {
124                 _E("Failed to stop sensor listener event");
125                 return;
126         }
127
128         ret = sensor_destroy_listener(accel_info.sensor_info->listener);
129         if (ret != SENSOR_ERROR_NONE) {
130                 _E("Failed to destroy sensor listener event");
131                 return;
132         }
133
134         resource_info_free(accel_info.sensor_info);
135         __initialize_average_value_info();
136 }
137
138 static void __sensor_value_changed_cb(sensor_h sensor, sensor_event_s *event, void *data)
139 {
140         _D("Time: [%llu], X: [%f], Y: [%f]", event->timestamp, event->values[0], event->values[1]);
141
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];
144
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;
147
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];
150
151         accel_info.avr_val.front++;
152
153         if (accel_info.avr_val.front >= 11)
154                 accel_info.avr_val.ready_to_send = 1;
155
156         if (accel_info.avr_val.front % 11 == 0) {
157                 accel_info.avr_val.front = 1;
158         }
159
160         if (!accel_info.avr_val.ready_to_send)
161                 return;
162
163         commu_data_s temp = { NULL, NULL };
164
165         temp.servo = accel_info.avr_val.servo_sum / 10;
166         temp.speed = accel_info.avr_val.speed_sum / 10;
167
168         _D("AVERAGE --- Time: [%llu], Servo: [%d], Speed: [%d]", event->timestamp, temp.servo, temp.speed);
169
170         view_update_view_with_data((void *)&temp);
171         communication_center_send_data((void *)&temp);
172 }
173
174 static void __start_sensing(void)
175 {
176         int ret = 0;
177
178         ret = sensor_listener_start(accel_info.sensor_info->listener);
179         if (ret != SENSOR_ERROR_NONE) {
180                 _E("Failed to start sensor listener");
181                 return;
182         }
183 }
184
185 static void __initialize_average_value_info(void)
186 {
187         memset(&(accel_info.avr_val), 0, sizeof(accel_info.avr_val));
188 }
189
190 static void __stop_sensing(void)
191 {
192         int ret = 0;
193
194         ret = sensor_listener_stop(accel_info.sensor_info->listener);
195         if (ret != SENSOR_ERROR_NONE) {
196                 _E("Failed to stop sensor listener event");
197                 return;
198         }
199
200         commu_data_s temp = { NULL, NULL };
201
202         temp.servo = 0;
203         temp.speed = 0;
204
205         view_update_view_with_data((void *)&temp);
206         communication_center_send_data((void *)&temp);
207         __initialize_average_value_info();
208 }
209