]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/common/dmm/fs9922.c
output: Use sr_config_get() wrapper
[libsigrok.git] / hardware / common / dmm / fs9922.c
... / ...
CommitLineData
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 driver-specific prefix string. */
33#define DRIVER_LOG_DOMAIN "fs9922: "
34#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
35#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
36#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
37#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
38#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
39#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
40
41static 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 /* Does the packet "measure" more than one type of value? */
58 count = 0;
59 count += (info->is_diode) ? 1 : 0;
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) {
70 sr_err("More than one measurement type detected in packet.");
71 return FALSE;
72 }
73
74 /* Both AC and DC set? */
75 if (info->is_ac && info->is_dc) {
76 sr_err("Both AC and DC flags detected in packet.");
77 return FALSE;
78 }
79
80 /* Both Celsius and Fahrenheit set? */
81 if (info->is_celsius && info->is_fahrenheit) {
82 sr_err("Both Celsius and Fahrenheit flags detected in packet.");
83 return FALSE;
84 }
85
86 return TRUE;
87}
88
89static int parse_value(const uint8_t *buf, float *result)
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 {
100 sr_err("Invalid sign byte: 0x%02x.", buf[0]);
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])) {
115 sr_err("Value contained invalid digits: %02x %02x %02x %02x ("
116 "%c %c %c %c).", buf[1], buf[2], buf[3], buf[4]);
117 return SR_ERR;
118 }
119 intval = 0;
120 intval += (buf[1] - '0') * 1000;
121 intval += (buf[2] - '0') * 100;
122 intval += (buf[3] - '0') * 10;
123 intval += (buf[4] - '0') * 1;
124
125 floatval = (float)intval;
126
127 /* Byte 5: Always ' ' (space, 0x20) */
128
129 /*
130 * Byte 6: Decimal point position ('0', '1', '2', or '4')
131 *
132 * Note: The Fortune Semiconductor FS9922-DMM3/4 datasheets both have
133 * an error/typo here. They claim that the values '0'/'1'/'2'/'3' are
134 * used, but '0'/'1'/'2'/'4' is actually correct.
135 */
136 if (buf[6] != '0' && buf[6] != '1' && buf[6] != '2' && buf[6] != '4') {
137 sr_err("Invalid decimal point value: 0x%02x.", buf[6]);
138 return SR_ERR;
139 }
140 if (buf[6] == '0')
141 floatval /= 1;
142 else if (buf[6] == '1')
143 floatval /= 1000;
144 else if (buf[6] == '2')
145 floatval /= 100;
146 else if (buf[6] == '4')
147 floatval /= 10;
148
149 /* Apply sign. */
150 floatval *= sign;
151
152 sr_spew("The display value is %f.", floatval);
153
154 *result = floatval;
155
156 return SR_OK;
157}
158
159static void parse_flags(const uint8_t *buf, struct fs9922_info *info)
160{
161 /* Z1/Z2/Z3/Z4 are bits for user-defined LCD symbols (on/off). */
162
163 /* Byte 7 */
164 /* Bit 7: Always 0 */
165 /* Bit 6: Always 0 */
166 info->is_auto = (buf[7] & (1 << 5)) != 0;
167 info->is_dc = (buf[7] & (1 << 4)) != 0;
168 info->is_ac = (buf[7] & (1 << 3)) != 0;
169 info->is_rel = (buf[7] & (1 << 2)) != 0;
170 info->is_hold = (buf[7] & (1 << 1)) != 0;
171 info->is_bpn = (buf[7] & (1 << 0)) != 0; /* Bargraph shown */
172
173 /* Byte 8 */
174 info->is_z1 = (buf[8] & (1 << 7)) != 0; /* User symbol 1 */
175 info->is_z2 = (buf[8] & (1 << 6)) != 0; /* User symbol 2 */
176 info->is_max = (buf[8] & (1 << 5)) != 0;
177 info->is_min = (buf[8] & (1 << 4)) != 0;
178 info->is_apo = (buf[8] & (1 << 3)) != 0; /* Auto-poweroff on */
179 info->is_bat = (buf[8] & (1 << 2)) != 0; /* Battery low */
180 info->is_nano = (buf[8] & (1 << 1)) != 0;
181 info->is_z3 = (buf[8] & (1 << 0)) != 0; /* User symbol 3 */
182
183 /* Byte 9 */
184 info->is_micro = (buf[9] & (1 << 7)) != 0;
185 info->is_milli = (buf[9] & (1 << 6)) != 0;
186 info->is_kilo = (buf[9] & (1 << 5)) != 0;
187 info->is_mega = (buf[9] & (1 << 4)) != 0;
188 info->is_beep = (buf[9] & (1 << 3)) != 0;
189 info->is_diode = (buf[9] & (1 << 2)) != 0;
190 info->is_percent = (buf[9] & (1 << 1)) != 0;
191 info->is_z4 = (buf[8] & (1 << 0)) != 0; /* User symbol 4 */
192
193 /* Byte 10 */
194 info->is_volt = (buf[10] & (1 << 7)) != 0;
195 info->is_ampere = (buf[10] & (1 << 6)) != 0;
196 info->is_ohm = (buf[10] & (1 << 5)) != 0;
197 info->is_hfe = (buf[10] & (1 << 4)) != 0;
198 info->is_hertz = (buf[10] & (1 << 3)) != 0;
199 info->is_farad = (buf[10] & (1 << 2)) != 0;
200 info->is_celsius = (buf[10] & (1 << 1)) != 0; /* Only FS9922-DMM4 */
201 info->is_fahrenheit = (buf[10] & (1 << 0)) != 0; /* Only FS9922-DMM4 */
202
203 /*
204 * Byte 11: Bar graph
205 *
206 * Bit 7 contains the sign of the bargraph number (if the bit is set,
207 * the number is negative), bits 6..0 contain the actual number.
208 * Valid range: 0-40 (FS9922-DMM3), 0-60 (FS9922-DMM4).
209 *
210 * Upon "over limit" the bargraph value is 1 count above the highest
211 * valid number (i.e. 41 or 61, depending on chip).
212 */
213 if (info->is_bpn) {
214 info->bargraph_sign = ((buf[11] & (1 << 7)) != 0) ? -1 : 1;
215 info->bargraph_value = (buf[11] & 0x7f);
216 info->bargraph_value *= info->bargraph_sign;
217 }
218
219 /* Byte 12: Always '\r' (carriage return, 0x0d, 13) */
220
221 /* Byte 13: Always '\n' (newline, 0x0a, 10) */
222}
223
224static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
225 const struct fs9922_info *info)
226{
227 /* Factors */
228 if (info->is_nano)
229 *floatval /= 1000000000;
230 if (info->is_micro)
231 *floatval /= 1000000;
232 if (info->is_milli)
233 *floatval /= 1000;
234 if (info->is_kilo)
235 *floatval *= 1000;
236 if (info->is_mega)
237 *floatval *= 1000000;
238
239 /* Measurement modes */
240 if (info->is_volt) {
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 < 0.0) ? 0.0 : 1.0;
276 }
277 if (info->is_diode) {
278 analog->mq = SR_MQ_VOLTAGE;
279 analog->unit = SR_UNIT_VOLT;
280 }
281 if (info->is_percent) {
282 analog->mq = SR_MQ_DUTY_CYCLE;
283 analog->unit = SR_UNIT_PERCENTAGE;
284 }
285
286 /* Measurement related flags */
287 if (info->is_ac)
288 analog->mqflags |= SR_MQFLAG_AC;
289 if (info->is_dc)
290 analog->mqflags |= SR_MQFLAG_DC;
291 if (info->is_auto)
292 analog->mqflags |= SR_MQFLAG_AUTORANGE;
293 if (info->is_hold)
294 analog->mqflags |= SR_MQFLAG_HOLD;
295 if (info->is_max)
296 analog->mqflags |= SR_MQFLAG_MAX;
297 if (info->is_min)
298 analog->mqflags |= SR_MQFLAG_MIN;
299 if (info->is_rel)
300 analog->mqflags |= SR_MQFLAG_RELATIVE;
301
302 /* Other flags */
303 if (info->is_apo)
304 sr_spew("Automatic power-off function is active.");
305 if (info->is_bat)
306 sr_spew("Battery is low.");
307 if (info->is_z1)
308 sr_spew("User-defined LCD symbol 1 is active.");
309 if (info->is_z2)
310 sr_spew("User-defined LCD symbol 2 is active.");
311 if (info->is_z3)
312 sr_spew("User-defined LCD symbol 3 is active.");
313 if (info->is_z4)
314 sr_spew("User-defined LCD symbol 4 is active.");
315 if (info->is_bpn)
316 sr_spew("The bargraph value is %d.", info->bargraph_value);
317 else
318 sr_spew("The bargraph is not active.");
319
320}
321
322SR_PRIV gboolean sr_fs9922_packet_valid(const uint8_t *buf)
323{
324 struct fs9922_info info;
325
326 /* Byte 0: Sign (must be '+' or '-') */
327 if (buf[0] != '+' && buf[0] != '-')
328 return FALSE;
329
330 /* Byte 12: Always '\r' (carriage return, 0x0d, 13) */
331 /* Byte 13: Always '\n' (newline, 0x0a, 10) */
332 if (buf[12] != '\r' || buf[13] != '\n')
333 return FALSE;
334
335 parse_flags(buf, &info);
336
337 return flags_valid(&info);
338}
339
340/**
341 * Parse a protocol packet.
342 *
343 * @param buf Buffer containing the protocol packet. Must not be NULL.
344 * @param floatval Pointer to a float variable. That variable will contain the
345 * result value upon parsing success. Must not be NULL.
346 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
347 * filled with data according to the protocol packet.
348 * Must not be NULL.
349 * @param info Pointer to a struct fs9922_info. The struct will be filled
350 * with data according to the protocol packet. Must not be NULL.
351 *
352 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
353 * 'analog' variable contents are undefined and should not be used.
354 */
355SR_PRIV int sr_fs9922_parse(const uint8_t *buf, float *floatval,
356 struct sr_datafeed_analog *analog, void *info)
357{
358 int ret;
359 struct fs9922_info *info_local;
360
361 info_local = (struct fs9922_info *)info;
362
363 if ((ret = parse_value(buf, floatval)) != SR_OK) {
364 sr_err("Error parsing value: %d.", ret);
365 return ret;
366 }
367
368 parse_flags(buf, info_local);
369 handle_flags(analog, floatval, info_local);
370
371 return SR_OK;
372}