summaryrefslogtreecommitdiffstats
path: root/libsensors_iio/software/simple_apps/common/helper.c
blob: 4d634bdeca8a7a111d090ddc7d195f82ea6ba079 (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
/*
 $License:
    Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
 $
 */
/*******************************************************************************
 *
 * $Id: helper.c 4367 2010-12-21 03:02:55Z prao $
 *
 *******************************************************************************/

#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#include <conio.h>
#endif
#ifdef LINUX
#include <sys/select.h>
#endif
#include <time.h>
#include <string.h>

#include "ml.h"
#include "slave.h"
#include "mldl.h"
#include "mltypes.h"
#include "mlstates.h"
#include "compass.h"

#include "mlsl.h"
#include "ml.h"

#include "helper.h"
#include "mlsetup.h"
#include "fopenCMake.h"
#include "int.h"
#include "mlos.h"

#include "log.h"
#undef MPL_LOG_TAG
#define MPL_LOG_TAG "MPL-helper"

#ifdef AIO
extern inv_error_t MLSLSetYamahaCompassDataMode(unsigned char mode);
#endif

// Keyboard hit function
int ConsoleKbhit(void)
{
#ifdef _WIN32
    return _kbhit();
#else
    struct timeval tv;
    fd_set read_fd;

    tv.tv_sec=0;
    tv.tv_usec=0;
    FD_ZERO(&read_fd);
    FD_SET(0,&read_fd);

    if(select(1, &read_fd, NULL, NULL, &tv) == -1)
        return 0;

    if(FD_ISSET(0,&read_fd))
        return 1;

    return 0;
#endif
}

char ConsoleGetChar(void) {
#ifdef _WIN32
    return _getch();
#else
    return getchar();
#endif
}
struct mpuirq_data** InterruptPoll(int *handles, int numHandles, long tv_sec, long tv_usec)
{
    struct mpuirq_data **data;
    void *tmp;
    int ii;
    const int irq_data_size = sizeof(**data) * numHandles + 
        sizeof(*data) * numHandles;

    tmp = (void *)inv_malloc(irq_data_size);
    memset(tmp, 0, irq_data_size);
    data = (struct mpuirq_data **)tmp;
    for (ii = 0; ii < numHandles; ii++) {
        data[ii] = (struct mpuirq_data *)((unsigned long)tmp +
            (sizeof(*data) * numHandles) + sizeof(**data) * ii);
    }

    if (IntProcess(handles, numHandles, data, tv_sec, tv_usec) > 0) {
        for (ii = 0; ii < numHandles; ii++) {
            if (data[ii]->interruptcount) {
                inv_interrupt_handler(ii);
            }
        }
    }
    
    /* Return data incase the application needs to look at the timestamp or
       other part of the data */
    return data;
}

void InterruptPollDone(struct mpuirq_data ** data)
{
    inv_free(data);
}