]> sigrok.org Git - libsigrok.git/blob - hardware/common/dmm/fs9922.c
Voltcraft VC-830: Fix diode mode handling.
[libsigrok.git] / hardware / common / dmm / fs9922.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 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 /*
22  * Fortune Semiconductor FS9922-DMM3/FS9922-DMM4 protocol parser.
23  */
24
25 #include <string.h>
26 #include <ctype.h>
27 #include <math.h>
28 #include <glib.h>
29 #include "libsigrok.h"
30 #include "libsigrok-internal.h"
31
32 /* Message logging helpers with subsystem-specific prefix string. */
33 #define LOG_PREFIX "fs9922: "
34 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
35 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
36 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
37 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
38 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
39 #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
40
41 static gboolean flags_valid(const struct fs9922_info *info)
42 {
43         int count;
44
45         /* Does the packet have more than one multiplier? */
46         count = 0;
47         count += (info->is_nano) ? 1 : 0;
48         count += (info->is_micro) ? 1 : 0;
49         count += (info->is_milli) ? 1 : 0;
50         count += (info->is_kilo) ? 1 : 0;
51         count += (info->is_mega) ? 1 : 0;
52         if (count > 1) {
53                 sr_err("More than one multiplier detected in packet.");
54                 return FALSE;
55         }
56
57         /*
58          * Does the packet "measure" more than one type of value?
59          *
60          * Note: In "diode mode", both is_diode and is_volt will be set.
61          * That is a valid use-case, so we don't want to error out below
62          * if it happens. Thus, we don't check for is_diode here.
63          */
64         count = 0;
65         // count += (info->is_diode) ? 1 : 0;
66         count += (info->is_percent) ? 1 : 0;
67         count += (info->is_volt) ? 1 : 0;
68         count += (info->is_ampere) ? 1 : 0;
69         count += (info->is_ohm) ? 1 : 0;
70         count += (info->is_hfe) ? 1 : 0;
71         count += (info->is_hertz) ? 1 : 0;
72         count += (info->is_farad) ? 1 : 0;
73         count += (info->is_celsius) ? 1 : 0;
74         count += (info->is_fahrenheit) ? 1 : 0;
75         if (count > 1) {
76                 sr_err("More than one measurement type detected in packet.");
77                 return FALSE;
78         }
79
80         /* Both AC and DC set? */
81         if (info->is_ac && info->is_dc) {
82                 sr_err("Both AC and DC flags detected in packet.");
83                 return FALSE;
84         }
85
86         /* Both Celsius and Fahrenheit set? */
87         if (info->is_celsius && info->is_fahrenheit) {
88                 sr_err("Both Celsius and Fahrenheit flags detected in packet.");
89                 return FALSE;
90         }
91
92         return TRUE;
93 }
94
95 static int parse_value(const uint8_t *buf, float *result)
96 {
97         int sign, intval;
98         float floatval;
99
100         /* Byte 0: Sign ('+' or '-') */
101         if (buf[0] == '+') {
102                 sign = 1;
103         } else if (buf[0] == '-') {
104                 sign = -1;
105         } else {
106                 sr_err("Invalid sign byte: 0x%02x.", buf[0]);
107                 return SR_ERR;
108         }
109
110         /*
111          * Bytes 1-4: Value (4 decimal digits)
112          *
113          * Over limit: "0.L" on the display, "?0:?" as protocol "digits".
114          */
115         if (buf[1] == '?' && buf[2] == '0' && buf[3] == ':' && buf[4] == '?') {
116                 sr_spew("Over limit.");
117                 *result = INFINITY;
118                 return SR_OK;
119         } else if (!isdigit(buf[1]) || !isdigit(buf[2]) ||
120                    !isdigit(buf[3]) || !isdigit(buf[4])) {
121                 sr_err("Value contained invalid digits: %02x %02x %02x %02x ("
122                        "%c %c %c %c).", buf[1], buf[2], buf[3], buf[4]);
123                 return SR_ERR;
124         }
125         intval = 0;
126         intval += (buf[1] - '0') * 1000;
127         intval += (buf[2] - '0') * 100;
128         intval += (buf[3] - '0') * 10;
129         intval += (buf[4] - '0') * 1;
130
131         floatval = (float)intval;
132
133         /* Byte 5: Always ' ' (space, 0x20) */
134
135         /*
136          * Byte 6: Decimal point position ('0', '1', '2', or '4')
137          *
138          * Note: The Fortune Semiconductor FS9922-DMM3/4 datasheets both have
139          * an error/typo here. They claim that the values '0'/'1'/'2'/'3' are
140          * used, but '0'/'1'/'2'/'4' is actually correct.
141          */
142         if (buf[6] != '0' && buf[6] != '1' && buf[6] != '2' && buf[6] != '4') {
143                 sr_err("Invalid decimal point value: 0x%02x.", buf[6]);
144                 return SR_ERR;
145         }
146         if (buf[6] == '0')
147                 floatval /= 1;
148         else if (buf[6] == '1')
149                 floatval /= 1000;
150         else if (buf[6] == '2')
151                 floatval /= 100;
152         else if (buf[6] == '4')
153                 floatval /= 10;
154
155         /* Apply sign. */
156         floatval *= sign;
157
158         sr_spew("The display value is %f.", floatval);
159
160         *result = floatval;
161
162         return SR_OK;
163 }
164
165 static void parse_flags(const uint8_t *buf, struct fs9922_info *info)
166 {
167         /* Z1/Z2/Z3/Z4 are bits for user-defined LCD symbols (on/off). */
168
169         /* Byte 7 */
170         /* Bit 7: Always 0 */
171         /* Bit 6: Always 0 */
172         info->is_auto       = (buf[7] & (1 << 5)) != 0;
173         info->is_dc         = (buf[7] & (1 << 4)) != 0;
174         info->is_ac         = (buf[7] & (1 << 3)) != 0;
175         info->is_rel        = (buf[7] & (1 << 2)) != 0;
176         info->is_hold       = (buf[7] & (1 << 1)) != 0;
177         info->is_bpn        = (buf[7] & (1 << 0)) != 0; /* Bargraph shown */
178
179         /* Byte 8 */
180         info->is_z1         = (buf[8] & (1 << 7)) != 0; /* User symbol 1 */
181         info->is_z2         = (buf[8] & (1 << 6)) != 0; /* User symbol 2 */
182         info->is_max        = (buf[8] & (1 << 5)) != 0;
183         info->is_min        = (buf[8] & (1 << 4)) != 0;
184         info->is_apo        = (buf[8] & (1 << 3)) != 0; /* Auto-poweroff on */
185         info->is_bat        = (buf[8] & (1 << 2)) != 0; /* Battery low */
186         info->is_nano       = (buf[8] & (1 << 1)) != 0;
187         info->is_z3         = (buf[8] & (1 << 0)) != 0; /* User symbol 3 */
188
189         /* Byte 9 */
190         info->is_micro      = (buf[9] & (1 << 7)) != 0;
191         info->is_milli      = (buf[9] & (1 << 6)) != 0;
192         info->is_kilo       = (buf[9] & (1 << 5)) != 0;
193         info->is_mega       = (buf[9] & (1 << 4)) != 0;
194         info->is_beep       = (buf[9] & (1 << 3)) != 0;
195         info->is_diode      = (buf[9] & (1 << 2)) != 0;
196         info->is_percent    = (buf[9] & (1 << 1)) != 0;
197         info->is_z4         = (buf[9] & (1 << 0)) != 0; /* User symbol 4 */
198
199         /* Byte 10 */
200         info->is_volt       = (buf[10] & (1 << 7)) != 0;
201         info->is_ampere     = (buf[10] & (1 << 6)) != 0;
202         info->is_ohm        = (buf[10] & (1 << 5)) != 0;
203         info->is_hfe        = (buf[10] & (1 << 4)) != 0;
204         info->is_hertz      = (buf[10] & (1 << 3)) != 0;
205         info->is_farad      = (buf[10] & (1 << 2)) != 0;
206         info->is_celsius    = (buf[10] & (1 << 1)) != 0; /* Only FS9922-DMM4 */
207         info->is_fahrenheit = (buf[10] & (1 << 0)) != 0; /* Only FS9922-DMM4 */
208
209         /*
210          * Byte 11: Bar graph
211          *
212          * Bit 7 contains the sign of the bargraph number (if the bit is set,
213          * the number is negative), bits 6..0 contain the actual number.
214          * Valid range: 0-40 (FS9922-DMM3), 0-60 (FS9922-DMM4).
215          *
216          * Upon "over limit" the bargraph value is 1 count above the highest
217          * valid number (i.e. 41 or 61, depending on chip).
218          */
219         if (info->is_bpn) {
220                 info->bargraph_sign = ((buf[11] & (1 << 7)) != 0) ? -1 : 1;
221                 info->bargraph_value = (buf[11] & 0x7f);
222                 info->bargraph_value *= info->bargraph_sign;
223         }
224
225         /* Byte 12: Always '\r' (carriage return, 0x0d, 13) */
226
227         /* Byte 13: Always '\n' (newline, 0x0a, 10) */
228 }
229
230 static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
231                          const struct fs9922_info *info)
232 {
233         /* Factors */
234         if (info->is_nano)
235                 *floatval /= 1000000000;
236         if (info->is_micro)
237                 *floatval /= 1000000;
238         if (info->is_milli)
239                 *floatval /= 1000;
240         if (info->is_kilo)
241                 *floatval *= 1000;
242         if (info->is_mega)
243                 *floatval *= 1000000;
244
245         /* Measurement modes */
246         if (info->is_volt || info->is_diode) {
247                 /* Note: In "diode mode" both is_diode and is_volt are set. */
248                 analog->mq = SR_MQ_VOLTAGE;
249                 analog->unit = SR_UNIT_VOLT;
250         }
251         if (info->is_ampere) {
252                 analog->mq = SR_MQ_CURRENT;
253                 analog->unit = SR_UNIT_AMPERE;
254         }
255         if (info->is_ohm) {
256                 analog->mq = SR_MQ_RESISTANCE;
257                 analog->unit = SR_UNIT_OHM;
258         }
259         if (info->is_hfe) {
260                 analog->mq = SR_MQ_GAIN;
261                 analog->unit = SR_UNIT_UNITLESS;
262         }
263         if (info->is_hertz) {
264                 analog->mq = SR_MQ_FREQUENCY;
265                 analog->unit = SR_UNIT_HERTZ;
266         }
267         if (info->is_farad) {
268                 analog->mq = SR_MQ_CAPACITANCE;
269                 analog->unit = SR_UNIT_FARAD;
270         }
271         if (info->is_celsius) {
272                 analog->mq = SR_MQ_TEMPERATURE;
273                 analog->unit = SR_UNIT_CELSIUS;
274         }
275         if (info->is_fahrenheit) {
276                 analog->mq = SR_MQ_TEMPERATURE;
277                 analog->unit = SR_UNIT_FAHRENHEIT;
278         }
279         if (info->is_beep) {
280                 analog->mq = SR_MQ_CONTINUITY;
281                 analog->unit = SR_UNIT_BOOLEAN;
282                 *floatval = (*floatval == INFINITY) ? 0.0 : 1.0;
283         }
284         if (info->is_percent) {
285                 analog->mq = SR_MQ_DUTY_CYCLE;
286                 analog->unit = SR_UNIT_PERCENTAGE;
287         }
288
289         /* Measurement related flags */
290         if (info->is_ac)
291                 analog->mqflags |= SR_MQFLAG_AC;
292         if (info->is_dc)
293                 analog->mqflags |= SR_MQFLAG_DC;
294         if (info->is_auto)
295                 analog->mqflags |= SR_MQFLAG_AUTORANGE;
296         if (info->is_diode)
297                 analog->mqflags |= SR_MQFLAG_DIODE;
298         if (info->is_hold)
299                 analog->mqflags |= SR_MQFLAG_HOLD;
300         if (info->is_max)
301                 analog->mqflags |= SR_MQFLAG_MAX;
302         if (info->is_min)
303                 analog->mqflags |= SR_MQFLAG_MIN;
304         if (info->is_rel)
305                 analog->mqflags |= SR_MQFLAG_RELATIVE;
306
307         /* Other flags */
308         if (info->is_apo)
309                 sr_spew("Automatic power-off function is active.");
310         if (info->is_bat)
311                 sr_spew("Battery is low.");
312         if (info->is_z1)
313                 sr_spew("User-defined LCD symbol 1 is active.");
314         if (info->is_z2)
315                 sr_spew("User-defined LCD symbol 2 is active.");
316         if (info->is_z3)
317                 sr_spew("User-defined LCD symbol 3 is active.");
318         if (info->is_z4)
319                 sr_spew("User-defined LCD symbol 4 is active.");
320         if (info->is_bpn)
321                 sr_spew("The bargraph value is %d.", info->bargraph_value);
322         else
323                 sr_spew("The bargraph is not active.");
324
325 }
326
327 SR_PRIV gboolean sr_fs9922_packet_valid(const uint8_t *buf)
328 {
329         struct fs9922_info info;
330
331         /* Byte 0: Sign (must be '+' or '-') */
332         if (buf[0] != '+' && buf[0] != '-')
333                 return FALSE;
334
335         /* Byte 12: Always '\r' (carriage return, 0x0d, 13) */
336         /* Byte 13: Always '\n' (newline, 0x0a, 10) */
337         if (buf[12] != '\r' || buf[13] != '\n')
338                 return FALSE;
339
340         parse_flags(buf, &info);
341
342         return flags_valid(&info);
343 }
344
345 /**
346  * Parse a protocol packet.
347  *
348  * @param buf Buffer containing the protocol packet. Must not be NULL.
349  * @param floatval Pointer to a float variable. That variable will contain the
350  *                 result value upon parsing success. Must not be NULL.
351  * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
352  *               filled with data according to the protocol packet.
353  *               Must not be NULL.
354  * @param info Pointer to a struct fs9922_info. The struct will be filled
355  *             with data according to the protocol packet. Must not be NULL.
356  *
357  * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
358  *         'analog' variable contents are undefined and should not be used.
359  */
360 SR_PRIV int sr_fs9922_parse(const uint8_t *buf, float *floatval,
361                             struct sr_datafeed_analog *analog, void *info)
362 {
363         int ret;
364         struct fs9922_info *info_local;
365
366         info_local = (struct fs9922_info *)info;
367
368         if ((ret = parse_value(buf, floatval)) != SR_OK) {
369                 sr_err("Error parsing value: %d.", ret);
370                 return ret;
371         }
372
373         parse_flags(buf, info_local);
374         handle_flags(analog, floatval, info_local);
375
376         return SR_OK;
377 }
378
379 SR_PRIV void sr_fs9922_z1_diode(struct sr_datafeed_analog *analog, void *info)
380 {
381         struct fs9922_info *info_local;
382
383         info_local = (struct fs9922_info *)info;
384
385         /* User-defined z1 flag means "diode mode". */
386         if (info_local->is_z1) {
387                 analog->mq = SR_MQ_VOLTAGE;
388                 analog->unit = SR_UNIT_VOLT;
389                 analog->mqflags |= SR_MQFLAG_DIODE;
390         }
391 }