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