]>
Commit | Line | Data |
---|---|---|
825da8b2 MH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz> | |
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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | /** \file | |
21 | * BBC Goerz Metrawatt M2110 ASCII protocol parser. | |
22 | * | |
23 | * Most probably the simplest multimeter protocol ever ;-) . | |
24 | */ | |
25 | ||
26 | ||
27 | #include <string.h> | |
28 | #include <math.h> | |
29 | #include <glib.h> | |
30 | #include "libsigrok.h" | |
31 | #include "libsigrok-internal.h" | |
32 | ||
33 | ||
34 | /* Message logging helpers with subsystem-specific prefix string. */ | |
35 | #define LOG_PREFIX "bbcgm-m2110: " | |
36 | #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args) | |
37 | #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args) | |
38 | #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args) | |
39 | #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args) | |
40 | #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) | |
41 | #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) | |
42 | ||
43 | SR_PRIV gboolean sr_m2110_packet_valid(const uint8_t *buf) | |
44 | { | |
45 | float val; | |
46 | ||
47 | if ((buf[7] != '\r') || (buf[8] != '\n')) | |
48 | return FALSE; | |
49 | ||
50 | if (!strncmp((const char*)buf, "OVERRNG", 7)) | |
51 | return TRUE; | |
52 | ||
53 | if (sscanf((const char*)buf, "%f", &val) == 1) | |
54 | return TRUE; | |
55 | else | |
56 | return FALSE; | |
57 | } | |
58 | ||
59 | SR_PRIV int sr_m2110_parse(const uint8_t *buf, float *floatval, | |
60 | struct sr_datafeed_analog *analog, void *info) | |
61 | { | |
62 | float val; | |
63 | ||
64 | (void)info; | |
65 | ||
66 | analog->mq = SR_MQ_GAIN; /* We don't know the unit, so that's the best we can do.*/ | |
67 | analog->unit = SR_UNIT_UNITLESS; | |
68 | analog->mqflags = 0; | |
69 | ||
70 | if (!strncmp((const char*)buf, "OVERRNG", 7)) { | |
71 | *floatval = INFINITY; | |
72 | } | |
73 | else if (sscanf((const char*)buf, "%f", &val) == 1) | |
74 | *floatval = val; | |
75 | ||
76 | return SR_OK; | |
77 | } |