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