]> sigrok.org Git - libsigrok.git/blame - src/dmm/ut372.c
ut372: Implement initial protocol parser.
[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
472bef39
ML
25#include <stdlib.h>
26#include <math.h>
f3cde309
ML
27#include "libsigrok.h"
28#include "libsigrok-internal.h"
29
30#define LOG_PREFIX "ut372"
31
472bef39
ML
32char lookup[] = {
33 0x7B,
34 0x60,
35 0x5E,
36 0x7C,
37 0x65,
38 0x3D,
39 0x3F,
40 0x70,
41 0x7F,
42 0x7D
43};
44
45#define DECIMAL_POINT_MASK 0x80
46
f3cde309
ML
47SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf)
48{
472bef39 49 return (buf[25] == '\r' && buf[26] == '\n');
f3cde309
ML
50}
51
52SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
53 struct sr_datafeed_analog *analog, void *info)
54{
472bef39
ML
55 unsigned int i, j, segments, value, divisor;
56 char hex[3];
57
58 (void) info;
59
60 hex[2] = '\0';
61 value = 0;
62 divisor = 1;
63
64 for (i = 0; i < 5; i++) {
65 for (j = 0; j < 2; j++) {
66 hex[j] = buf[2*i + 1 + j];
67 if (hex[j] > 0x39)
68 hex[j] += 7;
69 }
70 segments = strtol(hex, NULL, 16);
71 for (j = 0; j < ARRAY_SIZE(lookup); j++) {
72 if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) {
73 value += j * pow(10, i);
74 break;
75 }
76 }
77 if (segments & DECIMAL_POINT_MASK)
78 divisor = pow(10, i);
79 }
80
81 *floatval = (float) value / divisor;
82
83 analog->mq = SR_MQ_FREQUENCY;
84 analog->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE;
85
f3cde309
ML
86 return SR_OK;
87}