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