-
Notifications
You must be signed in to change notification settings - Fork 0
/
libmm7150.c
240 lines (200 loc) · 5.56 KB
/
libmm7150.c
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
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <iio.h>
#include <locale.h>
/* Accelerometer name */
static char *name = "accel_3d"; //FIXME
static int buffer_length = 1;
static int count = -1;
/* IIO structs required for streaming */
static struct iio_context *ctx;
// Streaming device
static struct iio_device *dev;
static struct iio_buffer *rxbuf;
static struct iio_channel **channels;
static int channel_count;
static bool stop;
static bool has_repeat;
/* cleanup and exit */
static void shutdown()
{
if (channels) { free(channels); }
printf("* Destroying context\n");
if (ctx) { iio_context_destroy(ctx); }
exit(0);
}
static void handle_sig(int sig)
{
printf("Waiting for process to finish...\n");
stop = true;
}
static struct {
const char *id;
const char *unit;
} map[] = {
{ "current", "A" },
{ "power", "W" },
{ "temp", "°C" },
{ "voltage", "V" },
{ "accel", "m/s^2"},
{ 0, },
};
static const char *id_to_unit(const char *id)
{
unsigned int i;
for (i = 0; map[i].id; i++) {
if (!strncmp(id, map[i].id, strlen(map[i].id)))
return map[i].unit;
}
return "";
}
static bool channel_has_attr(struct iio_channel *chn, const char *attr)
{
unsigned int i, nb = iio_channel_get_attrs_count(chn);
for (i = 0; i < nb; i++)
if (!strcmp(attr, iio_channel_get_attr(chn, i)))
return true;
return false;
}
static bool is_valid_channel(struct iio_channel *chn)
{
return !iio_channel_is_output(chn) &&
(channel_has_attr(chn, "raw") ||
channel_has_attr(chn, "input"));
}
static double get_channel_value(struct iio_channel *chn)
{
char *old_locale;
char buf[1024];
double val;
old_locale = strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C");
if (channel_has_attr(chn, "input")) {
iio_channel_attr_read(chn, "input", buf, sizeof(buf));
val = strtod(buf, NULL);
}
else {
iio_channel_attr_read(chn, "raw", buf, sizeof(buf));
val = strtod(buf, NULL);
if (channel_has_attr(chn, "offset")) {
iio_channel_attr_read(chn, "offset", buf, sizeof(buf));
val += strtod(buf, NULL);
}
if (channel_has_attr(chn, "scale")) {
iio_channel_attr_read(chn, "scale", buf, sizeof(buf));
val *= strtod(buf, NULL);
}
}
setlocale(LC_NUMERIC, old_locale);
free(old_locale);
return val / 1000.0;
}
/* simple configuration and streaming */
int main(int argc, char **argv)
{
// Hardware trigger
struct iio_device *trigger;
// Initialize variables
int i,j,sample;
// Listen to ctrl+c and assert
signal(SIGINT, handle_sig);
printf("* Acquiring IIO context\n");
// Create the context, if it fails the program will terminate
assert((ctx = iio_create_default_context()) && "No context");
// Counts how many devices are present in the context, if none the program will terminate
assert(iio_context_get_devices_count(ctx) > 0 && "No devices");
printf("* Acquiring device %s\n", name);
// Find and assign the device thanks to its name
dev = iio_context_find_device(ctx, name);
if (!dev) {
perror("No device found");
shutdown();
}
// Initialization of the channels corresponding to the device to allocate required memory
printf("* Initializing IIO streaming channels:\n");
for (i = 0; i < iio_device_get_channels_count(dev); ++i) {
struct iio_channel *chn = iio_device_get_channel(dev, i);
if (iio_channel_is_scan_element(chn)) {
printf("%s\n", iio_channel_get_id(chn));
channel_count++;
}
}
// If no scan elements, the program will terminate
if (channel_count == 0) {
printf("No scan elements found\n");
shutdown();
}
// allocate memory to the channels
channels = calloc(channel_count, sizeof *channels);
if (!channels) {
perror("Channel array allocation failed");
shutdown();
}
// Populate the allocated memory
for (i = 0; i < channel_count; ++i) {
struct iio_channel *chn = iio_device_get_channel(dev, i);
if (iio_channel_is_scan_element(chn))
channels[i] = chn;
}
/*// Enable the channels for buffered capture
printf("* Enabling IIO streaming channels for buffered capture\n");
for (i = 0; i < channel_count; ++i)
iio_channel_enable(channels[i]);
// Create a buffer with one sample
printf("* Creating non-cyclic IIO buffers with %d samples\n", buffer_length);
rxbuf = iio_device_create_buffer(dev, buffer_length, false);
if (!rxbuf) {
perror("Could not create buffer");
shutdown();
}
*/
// Starting the streaming
printf("* Starting IO streaming (press CTRL+C to cancel)\n");
//bool has_ts = strcmp(iio_channel_get_id(channels[channel_count - 1]), "timestamp") == 0;
//int64_t last_ts = 0;
while (!stop)
{
/*
ssize_t nbytes_rx;
void *p_dat, *p_end;
ptrdiff_t p_inc;
int64_t now_ts;
// Refill RX buffer
nbytes_rx = iio_buffer_refill(rxbuf);
if (nbytes_rx < 0) {
printf("Error refilling buf: %d\n", (int)nbytes_rx);
shutdown();
}
p_inc = iio_buffer_step(rxbuf);
p_end = iio_buffer_end(rxbuf);
// Print timestamp delta in ms
if (has_ts)
for (p_dat = iio_buffer_first(rxbuf, channels[channel_count - 1]); p_dat < p_end; p_dat += p_inc) {
now_ts = (((int64_t *)p_dat)[0]);
printf("[%04ld] ", last_ts > 0 ? (now_ts - last_ts) / 1000 / 1000 : 0);
last_ts = now_ts;
}
*/
// Print each captured sample
for (i = 0; i < channel_count; i++) {
const char *id;
const char *unit;
name = iio_channel_get_name(channels[i]);
id = iio_channel_get_id(channels[i]);
if (!name)
name = id;
unit = id_to_unit(id);
printf("%s: %.3lf %s ", name, get_channel_value(channels[i]), unit);
}
printf("\n");
}
shutdown();
return 0;
}