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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fcntl.h>
#include <errno.h>
#include <math.h>
#include <poll.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/select.h>
#include <dlfcn.h>
#include <cutils/log.h>
#include "BoschYamaha.h"
/*****************************************************************************/
int stub_is_sensor_enabled(uint32_t sensor_type) {
return 0;
}
int stub_enable_disable_sensor(uint32_t sensor_type) {
return -ENODEV;
}
int stub_set_delay(uint64_t delay) {
return -ENODEV;
}
BoschYamaha::BoschYamaha()
: SensorBase(NULL, NULL),
//mEnabled(0),
mPendingMask(0),
mInputReaderMagnetic(4),
mInputReaderAccel(4)
{
counterAccel = 0;
/* FIXME set input device name for magnetic sensor */
data_name = "input0";
data_compass_fd = openInput("geomagnetic");
//Open Compass
if (data_compass_fd) {
strcpy(input_sysfs_path, "/sys/class/input/");
strcat(input_sysfs_path, input_name);
strcat(input_sysfs_path, "/device/");
input_sysfs_path_len = strlen(input_sysfs_path);
//enable(MagneticField, 1);
}
/* FIXME set input device name for accelereometer sensor */
data_accel_name = "event6";
data_fd = openInput("SMB380-Sensor");
//Open Compass
if (data_fd) {
strcpy(input_accel_sysfs_path, "/sys/class/input/");
strcat(input_accel_sysfs_path, data_accel_name);
strcat(input_accel_sysfs_path, "/device/");
input_accel_sysfs_path_len = strlen(input_accel_sysfs_path);
enable(Accelerometer, 1);
}
memset(mPendingEvents, 0, sizeof(mPendingEvents));
mPendingEvents[Accelerometer].version = sizeof(sensors_event_t);
mPendingEvents[Accelerometer].sensor = ID_A;
mPendingEvents[Accelerometer].type = SENSOR_TYPE_ACCELEROMETER;
mPendingEvents[Accelerometer].acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
mPendingEvents[MagneticField].version = sizeof(sensors_event_t);
mPendingEvents[MagneticField].sensor = ID_M;
mPendingEvents[MagneticField].type = SENSOR_TYPE_MAGNETIC_FIELD;
mPendingEvents[MagneticField].magnetic.status = SENSOR_STATUS_ACCURACY_HIGH;
mPendingEvents[Orientation].version = sizeof(sensors_event_t);
mPendingEvents[Orientation].sensor = ID_O;
mPendingEvents[Orientation].type = SENSOR_TYPE_ORIENTATION;
mPendingEvents[Orientation].orientation.status = SENSOR_STATUS_ACCURACY_HIGH;
for (int i=0 ; i<numSensors ; i++)
mDelays[i] = 200000000; // 200 ms by default
}
BoschYamaha::~BoschYamaha()
{
if(compassEnabled){
enable(MagneticField, 0);
}
if(accelEnabled){
enable(Accelerometer, 0);
}
}
int BoschYamaha::enable(int32_t handle, int en)
{
int what = -1;
switch (handle) {
case ID_A: what = Accelerometer; break;
case ID_M: what = MagneticField; break;
case ID_O: what = Orientation; break;
}
if (uint32_t(what) >= numSensors)
return -EINVAL;
int newState = en ? 1 : 0;
int err = 0;
/*
//FIXME enabling the right sensor over sysfs interface
if(what == MagneticField){
what = Accelerometer; //Enable also Accel
//Compass
LOGD("BoschYamaha::~enable Compass(0, %d)", en);
int flags = en ? 1 : 0;
if (flags != compassEnabled) {
int fd;
strcpy(&input_sysfs_path[input_sysfs_path_len], "enable");
LOGD("BoschYamaha::~enable Compass(0, %d) open %s",en, input_sysfs_path);
fd = open(input_sysfs_path, O_RDWR);
if (fd >= 0) {
LOGD("CompassSensor::~enable(0, %d) opened %s",en, input_sysfs_path);
char buf[2];
int err;
buf[1] = 0;
if (flags) {
buf[0] = '1';
} else {
buf[0] = '0';
}
err = write(fd, buf, sizeof(buf));
close(fd);
//mEnabled = flags;
compassEnabled = flags;
return 0;
}
}
}
*/
if(what == Accelerometer){
//Accelerometer
LOGD("BoschYamaha::~enable Accel(0, %d)", en);
int flags = en ? 1 : 0;
if (flags != accelEnabled) {
int fd;
strcpy(&input_accel_sysfs_path[input_accel_sysfs_path_len], "enable");
LOGD("BoschYamaha::~enable Accel(0, %d) open %s",en, input_accel_sysfs_path);
fd = open(input_accel_sysfs_path, O_RDWR);
if (fd >= 0) {
LOGD("CompassSensor::~enable(0, %d) opened %s",en, input_accel_sysfs_path);
char buf[2];
int err;
buf[1] = 0;
if (flags) {
// counterAccel++;
buf[0] = '1';
} else {
// counterAccel--;
buf[0] = '0';
}
//if(counterAccel <= 1){
err = write(fd, buf, sizeof(buf));
//}
close(fd);
//mEnabled = flags;
accelEnabled = flags;
return 0;
}
}
}
return err;
}
int BoschYamaha::setDelay(int32_t handle, int64_t ns)
{
int what = -1;
switch (handle) {
case ID_A: what = Accelerometer; break;
case ID_M: what = MagneticField; break;
case ID_O: what = Orientation; break;
}
if (uint32_t(what) >= numSensors)
return -EINVAL;
if (ns < 0)
return -EINVAL;
mDelays[what] = ns;
return update_delay();
}
int BoschYamaha::update_delay()
{
return 0;
}
bool BoschYamaha::hasPendingEvents() {
//LOGD("hasPendingEvents was called");
if(accelEnabled || compassEnabled){
//LOGD("hasPendingEvents will return true");
return true;
}
else {
//LOGD("hasPendingEvents will return false");
return false;
}
}
int BoschYamaha::readEvents(sensors_event_t* data, int count)
{
if (count < 1)
return -EINVAL;
int numEventReceived = 0;
//LOGD("Sensor: Read events was called with count: %d", count);
if(compassEnabled)
{
//LOGD("Sensor: Compass is enabled, going to take care of it, count: %d", count);
ssize_t n = mInputReaderMagnetic.fill(data_compass_fd);
if (n < 0)
return n;
input_event const* event;
while ( count && mInputReaderMagnetic.readEvent(&event)) {
int type = event->type;
//Everything is ABS!
if (type == EV_ABS) {
float value = event->value;
if (event->code == EVENT_TYPE_MAGV_X) {
mPendingEvents[MagneticField].magnetic.x = (value * CONVERT_M_X);
} else if (event->code == EVENT_TYPE_MAGV_Y) {
mPendingEvents[MagneticField].magnetic.y = (value * CONVERT_M_Y);
} else if (event->code == EVENT_TYPE_MAGV_Z) {
mPendingEvents[MagneticField].magnetic.z = (value * CONVERT_M_Z);
}
}
else if (type == EV_SYN) {
mPendingEvents[MagneticField].timestamp = timevalToNano(event->time);
if (compassEnabled) {
//LOGD("Sensor: Compass was enabled, made a read and received a Sync");
compassDataReady = 1;
compassLastRead[0] = mPendingEvents[MagneticField].magnetic.x;
compassLastRead[1] = mPendingEvents[MagneticField].magnetic.y;
compassLastRead[2] = mPendingEvents[MagneticField].magnetic.z;
*data++ = mPendingEvents[MagneticField];
count--;
numEventReceived++;
}
} else {
LOGE("BoschYamaha: unknown event (type=%d, code=%d)", type, event->code);
}
mInputReaderMagnetic.next();
}
}
//LOGD("Sensor: Compass read quited, count: %d", count);
if(accelEnabled)
{
//LOGD("Sensor: Accel is enabled, going to take care of it, count: %d", count);
ssize_t n = mInputReaderAccel.fill(data_fd);
//LOGD("Sensor: Accel Input Reader was filled up: %d", count);
if (n < 0)
return n;
input_event const* event;
while (count && mInputReaderAccel.readEvent(&event)) {
int type = event->type;
if (type == EV_ABS) {
//LOGD("Sensor: Received one EV_ABS Event");
float value = event->value;
if (event->code == EVENT_TYPE_ACCEL_X) {
mPendingEvents[Accelerometer].acceleration.x = value * CONVERT_A_X;
} else if (event->code == EVENT_TYPE_ACCEL_Y) {
mPendingEvents[Accelerometer].acceleration.y = value * CONVERT_A_Y;
} else if (event->code == EVENT_TYPE_ACCEL_Z) {
mPendingEvents[Accelerometer].acceleration.z = value * CONVERT_A_Z;
}
} else if (type == EV_SYN) {
mPendingEvents[Accelerometer].timestamp = timevalToNano(event->time);
if (accelEnabled) {
//LOGD("Sensor: Accel was enabled, made a read and received a Sync");
accelDataReady = 1;
accelLastRead[0] = mPendingEvents[Accelerometer].acceleration.x;
accelLastRead[1] = mPendingEvents[Accelerometer].acceleration.y;
accelLastRead[2] = mPendingEvents[Accelerometer].acceleration.z;
*data++ = mPendingEvents[Accelerometer];
count--;
numEventReceived++;
}
}
else {
LOGE("BoschYamaha: unknown event (type=%d, code=%d)", type, event->code);
}
mInputReaderAccel.next();
}
}
//LOGD("Sensor: Accel read quited, count: %d, acceldata Ready: %d, compassData Ready = %d", count, accelDataReady, compassDataReady);
if( (accelDataReady == 1) && (compassDataReady == 1)){
//LOGD("BoschYamaha: Going to Process Orientation Data");
accelDataReady = 0;
compassDataReady = 0;
processOrientation();
*data++ = mPendingEvents[Orientation];
numEventReceived++;
}
return numEventReceived;
}
int BoschYamaha::processOrientation(){
float matrix[9], euler[3];
int rt, i;
rt = get_rotation_matrix(accelLastRead, compassLastRead, matrix);
if (rt < 0) {
for (i = 0; i < 3; i++) {
euler[i] = 0;
}
}
else {
get_euler(matrix, euler);
}
//LOGD("BoschYamaha: azimuth is %d", (int)(euler[0]));
//LOGD("BoschYamaha: pitch is %d", (int)(euler[1]));
//LOGD("BoschYamaha: roll is %d", (int)(euler[2]));
//Use CONVERT_O_A
mPendingEvents[Orientation].orientation.azimuth = (int)(euler[0]);
mPendingEvents[Orientation].orientation.pitch = (int)(euler[1]);
mPendingEvents[Orientation].orientation.roll = (int)(euler[2]);
//mPendingEvents[Orientation].orientation.status = uint8_t(value & SENSOR_STATE_MASK);
return 1;
}
float BoschYamaha::calc_intensity(float x, float y, float z)
{
return sqrt(x*x + y*y + z*z);
}
int BoschYamaha::get_rotation_matrix(const float *gsdata, const float *msdata, float *matrix)
{
float m_intensity, g_intensity, a_intensity, b_intensity;
float gdata[3], mdata[3], adata[3], bdata[3];
int i;
if (gsdata == NULL || msdata == NULL || matrix == NULL) {
return -1;
}
g_intensity = calc_intensity(gsdata[0], gsdata[1], gsdata[2]);
m_intensity = calc_intensity(msdata[0], msdata[1], msdata[2]);
if (g_intensity == 0 || m_intensity == 0) {
return -1;
}
for (i = 0; i < 3; i++) {
gdata[i] = -gsdata[i] / g_intensity;
mdata[i] = msdata[i] / m_intensity;
}
adata[0] = (gdata[1] * mdata[2] - gdata[2] * mdata[1]);
adata[1] = (gdata[2] * mdata[0] - gdata[0] * mdata[2]);
adata[2] = (gdata[0] * mdata[1] - gdata[1] * mdata[0]);
a_intensity = calc_intensity(adata[0], adata[1], adata[2]);
if (a_intensity == 0) {
return -1;
}
for (i = 0; i < 3; i++) {
adata[i] /= a_intensity;
}
bdata[0] = (adata[1] * gdata[2] - adata[2] * gdata[1]);
bdata[1] = (adata[2] * gdata[0] - adata[0] * gdata[2]);
bdata[2] = (adata[0] * gdata[1] - adata[1] * gdata[0]);
b_intensity = calc_intensity(bdata[0], bdata[1], bdata[2]);
if (b_intensity == 0) {
return -1;
}
for (i = 0; i < 3; i++) {
bdata[i] /= b_intensity;
}
matrix[0] = adata[0];
matrix[1] = adata[1];
matrix[2] = adata[2];
matrix[3] = bdata[0];
matrix[4] = bdata[1];
matrix[5] = bdata[2];
matrix[6] = -gdata[0];
matrix[7] = -gdata[1];
matrix[8] = -gdata[2];
return 0;
}
int BoschYamaha::get_euler(const float *matrix, float *euler)
{
float m11, m12;
float m21, m22;
float m31, m32, m33;
float yaw = 0, roll = 0, pitch = 0;
if (matrix == NULL || euler == NULL) {
return -1;
}
m11 = matrix[0];
m12 = matrix[1];
m21 = matrix[3];
m22 = matrix[4];
m31 = matrix[6];
m32 = matrix[7];
m33 = matrix[8];
yaw = atan2(m12-m21, m11+m22);
pitch = -asin(m32);
roll = asin(m31);
yaw *= 180.0 / M_PI;
pitch *= 180.0 / M_PI;
roll *= 180.0 / M_PI;
if (m33 < 0) {
pitch = -180 - pitch;
if (pitch < -180) {
pitch += 360;
}
}
if (yaw < 0) {
yaw += 360.0f;
}
euler[0] = (float)(int)yaw; /* yaw */
euler[1] = (float)(int)pitch; /* pitch */
euler[2] = (float)(int)roll; /* roll */
return 0;
}
|