summaryrefslogtreecommitdiff
path: root/libsensors_iio/software/simple_apps/common/gestureMenu.c
blob: 2a9487c3e2f485bf088642b9c399a73b1dc7167e (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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*
 $License:
    Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
 $
 */

/******************************************************************************
 * $Id: gestureMenu.c 5705 2011-06-28 19:32:29Z nroyer $
 *****************************************************************************/

#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stddef.h>
#include <math.h>
#include <string.h>

#include "ml.h"
#include "mlmath.h"
#include "gesture.h"
#include "orientation.h"
#include "gestureMenu.h"
#include "fifo.h"

#undef MPL_LOG_TAG
#define MPL_LOG_TAG "gest"
#include "log.h"
#include "mldl_cfg.h"

static unsigned long sensors[] = {
    INV_NINE_AXIS,
    INV_THREE_AXIS_GYRO,
    INV_DMP_PROCESSOR | INV_THREE_AXIS_ACCEL,
    INV_THREE_AXIS_ACCEL,
    INV_DMP_PROCESSOR | INV_THREE_AXIS_COMPASS,
    INV_THREE_AXIS_COMPASS,
    INV_SIX_AXIS_GYRO_ACCEL,
    INV_DMP_PROCESSOR | INV_SIX_AXIS_ACCEL_COMPASS,
    INV_SIX_AXIS_ACCEL_COMPASS,
};

static char *sensors_string[] = {
    "INV_NINE_AXIS",
    "INV_THREE_AXIS_GYRO",
    "INV_DMP_PROCESSOR | INV_THREE_AXIS_ACCEL",
    "INV_THREE_AXIS_ACCEL",
    "INV_DMP_PROCESSOR | INV_THREE_AXIS_COMPASS",
    "INV_THREE_AXIS_COMPASS",
    "INV_SIX_AXIS_GYRO_ACCEL",
    "INV_DMP_PROCESSOR | INV_SIX_AXIS_ACCEL_COMPASS",
    "INV_SIX_AXIS_ACCEL_COMPASS",
};

/**
 * Prints the menu with the current thresholds
 *
 * @param params The parameters to print
 */
void PrintGestureMenu(tGestureMenuParams const * const params)
{
    MPL_LOGI("Press h at any time to re-display this menu\n");
    MPL_LOGI("TAP PARAMETERS:\n");
    MPL_LOGI("    Use LEFT and RIGHT arrows to adjust Tap Time     \n\n");
    MPL_LOGI("    j          : Increase X threshold     : %5d\n",
             params->xTapThreshold);
    MPL_LOGI("    J (Shift-j): Decrease X threshold\n");
    MPL_LOGI("    k          : Increase Y threshold     : %5d\n",
             params->yTapThreshold);
    MPL_LOGI("    K (Shift-k): Decrease Y threshold\n");
    MPL_LOGI("    i          : Increase Z threshold     : %5d\n",
             params->zTapThreshold);
    MPL_LOGI("    I (Shift-i): Decrease Z threshold\n");
    MPL_LOGI("    l          : Increase tap time        : %5d\n",
             params->tapTime);
    MPL_LOGI("    L (Shift-l): Decrease tap time\n");
    MPL_LOGI("    o          : Increase next tap time   : %5d\n",
             params->nextTapTime);
    MPL_LOGI("    O (Shift-o): Increase next tap time\n");
    MPL_LOGI("    u          : Increase max Taps        : %5d\n",
             params->maxTaps);
    MPL_LOGI("    U (Shift-u): Increase max Taps\n");

    MPL_LOGI("SHAKE PARAMETERS:\n");
    MPL_LOGI("    x          : Increase X threshold     : %5d\n",
             params->xShakeThresh);
    MPL_LOGI("    X (Shift-x): Decrease X threshold\n");
    MPL_LOGI("    y          : Increase Y threshold     : %5d\n",
             params->yShakeThresh);
    MPL_LOGI("    Y (Shift-y): Decrease Y threshold\n");
    MPL_LOGI("    z          : Increase Z threshold     : %5d\n",
             params->zShakeThresh);
    MPL_LOGI("    Z (Shift-z): Decrease Z threshold\n");
    MPL_LOGI("    s          : Toggle Shake Function    : %5d\n",
             params->shakeFunction);
    MPL_LOGI("    t          : Increase Shake Time      : %5d\n",
             params->shakeTime);
    MPL_LOGI("    T (Shift-T): Decrease Shake Time\n");
    MPL_LOGI("    n          : Increase Next Shake Time : %5d\n",
             params->nextShakeTime);
    MPL_LOGI("    N (Shift-n): Decrease Next Shake Time\n");
    MPL_LOGI("    m          : Increase max Shakes      : %5d\n",
             params->maxShakes);
    MPL_LOGI("    M (Shift-m): Decrease max Shakes\n");
    MPL_LOGI("SNAP  PARAMETERS:\n");
    MPL_LOGI("    p          : Increase Pitch threshold : %5d\n",
             params->xSnapThresh);
    MPL_LOGI("    P (Shift-p): Decrease Pitch threshold\n");
    MPL_LOGI("    r          : Increase Roll  threshold : %5d\n",
             params->ySnapThresh);
    MPL_LOGI("    R (Shift-r): Decrease Roll  threshold\n");
    MPL_LOGI("    a          : Increase yAw   threshold : %5d\n",
             params->zSnapThresh);
    MPL_LOGI("    A (Shift-a): Decrease yAw   threshold\n");
    MPL_LOGI("YAW ROTATION PARAMETERS:\n");
    MPL_LOGI("    e          : Increase yaW Rotate time : %5d\n",
             params->yawRotateTime);
    MPL_LOGI("    E (Shift-r): Decrease yaW Rotate time\n");
    MPL_LOGI("    w          : Increase yaW Rotate threshold : %5d\n",
             params->yawRotateThreshold);
    MPL_LOGI("    W (Shift-w): Decrease yaW Rotate threshold\n");
    MPL_LOGI("ORIENTATION PARAMETER:\n");
    MPL_LOGI("    d          : Increase orientation angle threshold : %5f\n",
             params->orientationThreshold);
    MPL_LOGI("    D (Shift-d): Decrease orientation angle threshold\n");
    MPL_LOGI("FIFO RATE:\n");
    MPL_LOGI("    f          : Increase fifo divider    : %5d\n",
             inv_get_fifo_rate());
    MPL_LOGI("    F (Shift-f): Decrease fifo divider\n");
    MPL_LOGI("REQUESTED SENSORS:\n");
    MPL_LOGI("    S (Shift-s): Toggle in use sensors : %s\n",
             sensors_string[params->sensorsIndex]);
    MPL_LOGI("    F (Shift-f): Decrease fifo divider\n");

    /* V,v, B,b, Q,q, C,c, G,g, are available letters upper and lowercase */
    /* S is available */

    MPL_LOGI("\n\n");
}

/**
 * Handles a keyboard input and updates an appropriate threshold, prints then
 * menu or returns false if the character is not processed.
 *
 * @param params The parameters to modify if the thresholds are updated
 * @param ch     The input character
 *
 * @return        true if the character was processed, false otherwise
 */
inv_error_t GestureMenuProcessChar(tGestureMenuParams * const params, char ch)
{
    int result = INV_SUCCESS;
    /* Dynamic keyboard processing */

    switch (ch) {
    case 'j':
        params->xTapThreshold += 20;
        // Intentionally fall through
    case 'J':  {
        params->xTapThreshold -= 10;
        if (params->xTapThreshold < 0) params->xTapThreshold = 0;
        result = inv_set_tap_threshold(INV_TAP_AXIS_X, params->xTapThreshold);
        if (INV_SUCCESS != result) {
            MPL_LOGE("MLSetTapThresh returned :%d\n", result);
        }
        MPL_LOGI("inv_set_tap_threshold(INV_TAP_AXIS_X, %d)\n",
                 params->xTapThreshold);
    } break;
    case 'k':
        params->yTapThreshold += 20;
        // Intentionally fall through
    case 'K':  {
        params->yTapThreshold -= 10;
        if (params->yTapThreshold < 0) params->yTapThreshold = 0;
        result = inv_set_tap_threshold(INV_TAP_AXIS_Y, params->yTapThreshold);
        if (INV_SUCCESS != result) {
            MPL_LOGE("MLSetTapThresh returned :%d\n", result);
        }
        MPL_LOGI("inv_set_tap_threshold(INV_TAP_AXIS_Y, %d)\n",
                 params->yTapThreshold);
    } break;
    case 'i':
        params->zTapThreshold += 20;
        // Intentionally fall through
    case 'I':  {
        params->zTapThreshold -= 10;
        if (params->zTapThreshold < 0) params->zTapThreshold = 0;
        result = inv_set_tap_threshold(INV_TAP_AXIS_Z, params->zTapThreshold);
        if (INV_SUCCESS != result) {
            MPL_LOGE("MLSetTapThresh returned :%d\n", result);
        }
        MPL_LOGI("inv_set_tap_threshold(INV_TAP_AXIS_Z, %d)\n",
                 params->zTapThreshold);
    } break;

    case  'l':
        params->tapTime += 20;
        // Intentionally fall through
    case 'L':  {
        params->tapTime -= 10;
        if (params->tapTime < 0) params->tapTime = 0;
        result = inv_set_next_tap_time(params->tapTime);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_next_tap_time returned :%d\n", result);
        }
        MPL_LOGI("inv_set_next_tap_time(%d)\n", params->tapTime);
    } break;
    case  'o':
        params->nextTapTime += 20;
        // Intentionally fall through
    case 'O':  {
        params->nextTapTime -= 10;
        if (params->nextTapTime < 0) params->nextTapTime = 0;
        result = MLSetNextTapTime(params->nextTapTime);
        if (INV_SUCCESS != result) {
            MPL_LOGE("MLSetNextTapTime returned :%d\n", result);
        }
        MPL_LOGI("MLSetNextTapTime(%d)\n", params->nextTapTime);
    } break;
    case  'u':
        params->maxTaps += 2;
        // Intentionally fall through
    case 'U':  {
        params->maxTaps -= 1;
        if (params->maxTaps < 0) params->maxTaps = 0;
        result = inv_set_max_taps(params->maxTaps);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_max_taps returned :%d\n", result);
        }
        MPL_LOGI("inv_set_max_taps(%d)\n", params->maxTaps);
    } break;
    case 's': {
        int shakeParam;
        params->shakeFunction = (params->shakeFunction + 1) % 2;
        switch (params->shakeFunction)
        {
        case 0:
            shakeParam = INV_NO_RETRACTION;
            MPL_LOGE("inv_set_shake_func(INV_NO_RETRACTION)\n");
            break;
        case 1:
            shakeParam = INV_RETRACTION;
            MPL_LOGI("inv_set_shake_func(INV_RETRACTION)\n");
            break;
        };
        result = inv_set_shake_func(shakeParam);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_shake_func returned :%d\n", result);
        }
    } break;
    case 'x':
        params->xShakeThresh += 200;
        // Intentionally fall through
    case 'X': {
        params->xShakeThresh -= 100;
        result = inv_set_shake_thresh(INV_PITCH_SHAKE, params->xShakeThresh);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_shake_thresh returned :%d\n", result);
        }
        MPL_LOGI("inv_set_shake_thresh(INV_PITCH_SHAKE, %d)\n", params->xShakeThresh);
    } break;
    case 'y':
        params->yShakeThresh += 200;
        // Intentionally fall through
    case 'Y': {
        params->yShakeThresh -= 100;
        result = inv_set_shake_thresh(INV_ROLL_SHAKE, params->yShakeThresh);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_shake_thresh returned :%d\n", result);
        }
        MPL_LOGI("inv_set_shake_thresh(INV_ROLL_SHAKE, %d)\n", params->yShakeThresh);
    } break;
    case 'z':
        params->zShakeThresh += 200;
        // Intentionally fall through
    case 'Z':{
        params->zShakeThresh -= 100;
        result = inv_set_shake_thresh(INV_YAW_SHAKE, params->zShakeThresh);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_shake_thresh returned :%d\n", result);
        }
        MPL_LOGI("inv_set_shake_thresh(INV_YAW_SHAKE, %d)\n",params->zShakeThresh);
    } break;
    case 'r':
        params->ySnapThresh += 20;
        // Intentionally fall through
    case 'R': {
        params->ySnapThresh -= 10;
        result = inv_set_hard_shake_thresh(INV_ROLL_SHAKE, params->ySnapThresh);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_hard_shake_thresh returned :%d\n", result);
        }
        MPL_LOGI("inv_set_hard_shake_thresh(INV_ROLL_SHAKE, %d)\n",params->ySnapThresh);
    } break;
    case 'p':
        params->xSnapThresh += 20;
        // Intentionally fall through
    case 'P': {
        params->xSnapThresh -= 10;
        result = inv_set_hard_shake_thresh(INV_PITCH_SHAKE, params->xSnapThresh);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_hard_shake_thresh returned :%d\n", result);
        }
        MPL_LOGI("inv_set_hard_shake_thresh(INV_PITCH_SHAKE, %d)\n",
                 params->xSnapThresh);
    } break;
    case 'a':
        params->zSnapThresh += 20;
    case 'A': {
        params->zSnapThresh -= 10;
        result = inv_set_hard_shake_thresh(INV_YAW_SHAKE, params->zSnapThresh);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_hard_shake_thresh returned :%d\n", result);
        }
        MPL_LOGI("inv_set_hard_shake_thresh(INV_YAW_SHAKE, %d)\n",params->zSnapThresh);
    } break;

    case 't':
        params->shakeTime += 20;
    case 'T':{
        params->shakeTime -= 10;
        result = inv_set_shake_time(params->shakeTime);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_shake_time returned :%d\n", result);
        }
        MPL_LOGI("inv_set_shake_time(%d)\n", params->shakeTime);
    } break;
    case 'n':
        params->nextShakeTime += 20;
    case 'N':{
        params->nextShakeTime -= 10;
        result = inv_set_next_shake_time(params->nextShakeTime);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_next_shake_time returned :%d\n", result);
        }
        MPL_LOGI("inv_set_next_shake_time(%d)\n", params->nextShakeTime);
    } break;
    case 'm':
        params->maxShakes += 2;
    case 'M':{
        params->maxShakes -= 1;
        result = inv_set_max_shakes(INV_SHAKE_ALL, params->maxShakes);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_max_shakes returned :%d\n", result);
        }
        MPL_LOGI("inv_set_max_shakes(%d)\n", params->maxShakes);
    } break;
    case 'e':
        params->yawRotateTime += 20;
    case 'E':{
        params->yawRotateTime -= 10;
        result = inv_set_yaw_rotate_time(params->yawRotateTime);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_yaw_rotate_time returned :%d\n", result);
        }
        MPL_LOGI("inv_set_yaw_rotate_time(%d)\n", params->yawRotateTime);
    } break;
    case 'w':
        params->yawRotateThreshold += 2;
    case 'W':{
        params->yawRotateThreshold -= 1;
        result = inv_set_yaw_rotate_thresh(params->yawRotateThreshold);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_yaw_rotate_thresh returned :%d\n", result);
        }
        MPL_LOGI("inv_set_yaw_rotate_thresh(%d)\n", params->yawRotateThreshold);
    } break;
    case 'c':
        params->shakeRejectValue += 0.20f;
    case 'C':{
        params->shakeRejectValue -= 0.10f;
        result = inv_set_tap_shake_reject(params->shakeRejectValue);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_tap_shake_reject returned :%d\n", result);
        }
        MPL_LOGI("inv_set_tap_shake_reject(%f)\n", params->shakeRejectValue);
    } break;
    case 'd':
        params->orientationThreshold += 10;
    case 'D':{
        params->orientationThreshold -= 5;
        if (params->orientationThreshold > 90) {
            params->orientationThreshold = 90;
        }

        if (params->orientationThreshold < 0 ) {
            params->orientationThreshold = 0;
        }

        result = inv_set_orientation_thresh(params->orientationThreshold,
                                           5, 80,
                                           INV_X_AXIS | INV_Y_AXIS | INV_Z_AXIS);
        if (INV_SUCCESS != result) {
            MPL_LOGE("inv_set_orientation_thresh returned :%d\n", result);
        }
        MPL_LOGI("inv_set_orientation_thresh(%f, %d, %d,"
                 " INV_X_AXIS | INV_Y_AXIS | INV_Z_AXIS)\n",
                 params->orientationThreshold, 5, 80);
    } break;
    case 'f':
        result = inv_set_fifo_rate(inv_get_fifo_rate() + 1);
        MPL_LOGI("inv_set_fifo_rate(%d)\n",inv_get_fifo_rate());
        break;
    case 'F':
    {
        unsigned short newRate = inv_get_fifo_rate();
        if (newRate > 0)
            newRate--;
        result = inv_set_fifo_rate(newRate);
        MPL_LOGI("inv_set_fifo_rate(%d)\n",inv_get_fifo_rate());
        break;
    }
    case 'S':
        params->sensorsIndex++;
        if (params->sensorsIndex >= ARRAY_SIZE(sensors)) {
            params->sensorsIndex = 0;
        }
        result = inv_set_mpu_sensors(
            sensors[params->sensorsIndex] & params->available_sensors);
        if (result) {
            LOG_RESULT_LOCATION(result);
            return result;
        }
        MPL_LOGI("%d = inv_set_mpu_sensors(%s)\n", result,
                 sensors_string[params->sensorsIndex]);
        break;
    case 'h':
    case 'H': {
        PrintGestureMenu(params);
    } break;
    default: {
        result = INV_ERROR;
    } break;
    };
    return result;
}

