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