]> sigrok.org Git - libsigrok.git/blame - src/dmm/vc870.c
dmm: vc870: show display value properly in debug output
[libsigrok.git] / src / dmm / vc870.c
CommitLineData
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"). */
32static 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
60static int parse_value(const uint8_t *buf, struct vc870_info *info,
61 float *result)
62{
63 int i, intval;
c36f78f7
UH
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 "
6433156c
DE
73 "(%c %c %c %c %c).",
74 buf[3], buf[4], buf[5], buf[6], buf[7],
75 buf[3], buf[4], buf[5], buf[6], buf[7]);
c36f78f7
UH
76 return SR_ERR;
77 }
78
79 intval = 0;
80 for (i = 0; i < 5; i++)
81 intval = 10 * intval + (buf[i + 3] - '0'); /* Main display. */
82 // intval = 10 * intval + (buf[i + 8] - '0'); /* TODO: Aux display. */
83
84 /* Apply sign. */
85 intval *= info->is_sign1 ? -1 : 1;
86 // intval *= info->is_sign2 ? -1 : 1; /* TODO: Fahrenheit / aux display. */
87
c36f78f7
UH
88 /* Note: The decimal point position will be parsed later. */
89
b89462e4 90 sr_spew("The display value without comma is %05d.", intval);
c36f78f7 91
b89462e4 92 *result = (float)intval;
c36f78f7
UH
93
94 return SR_OK;
95}
96
97static int parse_range(uint8_t b, float *floatval,
98 const struct vc870_info *info)
99{
100 int idx, mode;
101 float factor = 0;
102
103 idx = b - '0';
104
105 if (idx < 0 || idx > 7) {
106 sr_dbg("Invalid range byte / index: 0x%02x / 0x%02x.", b, idx);
107 return SR_ERR;
108 }
109
110 /* Parse range byte (depends on the measurement mode). */
111 if (info->is_voltage && info->is_dc && !info->is_milli)
112 mode = 0; /* DCV */
113 else if (info->is_voltage && info->is_ac)
114 mode = 1; /* ACV */
115 else if (info->is_voltage && info->is_dc && info->is_milli)
116 mode = 2; /* DCmV */
117 else if (info->is_temperature)
118 mode = 3; /* Temperature */
119 else if (info->is_resistance || info->is_continuity)
120 mode = 4; /* Resistance */
121 else if (info->is_continuity)
122 mode = 5; /* Continuity */
123 else if (info->is_capacitance)
124 mode = 6; /* Capacitance */
125 else if (info->is_diode)
126 mode = 7; /* Diode */
127 else if (info->is_frequency)
128 mode = 8; /* Frequency */
129 else if (info->is_loop_current)
130 mode = 9; /* Loop current */
131 else if (info->is_current && info->is_micro && info->is_dc)
132 mode = 10; /* DCµA */
133 else if (info->is_current && info->is_micro && info->is_ac)
134 mode = 11; /* ACµA */
135 else if (info->is_current && info->is_milli && info->is_dc)
136 mode = 12; /* DCmA */
137 else if (info->is_current && info->is_milli && info->is_ac)
138 mode = 13; /* ACmA */
139 else if (info->is_current && !info->is_milli && !info->is_micro && info->is_dc)
140 mode = 14; /* DCA */
141 else if (info->is_current && !info->is_milli && !info->is_micro && info->is_ac)
142 mode = 15; /* ACA */
143 else if (info->is_power_apparent_power)
144 mode = 16; /* Act+apparent power */
145 else if (info->is_power_factor_freq)
146 mode = 17; /* Power factor / freq */
147 else if (info->is_v_a_eff_value)
148 mode = 18; /* V eff + A eff */
149 else {
150 sr_dbg("Invalid mode, range byte was: 0x%02x.", b);
151 return SR_ERR;
152 }
153
154 factor = factors[mode][idx];
155
156 if (factor == 0) {
157 sr_dbg("Invalid factor for range byte: 0x%02x (mode=%d, idx=%d).", b, mode, idx);
158 return SR_ERR;
159 }
160
161 /* Apply respective factor (mode-dependent) on the value. */
162 *floatval *= factor;
163 sr_dbg("Applying factor %f, new value is %f.", factor, *floatval);
164
165 return SR_OK;
166}
167
168static 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_eff_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 info->is_rms = TRUE;
278}
279
5faebab2 280static void handle_flags(struct sr_datafeed_analog_old *analog,
c36f78f7
UH
281 float *floatval, const struct vc870_info *info)
282{
283 /*
284 * Note: is_micro etc. are not used directly to multiply/divide
285 * floatval, this is handled via parse_range() and factors[][].
286 */
287
288 /* Measurement modes */
289 if (info->is_voltage) {
290 analog->mq = SR_MQ_VOLTAGE;
291 analog->unit = SR_UNIT_VOLT;
292 }
293 if (info->is_current) {
294 analog->mq = SR_MQ_CURRENT;
295 analog->unit = SR_UNIT_AMPERE;
296 }
297 if (info->is_resistance) {
298 analog->mq = SR_MQ_RESISTANCE;
299 analog->unit = SR_UNIT_OHM;
300 }
301 if (info->is_frequency) {
302 analog->mq = SR_MQ_FREQUENCY;
303 analog->unit = SR_UNIT_HERTZ;
304 }
305 if (info->is_capacitance) {
306 analog->mq = SR_MQ_CAPACITANCE;
307 analog->unit = SR_UNIT_FARAD;
308 }
309 if (info->is_temperature) {
310 analog->mq = SR_MQ_TEMPERATURE;
311 analog->unit = SR_UNIT_CELSIUS;
312 /* TODO: Handle Fahrenheit in auxiliary display. */
313 // analog->unit = SR_UNIT_FAHRENHEIT;
314 }
315 if (info->is_continuity) {
316 analog->mq = SR_MQ_CONTINUITY;
317 analog->unit = SR_UNIT_BOOLEAN;
318 /* Vendor docs: "< 20 Ohm acoustic" */
319 *floatval = (*floatval < 0.0 || *floatval > 20.0) ? 0.0 : 1.0;
320 }
321 if (info->is_diode) {
322 analog->mq = SR_MQ_VOLTAGE;
323 analog->unit = SR_UNIT_VOLT;
324 }
325 if (info->is_loop_current) {
326 /* 4mA = 0%, 20mA = 100% */
327 analog->mq = SR_MQ_CURRENT;
328 analog->unit = SR_UNIT_PERCENTAGE;
329 }
330 if (info->is_power) {
331 analog->mq = SR_MQ_POWER;
332 analog->unit = SR_UNIT_WATT;
333 }
334 if (info->is_power_factor_freq) {
335 /* TODO: Handle power factor. */
336 // analog->mq = SR_MQ_POWER_FACTOR;
337 // analog->unit = SR_UNIT_UNITLESS;
338 analog->mq = SR_MQ_FREQUENCY;
339 analog->unit = SR_UNIT_HERTZ;
340 }
341 if (info->is_power_apparent_power) {
342 analog->mq = SR_MQ_POWER;
343 analog->unit = SR_UNIT_WATT;
344 /* TODO: Handle apparent power. */
345 // analog->mq = SR_MQ_APPARENT_POWER;
346 // analog->unit = SR_UNIT_VOLT_AMPERE;
347 }
348
349 /* Measurement related flags */
350 if (info->is_ac)
351 analog->mqflags |= SR_MQFLAG_AC;
352 if (info->is_dc)
353 analog->mqflags |= SR_MQFLAG_DC;
354 if (info->is_auto)
355 analog->mqflags |= SR_MQFLAG_AUTORANGE;
356 if (info->is_diode)
357 analog->mqflags |= SR_MQFLAG_DIODE;
358 if (info->is_hold)
359 /*
360 * Note: HOLD only affects the number displayed on the LCD,
361 * but not the value sent via the protocol! It also does not
362 * affect the bargraph on the LCD.
363 */
364 analog->mqflags |= SR_MQFLAG_HOLD;
365 if (info->is_max)
366 analog->mqflags |= SR_MQFLAG_MAX;
367 if (info->is_min)
368 analog->mqflags |= SR_MQFLAG_MIN;
369 if (info->is_rel)
370 analog->mqflags |= SR_MQFLAG_RELATIVE;
371
372 /* Other flags */
373 if (info->is_batt)
374 sr_spew("Battery is low.");
375 if (info->is_auto_power)
376 sr_spew("Auto-Power-Off enabled.");
377}
378
379static gboolean flags_valid(const struct vc870_info *info)
380{
c35276dd
UH
381 (void)info;
382
c36f78f7
UH
383 /* TODO: Implement. */
384 return TRUE;
385}
386
387SR_PRIV gboolean sr_vc870_packet_valid(const uint8_t *buf)
388{
389 struct vc870_info info;
390
391 /* Byte 21: Always '\r' (carriage return, 0x0d, 13) */
392 /* Byte 22: Always '\n' (newline, 0x0a, 10) */
393 if (buf[21] != '\r' || buf[22] != '\n')
394 return FALSE;
395
396 parse_flags(buf, &info);
397
398 return flags_valid(&info);
399}
400
401SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval,
5faebab2 402 struct sr_datafeed_analog_old *analog, void *info)
c36f78f7
UH
403{
404 int ret;
405 struct vc870_info *info_local;
406
c36f78f7
UH
407 info_local = (struct vc870_info *)info;
408 memset(info_local, 0, sizeof(struct vc870_info));
409
410 if (!sr_vc870_packet_valid(buf))
411 return SR_ERR;
412
413 parse_flags(buf, info_local);
414
415 if ((ret = parse_value(buf, info_local, floatval)) != SR_OK) {
416 sr_dbg("Error parsing value: %d.", ret);
417 return ret;
418 }
419
420 if ((ret = parse_range(buf[2], floatval, info_local)) != SR_OK)
421 return ret;
422
423 handle_flags(analog, floatval, info_local);
424
425 return SR_OK;
426}