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