/**
 * Initializes the tGestureMenuParams to a set of defaults.
 *
 * @param params The parameters to initialize.
 */
void GestureMenuSetDefaults(tGestureMenuParams * const params)
{
    params->xTapThreshold           = 100;
    params->yTapThreshold           = 100;
    params->zTapThreshold           = 100;
    params->tapTime                 = 100;
    params->nextTapTime             = 600;
    params->maxTaps                 = 2;
    params->shakeRejectValue        = 0.8f;
    params->xShakeThresh            = 750;
    params->yShakeThresh            = 750;
    params->zShakeThresh            = 750;
    params->xSnapThresh             = 160;
    params->ySnapThresh             = 320;
    params->zSnapThresh             = 160;
    params->shakeTime               = 100;
    params->nextShakeTime           = 1000;
    params->shakeFunction           = 0;
    params->maxShakes               = 3;
    params->yawRotateTime           = 80;
    params->yawRotateThreshold      = 70;
    params->orientationThreshold    = 60;
    params->sensorsIndex            = 0;
    params->available_sensors       = INV_NINE_AXIS;
}

void GestureMenuSetAvailableSensors(tGestureMenuParams * const params,
                                    unsigned long available_sensors)
{
    params->available_sensors       = available_sensors;
}
/**
 * Call the appropriate MPL set threshold functions and checkes the error codes
 *
 * @param params The parametrs to use in setting the thresholds
 *
 * @return INV_SUCCESS or the first error code encountered.
 */
