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