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