inv_error_t GestureMenuSetMpl(tGestureMenuParams const * const params)
{
    inv_error_t result = INV_SUCCESS;

    result = inv_set_tap_threshold(INV_TAP_AXIS_X, params->xTapThreshold);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_tap_threshold returned :%d\n", result);
        return result;
    }
    result = inv_set_tap_threshold(INV_TAP_AXIS_Y, params->yTapThreshold);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_tap_threshold returned :%d\n", result);
        return result;
    }
    result = inv_set_tap_threshold(INV_TAP_AXIS_Z, params->zTapThreshold);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_tap_threshold returned :%d\n", result);
        return result;
    }
    result = inv_set_next_tap_time(params->tapTime);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_next_tap_time returned :%d\n", result);
        return result;
    }
    result = MLSetNextTapTime(params->nextTapTime);
    if (INV_SUCCESS != result) {
        MPL_LOGE("MLSetNextTapTime returned :%d\n", result);
        return result;
    }
    result = inv_set_max_taps(params->maxTaps);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_max_taps returned :%d\n", result);
        return result;
    }
    result = inv_set_tap_shake_reject(params->shakeRejectValue);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_tap_shake_reject returned :%d\n", result);
        return result;
    }

    //Set up shake gesture
    result = inv_set_shake_func(params->shakeFunction);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_shake_func returned :%d\n", result);
        return result;
    }
    result = inv_set_shake_thresh(INV_ROLL_SHAKE, params->xShakeThresh);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_shake_thresh returned :%d\n", result);
        return result;
    }
    result = inv_set_shake_thresh(INV_PITCH_SHAKE, params->yShakeThresh);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_shake_thresh returned :%d\n", result);
        return result;
    }
    result = inv_set_shake_thresh(INV_YAW_SHAKE, params->zShakeThresh);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_shake_thresh returned :%d\n", result);
        return result;
    }
    result = inv_set_shake_time(params->shakeTime);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_shake_time returned :%d\n", result);
        return result;
    }
    result = inv_set_next_shake_time(params->nextShakeTime);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_next_shake_time returned :%d\n", result);
        return result;
    }
    result = inv_set_max_shakes(INV_SHAKE_ALL,params->maxShakes);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_max_shakes returned :%d\n", result);
        return result;
    }

    // Yaw rotate settings
    result = inv_set_yaw_rotate_time(params->yawRotateTime);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_yaw_rotate_time returned :%d\n", result);
        return result;
    }
    result = inv_set_yaw_rotate_thresh(params->yawRotateThreshold);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_yaw_rotate_thresh returned :%d\n", result);
        return result;
    }

    // Orientation settings
    result = inv_set_orientation_thresh(params->orientationThreshold, 5, 80,
                                       INV_X_AXIS | INV_Y_AXIS | INV_Z_AXIS);
    if (INV_SUCCESS != result) {
        MPL_LOGE("inv_set_orientation_thresh returned: %d\n", result);
        return result;
    }

    // Requested Sensors
    result = inv_set_mpu_sensors(
        sensors[params->sensorsIndex] & params->available_sensors);
    if (INV_SUCCESS != result) {
        MPL_LOGE("MLSetMPUSesnors returned: %d %lx\n", result,
                 sensors[params->sensorsIndex] & params->available_sensors);
        return result;
    }

    return INV_SUCCESS;
}

