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