summaryrefslogtreecommitdiffstats
path: root/libsensors/mlsdk/mllite/mlcontrol.c
blob: 278438af30cd67a7608ae042244d0b3bd776f62d (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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/*
 $License:
   Copyright 2011 InvenSense, Inc.

 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.
  $
 */

/*******************************************************************************
 *
 * $Id: mlcontrol.c 5641 2011-06-14 02:10:02Z mcaramello $
 *
 *******************************************************************************/

/**
 *  @defgroup   CONTROL
 *  @brief      Motion Library - Control Engine.
 *              The Control Library processes gyroscopes, accelerometers, and
 *              compasses to provide control signals that can be used in user
 *              interfaces.
 *              These signals can be used to manipulate objects such as documents,
 *              images, cursors, menus, etc.
 *
 *  @{
 *      @file   mlcontrol.c
 *      @brief  The Control Library.
 *
 */

/* ------------------ */
/* - Include Files. - */
/* ------------------ */

#include "mltypes.h"
#include "mlinclude.h"
#include "mltypes.h"
#include "ml.h"
#include "mlos.h"
#include "mlsl.h"
#include "mldl.h"
#include "mlcontrol.h"
#include "dmpKey.h"
#include "mlstates.h"
#include "mlFIFO.h"
#include "string.h"

/* - Global Vars. - */
struct control_params cntrl_params = {
    {
     MLCTRL_SENSITIVITY_0_DEFAULT,
     MLCTRL_SENSITIVITY_1_DEFAULT,
     MLCTRL_SENSITIVITY_2_DEFAULT,
     MLCTRL_SENSITIVITY_3_DEFAULT}, // sensitivity
    MLCTRL_FUNCTIONS_DEFAULT,   // functions
    {
     MLCTRL_PARAMETER_ARRAY_0_DEFAULT,
     MLCTRL_PARAMETER_ARRAY_1_DEFAULT,
     MLCTRL_PARAMETER_ARRAY_2_DEFAULT,
     MLCTRL_PARAMETER_ARRAY_3_DEFAULT}, // parameterArray
    {
     MLCTRL_PARAMETER_AXIS_0_DEFAULT,
     MLCTRL_PARAMETER_AXIS_1_DEFAULT,
     MLCTRL_PARAMETER_AXIS_2_DEFAULT,
     MLCTRL_PARAMETER_AXIS_3_DEFAULT},  // parameterAxis
    {
     MLCTRL_GRID_THRESHOLD_0_DEFAULT,
     MLCTRL_GRID_THRESHOLD_1_DEFAULT,
     MLCTRL_GRID_THRESHOLD_2_DEFAULT,
     MLCTRL_GRID_THRESHOLD_3_DEFAULT},  // gridThreshold
    {
     MLCTRL_GRID_MAXIMUM_0_DEFAULT,
     MLCTRL_GRID_MAXIMUM_1_DEFAULT,
     MLCTRL_GRID_MAXIMUM_2_DEFAULT,
     MLCTRL_GRID_MAXIMUM_3_DEFAULT},    // gridMaximum
    MLCTRL_GRID_CALLBACK_DEFAULT    // gridCallback
};

/* - Extern Vars. - */
struct control_obj cntrl_obj;
extern const unsigned char *dmpConfig1;

/* -------------- */
/* - Functions. - */
/* -------------- */

/**
 *  @brief  inv_set_control_sensitivity is used to set the sensitivity for a control
 *          signal.
 *
 *  @pre    inv_dmp_open() Must be called with MLDmpDefaultOpen() or
 *          inv_open_low_power_pedometer().
 *
 *  @param controlSignal    Indicates which control signal is being modified.
 *                          Must be one of:
 *                          - INV_CONTROL_1,
 *                          - INV_CONTROL_2,
 *                          - INV_CONTROL_3 or
 *                          - INV_CONTROL_4.
 *
 *  @param sensitivity      The sensitivity of the control signal.
 *
 *  @return error code
 */
inv_error_t inv_set_control_sensitivity(unsigned short controlSignal,
                                        long sensitivity)
{
    INVENSENSE_FUNC_START;
    unsigned char regs[2];
    long finalSens = 0;
    inv_error_t result;

    if (inv_get_state() < INV_STATE_DMP_OPENED)
        return INV_ERROR_SM_IMPROPER_STATE;

    finalSens = sensitivity * 100;
    if (finalSens > 16384) {
        finalSens = 16384;
    }
    regs[0] = (unsigned char)(finalSens / 256);
    regs[1] = (unsigned char)(finalSens % 256);
    switch (controlSignal) {
    case INV_CONTROL_1:
        result = inv_set_mpu_memory(KEY_D_0_224, 2, regs);
        if (result) {
            LOG_RESULT_LOCATION(result);
            return result;
        }
        cntrl_params.sensitivity[0] = (unsigned short)sensitivity;
        break;
    case INV_CONTROL_2:
        result = inv_set_mpu_memory(KEY_D_0_228, 2, regs);
        if (result) {
            LOG_RESULT_LOCATION(result);
            return result;
        }
        cntrl_params.sensitivity[1] = (unsigned short)sensitivity;
        break;
    case INV_CONTROL_3:
        result = inv_set_mpu_memory(KEY_D_0_232, 2, regs);
        if (result) {
            LOG_RESULT_LOCATION(result);
            return result;
        }
        cntrl_params.sensitivity[2] = (unsigned short)sensitivity;
        break;
    case INV_CONTROL_4:
        result = inv_set_mpu_memory(KEY_D_0_236, 2, regs);
        if (result) {
            LOG_RESULT_LOCATION(result);
            return result;
        }
        cntrl_params.sensitivity[3] = (unsigned short)sensitivity;
        break;
    default:
        break;
    }
    if (finalSens != sensitivity * 100) {
        return INV_ERROR_INVALID_PARAMETER;
    } else {
        return INV_SUCCESS;
    }
}

/**
 *  @brief  inv_set_control_func allows the user to choose how the sensor data will
 *          be processed in order to provide a control parameter.
 *          inv_set_control_func allows the user to choose which control functions
 *          will be incorporated in the sensor data processing.
 *          The control functions are:
 *          - INV_GRID
 *          Indicates that the user will be controlling a system that
 *          has discrete steps, such as icons, menu entries, pixels, etc.
 *          - INV_SMOOTH
 *          Indicates that noise from unintentional motion should be filtered out.
 *          - INV_DEAD_ZONE
 *          Indicates that a dead zone should be used, below which sensor
 *          data is set to zero.
 *          - INV_HYSTERESIS
 *          Indicates that, when INV_GRID is selected, hysteresis should
 *          be used to prevent the control signal from switching rapidly across
 *          elements of the grid.
 *
 *  @pre    inv_dmp_open() Must be called with MLDmpDefaultOpen() or
 *          inv_open_low_power_pedometer().
 *
 *  @param  function    Indicates what functions will be used.
 *                      Can be a bitwise OR of several values.
 *
 *  @return Zero if the command is successful; an ML error code otherwise.
 */
inv_error_t inv_set_control_func(unsigned short function)
{
    INVENSENSE_FUNC_START;
    unsigned char regs[8] = { DINA06, DINA26,
        DINA46, DINA66,
        DINA0E, DINA2E,
        DINA4E, DINA6E
    };
    unsigned char i;
    inv_error_t result;

    if (inv_get_state() < INV_STATE_DMP_OPENED)
        return INV_ERROR_SM_IMPROPER_STATE;

    if ((function & INV_SMOOTH) == 0) {
        for (i = 0; i < 8; i++) {
            regs[i] = DINA80 + 3;
        }
    }
    result = inv_set_mpu_memory(KEY_CFG_4, 8, regs);
    if (result) {
        LOG_RESULT_LOCATION(result);
        return result;
    }
    cntrl_params.functions = function;
    result = inv_set_dead_zone();

    return result;
}

/**
 *  @brief  inv_get_control_signal is used to get the current control signal with
 *          high precision.
 *          inv_get_control_signal is used to acquire the current data of a control signal.
 *          If INV_GRID is being used, inv_get_grid_number will probably be preferrable.
 *
 *  @param  controlSignal   Indicates which control signal is being queried.
 *          Must be one of:
 *          - INV_CONTROL_1,
 *          - INV_CONTROL_2,
 *          - INV_CONTROL_3 or
 *          - INV_CONTROL_4.
 *
 *  @param  reset   Indicates whether the control signal should be reset to zero.
 *                  Options are INV_RESET or INV_NO_RESET
 *  @param  data    A pointer to the current control signal data.
 *
 *  @return Zero if the command is successful; an ML error code otherwise.
 */
inv_error_t inv_get_control_signal(unsigned short controlSignal,
                                   unsigned short reset, long *data)
{
    INVENSENSE_FUNC_START;

    if (inv_get_state() != INV_STATE_DMP_STARTED)
        return INV_ERROR_SM_IMPROPER_STATE;

    switch (controlSignal) {
    case INV_CONTROL_1:
        *data = cntrl_obj.controlInt[0];
        if (reset == INV_RESET) {
            cntrl_obj.controlInt[0] = 0;
        }
        break;
    case INV_CONTROL_2:
        *data = cntrl_obj.controlInt[1];
        if (reset == INV_RESET) {
            cntrl_obj.controlInt[1] = 0;
        }
        break;
    case INV_CONTROL_3:
        *data = cntrl_obj.controlInt[2];
        if (reset == INV_RESET) {
            cntrl_obj.controlInt[2] = 0;
        }
        break;
    case INV_CONTROL_4:
        *data = cntrl_obj.controlInt[3];
        if (reset == INV_RESET) {
            cntrl_obj.controlInt[3] = 0;
        }
        break;
    default:
        break;
    }
    return INV_SUCCESS;
}

/**
 *  @brief  inv_get_grid_num is used to get the current grid location for a certain
 *          control signal.
 *          inv_get_grid_num is used to acquire the current grid location.
 *
 *  @pre    inv_dmp_open() Must be called with MLDmpDefaultOpen() or
 *          inv_open_low_power_pedometer().
 *
 *  @param  controlSignal   Indicates which control signal is being queried.
 *          Must be one of:
 *          - INV_CONTROL_1,
 *          - INV_CONTROL_2,
 *          - INV_CONTROL_3 or
 *          - INV_CONTROL_4.
 *
 *  @param  reset   Indicates whether the control signal should be reset to zero.
 *                  Options are INV_RESET or INV_NO_RESET
 *  @param  data    A pointer to the current grid number.
 *
 *  @return Zero if the command is successful; an ML error code otherwise.
 */

inv_error_t inv_get_grid_num(unsigned short controlSignal, unsigned short reset,
                             long *data)
{
    INVENSENSE_FUNC_START;

    if (inv_get_state() != INV_STATE_DMP_STARTED)
        return INV_ERROR_SM_IMPROPER_STATE;

    switch (controlSignal) {
    case INV_CONTROL_1:
        *data = cntrl_obj.gridNum[0];
        if (reset == INV_RESET) {
            cntrl_obj.gridNum[0] = 0;
        }
        break;
    case INV_CONTROL_2:
        *data = cntrl_obj.gridNum[1];
        if (reset == INV_RESET) {
            cntrl_obj.gridNum[1] = 0;
        }
        break;
    case INV_CONTROL_3:
        *data = cntrl_obj.gridNum[2];
        if (reset == INV_RESET) {
            cntrl_obj.gridNum[2] = 0;
        }
        break;
    case INV_CONTROL_4:
        *data = cntrl_obj.gridNum[3];
        if (reset == INV_RESET) {
            cntrl_obj.gridNum[3] = 0;
        }
        break;
    default:
        break;
    }

    return INV_SUCCESS;
}

/**
 *  @brief  inv_set_grid_thresh is used to set the grid size for a control signal.
 *          inv_set_grid_thresh is used to adjust the size of the grid being controlled.
 *  @param  controlSignal   Indicates which control signal is being modified.
 *                          Must be one of:
 *                          - INV_CONTROL_1,
 *                          - INV_CONTROL_2,
 *                          - INV_CONTROL_3 and
 *                          - INV_CONTROL_4.
 *  @param  threshold       The threshold of the control signal at which the grid
 *                          number will be incremented or decremented.
 *  @return Zero if the command is successful; an ML error code otherwise.
 */

inv_error_t inv_set_grid_thresh(unsigned short controlSignal, long threshold)
{
    INVENSENSE_FUNC_START;

    if (inv_get_state() < INV_STATE_DMP_OPENED)
        return INV_ERROR_SM_IMPROPER_STATE;

    switch (controlSignal) {
    case INV_CONTROL_1:
        cntrl_params.gridThreshold[0] = threshold;
        break;
    case INV_CONTROL_2:
        cntrl_params.gridThreshold[1] = threshold;
        break;
    case INV_CONTROL_3:
        cntrl_params.gridThreshold[2] = threshold;
        break;
    case INV_CONTROL_4:
        cntrl_params.gridThreshold[3] = threshold;
        break;
    default:
        return INV_ERROR_INVALID_PARAMETER;
        break;
    }

    return INV_SUCCESS;
}

/**
 *  @brief  inv_set_grid_max is used to set the maximum grid number for a control signal.
 *          inv_set_grid_max is used to adjust the maximum allowed grid number, above
 *          which the grid number will not be incremented.
 *          The minimum grid number is always zero.
 *
 *  @pre    inv_dmp_open() Must be called with MLDmpDefaultOpen() or
 *          inv_open_low_power_pedometer().
 *
 *  @param controlSignal    Indicates which control signal is being modified.
 *                          Must be one of:
 *                          - INV_CONTROL_1,
 *                          - INV_CONTROL_2,
 *                          - INV_CONTROL_3 and
 *                          - INV_CONTROL_4.
 *
 *  @param  maximum         The maximum grid number for a control signal.
 *  @return Zero if the command is successful; an ML error code otherwise.
 */

inv_error_t inv_set_grid_max(unsigned short controlSignal, long maximum)
{
    INVENSENSE_FUNC_START;

    if (inv_get_state() != INV_STATE_DMP_OPENED)
        return INV_ERROR_SM_IMPROPER_STATE;

    switch (controlSignal) {
    case INV_CONTROL_1:
        cntrl_params.gridMaximum[0] = maximum;
        break;
    case INV_CONTROL_2:
        cntrl_params.gridMaximum[1] = maximum;
        break;
    case INV_CONTROL_3:
        cntrl_params.gridMaximum[2] = maximum;
        break;
    case INV_CONTROL_4:
        cntrl_params.gridMaximum[3] = maximum;
        break;
    default:
        return INV_ERROR_INVALID_PARAMETER;
        break;
    }

    return INV_SUCCESS;
}

/**
 *  @brief  GridCallback function pointer type, to be passed as argument of
 *          inv_set_grid_callback.
 *
 *  @param  controlSignal   Indicates which control signal crossed a grid threshold.
 *                          Must be one of:
 *                          - INV_CONTROL_1,
 *                          - INV_CONTROL_2,
 *                          - INV_CONTROL_3 and
 *                          - INV_CONTROL_4.
 *
 *  @param  gridNumber  An array of four numbers representing the grid number for each
 *                      control signal.
 *  @param  gridChange  An array of four numbers representing the change in grid number
 *                      for each control signal.
**/
typedef void (*fpGridCb) (unsigned short controlSignal, long *gridNum,
                          long *gridChange);

/**
 *  @brief  inv_set_grid_callback is used to register a callback function that
 *          will trigger when the grid location changes.
 *          inv_set_grid_callback allows a user to define a callback function that will
 *          run when a control signal crosses a grid threshold.

 *  @pre    inv_dmp_open() Must be called with MLDmpDefaultOpen() or
 *          inv_open_low_power_pedometer().  inv_dmp_start must <b>NOT</b> have
 *          been called.
 *
 *  @param  func    A user defined callback function
 *  @return Zero if the command is successful; an ML error code otherwise.
**/
inv_error_t inv_set_grid_callback(fpGridCb func)
{
    INVENSENSE_FUNC_START;

    if (inv_get_state() != INV_STATE_DMP_OPENED)
        return INV_ERROR_SM_IMPROPER_STATE;

    cntrl_params.gridCallback = func;
    return INV_SUCCESS;
}

/**
 *  @brief  inv_set_control_data is used to assign physical parameters to control signals.
 *          inv_set_control_data allows flexibility in assigning physical parameters to
 *          control signals. For example, the user is allowed to use raw gyroscope data
 *          as an input to the control algorithm.
 *          Alternatively, angular velocity can be used, which combines gyroscopes and
 *          accelerometers to provide a more robust physical parameter. Finally, angular
 *          velocity in world coordinates can be used, providing a control signal in
 *          which pitch and yaw are provided relative to gravity.
 *
 *  @pre    inv_dmp_open() Must be called with MLDmpDefaultOpen() or
 *          inv_open_low_power_pedometer().
 *
 *  @param  controlSignal   Indicates which control signal is being modified.
 *                          Must be one of:
 *                          - INV_CONTROL_1,
 *                          - INV_CONTROL_2,
 *                          - INV_CONTROL_3 or
 *                          - INV_CONTROL_4.
 *
 *  @param  parameterArray   Indicates which parameter array is being assigned to a
 *                          control signal. Must be one of:
 *                          - INV_GYROS,
 *                          - INV_ANGULAR_VELOCITY, or
 *
 *  @param  parameterAxis   Indicates which axis of the parameter array will be used.
 *                          Must be:
 *                          - INV_ROLL,
 *                          - INV_PITCH, or
 *                          - INV_YAW.
 */

inv_error_t inv_set_control_data(unsigned short controlSignal,
                                 unsigned short parameterArray,
                                 unsigned short parameterAxis)
{
    INVENSENSE_FUNC_START;
    unsigned char regs[2] = { DINA80 + 10, DINA20 };
    inv_error_t result;

    if (inv_get_state() != INV_STATE_DMP_OPENED)
        return INV_ERROR_SM_IMPROPER_STATE;

    if (parameterArray == INV_ANGULAR_VELOCITY) {
        regs[0] = DINA80 + 5;
        regs[1] = DINA00;
    }
    switch (controlSignal) {
    case INV_CONTROL_1:
        cntrl_params.parameterArray[0] = parameterArray;
        switch (parameterAxis) {
        case INV_PITCH:
            regs[1] += 0x02;
            cntrl_params.parameterAxis[0] = 0;
            break;
        case INV_ROLL:
            regs[1] = DINA22;
            cntrl_params.parameterAxis[0] = 1;
            break;
        case INV_YAW:
            regs[1] = DINA42;
            cntrl_params.parameterAxis[0] = 2;
            break;
        default:
            return INV_ERROR_INVALID_PARAMETER;
        }
        result = inv_set_mpu_memory(KEY_CFG_3, 2, regs);
        if (result) {
            LOG_RESULT_LOCATION(result);
            return result;
        }
        break;
    case INV_CONTROL_2:
        cntrl_params.parameterArray[1] = parameterArray;
        switch (parameterAxis) {
        case INV_PITCH:
            regs[1] += DINA0E;
            cntrl_params.parameterAxis[1] = 0;
            break;
        case INV_ROLL:
            regs[1] += DINA2E;
            cntrl_params.parameterAxis[1] = 1;
            break;
        case INV_YAW:
            regs[1] += DINA4E;
            cntrl_params.parameterAxis[1] = 2;
            break;
        default:
            return INV_ERROR_INVALID_PARAMETER;
        }
        result = inv_set_mpu_memory(KEY_CFG_3B, 2, regs);
        if (result) {
            LOG_RESULT_LOCATION(result);
            return result;
        }
        break;
    case INV_CONTROL_3:
        cntrl_params.parameterArray[2] = parameterArray;
        switch (parameterAxis) {
        case INV_PITCH:
            regs[1] += DINA0E;
            cntrl_params.parameterAxis[2] = 0;
            break;
        case INV_ROLL:
            regs[1] += DINA2E;
            cntrl_params.parameterAxis[2] = 1;
            break;
        case INV_YAW:
            regs[1] += DINA4E;
            cntrl_params.parameterAxis[2] = 2;
            break;
        default:
            return INV_ERROR_INVALID_PARAMETER;
        }
        result = inv_set_mpu_memory(KEY_CFG_3C, 2, regs);
        if (result) {
            LOG_RESULT_LOCATION(result);
            return result;
        }
        break;
    case INV_CONTROL_4:
        cntrl_params.parameterArray[3] = parameterArray;
        switch (parameterAxis) {
        case INV_PITCH:
            regs[1] += DINA0E;
            cntrl_params.parameterAxis[3] = 0;
            break;
        case INV_ROLL:
            regs[1] += DINA2E;
            cntrl_params.parameterAxis[3] = 1;
            break;
        case INV_YAW:
            regs[1] += DINA4E;
            cntrl_params.parameterAxis[3] = 2;
            break;
        default:
            return INV_ERROR_INVALID_PARAMETER;
        }
        result = inv_set_mpu_memory(KEY_CFG_3D, 2, regs);
        if (result) {
            LOG_RESULT_LOCATION(result);
            return result;
        }
        break;
    default:
        result = INV_ERROR_INVALID_PARAMETER;
        break;
    }
    return result;
}

/**
 *  @brief  inv_get_control_data is used to get the current control data.
 *
 *  @pre    inv_dmp_open() Must be called with MLDmpDefaultOpen() or
 *          inv_open_low_power_pedometer().
 *
 *  @param  controlSignal   Indicates which control signal is being queried.
 *                          Must be one of:
 *                          - INV_CONTROL_1,
 *                          - INV_CONTROL_2,
 *                          - INV_CONTROL_3 or
 *                          - INV_CONTROL_4.
 *
 *  @param  gridNum     A pointer to pass gridNum info back to the user.
 *  @param  gridChange  A pointer to pass gridChange info back to the user.
 *
 *  @return Zero if the command is successful; an ML error code otherwise.
 */

inv_error_t inv_get_control_data(long *controlSignal, long *gridNum,
                                 long *gridChange)
{
    INVENSENSE_FUNC_START;
    int_fast8_t i = 0;

    if (inv_get_state() != INV_STATE_DMP_STARTED)
        return INV_ERROR_SM_IMPROPER_STATE;

    for (i = 0; i < 4; i++) {
        controlSignal[i] = cntrl_obj.controlInt[i];
        gridNum[i] = cntrl_obj.gridNum[i];
        gridChange[i] = cntrl_obj.gridChange[i];
    }
    return INV_SUCCESS;
}

/**
 * @internal
 * @brief   Update the ML Control engine.  This function should be called
 *          every time new data from the MPU becomes available.
 *          Control engine outputs are written to the cntrl_obj data
 *          structure.
 * @return  INV_SUCCESS or an error code.
**/
inv_error_t inv_update_control(struct inv_obj_t * inv_obj __unused)
{
    INVENSENSE_FUNC_START;
    unsigned char i;
    long gridTmp;
    long tmp;

    inv_get_cntrl_data(cntrl_obj.mlGridNumDMP);

    for (i = 0; i < 4; i++) {
        if (cntrl_params.functions & INV_GRID) {
            if (cntrl_params.functions & INV_HYSTERESIS) {
                cntrl_obj.mlGridNumDMP[i] += cntrl_obj.gridNumOffset[i];
            }
            cntrl_obj.mlGridNumDMP[i] =
                cntrl_obj.mlGridNumDMP[i] / 2 + 1073741824L;
            cntrl_obj.controlInt[i] =
                (cntrl_obj.mlGridNumDMP[i] %
                 (128 * cntrl_params.gridThreshold[i])) / 128;
            gridTmp =
                cntrl_obj.mlGridNumDMP[i] / (128 *
                                             cntrl_params.gridThreshold[i]);
            tmp = 1 + 16777216L / cntrl_params.gridThreshold[i];
            cntrl_obj.gridChange[i] = gridTmp - cntrl_obj.lastGridNum[i];
            if (cntrl_obj.gridChange[i] > tmp / 2) {
                cntrl_obj.gridChange[i] =
                    gridTmp - tmp - cntrl_obj.lastGridNum[i];
            } else if (cntrl_obj.gridChange[i] < -tmp / 2) {
                cntrl_obj.gridChange[i] =
                    gridTmp + tmp - cntrl_obj.lastGridNum[i];
            }
            if ((cntrl_params.functions & INV_HYSTERESIS)
                && (cntrl_obj.gridChange[i] != 0)) {
                if (cntrl_obj.gridChange[i] > 0) {
                    cntrl_obj.gridNumOffset[i] +=
                        128 * cntrl_params.gridThreshold[i];
                    cntrl_obj.controlInt[i] = cntrl_params.gridThreshold[i] / 2;
                }
                if (cntrl_obj.gridChange[i] < 0) {
                    cntrl_obj.gridNumOffset[i] -=
                        128 * cntrl_params.gridThreshold[i];
                    cntrl_obj.controlInt[i] = cntrl_params.gridThreshold[i] / 2;
                }
            }
            cntrl_obj.gridNum[i] += cntrl_obj.gridChange[i];
            if (cntrl_obj.gridNum[i] >= cntrl_params.gridMaximum[i]) {
                cntrl_obj.gridNum[i] = cntrl_params.gridMaximum[i];
                if (cntrl_obj.controlInt[i] >=
                    cntrl_params.gridThreshold[i] / 2) {
                    cntrl_obj.controlInt[i] = cntrl_params.gridThreshold[i] / 2;
                }
            } else if (cntrl_obj.gridNum[i] <= 0) {
                cntrl_obj.gridNum[i] = 0;
                if (cntrl_obj.controlInt[i] < cntrl_params.gridThreshold[i] / 2) {
                    cntrl_obj.controlInt[i] = cntrl_params.gridThreshold[i] / 2;
                }
            }
            cntrl_obj.lastGridNum[i] = gridTmp;
            if ((cntrl_params.gridCallback) && (cntrl_obj.gridChange[i] != 0)) {
                cntrl_params.gridCallback((INV_CONTROL_1 << i),
                                          cntrl_obj.gridNum,
                                          cntrl_obj.gridChange);
            }

        } else {
            cntrl_obj.controlInt[i] = cntrl_obj.mlGridNumDMP[i];
        }

    }

    return INV_SUCCESS;
}

/**
 * @brief Enables the INV_CONTROL engine.
 *
 * @note  This function replaces MLEnable(INV_CONTROL)
 *
 * @pre inv_dmp_open() with MLDmpDefaultOpen or MLDmpPedometerStandAlone() must
 *      have been called.
 *
 * @return INV_SUCCESS or non-zero error code
 */
inv_error_t inv_enable_control(void)
{
    INVENSENSE_FUNC_START;

    if (inv_get_state() != INV_STATE_DMP_OPENED)
        return INV_ERROR_SM_IMPROPER_STATE;

    memset(&cntrl_obj, 0, sizeof(cntrl_obj));

    inv_register_fifo_rate_process(inv_update_control, INV_PRIORITY_CONTROL);   // fixme, someone needs to send control data to the fifo
    return INV_SUCCESS;
}

/**
 * @brief Disables the INV_CONTROL engine.
 *
 * @note  This function replaces MLDisable(INV_CONTROL)
 *
 * @pre inv_dmp_open() with MLDmpDefaultOpen or MLDmpPedometerStandAlone() must
 *      have been called.
 *
 * @return INV_SUCCESS or non-zero error code
 */
inv_error_t inv_disable_control(void)
{
    INVENSENSE_FUNC_START;

    if (inv_get_state() < INV_STATE_DMP_STARTED)
        return INV_ERROR_SM_IMPROPER_STATE;

    return INV_SUCCESS;
}

/**
 * @}
 */