void PrintGesture(tGesture* gesture)
{
    float speed;
    char type[1024];
    switch (gesture->type)
    {
    case INV_TAP:
    {
        if (gesture->meta < 0) {
            snprintf(type,sizeof(type),"-");
        } else {
            snprintf(type,sizeof(type),"+");
        }

        switch (ABS(gesture->meta))
        {
        case 1:
            strcat(type,"X");
            break;
        case 2:
            strcat(type,"Y");
            break;
        case 3:
            strcat(type,"Z");
            break;
        default:
            strcat(type,"ERROR");
            break;
        };
        MPL_LOGI("TAP: %s  %2d, X: %6d Y: %6d Z: %6d XY: %6.2f, YZ: %6.2f, XZ: %6.2f\n",
                 type,
                 gesture->num,
                 gesture->strength,
                 gesture->speed,
                 gesture->reserved,
                 (180 / M_PI) * atan2(
                    (float)gesture->strength, (float)gesture->speed),
                 (180 / M_PI) * atan2(
                    (float)gesture->speed, (float)gesture->reserved),
                 (180 / M_PI) * atan2(
                    (float)gesture->strength, (float)gesture->reserved)
            );
    }
    break;
    case INV_ROLL_SHAKE:
    case INV_PITCH_SHAKE:
    case INV_YAW_SHAKE:
    {
        if (gesture->strength){
            snprintf(type, sizeof(type), "Snap : ");
        } else {
            snprintf(type, sizeof(type), "Shake: ");
        }

        if (gesture->meta==0) {
            strcat(type, "+");
        } else {
            strcat(type, "-");
        }

        if (gesture->type == INV_ROLL_SHAKE) {
            strcat(type, "Roll  ");
        } else if (gesture->type == INV_PITCH_SHAKE) {
            strcat(type, "Pitch ");
        } else if (gesture->type == INV_YAW_SHAKE) {
            strcat(type, "Yaw   ");
        }

        speed = (float)gesture->speed +
            (float)(gesture->reserved / (float)(1 << 16));
        MPL_LOGI("%s:%3d (speed: %8.2f)\n",type, gesture->num, speed);
    }
    break;
    case INV_YAW_IMAGE_ROTATE:
    {
        if (gesture->meta == 0) {
            snprintf(type, sizeof(type), "Positive ");
        } else {
            snprintf(type, sizeof(type), "Negative ");
        }
        MPL_LOGI("%s Yaw Image Rotation\n", type);
    }
    break;
    default:
        MPL_LOGE("Unknown Gesture received\n");
        break;
    }
}

