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