]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014-2015 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <string.h> | |
23 | #include <ctype.h> | |
24 | #include <math.h> | |
25 | #include <glib.h> | |
26 | #include <libsigrok/libsigrok.h> | |
27 | #include "libsigrok-internal.h" | |
28 | ||
29 | #define LOG_PREFIX "vc870" | |
30 | ||
31 | /* Exponents for the respective measurement mode. */ | |
32 | static const int exponents[][8] = { | |
33 | { -4, -3, -2, -1, 0, 0, 0, 0 }, /* DCV */ | |
34 | { -3, -2, -1, 0, 0, 0, 0, 0 }, /* ACV */ | |
35 | { -5, 0, 0, 0, 0, 0, 0, 0 }, /* DCmV */ | |
36 | { -1, 0, 0, 0, 0, 0, 0, 0 }, /* Temperature (C) */ | |
37 | // { -2, 0, 0, 0, 0, 0, 0, 0 }, /* TODO: Temperature (F) */ | |
38 | /* | |
39 | * Note: The sequence -1 -> 1 for the resistance | |
40 | * value is correct and verified in practice! | |
41 | * Don't trust the vendor docs on this. | |
42 | */ | |
43 | { -2, -1, 1, 2, 3, 4, 0, 0 }, /* Resistance */ | |
44 | { -2, 0, 0, 0, 0, 0, 0, 0 }, /* Continuity */ | |
45 | { -12, -11, -10, -9, -8, -7, -6, 0 }, /* Capacitance */ | |
46 | { -4, 0, 0, 0, 0, 0, 0, 0 }, /* Diode */ | |
47 | { -3, -2, -1, 0, 1, 2, 3, 4 }, /* Frequency */ | |
48 | { -2, 0, 0, 0, 0, 0, 0, 0 }, /* Loop current */ | |
49 | /* | |
50 | * Note: Measurements showed that AC and DC differ | |
51 | * in the exponents used, although docs say they should | |
52 | * be the same. | |
53 | */ | |
54 | { -8, -7, 0, 0, 0, 0, 0, 0 }, /* DCµA */ | |
55 | { -7, -6, 0, 0, 0, 0, 0, 0 }, /* ACµA */ | |
56 | { -6, -5, 0, 0, 0, 0, 0, 0 }, /* DCmA */ | |
57 | { -5, -4, 0, 0, 0, 0, 0, 0 }, /* ACmA */ | |
58 | { -3, 0, 0, 0, 0, 0, 0, 0 }, /* DCA */ | |
59 | /* TODO: Verify exponent for ACA */ | |
60 | { -3, 0, 0, 0, 0, 0, 0, 0 }, /* ACA */ | |
61 | { -1, 0, 0, 0, 0, 0, 0, 0 }, /* Act+apparent power */ | |
62 | { -3, 0, 0, 0, 0, 0, 0, 0 }, /* Power exponent / freq */ | |
63 | { -1, 0, 0, 0, 0, 0, 0, 0 }, /* V eff + A eff */ | |
64 | }; | |
65 | ||
66 | static int parse_value(const uint8_t *buf, struct vc870_info *info, | |
67 | float *result) | |
68 | { | |
69 | int i, intval; | |
70 | ||
71 | /* Bytes 3-7: Main display value (5 decimal digits) */ | |
72 | if (info->is_open || info->is_ol1) { | |
73 | sr_spew("Over limit."); | |
74 | *result = INFINITY; | |
75 | return SR_OK; | |
76 | } else if (!isdigit(buf[3]) || !isdigit(buf[4]) || | |
77 | !isdigit(buf[5]) || !isdigit(buf[6]) || !isdigit(buf[7])) { | |
78 | sr_dbg("Invalid digits: %02x %02x %02x %02x %02X " | |
79 | "(%c %c %c %c %c).", | |
80 | buf[3], buf[4], buf[5], buf[6], buf[7], | |
81 | buf[3], buf[4], buf[5], buf[6], buf[7]); | |
82 | return SR_ERR; | |
83 | } | |
84 | ||
85 | intval = 0; | |
86 | for (i = 0; i < 5; i++) | |
87 | intval = 10 * intval + (buf[i + 3] - '0'); /* Main display. */ | |
88 | // intval = 10 * intval + (buf[i + 8] - '0'); /* TODO: Aux display. */ | |
89 | ||
90 | /* Apply sign. */ | |
91 | intval *= info->is_sign1 ? -1 : 1; | |
92 | // intval *= info->is_sign2 ? -1 : 1; /* TODO: Fahrenheit / aux display. */ | |
93 | ||
94 | /* Note: The decimal point position will be parsed later. */ | |
95 | ||
96 | sr_spew("The display value without comma is %05d.", intval); | |
97 | ||
98 | *result = (float)intval; | |
99 | ||
100 | return SR_OK; | |
101 | } | |
102 | ||
103 | static int parse_range(uint8_t b, float *floatval, int *exponent, | |
104 | const struct vc870_info *info) | |
105 | { | |
106 | int idx, mode; | |
107 | ||
108 | idx = b - '0'; | |
109 | ||
110 | if (idx < 0 || idx > 7) { | |
111 | sr_dbg("Invalid range byte / index: 0x%02x / 0x%02x.", b, idx); | |
112 | return SR_ERR; | |
113 | } | |
114 | ||
115 | /* Parse range byte (depends on the measurement mode). */ | |
116 | if (info->is_voltage && info->is_dc && !info->is_milli) | |
117 | mode = 0; /* DCV */ | |
118 | else if (info->is_voltage && info->is_ac) | |
119 | mode = 1; /* ACV */ | |
120 | else if (info->is_voltage && info->is_dc && info->is_milli) | |
121 | mode = 2; /* DCmV */ | |
122 | else if (info->is_temperature) | |
123 | mode = 3; /* Temperature */ | |
124 | else if (info->is_resistance || info->is_continuity) | |
125 | mode = 4; /* Resistance */ | |
126 | else if (info->is_continuity) | |
127 | mode = 5; /* Continuity */ | |
128 | else if (info->is_capacitance) | |
129 | mode = 6; /* Capacitance */ | |
130 | else if (info->is_diode) | |
131 | mode = 7; /* Diode */ | |
132 | else if (info->is_frequency) | |
133 | mode = 8; /* Frequency */ | |
134 | else if (info->is_loop_current) | |
135 | mode = 9; /* Loop current */ | |
136 | else if (info->is_current && info->is_micro && info->is_dc) | |
137 | mode = 10; /* DCµA */ | |
138 | else if (info->is_current && info->is_micro && info->is_ac) | |
139 | mode = 11; /* ACµA */ | |
140 | else if (info->is_current && info->is_milli && info->is_dc) | |
141 | mode = 12; /* DCmA */ | |
142 | else if (info->is_current && info->is_milli && info->is_ac) | |
143 | mode = 13; /* ACmA */ | |
144 | else if (info->is_current && !info->is_milli && !info->is_micro && info->is_dc) | |
145 | mode = 14; /* DCA */ | |
146 | else if (info->is_current && !info->is_milli && !info->is_micro && info->is_ac) | |
147 | mode = 15; /* ACA */ | |
148 | else if (info->is_power_apparent_power) | |
149 | mode = 16; /* Act+apparent power */ | |
150 | else if (info->is_power_factor_freq) | |
151 | mode = 17; /* Power factor / freq */ | |
152 | else if (info->is_v_a_rms_value) | |
153 | mode = 18; /* V eff + A eff */ | |
154 | else { | |
155 | sr_dbg("Invalid mode, range byte was: 0x%02x.", b); | |
156 | return SR_ERR; | |
157 | } | |
158 | ||
159 | *exponent = exponents[mode][idx]; | |
160 | ||
161 | /* Apply respective exponent (mode-dependent) on the value. */ | |
162 | *floatval *= powf(10, *exponent); | |
163 | sr_dbg("Applying exponent %d, new value is %f.", *exponent, *floatval); | |
164 | ||
165 | return SR_OK; | |
166 | } | |
167 | ||
168 | static void parse_flags(const uint8_t *buf, struct vc870_info *info) | |
169 | { | |
170 | /* Bytes 0/1: Function / function select */ | |
171 | /* Note: Some of these mappings are fixed up later. */ | |
172 | switch (buf[0]) { | |
173 | case 0x30: /* DCV / ACV */ | |
174 | info->is_voltage = TRUE; | |
175 | info->is_dc = (buf[1] == 0x30); | |
176 | info->is_ac = (buf[1] == 0x31); | |
177 | break; | |
178 | case 0x31: /* DCmV / Celsius */ | |
179 | if (buf[1] == 0x30) | |
180 | info->is_voltage = info->is_milli = info->is_dc = TRUE; | |
181 | else if (buf[1] == 0x31) | |
182 | info->is_temperature = TRUE; | |
183 | break; | |
184 | case 0x32: /* Resistance / Short-circuit test */ | |
185 | info->is_resistance = (buf[1] == 0x30); | |
186 | info->is_continuity = (buf[1] == 0x31); | |
187 | break; | |
188 | case 0x33: /* Capacitance */ | |
189 | info->is_capacitance = (buf[1] == 0x30); | |
190 | break; | |
191 | case 0x34: /* Diode */ | |
192 | info->is_diode = (buf[1] == 0x30); | |
193 | break; | |
194 | case 0x35: /* (4~20mA)% */ | |
195 | info->is_frequency = (buf[1] == 0x30); | |
196 | info->is_loop_current = (buf[1] == 0x31); | |
197 | break; | |
198 | case 0x36: /* DCµA / ACµA */ | |
199 | info->is_current = info->is_micro = TRUE; | |
200 | info->is_dc = (buf[1] == 0x30); | |
201 | info->is_ac = (buf[1] == 0x31); | |
202 | break; | |
203 | case 0x37: /* DCmA / ACmA */ | |
204 | info->is_current = info->is_milli = TRUE; | |
205 | info->is_dc = (buf[1] == 0x30); | |
206 | info->is_ac = (buf[1] == 0x31); | |
207 | break; | |
208 | case 0x38: /* DCA / ACA */ | |
209 | info->is_current = TRUE; | |
210 | info->is_dc = (buf[1] == 0x30); | |
211 | info->is_ac = (buf[1] == 0x31); | |
212 | break; | |
213 | case 0x39: /* Active power + apparent power / power factor + frequency */ | |
214 | if (buf[1] == 0x30) | |
215 | /* Active power + apparent power */ | |
216 | info->is_power_apparent_power = TRUE; | |
217 | else if (buf[1] == 0x31) | |
218 | /* Power factor + frequency */ | |
219 | info->is_power_factor_freq = TRUE; | |
220 | else if (buf[1] == 0x32) | |
221 | /* Voltage effective value + current effective value */ | |
222 | info->is_v_a_rms_value = TRUE; | |
223 | break; | |
224 | default: | |
225 | sr_dbg("Invalid function bytes: %02x %02x.", buf[0], buf[1]); | |
226 | break; | |
227 | } | |
228 | ||
229 | /* Byte 2: Range */ | |
230 | ||
231 | /* Byte 3-7: Main display digits */ | |
232 | ||
233 | /* Byte 8-12: Auxiliary display digits */ | |
234 | ||
235 | /* Byte 13: TODO: "Simulate strip tens digit". */ | |
236 | ||
237 | /* Byte 14: TODO: "Simulate strip the single digit". */ | |
238 | ||
239 | /* Byte 15: Status */ | |
240 | info->is_sign2 = (buf[15] & (1 << 3)) != 0; | |
241 | info->is_sign1 = (buf[15] & (1 << 2)) != 0; | |
242 | info->is_batt = (buf[15] & (1 << 1)) != 0; /* Bat. low */ | |
243 | info->is_ol1 = (buf[15] & (1 << 0)) != 0; /* Overflow (main display) */ | |
244 | ||
245 | /* Byte 16: Option 1 */ | |
246 | info->is_max = (buf[16] & (1 << 3)) != 0; | |
247 | info->is_min = (buf[16] & (1 << 2)) != 0; | |
248 | info->is_maxmin = (buf[16] & (1 << 1)) != 0; | |
249 | info->is_rel = (buf[16] & (1 << 0)) != 0; | |
250 | ||
251 | /* Byte 17: Option 2 */ | |
252 | info->is_ol2 = (buf[17] & (1 << 3)) != 0; | |
253 | info->is_open = (buf[17] & (1 << 2)) != 0; | |
254 | info->is_manu = (buf[17] & (1 << 1)) != 0; /* Manual mode */ | |
255 | info->is_hold = (buf[17] & (1 << 0)) != 0; /* Hold */ | |
256 | ||
257 | /* Byte 18: Option 3 */ | |
258 | info->is_light = (buf[18] & (1 << 3)) != 0; | |
259 | info->is_usb = (buf[18] & (1 << 2)) != 0; /* Always on */ | |
260 | info->is_warning = (buf[18] & (1 << 1)) != 0; /* Never seen? */ | |
261 | info->is_auto_power = (buf[18] & (1 << 0)) != 0; /* Always on */ | |
262 | ||
263 | /* Byte 19: Option 4 */ | |
264 | info->is_misplug_warn = (buf[19] & (1 << 3)) != 0; /* Never gets set? */ | |
265 | info->is_lo = (buf[19] & (1 << 2)) != 0; | |
266 | info->is_hi = (buf[19] & (1 << 1)) != 0; | |
267 | info->is_open2 = (buf[19] & (1 << 0)) != 0; /* TODO: Unknown. */ | |
268 | ||
269 | /* Byte 20: Dual display bit */ | |
270 | info->is_dual_display = (buf[20] & (1 << 0)) != 0; | |
271 | ||
272 | /* Byte 21: Always '\r' (carriage return, 0x0d, 13) */ | |
273 | ||
274 | /* Byte 22: Always '\n' (newline, 0x0a, 10) */ | |
275 | ||
276 | info->is_auto = !info->is_manu; | |
277 | } | |
278 | ||
279 | static void handle_flags(struct sr_datafeed_analog *analog, | |
280 | float *floatval, const struct vc870_info *info) | |
281 | { | |
282 | /* | |
283 | * Note: is_micro etc. are not used directly to multiply/divide | |
284 | * floatval, this is handled via parse_range() and exponents[][]. | |
285 | */ | |
286 | ||
287 | /* Measurement modes */ | |
288 | if (info->is_voltage) { | |
289 | analog->meaning->mq = SR_MQ_VOLTAGE; | |
290 | analog->meaning->unit = SR_UNIT_VOLT; | |
291 | } | |
292 | if (info->is_current) { | |
293 | analog->meaning->mq = SR_MQ_CURRENT; | |
294 | analog->meaning->unit = SR_UNIT_AMPERE; | |
295 | } | |
296 | if (info->is_resistance) { | |
297 | analog->meaning->mq = SR_MQ_RESISTANCE; | |
298 | analog->meaning->unit = SR_UNIT_OHM; | |
299 | } | |
300 | if (info->is_frequency) { | |
301 | analog->meaning->mq = SR_MQ_FREQUENCY; | |
302 | analog->meaning->unit = SR_UNIT_HERTZ; | |
303 | } | |
304 | if (info->is_capacitance) { | |
305 | analog->meaning->mq = SR_MQ_CAPACITANCE; | |
306 | analog->meaning->unit = SR_UNIT_FARAD; | |
307 | } | |
308 | if (info->is_temperature) { | |
309 | analog->meaning->mq = SR_MQ_TEMPERATURE; | |
310 | analog->meaning->unit = SR_UNIT_CELSIUS; | |
311 | /* TODO: Handle Fahrenheit in auxiliary display. */ | |
312 | // analog->meaning->unit = SR_UNIT_FAHRENHEIT; | |
313 | } | |
314 | if (info->is_continuity) { | |
315 | analog->meaning->mq = SR_MQ_CONTINUITY; | |
316 | analog->meaning->unit = SR_UNIT_BOOLEAN; | |
317 | /* Vendor docs: "< 20 Ohm acoustic" */ | |
318 | *floatval = (*floatval < 0.0 || *floatval > 20.0) ? 0.0 : 1.0; | |
319 | } | |
320 | if (info->is_diode) { | |
321 | analog->meaning->mq = SR_MQ_VOLTAGE; | |
322 | analog->meaning->unit = SR_UNIT_VOLT; | |
323 | } | |
324 | if (info->is_loop_current) { | |
325 | /* 4mA = 0%, 20mA = 100% */ | |
326 | analog->meaning->mq = SR_MQ_CURRENT; | |
327 | analog->meaning->unit = SR_UNIT_PERCENTAGE; | |
328 | } | |
329 | if (info->is_power) { | |
330 | analog->meaning->mq = SR_MQ_POWER; | |
331 | analog->meaning->unit = SR_UNIT_WATT; | |
332 | } | |
333 | if (info->is_power_apparent_power) { | |
334 | analog->meaning->mq = SR_MQ_POWER; | |
335 | analog->meaning->unit = SR_UNIT_WATT; | |
336 | /* TODO: Handle apparent power. */ | |
337 | // analog->meaning->mq = SR_MQ_APPARENT_POWER; | |
338 | // analog->meaning->unit = SR_UNIT_VOLT_AMPERE; | |
339 | } | |
340 | if (info->is_power_factor_freq) { | |
341 | analog->meaning->mq = SR_MQ_POWER_FACTOR; | |
342 | analog->meaning->unit = SR_UNIT_UNITLESS; | |
343 | /* TODO: Handle frequency. */ | |
344 | // analog->meaning->mq = SR_MQ_FREQUENCY; | |
345 | // analog->meaning->unit = SR_UNIT_HERTZ; | |
346 | } | |
347 | if (info->is_v_a_rms_value) { | |
348 | analog->meaning->mqflags |= SR_MQFLAG_RMS; | |
349 | analog->meaning->mq = SR_MQ_VOLTAGE; | |
350 | analog->meaning->unit = SR_UNIT_VOLT; | |
351 | /* TODO: Handle effective current value */ | |
352 | // analog->meaning->mq = SR_MQ_CURRENT; | |
353 | // analog->meaning->unit = SR_UNIT_AMPERE; | |
354 | } | |
355 | ||
356 | /* Measurement related flags */ | |
357 | if (info->is_ac) | |
358 | analog->meaning->mqflags |= SR_MQFLAG_AC; | |
359 | if (info->is_dc) | |
360 | analog->meaning->mqflags |= SR_MQFLAG_DC; | |
361 | if (info->is_auto) | |
362 | analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE; | |
363 | if (info->is_diode) | |
364 | analog->meaning->mqflags |= SR_MQFLAG_DIODE; | |
365 | if (info->is_hold) | |
366 | /* | |
367 | * Note: HOLD only affects the number displayed on the LCD, | |
368 | * but not the value sent via the protocol! It also does not | |
369 | * affect the bargraph on the LCD. | |
370 | */ | |
371 | analog->meaning->mqflags |= SR_MQFLAG_HOLD; | |
372 | if (info->is_max) | |
373 | analog->meaning->mqflags |= SR_MQFLAG_MAX; | |
374 | if (info->is_min) | |
375 | analog->meaning->mqflags |= SR_MQFLAG_MIN; | |
376 | if (info->is_rel) | |
377 | analog->meaning->mqflags |= SR_MQFLAG_RELATIVE; | |
378 | ||
379 | /* Other flags */ | |
380 | if (info->is_batt) | |
381 | sr_spew("Battery is low."); | |
382 | if (info->is_auto_power) | |
383 | sr_spew("Auto-Power-Off enabled."); | |
384 | } | |
385 | ||
386 | static gboolean flags_valid(const struct vc870_info *info) | |
387 | { | |
388 | (void)info; | |
389 | ||
390 | /* TODO: Implement. */ | |
391 | return TRUE; | |
392 | } | |
393 | ||
394 | SR_PRIV gboolean sr_vc870_packet_valid(const uint8_t *buf) | |
395 | { | |
396 | struct vc870_info info; | |
397 | ||
398 | /* Byte 21: Always '\r' (carriage return, 0x0d, 13) */ | |
399 | /* Byte 22: Always '\n' (newline, 0x0a, 10) */ | |
400 | if (buf[21] != '\r' || buf[22] != '\n') | |
401 | return FALSE; | |
402 | ||
403 | parse_flags(buf, &info); | |
404 | ||
405 | return flags_valid(&info); | |
406 | } | |
407 | ||
408 | SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval, | |
409 | struct sr_datafeed_analog *analog, void *info) | |
410 | { | |
411 | int ret, exponent = 0; | |
412 | struct vc870_info *info_local; | |
413 | ||
414 | info_local = (struct vc870_info *)info; | |
415 | memset(info_local, 0, sizeof(struct vc870_info)); | |
416 | ||
417 | if (!sr_vc870_packet_valid(buf)) | |
418 | return SR_ERR; | |
419 | ||
420 | parse_flags(buf, info_local); | |
421 | ||
422 | if ((ret = parse_value(buf, info_local, floatval)) != SR_OK) { | |
423 | sr_dbg("Error parsing value: %d.", ret); | |
424 | return ret; | |
425 | } | |
426 | ||
427 | if ((ret = parse_range(buf[2], floatval, &exponent, info_local)) != SR_OK) | |
428 | return ret; | |
429 | ||
430 | handle_flags(analog, floatval, info_local); | |
431 | ||
432 | analog->encoding->digits = -exponent; | |
433 | analog->spec->spec_digits = -exponent; | |
434 | ||
435 | return SR_OK; | |
436 | } |