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