summaryrefslogtreecommitdiffstats
path: root/sensors/orientationd/orientationd.c
blob: 7cc9f0ab144b9006f26393220b623be64fee6333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Copyright (C) 2013 Paul Kocialkowski
 *
 * Orientation calculation based on AK8975_FS:
 * Copyright (C) 2012 Asahi Kasei Microdevices Corporation, Japan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <poll.h>
#include <math.h>
#include <linux/ioctl.h>
#include <linux/input.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "orientationd.h"

#include <hardware/sensors.h>

#define ABS_CONTROL_REPORT	(ABS_THROTTLE)

#define FLAG_X		(1 << 0)
#define FLAG_Y		(1 << 1)
#define FLAG_Z		(1 << 2)
#define FLAG_ALL	(FLAG_X | FLAG_Y | FLAG_Z)

struct sensor_device {
	char *name;
	int handle;
	float (*get_data)(int value);
	int (*set_data)(float value);

	int fd;
};

struct sensor_data {
	vector v;
	int flags;
};

float rad2deg(float v)
{
	return (v * 180.0f / 3.1415926535f);
}

void orientation_calculate(vector *a, vector *m, vector *o)
{
	float azimuth, pitch, roll;
	float la, sinp, cosp, sinr, cosr, x, y;

	if (a == NULL || m == NULL || o == NULL)
		return;

	la = vector_length(a);
	pitch = asinf(-(a->y) / la);
	roll = asinf((a->x) / la);

	sinp = sinf(pitch);
	cosp = cosf(pitch);
	sinr = sinf(roll);
	cosr = cosf(roll);

	y = -(m->x) * cosr + m->z * sinr;
	x = m->x * sinp * sinr + m->y * cosp + m->z * sinp * cosr;
	azimuth = atan2f(y, x);

	o->x = rad2deg(azimuth);
	o->y = rad2deg(pitch);
	o->z = rad2deg(roll);

	if (o->x < 0)
		o->x += 360.0f;
}

float bma250_acceleration(int value)
{
	return (float) (value * GRAVITY_EARTH) / 256.0f;
}

float yas530c_magnetic(int value)
{
	return (float) value / 1000.0f;
}

int yas_orientation(float value)
{
	return (int) (value * 1000);
}

struct sensor_device bma250_device = {
	.name = "accelerometer",
	.handle = SENSOR_TYPE_ACCELEROMETER,
	.get_data = bma250_acceleration,
	.set_data = NULL,
	.fd = -1,
};

struct sensor_device yas530c_device = {
	.name = "geomagnetic",
	.handle = SENSOR_TYPE_MAGNETIC_FIELD,
	.get_data = yas530c_magnetic,
	.set_data = NULL,
	.fd = -1,
};

struct sensor_device yas_orientation_device = {
	.name = "orientation",
	.handle = SENSOR_TYPE_ORIENTATION,
	.get_data = NULL,
	.set_data = yas_orientation,
	.fd = -1,
};

struct sensor_device *sensor_devices[] = {
	&bma250_device,
	&yas530c_device,
	&yas_orientation_device,
};

int sensors_devices_count = sizeof(sensor_devices) / sizeof(struct sensor_device *);

int sensor_device_open(struct sensor_device *dev)
{
	int fd;

	if (dev == NULL || dev->name == NULL)
		return -EINVAL;

	printf("Opening %s\n", dev->name);

	fd = input_open(dev->name, dev->handle == SENSOR_TYPE_ORIENTATION ? 1 : 0);
	if (fd < 0)
		return -1;

	dev->fd = fd;

	return 0;
}

void sensor_device_close(struct sensor_device *dev)
{
	if (dev == NULL || dev->fd < 0)
		return;

	close(dev->fd);
	dev->fd = -1;
}

struct sensor_device *sensor_device_find_handle(int handle)
{
	int i;

	for (i=0 ; i < sensors_devices_count ; i++) {
		if (sensor_devices[i]->handle == handle)
			return sensor_devices[i];
	}

	return NULL;
}

struct sensor_device *sensor_device_find_fd(int fd)
{
	int i;

	for (i=0 ; i < sensors_devices_count ; i++) {
		if (sensor_devices[i]->fd == fd)
			return sensor_devices[i];
	}

	return NULL;
}

int sensor_device_get_data(struct sensor_device *dev, struct sensor_data *d,
	struct input_event *e)
{
	if (dev == NULL || d == NULL || e == NULL || dev->get_data == NULL)
		return -EINVAL;