/**
 * Prints the new or current orientation using MPL_LOGI and remembers the last
 * orientation to print orientation flips.
 *
 * @param orientation the new or current orientation.  0 to reset.
 */
void PrintOrientation(unsigned short orientation)
{
    // Determine if it was a flip
    static int sLastOrientation = 0;
    int flip = orientation | sLastOrientation;

    if ((INV_X_UP | INV_X_DOWN) == flip) {
        MPL_LOGI("Flip about the X Axis: \n");
    } else if ((INV_Y_UP | INV_Y_DOWN) == flip) {
        MPL_LOGI("Flip about the Y axis: \n");
    } else if ((INV_Z_UP | INV_Z_DOWN) == flip) {
        MPL_LOGI("Flip about the Z axis: \n");
    }
    sLastOrientation = orientation;

    switch (orientation) {
    case INV_X_UP:
        MPL_LOGI("X Axis is up\n");
        break;
    case INV_X_DOWN:
        MPL_LOGI("X Axis is down\n");
        break;
    case INV_Y_UP:
        MPL_LOGI("Y Axis is up\n");
        break;
    case INV_Y_DOWN:
        MPL_LOGI("Y Axis is down\n");
        break;
    case INV_Z_UP:
        MPL_LOGI("Z Axis is up\n");
        break;
    case INV_Z_DOWN:
        MPL_LOGI("Z Axis is down\n");
        break;
    case 0:
        break; /* Not an error.  Resets sLastOrientation */
    default:
        MPL_LOGE("%s: Unreconized orientation %hx\n", __func__, orientation);
        break;
    }
}