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