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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * UNI-T UT372 protocol parser.
28 #include "libsigrok.h"
29 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "ut372"
46 #define DECIMAL_POINT_MASK 0x80
48 /* Decode a pair of characters into a byte. */
49 static uint8_t decode_pair(const uint8_t *buf)
56 for (i = 0; i < 2; i++) {
62 return strtol(hex, NULL, 16);
65 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf)
67 return (buf[25] == '\r' && buf[26] == '\n');
70 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
71 struct sr_datafeed_analog *analog, void *info)
73 unsigned int i, j, value, divisor;
81 for (i = 0; i < 5; i++) {
82 segments = decode_pair(buf + 1 + 2*i);
83 for (j = 0; j < ARRAY_SIZE(lookup); j++) {
84 if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) {
85 value += j * pow(10, i);
89 if (segments & DECIMAL_POINT_MASK)
93 *floatval = (float) value / divisor;
95 analog->mq = SR_MQ_FREQUENCY;
96 analog->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE;