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