	if (e->type == EV_ABS) {
		switch (e->code) {
			case ABS_X:
				d->v.x = dev->get_data(e->value);
				d->flags |= FLAG_X;
				return 0;
			case ABS_Y:
				d->v.y = dev->get_data(e->value);
				d->flags |= FLAG_Y;
				return 0;
			case ABS_Z:
				d->v.z = dev->get_data(e->value);
				d->flags |= FLAG_Z;
				return 0;
		}
	}

	return -1;
}

int sensor_device_set_data(struct sensor_device *dev, struct sensor_data *d)
{
	struct input_event event;

	if (dev == NULL || d == NULL || dev->set_data == NULL)
		return -EINVAL;

	event.type = EV_ABS;
	event.code = ABS_X;
	event.value = dev->set_data(d->v.x);
	gettimeofday(&event.time, NULL);
	write(dev->fd, &event, sizeof(event));

	event.type = EV_ABS;
	event.code = ABS_Y;
	event.value = dev->set_data(d->v.y);
	gettimeofday(&event.time, NULL);
	write(dev->fd, &event, sizeof(event));

	event.type = EV_ABS;
	event.code = ABS_Z;
	event.value = dev->set_data(d->v.z);
	gettimeofday(&event.time, NULL);
	write(dev->fd, &event, sizeof(event));

	event.type = EV_SYN;
	event.code = SYN_REPORT;
	event.value = 0;
	gettimeofday(&event.time, NULL);
	write(dev->fd, &event, sizeof(event));

	return 0;
}

int sensor_device_control(struct sensor_device *dev, struct input_event *e)
{
	int enabled;

	if (dev == NULL || e == NULL)
		return -EINVAL;

	if (e->type == EV_ABS && e->code == ABS_CONTROL_REPORT) {
		enabled = e->value & (1 << 16);
		if (enabled)
			return 1;
		else
			return 0;
	}

	return -1;
}

int main(int argc, char *argv[])
{
	struct input_event event;
	struct sensor_data a, m, o;

	struct sensor_device *dev;
	struct pollfd *poll_fds;

	int enabled, data;
	int index;

	int rc, c, i;

	memset(&a, 0, sizeof(a));
	memset(&m, 0, sizeof(m));
	memset(&o, 0, sizeof(o));

	poll_fds = (struct pollfd *) calloc(1, sizeof(struct pollfd) * sensors_devices_count);

	index = -1;
	c = 0;

	for (i=0 ; i < sensors_devices_count ; i++) {
		rc = sensor_device_open(sensor_devices[i]);
		if (rc < 0)
			continue;

		poll_fds[c].fd = sensor_devices[i]->fd;
		poll_fds[c].events = POLLIN;

		if (sensor_devices[i]->handle == SENSOR_TYPE_ORIENTATION && index < 0)
			index = c;

		c++;
	}

	if (c <= 0 || index <= 0)
		goto exit;

	printf("Starting main loop\n");

	enabled = 0;
	while (1) {
		data = 0;

		if (enabled)
			rc = poll(poll_fds, c, -1);
		else
			rc = poll(&poll_fds[index], 1, -1);

		if (rc < 0)
			goto exit;

		for (i=0 ; i < c ; i++) {
			if (poll_fds[i].revents & POLLIN) {
				dev = sensor_device_find_fd(poll_fds[i].fd);
				if (dev == NULL)
					continue;

				read(dev->fd, &event, sizeof(event));

				switch (dev->handle) {
					case SENSOR_TYPE_ACCELEROMETER:
						rc = sensor_device_get_data(dev, &a, &event);
						if (rc >= 0)
							data = 1;
						break;
					case SENSOR_TYPE_MAGNETIC_FIELD:
						rc = sensor_device_get_data(dev, &m, &event);
						if (rc >= 0)
							data = 1;
						break;
					case SENSOR_TYPE_ORIENTATION:
						rc = sensor_device_control(dev, &event);
						if (rc == 1)
							enabled = 1;
						else if (rc == 0)
							enabled = 0;
						break;
				}
			}

			if (data && a.flags & FLAG_ALL && m.flags & FLAG_ALL) {
				dev = sensor_device_find_handle(SENSOR_TYPE_ORIENTATION);
				if (dev == NULL)
					continue;

				orientation_calculate(&a.v, &m.v, &o.v);
				sensor_device_set_data(dev, &o);
			}
		}
	}


exit:
	for (i=0 ; i < sensors_devices_count ; i++)
		sensor_device_close(sensor_devices[i]);

	while (1) {
		sleep(3600);
	}

	return 0;
}