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