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