ea3066becd67ce9cd975604fc8dd4f0478496c57
[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 #define AVERAGE_COUNT 11
30 #define BASE_VALUE 100
31
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);
37
38 typedef struct _average_value_info {
39         int servo[11];
40         int speed[11];
41         int servo_sum;
42         int speed_sum;
43         int front;
44         bool ready_to_send;
45 } average_value_info;
46
47 static struct __accel_info_s {
48         resource_info *sensor_info;
49         average_value_info avr_val;
50 } accel_info = {
51         .sensor_info = NULL,
52         .avr_val = { 0, },
53 };
54
55 resource_info *resource_accelerometer_init_sensor(resource_info *sensor_info)
56 {
57         bool is_supported = false;
58         int ret = 0;
59
60         if (sensor_info == NULL) {
61                 _E("There's no allocated memory for Accelerometer Sensor");
62                 return NULL;
63         }
64
65         ret = sensor_is_supported(SENSOR_ACCELEROMETER, &is_supported);
66         if (ret != SENSOR_ERROR_NONE) {
67                 _E("Failed to check sensor support");
68                 goto ERROR;
69         }
70
71         if (!is_supported) {
72                 _E("This Device Do Not Support Accelerometer Sensor");
73                 goto ERROR;
74         }
75         _D("Accelerometer is available");
76
77         ret = sensor_get_default_sensor(SENSOR_ACCELEROMETER, &(sensor_info->sensor));
78         if (ret != SENSOR_ERROR_NONE) {
79                 _E("Failed to get sensor handler");
80                 goto ERROR;
81         }
82
83         ret = sensor_create_listener(sensor_info->sensor, &(sensor_info->listener));
84         if (ret != SENSOR_ERROR_NONE) {
85                 _E("Failed to create sensor listener");
86                 goto ERROR;
87         }
88
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");
92                 goto ERROR;
93         }
94
95         sensor_info->finalize = __finalize;
96         sensor_info->start_sensing = __start_sensing;
97         sensor_info->stop_sensing = __stop_sensing;
98
99         accel_info.sensor_info = sensor_info;
100
101         return sensor_info;
102
103 ERROR:
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");
108         }
109
110         resource_info_free(sensor_info);
111
112         return NULL;
113 }
114
115 static void __finalize(void)
116 {
117         int ret = 0;
118
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");
122                 return;
123         }
124
125         ret = sensor_listener_stop(accel_info.sensor_info->listener);
126         if (ret != SENSOR_ERROR_NONE) {
127                 _E("Failed to stop sensor listener event");
128                 return;
129         }
130
131         ret = sensor_destroy_listener(accel_info.sensor_info->listener);
132         if (ret != SENSOR_ERROR_NONE) {
133                 _E("Failed to destroy sensor listener event");
134                 return;
135         }
136
137         resource_info_free(accel_info.sensor_info);
138         __initialize_average_value_info();
139 }
140
141 static void __sensor_value_changed_cb(sensor_h sensor, sensor_event_s *event, void *data)
142 {
143         _D("Time: [%llu], X: [%f], Y: [%f]", event->timestamp, event->values[0], event->values[1]);
144
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];
147
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;
150
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];
153
154         accel_info.avr_val.front++;
155
156         if (accel_info.avr_val.front >= AVERAGE_COUNT)
157                 accel_info.avr_val.ready_to_send = 1;
158
159         if (accel_info.avr_val.front % AVERAGE_COUNT == 0)
160                 accel_info.avr_val.front = 1;
161
162         if (!accel_info.avr_val.ready_to_send)
163                 return;
164
165         commu_data_s temp = { NULL, NULL };
166
167         temp.servo = accel_info.avr_val.servo_sum / 10;
168         temp.speed = accel_info.avr_val.speed_sum / 10;
169
170         _D("AVERAGE --- Time: [%llu], Servo: [%d], Speed: [%d]", event->timestamp, temp.servo, temp.speed);
171
172         view_update_view_with_data((void *)&temp);
173         communication_center_send_data((void *)&temp);
174 }
175
176 static void __start_sensing(void)
177 {
178         int ret = 0;
179
180         ret = sensor_listener_start(accel_info.sensor_info->listener);
181         if (ret != SENSOR_ERROR_NONE) {
182                 _E("Failed to start sensor listener");
183                 return;
184         }
185 }
186
187 static void __initialize_average_value_info(void)
188 {
189         memset(&(accel_info.avr_val), 0, sizeof(accel_info.avr_val));
190 }
191
192 static void __stop_sensing(void)
193 {
194         int ret = 0;
195
196         ret = sensor_listener_stop(accel_info.sensor_info->listener);
197         if (ret != SENSOR_ERROR_NONE) {
198                 _E("Failed to stop sensor listener event");
199                 return;
200         }
201
202         commu_data_s temp = { NULL, NULL };
203
204         temp.servo = 0;
205         temp.speed = 0;
206
207         view_update_view_with_data((void *)&temp);
208         communication_center_send_data((void *)&temp);
209         __initialize_average_value_info();
210 }
211