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