]> sigrok.org Git - libsigrok.git/blob - src/dmm/ut372.c
c5944cc47558badcad7b8a5b6603d2d9a5c99272
[libsigrok.git] / src / dmm / ut372.c
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
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <math.h>
28 #include "libsigrok.h"
29 #include "libsigrok-internal.h"
30
31 #define LOG_PREFIX "ut372"
32
33 uint8_t lookup[] = {
34         0x7B,
35         0x60,
36         0x5E,
37         0x7C,
38         0x65,
39         0x3D,
40         0x3F,
41         0x70,
42         0x7F,
43         0x7D
44 };
45
46 #define DECIMAL_POINT_MASK 0x80
47
48 #define FLAGS1_HOLD_MASK (1 << 2)
49
50 #define FLAGS2_RPM_MASK (1 << 0)
51 #define FLAGS2_MAX_MASK (1 << 4)
52 #define FLAGS2_MIN_MASK (1 << 5)
53 #define FLAGS2_AVE_MASK (1 << 6)
54
55 /* Decode a pair of characters into a byte. */
56 static uint8_t decode_pair(const uint8_t *buf)
57 {
58         unsigned int i;
59         char hex[3];
60
61         hex[2] = '\0';
62
63         for (i = 0; i < 2; i++) {
64                 hex[i] = buf[i];
65                 if (hex[i] > 0x39)
66                         hex[i] += 7;
67         }
68
69         return strtol(hex, NULL, 16);
70 }
71
72 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf)
73 {
74         return (buf[25] == '\r' && buf[26] == '\n');
75 }
76
77 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
78                 struct sr_datafeed_analog *analog, void *info)
79 {
80         unsigned int i, j, value, divisor;
81         uint8_t segments, flags1, flags2;
82
83         (void) info;
84
85         flags1 = decode_pair(buf + 21);
86         flags2 = decode_pair(buf + 23);
87
88         /* If not in RPM mode, ignore packet. */
89         if (!(flags2 & FLAGS2_RPM_MASK)) {
90                 sr_dbg("Not in RPM mode. Count mode is unsupported.");
91                 return SR_ERR_NA;
92         }
93
94         if (flags1 & FLAGS1_HOLD_MASK)
95                 analog->mqflags |= SR_MQFLAG_HOLD;
96         if (flags2 & FLAGS2_MIN_MASK)
97                 analog->mqflags |= SR_MQFLAG_MIN;
98         if (flags2 & FLAGS2_MAX_MASK)
99                 analog->mqflags |= SR_MQFLAG_MAX;
100         if (flags2 & FLAGS2_AVE_MASK)
101                 analog->mqflags |= SR_MQFLAG_AVG;
102
103         value = 0;
104         divisor = 1;
105
106         for (i = 0; i < 5; i++) {
107                 segments = decode_pair(buf + 1 + 2*i);
108                 for (j = 0; j < ARRAY_SIZE(lookup); j++) {
109                         if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) {
110                                 value += j * pow(10, i);
111                                 break;
112                         }
113                 }
114                 if (segments & DECIMAL_POINT_MASK)
115                         divisor = pow(10, i);
116         }
117
118         *floatval = (float) value / divisor;
119
120         analog->mq = SR_MQ_FREQUENCY;
121         analog->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE;
122
123         return SR_OK;
124 }