]>
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 | ||
c2a25ebb BV |
20 | #include <stdio.h> |
21 | #include <stdint.h> | |
fb019a0e | 22 | #include <string.h> |
c2a25ebb | 23 | #include <ctype.h> |
fb019a0e BV |
24 | #include "libsigrok.h" |
25 | #include "libsigrok-internal.h" | |
26 | ||
27 | #define LOG_PREFIX "analog" | |
28 | ||
a5892391 BV |
29 | struct unit_mq_string { |
30 | uint64_t value; | |
31 | char *str; | |
32 | }; | |
33 | ||
34 | static struct unit_mq_string unit_strings[] = { | |
35 | { SR_UNIT_VOLT, "V" }, | |
36 | { SR_UNIT_AMPERE, "A" }, | |
37 | { SR_UNIT_OHM, "\xe2\x84\xa6" }, | |
38 | { SR_UNIT_FARAD, "F" }, | |
39 | { SR_UNIT_HENRY, "H" }, | |
40 | { SR_UNIT_KELVIN, "K" }, | |
41 | { SR_UNIT_CELSIUS, "\xc2\xb0""C" }, | |
42 | { SR_UNIT_FAHRENHEIT, "\xc2\xb0""F" }, | |
43 | { SR_UNIT_HERTZ, "Hz" }, | |
44 | { SR_UNIT_PERCENTAGE, "%" }, | |
45 | { SR_UNIT_SECOND, "s" }, | |
46 | { SR_UNIT_SIEMENS, "S" }, | |
47 | { SR_UNIT_DECIBEL_MW, "dBu" }, | |
48 | { SR_UNIT_DECIBEL_VOLT, "dBv" }, | |
49 | { SR_UNIT_DECIBEL_SPL, "dB" }, | |
50 | { SR_UNIT_CONCENTRATION, "ppm" }, | |
51 | { SR_UNIT_REVOLUTIONS_PER_MINUTE, "RPM" }, | |
52 | { SR_UNIT_VOLT_AMPERE, "VA" }, | |
53 | { SR_UNIT_WATT, "W" }, | |
54 | { SR_UNIT_WATT_HOUR, "Wh" }, | |
55 | { SR_UNIT_METER_SECOND, "m/s" }, | |
56 | { SR_UNIT_HECTOPASCAL, "hPa" }, | |
57 | { SR_UNIT_HUMIDITY_293K, "%rF" }, | |
58 | { SR_UNIT_DEGREE, "\xc2\xb0" }, | |
59 | ALL_ZERO | |
60 | }; | |
61 | ||
62 | static struct unit_mq_string mq_strings[] = { | |
63 | { SR_MQFLAG_SPL_FREQ_WEIGHT_A, "(A)" }, | |
64 | { SR_MQFLAG_SPL_FREQ_WEIGHT_C, "(C)" }, | |
65 | { SR_MQFLAG_SPL_FREQ_WEIGHT_Z, "(Z)" }, | |
66 | { SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT, "(SPL)" }, | |
67 | { SR_MQFLAG_SPL_TIME_WEIGHT_S, " S" }, | |
68 | { SR_MQFLAG_SPL_TIME_WEIGHT_F, " F" }, | |
69 | { SR_MQFLAG_SPL_LAT, " LAT" }, | |
70 | /* Not a standard function for SLMs, so this is a made-up notation. */ | |
71 | { SR_MQFLAG_SPL_PCT_OVER_ALARM, "%oA" }, | |
72 | { SR_MQFLAG_AC, " AC" }, | |
73 | { SR_MQFLAG_DC, " DC" }, | |
74 | { SR_MQFLAG_RMS, " RMS" }, | |
75 | { SR_MQFLAG_DIODE, " DIODE" }, | |
76 | { SR_MQFLAG_HOLD, " HOLD" }, | |
77 | { SR_MQFLAG_MAX, " MAX" }, | |
78 | { SR_MQFLAG_MIN, " MIN" }, | |
79 | { SR_MQFLAG_AUTORANGE, " AUTO" }, | |
80 | { SR_MQFLAG_RELATIVE, " REL" }, | |
81 | { SR_MQFLAG_AVG, " AVG" }, | |
82 | { SR_MQFLAG_REFERENCE, " REF" }, | |
83 | ALL_ZERO | |
84 | }; | |
85 | ||
41caa319 AJ |
86 | SR_PRIV int sr_analog_init(struct sr_datafeed_analog2 *analog, |
87 | struct sr_analog_encoding *encoding, | |
88 | struct sr_analog_meaning *meaning, | |
89 | struct sr_analog_spec *spec, | |
90 | int digits) | |
91 | { | |
92 | memset(analog, 0, sizeof(*analog)); | |
93 | memset(encoding, 0, sizeof(*encoding)); | |
94 | memset(meaning, 0, sizeof(*meaning)); | |
95 | memset(spec, 0, sizeof(*spec)); | |
96 | ||
97 | analog->encoding = encoding; | |
98 | analog->meaning = meaning; | |
99 | analog->spec = spec; | |
100 | ||
101 | encoding->unitsize = sizeof(float); | |
102 | encoding->is_float = TRUE; | |
103 | #ifdef WORDS_BIGENDIAN | |
104 | encoding->is_bigendian = TRUE; | |
105 | #else | |
106 | encoding->is_bigendian = FALSE; | |
107 | #endif | |
108 | encoding->digits = digits; | |
109 | encoding->is_digits_decimal = TRUE; | |
110 | encoding->scale.p = 1; | |
111 | encoding->scale.q = 1; | |
112 | encoding->offset.p = 0; | |
113 | encoding->offset.q = 1; | |
114 | ||
115 | spec->spec_digits = digits; | |
116 | ||
117 | return SR_OK; | |
118 | } | |
119 | ||
fb019a0e | 120 | SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog, |
4b4fdeea | 121 | float *outbuf) |
fb019a0e BV |
122 | { |
123 | float offset; | |
124 | unsigned int b, i; | |
125 | gboolean bigendian; | |
126 | ||
127 | #ifdef WORDS_BIGENDIAN | |
128 | bigendian = TRUE; | |
129 | #else | |
130 | bigendian = FALSE; | |
131 | #endif | |
132 | if (!analog->encoding->is_float) { | |
133 | /* TODO */ | |
4b4fdeea | 134 | sr_err("Only floating-point encoding supported so far."); |
fb019a0e BV |
135 | return SR_ERR; |
136 | } | |
137 | ||
138 | if (analog->encoding->unitsize == sizeof(float) | |
139 | && analog->encoding->is_bigendian == bigendian | |
140 | && (analog->encoding->scale.p == analog->encoding->scale.q) | |
4b4fdeea | 141 | && analog->encoding->offset.p / (float)analog->encoding->offset.q == 0) { |
fb019a0e | 142 | /* The data is already in the right format. */ |
4b4fdeea | 143 | memcpy(outbuf, analog->data, analog->num_samples * sizeof(float)); |
fb019a0e BV |
144 | } else { |
145 | for (i = 0; i < analog->num_samples; i += analog->encoding->unitsize) { | |
146 | for (b = 0; b < analog->encoding->unitsize; b++) { | |
147 | if (analog->encoding->is_bigendian == bigendian) | |
4b4fdeea | 148 | outbuf[i + b] = ((float *)analog->data)[i * analog->encoding->unitsize + b]; |
fb019a0e | 149 | else |
4b4fdeea | 150 | outbuf[i + (analog->encoding->unitsize - b)] = ((float *)analog->data)[i * analog->encoding->unitsize + b]; |
fb019a0e BV |
151 | } |
152 | if (analog->encoding->scale.p != analog->encoding->scale.q) | |
4b4fdeea BV |
153 | outbuf[i] = (outbuf[i] * analog->encoding->scale.p) / analog->encoding->scale.q; |
154 | offset = ((float)analog->encoding->offset.p / (float)analog->encoding->offset.q); | |
155 | outbuf[i] += offset; | |
fb019a0e BV |
156 | } |
157 | } | |
158 | ||
159 | return SR_OK; | |
160 | } | |
c2a25ebb BV |
161 | |
162 | /* | |
163 | * Convert a floating point value to a string, limited to the given | |
164 | * number of decimal digits. | |
165 | * | |
166 | * @param value The value to convert. | |
167 | * @param digits Number of digits after the decimal point to print. | |
168 | * @param outbuf Buffer in which the resulting string will be placed. | |
169 | * @param bufsize Size of the buffer in bytes. | |
170 | * | |
171 | * @retval SR_OK | |
172 | * | |
173 | * @since 0.4.0 | |
174 | */ | |
175 | SR_API int sr_analog_float_to_string(float value, int digits, char *outbuf, | |
176 | int bufsize) | |
177 | { | |
178 | int cnt, i; | |
179 | ||
180 | /* This produces at least one too many digits */ | |
181 | snprintf(outbuf, bufsize, "%.*f", digits, value); | |
182 | for (i = 0, cnt = 0; outbuf[i] && i < bufsize; i++) { | |
183 | if (isdigit(outbuf[i++])) | |
184 | cnt++; | |
185 | if (cnt == digits) { | |
186 | outbuf[i] = 0; | |
187 | break; | |
188 | } | |
189 | } | |
190 | ||
191 | return SR_OK; | |
192 | } | |
193 | ||
a5892391 BV |
194 | /* |
195 | * Convert the unit/MQ/MQ flags in the analog struct to a string. | |
196 | * | |
197 | * @param analog Struct containing the unit, MQ and MQ flags. | |
198 | * @param outbuf Buffer in which the resulting string will be placed. | |
199 | * @param bufsize Size of the buffer in bytes. | |
200 | * | |
201 | * @retval SR_OK | |
202 | * | |
203 | * @since 0.4.0 | |
204 | */ | |
205 | SR_API int sr_analog_unit_to_string(const struct sr_datafeed_analog2 *analog, | |
206 | char *outbuf, int bufsize) | |
207 | { | |
208 | int len, i; | |
209 | ||
210 | outbuf[0] = 0; | |
211 | for (i = 0; unit_strings[i].value; i++) { | |
212 | if (analog->meaning->unit == unit_strings[i].value) { | |
213 | strncpy(outbuf, unit_strings[i].str, bufsize); | |
214 | break; | |
215 | } | |
216 | } | |
217 | ||
218 | /* More than one MQ flag may apply. */ | |
219 | for (i = 0; mq_strings[i].value; i++) { | |
220 | if (analog->meaning->mqflags & mq_strings[i].value) { | |
221 | len = strlen(outbuf); | |
222 | strncat(outbuf + len, mq_strings[i].str, bufsize - len); | |
223 | } | |
224 | } | |
225 | ||
226 | return SR_OK; | |
227 | } | |
228 |