]>
Commit | Line | Data |
---|---|---|
fb019a0e BV |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <string.h> | |
21 | #include "libsigrok.h" | |
22 | #include "libsigrok-internal.h" | |
23 | ||
24 | #define LOG_PREFIX "analog" | |
25 | ||
26 | SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog, | |
27 | float *buf) | |
28 | { | |
29 | float offset; | |
30 | unsigned int b, i; | |
31 | gboolean bigendian; | |
32 | ||
33 | #ifdef WORDS_BIGENDIAN | |
34 | bigendian = TRUE; | |
35 | #else | |
36 | bigendian = FALSE; | |
37 | #endif | |
38 | if (!analog->encoding->is_float) { | |
39 | /* TODO */ | |
40 | sr_err("Only floating-point analog data supported so far."); | |
41 | return SR_ERR; | |
42 | } | |
43 | ||
44 | if (analog->encoding->unitsize == sizeof(float) | |
45 | && analog->encoding->is_bigendian == bigendian | |
46 | && (analog->encoding->scale.p == analog->encoding->scale.q) | |
47 | && analog->encoding->scale.p / (float)analog->encoding->scale.q == 0) { | |
48 | /* The data is already in the right format. */ | |
49 | memcpy(buf, analog->data, analog->num_samples * sizeof(float)); | |
50 | } else { | |
51 | for (i = 0; i < analog->num_samples; i += analog->encoding->unitsize) { | |
52 | for (b = 0; b < analog->encoding->unitsize; b++) { | |
53 | if (analog->encoding->is_bigendian == bigendian) | |
54 | buf[i + b] = ((float *)analog->data)[i * analog->encoding->unitsize + b]; | |
55 | else | |
56 | buf[i + (analog->encoding->unitsize - b)] = ((float *)analog->data)[i * analog->encoding->unitsize + b]; | |
57 | } | |
58 | if (analog->encoding->scale.p != analog->encoding->scale.q) | |
59 | buf[i] = (buf[i] * analog->encoding->scale.p) / analog->encoding->scale.q; | |
60 | offset = ((float)analog->encoding->scale.p / (float)analog->encoding->scale.q); | |
61 | buf[i] += offset; | |
62 | } | |
63 | } | |
64 | ||
65 | return SR_OK; | |
66 | } |