2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Martin Ling <martin-sigrok@earth.li>
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.
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.
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/>.
21 * UNI-T UT372 protocol parser.
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "ut372"
33 static const uint8_t lookup[] = {
46 #define DECIMAL_POINT_MASK 0x80
48 #define FLAGS1_HOLD_MASK (1 << 2)
50 #define FLAGS2_RPM_MASK (1 << 0)
51 #define FLAGS2_COUNT_MASK (1 << 1)
52 #define FLAGS2_MAX_MASK (1 << 4)
53 #define FLAGS2_MIN_MASK (1 << 5)
54 #define FLAGS2_AVG_MASK (1 << 6)
56 /* Decode a pair of characters into a byte. */
57 static uint8_t decode_pair(const uint8_t *buf)
64 for (i = 0; i < 2; i++) {
70 return strtol(hex, NULL, 16);
73 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf)
77 if (!(buf[25] == '\r' && buf[26] == '\n'))
80 flags2 = decode_pair(buf + 23);
82 if (!(flags2 & (FLAGS2_RPM_MASK | FLAGS2_COUNT_MASK)))
83 /* Device is in the setup menu - no valid data shown. */
89 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
90 struct sr_datafeed_analog *analog, void *info)
92 unsigned int i, j, value;
93 uint8_t segments, flags1, flags2;
98 flags1 = decode_pair(buf + 21);
99 flags2 = decode_pair(buf + 23);
101 if (flags2 & FLAGS2_RPM_MASK) {
102 analog->meaning->mq = SR_MQ_FREQUENCY;
103 analog->meaning->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE;
104 } else if (flags2 & FLAGS2_COUNT_MASK) {
105 analog->meaning->mq = SR_MQ_COUNT;
106 analog->meaning->unit = SR_UNIT_UNITLESS;
109 if (flags1 & FLAGS1_HOLD_MASK)
110 analog->meaning->mqflags |= SR_MQFLAG_HOLD;
111 if (flags2 & FLAGS2_MIN_MASK)
112 analog->meaning->mqflags |= SR_MQFLAG_MIN;
113 if (flags2 & FLAGS2_MAX_MASK)
114 analog->meaning->mqflags |= SR_MQFLAG_MAX;
115 if (flags2 & FLAGS2_AVG_MASK)
116 analog->meaning->mqflags |= SR_MQFLAG_AVG;
121 for (i = 0; i < 5; i++) {
122 segments = decode_pair(buf + 1 + (2 * i));
123 for (j = 0; j < ARRAY_SIZE(lookup); j++) {
124 if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) {
125 value += j * pow(10, i);
129 if (segments & DECIMAL_POINT_MASK)
133 *floatval = (float) value * powf(10, exponent);
135 analog->encoding->digits = -exponent;
136 analog->spec->spec_digits = -exponent;