]> sigrok.org Git - libsigrok.git/blame - src/dmm/ut372.c
asyc-ii: Rephrase "exponent" logic when parsing packets
[libsigrok.git] / src / dmm / ut372.c
CommitLineData
f3cde309
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Martin Ling <martin-sigrok@earth.li>
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 * UNI-T UT372 protocol parser.
23 */
24
6ec6c43b 25#include <config.h>
472bef39 26#include <stdlib.h>
118268a2 27#include <stdint.h>
472bef39 28#include <math.h>
c1aae900 29#include <libsigrok/libsigrok.h>
f3cde309
ML
30#include "libsigrok-internal.h"
31
32#define LOG_PREFIX "ut372"
33
6df8e239 34static const uint8_t lookup[] = {
472bef39
ML
35 0x7B,
36 0x60,
37 0x5E,
38 0x7C,
39 0x65,
40 0x3D,
41 0x3F,
42 0x70,
43 0x7F,
6df8e239 44 0x7D,
472bef39
ML
45};
46
47#define DECIMAL_POINT_MASK 0x80
48
8c9092b0
ML
49#define FLAGS1_HOLD_MASK (1 << 2)
50
51#define FLAGS2_RPM_MASK (1 << 0)
12318aab 52#define FLAGS2_COUNT_MASK (1 << 1)
8c9092b0
ML
53#define FLAGS2_MAX_MASK (1 << 4)
54#define FLAGS2_MIN_MASK (1 << 5)
6df8e239 55#define FLAGS2_AVG_MASK (1 << 6)
8c9092b0 56
118268a2
ML
57/* Decode a pair of characters into a byte. */
58static uint8_t decode_pair(const uint8_t *buf)
59{
60 unsigned int i;
61 char hex[3];
62
63 hex[2] = '\0';
64
65 for (i = 0; i < 2; i++) {
66 hex[i] = buf[i];
67 if (hex[i] > 0x39)
68 hex[i] += 7;
69 }
70
71 return strtol(hex, NULL, 16);
72}
73
f3cde309
ML
74SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf)
75{
12318aab
ML
76 uint8_t flags2;
77
78 if (!(buf[25] == '\r' && buf[26] == '\n'))
79 return FALSE;
80
81 flags2 = decode_pair(buf + 23);
82
83 if (!(flags2 & (FLAGS2_RPM_MASK | FLAGS2_COUNT_MASK)))
84 /* Device is in the setup menu - no valid data shown. */
85 return FALSE;
86
87 return TRUE;
f3cde309
ML
88}
89
90SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
b02bb45f 91 struct sr_datafeed_analog *analog, void *info)
f3cde309 92{
47166326 93 unsigned int i, j, value;
8c9092b0 94 uint8_t segments, flags1, flags2;
47166326 95 int exponent;
472bef39
ML
96
97 (void) info;
98
8c9092b0
ML
99 flags1 = decode_pair(buf + 21);
100 flags2 = decode_pair(buf + 23);
101
12318aab 102 if (flags2 & FLAGS2_RPM_MASK) {
b02bb45f
UH
103 analog->meaning->mq = SR_MQ_FREQUENCY;
104 analog->meaning->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE;
12318aab 105 } else if (flags2 & FLAGS2_COUNT_MASK) {
b02bb45f
UH
106 analog->meaning->mq = SR_MQ_COUNT;
107 analog->meaning->unit = SR_UNIT_UNITLESS;
8c9092b0
ML
108 }
109
110 if (flags1 & FLAGS1_HOLD_MASK)
b02bb45f 111 analog->meaning->mqflags |= SR_MQFLAG_HOLD;
8c9092b0 112 if (flags2 & FLAGS2_MIN_MASK)
b02bb45f 113 analog->meaning->mqflags |= SR_MQFLAG_MIN;
8c9092b0 114 if (flags2 & FLAGS2_MAX_MASK)
b02bb45f 115 analog->meaning->mqflags |= SR_MQFLAG_MAX;
6df8e239 116 if (flags2 & FLAGS2_AVG_MASK)
b02bb45f 117 analog->meaning->mqflags |= SR_MQFLAG_AVG;
8c9092b0 118
472bef39 119 value = 0;
47166326 120 exponent = 0;
472bef39
ML
121
122 for (i = 0; i < 5; i++) {
07ffa5b3 123 segments = decode_pair(buf + 1 + (2 * i));
472bef39
ML
124 for (j = 0; j < ARRAY_SIZE(lookup); j++) {
125 if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) {
126 value += j * pow(10, i);
127 break;
128 }
129 }
130 if (segments & DECIMAL_POINT_MASK)
47166326 131 exponent = -i;
472bef39
ML
132 }
133
47166326
AJ
134 *floatval = (float) value * powf(10, exponent);
135
136 analog->encoding->digits = -exponent;
137 analog->spec->spec_digits = -exponent;
472bef39 138
f3cde309
ML
139 return SR_OK;
140}