]>
Commit | Line | Data |
---|---|---|
c01bdebc AJ |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | * Copyright (C) 2013 Aurelien Jacobs <aurel@gnuage.org> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
2ea1fdf1 | 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
c01bdebc AJ |
19 | */ |
20 | ||
21 | /* | |
22 | * Cyrustek ES519XX protocol parser. | |
23 | * | |
24 | * Communication parameters: Unidirectional, 2400/7o1 or 19230/7o1 | |
25 | */ | |
26 | ||
6ec6c43b | 27 | #include <config.h> |
c01bdebc AJ |
28 | #include <string.h> |
29 | #include <ctype.h> | |
30 | #include <math.h> | |
31 | #include <glib.h> | |
c1aae900 | 32 | #include <libsigrok/libsigrok.h> |
c01bdebc AJ |
33 | #include "libsigrok-internal.h" |
34 | ||
3544f848 | 35 | #define LOG_PREFIX "es519xx" |
c01bdebc | 36 | |
8cbf5627 AJ |
37 | /* Exponents for the respective measurement mode. */ |
38 | static const int exponents_2400_11b[9][8] = { | |
39 | { -4, -3, -2, -1, 0, 0, 0, 0 }, /* V */ | |
40 | { -7, -6, 0, 0, 0, 0, 0, 0 }, /* uA */ | |
41 | { -5, -4, 0, 0, 0, 0, 0, 0 }, /* mA */ | |
42 | { -2, 0, 0, 0, 0, 0, 0, 0 }, /* A */ | |
43 | { 1, 2, 3, 4, 5, 6, 0, 0 }, /* RPM */ | |
44 | { -1, 0, 1, 2, 3, 4, 0, 0 }, /* Resistance */ | |
45 | { 0, 1, 2, 3, 4, 5, 0, 0 }, /* Frequency */ | |
46 | { -12, -11, -10, -9, -8, -7, -6, -5 }, /* Capacitance */ | |
47 | { -3, 0, 0, 0, 0, 0, 0, 0 }, /* Diode */ | |
c01bdebc | 48 | }; |
8cbf5627 AJ |
49 | static const int exponents_19200_11b_5digits[9][8] = { |
50 | { -4, -3, -2, -1, -5, 0, 0, 0 }, /* V */ | |
51 | { -8, -7, 0, 0, 0, 0, 0, 0 }, /* uA */ | |
52 | { -6, -5, 0, 0, 0, 0, 0, 0 }, /* mA */ | |
53 | { 0, -3, 0, 0, 0, 0, 0, 0 }, /* A */ | |
54 | { -4, -3, -2, -1, 0, 0, 0, 0 }, /* Manual A */ | |
55 | { -2, -1, 0, 1, 2, 3, 4, 0 }, /* Resistance */ | |
56 | { -1, 0, 0, 1, 2, 3, 4, 0 }, /* Frequency */ | |
57 | { -12, -11, -10, -9, -8, -7, -6, -5 }, /* Capacitance */ | |
58 | { -4, 0, 0, 0, 0, 0, 0, 0 }, /* Diode */ | |
c01bdebc | 59 | }; |
8cbf5627 AJ |
60 | static const int exponents_19200_11b_clampmeter[9][8] = { |
61 | { -3, -2, -1, 0, -4, 0, 0, 0 }, /* V */ | |
62 | { -7, -6, 0, 0, 0, 0, 0, 0 }, /* uA */ | |
63 | { -5, -4, 0, 0, 0, 0, 0, 0 }, /* mA */ | |
64 | { -2, 0, 0, 0, 0, 0, 0, 0 }, /* A */ | |
65 | { -3, -2, -1, 0, 0, 0, 0, 0 }, /* Manual A */ | |
66 | { -1, 0, 1, 2, 3, 4, 0, 0 }, /* Resistance */ | |
67 | { -1, 0, 0, 1, 2, 3, 4, 0 }, /* Frequency */ | |
68 | { -12, -11, -10, -9, -8, -7, -6, -5 }, /* Capacitance */ | |
69 | { -3, 0, 0, 0, 0, 0, 0, 0 }, /* Diode */ | |
c01bdebc | 70 | }; |
8cbf5627 AJ |
71 | static const int exponents_19200_11b[9][8] = { |
72 | { -3, -2, -1, 0, -4, 0, 0, 0 }, /* V */ | |
73 | { -7, -6, 0, 0, 0, 0, 0, 0 }, /* uA */ | |
74 | { -5, -4, 0, 0, 0, 0, 0, 0 }, /* mA */ | |
75 | { -3, -2, 0, 0, 0, 0, 0, 0 }, /* A */ | |
76 | { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Manual A */ | |
77 | { -1, 0, 1, 2, 3, 4, 0, 0 }, /* Resistance */ | |
78 | { 0, 1, 2, 3, 4, 0, 0, 0 }, /* Frequency */ | |
79 | { -12, -11, -10, -9, -8, -7, -6, 0 }, /* Capacitance */ | |
80 | { -3, 0, 0, 0, 0, 0, 0, 0 }, /* Diode */ | |
c01bdebc | 81 | }; |
8cbf5627 AJ |
82 | static const int exponents_19200_14b[9][8] = { |
83 | { -4, -3, -2, -1, -5, 0, 0, 0 }, /* V */ | |
84 | { -8, -7, 0, 0, 0, 0, 0, 0 }, /* uA */ | |
85 | { -6, -5, 0, 0, 0, 0, 0, 0 }, /* mA */ | |
86 | { -3, 0, 0, 0, 0, 0, 0, 0 }, /* A */ | |
87 | { -4, -3, -2, -1, 0, 0, 0, 0 }, /* Manual A */ | |
88 | { -2, -1, 0, 1, 2, 3, 4, 0 }, /* Resistance */ | |
89 | { -2, -1, 0, 0, 1, 2, 3, 4 }, /* Frequency */ | |
90 | { -12, -11, -10, -9, -8, -7, -6, -5 }, /* Capacitance */ | |
91 | { -4, 0, 0, 0, 0, 0, 0, 0 }, /* Diode */ | |
c01bdebc AJ |
92 | }; |
93 | ||
94 | static int parse_value(const uint8_t *buf, struct es519xx_info *info, | |
95 | float *result) | |
96 | { | |
72e1672f | 97 | int i, intval, num_digits; |
c01bdebc AJ |
98 | float floatval; |
99 | ||
72e1672f UH |
100 | num_digits = 4 + ((info->packet_size == 14) ? 1 : 0); |
101 | ||
c01bdebc AJ |
102 | /* Bytes 1-4 (or 5): Value (4 or 5 decimal digits) */ |
103 | if (info->is_ol) { | |
104 | sr_spew("Over limit."); | |
105 | *result = INFINITY; | |
106 | return SR_OK; | |
107 | } else if (info->is_ul) { | |
108 | sr_spew("Under limit."); | |
109 | *result = INFINITY; | |
110 | return SR_OK; | |
111 | } else if (!isdigit(buf[1]) || !isdigit(buf[2]) || | |
112 | !isdigit(buf[3]) || !isdigit(buf[4]) || | |
72e1672f | 113 | (num_digits == 5 && !isdigit(buf[5]))) { |
ec5186f9 | 114 | sr_dbg("Value contained invalid digits: %02x %02x %02x %02x " |
c01bdebc AJ |
115 | "(%c %c %c %c).", buf[1], buf[2], buf[3], buf[4], |
116 | buf[1], buf[2], buf[3], buf[4]); | |
117 | return SR_ERR; | |
118 | } | |
72e1672f UH |
119 | intval = (info->is_digit4) ? 1 : 0; |
120 | for (i = 0; i < num_digits; i++) | |
121 | intval = 10 * intval + (buf[i + 1] - '0'); | |
c01bdebc AJ |
122 | |
123 | /* Apply sign. */ | |
124 | intval *= info->is_sign ? -1 : 1; | |
125 | ||
126 | floatval = (float)intval; | |
127 | ||
128 | /* Note: The decimal point position will be parsed later. */ | |
129 | ||
130 | sr_spew("The display value is %f.", floatval); | |
131 | ||
132 | *result = floatval; | |
72e1672f | 133 | |
c01bdebc AJ |
134 | return SR_OK; |
135 | } | |
136 | ||
8cbf5627 | 137 | static int parse_range(uint8_t b, float *floatval, struct es519xx_info *info) |
c01bdebc | 138 | { |
72e1672f | 139 | int idx, mode; |
8cbf5627 | 140 | int exponent = 0; |
c01bdebc | 141 | |
72e1672f UH |
142 | idx = b - '0'; |
143 | ||
c01bdebc AJ |
144 | if (idx < 0 || idx > 7) { |
145 | sr_dbg("Invalid range byte / index: 0x%02x / 0x%02x.", b, idx); | |
146 | return SR_ERR; | |
147 | } | |
148 | ||
149 | /* Parse range byte (depends on the measurement mode). */ | |
150 | if (info->is_voltage) | |
151 | mode = 0; /* V */ | |
152 | else if (info->is_current && info->is_micro) | |
153 | mode = 1; /* uA */ | |
154 | else if (info->is_current && info->is_milli) | |
155 | mode = 2; /* mA */ | |
156 | else if (info->is_current && info->is_auto) | |
157 | mode = 3; /* A */ | |
72e1672f | 158 | else if (info->is_current && !info->is_auto) |
c01bdebc AJ |
159 | mode = 4; /* Manual A */ |
160 | else if (info->is_rpm) | |
8cbf5627 | 161 | /* Not a typo, it's really index 4 in exponents_2400_11b[][]. */ |
c01bdebc | 162 | mode = 4; /* RPM */ |
a7c01629 | 163 | else if (info->is_resistance || info->is_continuity) |
c01bdebc AJ |
164 | mode = 5; /* Resistance */ |
165 | else if (info->is_frequency) | |
166 | mode = 6; /* Frequency */ | |
167 | else if (info->is_capacitance) | |
168 | mode = 7; /* Capacitance */ | |
4d2630e6 AJ |
169 | else if (info->is_diode) |
170 | mode = 8; /* Diode */ | |
c3e871dc AJ |
171 | else if (info->is_duty_cycle) |
172 | mode = 0; /* Dummy, unused */ | |
c01bdebc AJ |
173 | else { |
174 | sr_dbg("Invalid mode, range byte was: 0x%02x.", b); | |
175 | return SR_ERR; | |
176 | } | |
177 | ||
178 | if (info->is_vbar) { | |
179 | if (info->is_micro) | |
8cbf5627 | 180 | exponent = (const int[]){-1, 0}[idx]; |
c01bdebc | 181 | else if (info->is_milli) |
8cbf5627 | 182 | exponent = (const int[]){-2, -1}[idx]; |
c01bdebc | 183 | } |
c3e871dc | 184 | else if (info->is_duty_cycle) |
8cbf5627 | 185 | exponent = -1; |
c01bdebc | 186 | else if (info->baudrate == 2400) |
8cbf5627 | 187 | exponent = exponents_2400_11b[mode][idx]; |
c01bdebc | 188 | else if (info->fivedigits) |
8cbf5627 | 189 | exponent = exponents_19200_11b_5digits[mode][idx]; |
c01bdebc | 190 | else if (info->clampmeter) |
8cbf5627 | 191 | exponent = exponents_19200_11b_clampmeter[mode][idx]; |
c01bdebc | 192 | else if (info->packet_size == 11) |
8cbf5627 | 193 | exponent = exponents_19200_11b[mode][idx]; |
c01bdebc | 194 | else if (info->packet_size == 14) |
8cbf5627 | 195 | exponent = exponents_19200_14b[mode][idx]; |
c01bdebc | 196 | |
8cbf5627 AJ |
197 | /* Apply respective exponent (mode-dependent) on the value. */ |
198 | *floatval *= powf(10, exponent); | |
199 | sr_dbg("Applying exponent %d, new value is %f.", exponent, *floatval); | |
c01bdebc | 200 | |
8cbf5627 | 201 | info->digits = -exponent; |
c01bdebc AJ |
202 | |
203 | return SR_OK; | |
204 | } | |
205 | ||
206 | static void parse_flags(const uint8_t *buf, struct es519xx_info *info) | |
207 | { | |
72e1672f | 208 | int function, status; |
c01bdebc | 209 | |
72e1672f UH |
210 | function = 5 + ((info->packet_size == 14) ? 1 : 0); |
211 | status = function + 1; | |
212 | ||
213 | /* Status byte */ | |
c01bdebc AJ |
214 | if (info->alt_functions) { |
215 | info->is_sign = (buf[status] & (1 << 3)) != 0; | |
72e1672f UH |
216 | info->is_batt = (buf[status] & (1 << 2)) != 0; /* Bat. low */ |
217 | info->is_ol = (buf[status] & (1 << 1)) != 0; /* Overflow */ | |
218 | info->is_ol |= (buf[status] & (1 << 0)) != 0; /* Overflow */ | |
c01bdebc AJ |
219 | } else { |
220 | info->is_judge = (buf[status] & (1 << 3)) != 0; | |
221 | info->is_sign = (buf[status] & (1 << 2)) != 0; | |
72e1672f UH |
222 | info->is_batt = (buf[status] & (1 << 1)) != 0; /* Bat. low */ |
223 | info->is_ol = (buf[status] & (1 << 0)) != 0; /* Overflow */ | |
c01bdebc AJ |
224 | } |
225 | ||
226 | if (info->packet_size == 14) { | |
72e1672f UH |
227 | /* Option 1 byte */ |
228 | info->is_max = (buf[8] & (1 << 3)) != 0; | |
229 | info->is_min = (buf[8] & (1 << 2)) != 0; | |
230 | info->is_rel = (buf[8] & (1 << 1)) != 0; | |
231 | info->is_rmr = (buf[8] & (1 << 0)) != 0; | |
232 | ||
233 | /* Option 2 byte */ | |
234 | info->is_ul = (buf[9] & (1 << 3)) != 0; /* Underflow */ | |
235 | info->is_pmax = (buf[9] & (1 << 2)) != 0; /* Max. peak value */ | |
236 | info->is_pmin = (buf[9] & (1 << 1)) != 0; /* Min. peak value */ | |
237 | ||
238 | /* Option 3 byte */ | |
239 | info->is_dc = (buf[10] & (1 << 3)) != 0; | |
240 | info->is_ac = (buf[10] & (1 << 2)) != 0; | |
241 | info->is_auto = (buf[10] & (1 << 1)) != 0; | |
242 | info->is_vahz = (buf[10] & (1 << 0)) != 0; | |
243 | ||
244 | /* LPF: Low-pass filter(s) */ | |
c01bdebc | 245 | if (info->selectable_lpf) { |
72e1672f UH |
246 | /* Option 4 byte */ |
247 | info->is_hold = (buf[11] & (1 << 3)) != 0; | |
248 | info->is_vbar = (buf[11] & (1 << 2)) != 0; | |
249 | info->is_lpf1 = (buf[11] & (1 << 1)) != 0; | |
250 | info->is_lpf0 = (buf[11] & (1 << 0)) != 0; | |
c01bdebc | 251 | } else { |
72e1672f UH |
252 | /* Option 4 byte */ |
253 | info->is_vbar = (buf[11] & (1 << 2)) != 0; | |
254 | info->is_hold = (buf[11] & (1 << 1)) != 0; | |
255 | info->is_lpf1 = (buf[11] & (1 << 0)) != 0; | |
c01bdebc AJ |
256 | } |
257 | } else if (info->alt_functions) { | |
72e1672f UH |
258 | /* Option 2 byte */ |
259 | info->is_dc = (buf[8] & (1 << 3)) != 0; | |
260 | info->is_auto = (buf[8] & (1 << 2)) != 0; | |
261 | info->is_apo = (buf[8] & (1 << 0)) != 0; | |
262 | info->is_ac = !info->is_dc; | |
c01bdebc | 263 | } else { |
72e1672f | 264 | /* Option 1 byte */ |
c01bdebc AJ |
265 | if (info->baudrate == 2400) { |
266 | info->is_pmax = (buf[7] & (1 << 3)) != 0; | |
267 | info->is_pmin = (buf[7] & (1 << 2)) != 0; | |
268 | info->is_vahz = (buf[7] & (1 << 0)) != 0; | |
269 | } else if (info->fivedigits) { | |
270 | info->is_ul = (buf[7] & (1 << 3)) != 0; | |
271 | info->is_pmax = (buf[7] & (1 << 2)) != 0; | |
272 | info->is_pmin = (buf[7] & (1 << 1)) != 0; | |
273 | info->is_digit4 = (buf[7] & (1 << 0)) != 0; | |
274 | } else if (info->clampmeter) { | |
275 | info->is_ul = (buf[7] & (1 << 3)) != 0; | |
276 | info->is_vasel = (buf[7] & (1 << 2)) != 0; | |
277 | info->is_vbar = (buf[7] & (1 << 1)) != 0; | |
278 | } else { | |
279 | info->is_hold = (buf[7] & (1 << 3)) != 0; | |
280 | info->is_max = (buf[7] & (1 << 2)) != 0; | |
281 | info->is_min = (buf[7] & (1 << 1)) != 0; | |
282 | } | |
283 | ||
72e1672f UH |
284 | /* Option 2 byte */ |
285 | info->is_dc = (buf[8] & (1 << 3)) != 0; | |
286 | info->is_ac = (buf[8] & (1 << 2)) != 0; | |
287 | info->is_auto = (buf[8] & (1 << 1)) != 0; | |
c01bdebc AJ |
288 | if (info->baudrate == 2400) |
289 | info->is_apo = (buf[8] & (1 << 0)) != 0; | |
290 | else | |
291 | info->is_vahz = (buf[8] & (1 << 0)) != 0; | |
292 | } | |
293 | ||
72e1672f | 294 | /* Function byte */ |
c01bdebc AJ |
295 | if (info->alt_functions) { |
296 | switch (buf[function]) { | |
297 | case 0x3f: /* A */ | |
298 | info->is_current = info->is_auto = TRUE; | |
299 | break; | |
300 | case 0x3e: /* uA */ | |
301 | info->is_current = info->is_micro = info->is_auto = TRUE; | |
302 | break; | |
303 | case 0x3d: /* mA */ | |
304 | info->is_current = info->is_milli = info->is_auto = TRUE; | |
305 | break; | |
306 | case 0x3c: /* V */ | |
307 | info->is_voltage = TRUE; | |
308 | break; | |
309 | case 0x37: /* Resistance */ | |
310 | info->is_resistance = TRUE; | |
311 | break; | |
312 | case 0x36: /* Continuity */ | |
313 | info->is_continuity = TRUE; | |
314 | break; | |
315 | case 0x3b: /* Diode */ | |
316 | info->is_diode = TRUE; | |
317 | break; | |
318 | case 0x3a: /* Frequency */ | |
319 | info->is_frequency = TRUE; | |
320 | break; | |
321 | case 0x34: /* ADP0 */ | |
322 | case 0x35: /* ADP0 */ | |
323 | info->is_adp0 = TRUE; | |
324 | break; | |
325 | case 0x38: /* ADP1 */ | |
326 | case 0x39: /* ADP1 */ | |
327 | info->is_adp1 = TRUE; | |
328 | break; | |
329 | case 0x32: /* ADP2 */ | |
330 | case 0x33: /* ADP2 */ | |
331 | info->is_adp2 = TRUE; | |
332 | break; | |
333 | case 0x30: /* ADP3 */ | |
334 | case 0x31: /* ADP3 */ | |
335 | info->is_adp3 = TRUE; | |
336 | break; | |
337 | default: | |
ec5186f9 | 338 | sr_dbg("Invalid function byte: 0x%02x.", buf[function]); |
c01bdebc AJ |
339 | break; |
340 | } | |
341 | } else { | |
72e1672f | 342 | /* Note: Some of these mappings are fixed up later. */ |
c01bdebc AJ |
343 | switch (buf[function]) { |
344 | case 0x3b: /* V */ | |
345 | info->is_voltage = TRUE; | |
346 | break; | |
347 | case 0x3d: /* uA */ | |
348 | info->is_current = info->is_micro = info->is_auto = TRUE; | |
349 | break; | |
350 | case 0x3f: /* mA */ | |
351 | info->is_current = info->is_milli = info->is_auto = TRUE; | |
352 | break; | |
353 | case 0x30: /* A */ | |
354 | info->is_current = info->is_auto = TRUE; | |
355 | break; | |
356 | case 0x39: /* Manual A */ | |
357 | info->is_current = TRUE; | |
358 | info->is_auto = FALSE; /* Manual mode */ | |
359 | break; | |
360 | case 0x33: /* Resistance */ | |
361 | info->is_resistance = TRUE; | |
362 | break; | |
363 | case 0x35: /* Continuity */ | |
364 | info->is_continuity = TRUE; | |
365 | break; | |
366 | case 0x31: /* Diode */ | |
367 | info->is_diode = TRUE; | |
368 | break; | |
369 | case 0x32: /* Frequency / RPM / duty cycle */ | |
370 | if (info->packet_size == 14) { | |
371 | if (info->is_judge) | |
c01bdebc | 372 | info->is_duty_cycle = TRUE; |
e1f9f1e1 AJ |
373 | else |
374 | info->is_frequency = TRUE; | |
c01bdebc AJ |
375 | } else { |
376 | if (info->is_judge) | |
377 | info->is_rpm = TRUE; | |
378 | else | |
379 | info->is_frequency = TRUE; | |
380 | } | |
381 | break; | |
382 | case 0x36: /* Capacitance */ | |
383 | info->is_capacitance = TRUE; | |
384 | break; | |
385 | case 0x34: /* Temperature */ | |
386 | info->is_temperature = TRUE; | |
387 | if (info->is_judge) | |
388 | info->is_celsius = TRUE; | |
389 | else | |
390 | info->is_fahrenheit = TRUE; | |
72e1672f | 391 | /* IMPORTANT: The digits always represent Celsius! */ |
c01bdebc AJ |
392 | break; |
393 | case 0x3e: /* ADP0 */ | |
394 | info->is_adp0 = TRUE; | |
395 | break; | |
396 | case 0x3c: /* ADP1 */ | |
397 | info->is_adp1 = TRUE; | |
398 | break; | |
399 | case 0x38: /* ADP2 */ | |
400 | info->is_adp2 = TRUE; | |
401 | break; | |
402 | case 0x3a: /* ADP3 */ | |
403 | info->is_adp3 = TRUE; | |
404 | break; | |
405 | default: | |
ec5186f9 | 406 | sr_dbg("Invalid function byte: 0x%02x.", buf[function]); |
c01bdebc AJ |
407 | break; |
408 | } | |
409 | } | |
410 | ||
29bad967 AJ |
411 | if (info->is_vahz && (info->is_voltage || info->is_current)) { |
412 | info->is_voltage = FALSE; | |
413 | info->is_current = FALSE; | |
414 | info->is_milli = info->is_micro = FALSE; | |
415 | if (info->packet_size == 14) { | |
416 | if (info->is_judge) | |
417 | info->is_duty_cycle = TRUE; | |
418 | else | |
419 | info->is_frequency = TRUE; | |
420 | } else { | |
421 | if (info->is_judge) | |
422 | info->is_rpm = TRUE; | |
423 | else | |
424 | info->is_frequency = TRUE; | |
425 | } | |
426 | } | |
427 | ||
c01bdebc AJ |
428 | if (info->is_current && (info->is_micro || info->is_milli) && info->is_vasel) { |
429 | info->is_current = info->is_auto = FALSE; | |
430 | info->is_voltage = TRUE; | |
431 | } | |
432 | ||
433 | if (info->baudrate == 2400) { | |
72e1672f | 434 | /* Inverted mapping between mA and A, and no manual A. */ |
c01bdebc AJ |
435 | if (info->is_current && (info->is_milli || !info->is_auto)) { |
436 | info->is_milli = !info->is_milli; | |
437 | info->is_auto = TRUE; | |
438 | } | |
439 | } | |
440 | } | |
441 | ||
b02bb45f | 442 | static void handle_flags(struct sr_datafeed_analog *analog, |
72e1672f | 443 | float *floatval, const struct es519xx_info *info) |
c01bdebc AJ |
444 | { |
445 | /* | |
446 | * Note: is_micro etc. are not used directly to multiply/divide | |
8cbf5627 | 447 | * floatval, this is handled via parse_range() and exponents[][]. |
c01bdebc AJ |
448 | */ |
449 | ||
450 | /* Measurement modes */ | |
451 | if (info->is_voltage) { | |
b02bb45f UH |
452 | analog->meaning->mq = SR_MQ_VOLTAGE; |
453 | analog->meaning->unit = SR_UNIT_VOLT; | |
c01bdebc AJ |
454 | } |
455 | if (info->is_current) { | |
b02bb45f UH |
456 | analog->meaning->mq = SR_MQ_CURRENT; |
457 | analog->meaning->unit = SR_UNIT_AMPERE; | |
c01bdebc AJ |
458 | } |
459 | if (info->is_resistance) { | |
b02bb45f UH |
460 | analog->meaning->mq = SR_MQ_RESISTANCE; |
461 | analog->meaning->unit = SR_UNIT_OHM; | |
c01bdebc AJ |
462 | } |
463 | if (info->is_frequency) { | |
b02bb45f UH |
464 | analog->meaning->mq = SR_MQ_FREQUENCY; |
465 | analog->meaning->unit = SR_UNIT_HERTZ; | |
c01bdebc AJ |
466 | } |
467 | if (info->is_capacitance) { | |
b02bb45f UH |
468 | analog->meaning->mq = SR_MQ_CAPACITANCE; |
469 | analog->meaning->unit = SR_UNIT_FARAD; | |
c01bdebc AJ |
470 | } |
471 | if (info->is_temperature && info->is_celsius) { | |
b02bb45f UH |
472 | analog->meaning->mq = SR_MQ_TEMPERATURE; |
473 | analog->meaning->unit = SR_UNIT_CELSIUS; | |
c01bdebc AJ |
474 | } |
475 | if (info->is_temperature && info->is_fahrenheit) { | |
b02bb45f UH |
476 | analog->meaning->mq = SR_MQ_TEMPERATURE; |
477 | analog->meaning->unit = SR_UNIT_FAHRENHEIT; | |
c01bdebc AJ |
478 | } |
479 | if (info->is_continuity) { | |
b02bb45f UH |
480 | analog->meaning->mq = SR_MQ_CONTINUITY; |
481 | analog->meaning->unit = SR_UNIT_BOOLEAN; | |
a7c01629 | 482 | *floatval = (*floatval < 0.0 || *floatval > 25.0) ? 0.0 : 1.0; |
c01bdebc AJ |
483 | } |
484 | if (info->is_diode) { | |
b02bb45f UH |
485 | analog->meaning->mq = SR_MQ_VOLTAGE; |
486 | analog->meaning->unit = SR_UNIT_VOLT; | |
c01bdebc AJ |
487 | } |
488 | if (info->is_rpm) { | |
b02bb45f UH |
489 | analog->meaning->mq = SR_MQ_FREQUENCY; |
490 | analog->meaning->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE; | |
c01bdebc AJ |
491 | } |
492 | if (info->is_duty_cycle) { | |
b02bb45f UH |
493 | analog->meaning->mq = SR_MQ_DUTY_CYCLE; |
494 | analog->meaning->unit = SR_UNIT_PERCENTAGE; | |
c01bdebc AJ |
495 | } |
496 | ||
497 | /* Measurement related flags */ | |
498 | if (info->is_ac) | |
b02bb45f | 499 | analog->meaning->mqflags |= SR_MQFLAG_AC; |
c01bdebc | 500 | if (info->is_dc) |
b02bb45f | 501 | analog->meaning->mqflags |= SR_MQFLAG_DC; |
c01bdebc | 502 | if (info->is_auto) |
b02bb45f | 503 | analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE; |
c01bdebc | 504 | if (info->is_diode) |
b02bb45f | 505 | analog->meaning->mqflags |= SR_MQFLAG_DIODE; |
c01bdebc | 506 | if (info->is_hold) |
72e1672f UH |
507 | /* |
508 | * Note: HOLD only affects the number displayed on the LCD, | |
509 | * but not the value sent via the protocol! It also does not | |
510 | * affect the bargraph on the LCD. | |
511 | */ | |
b02bb45f | 512 | analog->meaning->mqflags |= SR_MQFLAG_HOLD; |
c01bdebc | 513 | if (info->is_max) |
b02bb45f | 514 | analog->meaning->mqflags |= SR_MQFLAG_MAX; |
c01bdebc | 515 | if (info->is_min) |
b02bb45f | 516 | analog->meaning->mqflags |= SR_MQFLAG_MIN; |
c01bdebc | 517 | if (info->is_rel) |
b02bb45f | 518 | analog->meaning->mqflags |= SR_MQFLAG_RELATIVE; |
c01bdebc AJ |
519 | |
520 | /* Other flags */ | |
521 | if (info->is_judge) | |
522 | sr_spew("Judge bit is set."); | |
523 | if (info->is_batt) | |
524 | sr_spew("Battery is low."); | |
525 | if (info->is_ol) | |
526 | sr_spew("Input overflow."); | |
527 | if (info->is_ul) | |
528 | sr_spew("Input underflow."); | |
529 | if (info->is_pmax) | |
530 | sr_spew("pMAX active, LCD shows max. peak value."); | |
531 | if (info->is_pmin) | |
532 | sr_spew("pMIN active, LCD shows min. peak value."); | |
533 | if (info->is_vahz) | |
534 | sr_spew("VAHZ active."); | |
535 | if (info->is_apo) | |
536 | sr_spew("Auto-Power-Off enabled."); | |
537 | if (info->is_vbar) | |
538 | sr_spew("VBAR active."); | |
539 | if ((!info->selectable_lpf && info->is_lpf1) || | |
540 | (info->selectable_lpf && (!info->is_lpf0 || !info->is_lpf1))) | |
541 | sr_spew("Low-pass filter feature is active."); | |
542 | } | |
543 | ||
544 | static gboolean flags_valid(const struct es519xx_info *info) | |
545 | { | |
546 | int count; | |
547 | ||
548 | /* Does the packet have more than one multiplier? */ | |
72e1672f UH |
549 | count = (info->is_micro) ? 1 : 0; |
550 | count += (info->is_milli) ? 1 : 0; | |
c01bdebc | 551 | if (count > 1) { |
ec5186f9 | 552 | sr_dbg("More than one multiplier detected in packet."); |
c01bdebc AJ |
553 | return FALSE; |
554 | } | |
555 | ||
556 | /* Does the packet "measure" more than one type of value? */ | |
72e1672f UH |
557 | count = (info->is_voltage) ? 1 : 0; |
558 | count += (info->is_current) ? 1 : 0; | |
559 | count += (info->is_resistance) ? 1 : 0; | |
560 | count += (info->is_frequency) ? 1 : 0; | |
561 | count += (info->is_capacitance) ? 1 : 0; | |
562 | count += (info->is_temperature) ? 1 : 0; | |
563 | count += (info->is_continuity) ? 1 : 0; | |
564 | count += (info->is_diode) ? 1 : 0; | |
565 | count += (info->is_rpm) ? 1 : 0; | |
c01bdebc | 566 | if (count > 1) { |
ec5186f9 | 567 | sr_dbg("More than one measurement type detected in packet."); |
c01bdebc AJ |
568 | return FALSE; |
569 | } | |
570 | ||
571 | /* Both AC and DC set? */ | |
572 | if (info->is_ac && info->is_dc) { | |
ec5186f9 | 573 | sr_dbg("Both AC and DC flags detected in packet."); |
c01bdebc AJ |
574 | return FALSE; |
575 | } | |
576 | ||
577 | return TRUE; | |
578 | } | |
579 | ||
580 | static gboolean sr_es519xx_packet_valid(const uint8_t *buf, | |
581 | struct es519xx_info *info) | |
582 | { | |
72e1672f UH |
583 | int s; |
584 | ||
585 | s = info->packet_size; | |
c01bdebc | 586 | |
72e1672f | 587 | if (s == 11 && memcmp(buf, buf + s, s)) |
c01bdebc AJ |
588 | return FALSE; |
589 | ||
72e1672f | 590 | if (buf[s - 2] != '\r' || buf[s - 1] != '\n') |
c01bdebc AJ |
591 | return FALSE; |
592 | ||
593 | parse_flags(buf, info); | |
594 | ||
595 | if (!flags_valid(info)) | |
596 | return FALSE; | |
597 | ||
598 | return TRUE; | |
599 | } | |
600 | ||
601 | static int sr_es519xx_parse(const uint8_t *buf, float *floatval, | |
b02bb45f | 602 | struct sr_datafeed_analog *analog, |
c01bdebc AJ |
603 | struct es519xx_info *info) |
604 | { | |
605 | int ret; | |
606 | ||
607 | if (!sr_es519xx_packet_valid(buf, info)) | |
608 | return SR_ERR; | |
609 | ||
610 | if ((ret = parse_value(buf, info, floatval)) != SR_OK) { | |
ec5186f9 | 611 | sr_dbg("Error parsing value: %d.", ret); |
c01bdebc AJ |
612 | return ret; |
613 | } | |
614 | ||
a7c01629 AJ |
615 | if ((ret = parse_range(buf[0], floatval, info)) != SR_OK) |
616 | return ret; | |
c01bdebc | 617 | |
8cbf5627 AJ |
618 | analog->encoding->digits = info->digits; |
619 | analog->spec->spec_digits = info->digits; | |
620 | ||
a7c01629 AJ |
621 | handle_flags(analog, floatval, info); |
622 | return SR_OK; | |
c01bdebc AJ |
623 | } |
624 | ||
c01bdebc | 625 | /* |
72e1672f | 626 | * Functions for 2400 baud / 11 bytes protocols. |
c01bdebc AJ |
627 | * This includes ES51962, ES51971, ES51972, ES51978 and ES51989. |
628 | */ | |
72e1672f | 629 | SR_PRIV gboolean sr_es519xx_2400_11b_packet_valid(const uint8_t *buf) |
c01bdebc | 630 | { |
35b904a7 | 631 | struct es519xx_info info; |
72e1672f | 632 | |
35b904a7 | 633 | memset(&info, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
634 | info.baudrate = 2400; |
635 | info.packet_size = 11; | |
636 | ||
c01bdebc AJ |
637 | return sr_es519xx_packet_valid(buf, &info); |
638 | } | |
639 | ||
72e1672f | 640 | SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval, |
b02bb45f | 641 | struct sr_datafeed_analog *analog, void *info) |
c01bdebc | 642 | { |
72e1672f UH |
643 | struct es519xx_info *info_local; |
644 | ||
645 | info_local = info; | |
94e9021b | 646 | memset(info_local, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
647 | info_local->baudrate = 2400; |
648 | info_local->packet_size = 11; | |
649 | ||
c01bdebc AJ |
650 | return sr_es519xx_parse(buf, floatval, analog, info); |
651 | } | |
652 | ||
c01bdebc | 653 | /* |
72e1672f | 654 | * Functions for 2400 baud / 11 byte protocols. |
c01bdebc AJ |
655 | * This includes ES51960, ES51977 and ES51988. |
656 | */ | |
72e1672f | 657 | SR_PRIV gboolean sr_es519xx_2400_11b_altfn_packet_valid(const uint8_t *buf) |
c01bdebc | 658 | { |
35b904a7 | 659 | struct es519xx_info info; |
72e1672f | 660 | |
35b904a7 | 661 | memset(&info, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
662 | info.baudrate = 2400; |
663 | info.packet_size = 11; | |
664 | info.alt_functions = TRUE; | |
665 | ||
c01bdebc AJ |
666 | return sr_es519xx_packet_valid(buf, &info); |
667 | } | |
668 | ||
72e1672f | 669 | SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf, |
b02bb45f | 670 | float *floatval, struct sr_datafeed_analog *analog, void *info) |
c01bdebc | 671 | { |
72e1672f UH |
672 | struct es519xx_info *info_local; |
673 | ||
674 | info_local = info; | |
94e9021b | 675 | memset(info_local, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
676 | info_local->baudrate = 2400; |
677 | info_local->packet_size = 11; | |
678 | info_local->alt_functions = TRUE; | |
679 | ||
c01bdebc AJ |
680 | return sr_es519xx_parse(buf, floatval, analog, info); |
681 | } | |
682 | ||
c01bdebc | 683 | /* |
72e1672f | 684 | * Functions for 19200 baud / 11 bytes protocols with 5 digits display. |
c01bdebc AJ |
685 | * This includes ES51911, ES51916 and ES51918. |
686 | */ | |
72e1672f | 687 | SR_PRIV gboolean sr_es519xx_19200_11b_5digits_packet_valid(const uint8_t *buf) |
c01bdebc | 688 | { |
35b904a7 | 689 | struct es519xx_info info; |
72e1672f | 690 | |
35b904a7 | 691 | memset(&info, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
692 | info.baudrate = 19200; |
693 | info.packet_size = 11; | |
694 | info.fivedigits = TRUE; | |
695 | ||
c01bdebc AJ |
696 | return sr_es519xx_packet_valid(buf, &info); |
697 | } | |
698 | ||
93d719cd | 699 | SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf, |
b02bb45f | 700 | float *floatval, struct sr_datafeed_analog *analog, void *info) |
c01bdebc | 701 | { |
72e1672f UH |
702 | struct es519xx_info *info_local; |
703 | ||
704 | info_local = info; | |
94e9021b | 705 | memset(info_local, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
706 | info_local->baudrate = 19200; |
707 | info_local->packet_size = 11; | |
708 | info_local->fivedigits = TRUE; | |
709 | ||
c01bdebc AJ |
710 | return sr_es519xx_parse(buf, floatval, analog, info); |
711 | } | |
712 | ||
c01bdebc | 713 | /* |
72e1672f | 714 | * Functions for 19200 baud / 11 bytes protocols with clamp meter support. |
c01bdebc AJ |
715 | * This includes ES51967 and ES51969. |
716 | */ | |
72e1672f | 717 | SR_PRIV gboolean sr_es519xx_19200_11b_clamp_packet_valid(const uint8_t *buf) |
c01bdebc | 718 | { |
35b904a7 | 719 | struct es519xx_info info; |
72e1672f | 720 | |
35b904a7 | 721 | memset(&info, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
722 | info.baudrate = 19200; |
723 | info.packet_size = 11; | |
724 | info.clampmeter = TRUE; | |
725 | ||
c01bdebc AJ |
726 | return sr_es519xx_packet_valid(buf, &info); |
727 | } | |
728 | ||
72e1672f | 729 | SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf, |
b02bb45f | 730 | float *floatval, struct sr_datafeed_analog *analog, void *info) |
c01bdebc | 731 | { |
72e1672f UH |
732 | struct es519xx_info *info_local; |
733 | ||
734 | info_local = info; | |
94e9021b | 735 | memset(info_local, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
736 | info_local->baudrate = 19200; |
737 | info_local->packet_size = 11; | |
738 | info_local->clampmeter = TRUE; | |
739 | ||
c01bdebc AJ |
740 | return sr_es519xx_parse(buf, floatval, analog, info); |
741 | } | |
742 | ||
c01bdebc | 743 | /* |
72e1672f | 744 | * Functions for 19200 baud / 11 bytes protocols. |
c01bdebc AJ |
745 | * This includes ES51981, ES51982, ES51983, ES51984 and ES51986. |
746 | */ | |
72e1672f | 747 | SR_PRIV gboolean sr_es519xx_19200_11b_packet_valid(const uint8_t *buf) |
c01bdebc | 748 | { |
35b904a7 | 749 | struct es519xx_info info; |
72e1672f | 750 | |
35b904a7 | 751 | memset(&info, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
752 | info.baudrate = 19200; |
753 | info.packet_size = 11; | |
754 | ||
c01bdebc AJ |
755 | return sr_es519xx_packet_valid(buf, &info); |
756 | } | |
757 | ||
72e1672f | 758 | SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval, |
b02bb45f | 759 | struct sr_datafeed_analog *analog, void *info) |
c01bdebc | 760 | { |
72e1672f UH |
761 | struct es519xx_info *info_local; |
762 | ||
763 | info_local = info; | |
94e9021b | 764 | memset(info_local, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
765 | info_local->baudrate = 19200; |
766 | info_local->packet_size = 11; | |
767 | ||
c01bdebc AJ |
768 | return sr_es519xx_parse(buf, floatval, analog, info); |
769 | } | |
770 | ||
c01bdebc | 771 | /* |
72e1672f | 772 | * Functions for 19200 baud / 14 bytes protocols. |
c01bdebc AJ |
773 | * This includes ES51921 and ES51922. |
774 | */ | |
72e1672f | 775 | SR_PRIV gboolean sr_es519xx_19200_14b_packet_valid(const uint8_t *buf) |
c01bdebc | 776 | { |
35b904a7 | 777 | struct es519xx_info info; |
72e1672f | 778 | |
35b904a7 | 779 | memset(&info, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
780 | info.baudrate = 19200; |
781 | info.packet_size = 14; | |
782 | ||
c01bdebc AJ |
783 | return sr_es519xx_packet_valid(buf, &info); |
784 | } | |
785 | ||
72e1672f | 786 | SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval, |
b02bb45f | 787 | struct sr_datafeed_analog *analog, void *info) |
c01bdebc | 788 | { |
72e1672f UH |
789 | struct es519xx_info *info_local; |
790 | ||
791 | info_local = info; | |
94e9021b | 792 | memset(info_local, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
793 | info_local->baudrate = 19200; |
794 | info_local->packet_size = 14; | |
795 | ||
c01bdebc AJ |
796 | return sr_es519xx_parse(buf, floatval, analog, info); |
797 | } | |
798 | ||
c01bdebc | 799 | /* |
72e1672f | 800 | * Functions for 19200 baud / 14 bytes protocols with selectable LPF. |
c01bdebc AJ |
801 | * This includes ES51931 and ES51932. |
802 | */ | |
72e1672f | 803 | SR_PRIV gboolean sr_es519xx_19200_14b_sel_lpf_packet_valid(const uint8_t *buf) |
c01bdebc | 804 | { |
35b904a7 | 805 | struct es519xx_info info; |
72e1672f | 806 | |
35b904a7 | 807 | memset(&info, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
808 | info.baudrate = 19200; |
809 | info.packet_size = 14; | |
810 | info.selectable_lpf = TRUE; | |
811 | ||
c01bdebc AJ |
812 | return sr_es519xx_packet_valid(buf, &info); |
813 | } | |
814 | ||
72e1672f | 815 | SR_PRIV int sr_es519xx_19200_14b_sel_lpf_parse(const uint8_t *buf, |
b02bb45f | 816 | float *floatval, struct sr_datafeed_analog *analog, void *info) |
c01bdebc | 817 | { |
72e1672f UH |
818 | struct es519xx_info *info_local; |
819 | ||
820 | info_local = info; | |
94e9021b | 821 | memset(info_local, 0, sizeof(struct es519xx_info)); |
72e1672f UH |
822 | info_local->baudrate = 19200; |
823 | info_local->packet_size = 14; | |
824 | info_local->selectable_lpf = TRUE; | |
825 | ||
c01bdebc AJ |
826 | return sr_es519xx_parse(buf, floatval, analog, info); |
827 | } |