]> sigrok.org Git - libsigrok.git/blame - src/dmm/bm25x.c
SR_DF_ANALOG_OLD and sr_datafeed_analog_old renames.
[libsigrok.git] / src / dmm / bm25x.c
CommitLineData
a24c3f4a
JH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Janne Huttunen <jahuttun@gmail.com>
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * @file
22 *
23 * Brymen BM25x serial protocol parser.
24 */
25
6ec6c43b 26#include <config.h>
a24c3f4a 27#include <math.h>
c1aae900 28#include <libsigrok/libsigrok.h>
a24c3f4a
JH
29#include "libsigrok-internal.h"
30
31#define LOG_PREFIX "brymen-bm25x"
32
33#define MAX_DIGITS 4
34
35SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf)
36{
37 int i;
38
39 if (buf[0] != 2)
40 return FALSE;
41
42 for (i = 1; i < BRYMEN_BM25X_PACKET_SIZE; i++)
43 if ((buf[i] >> 4) != i)
44 return FALSE;
45
46 return TRUE;
47}
48
49static int decode_digit(int num, const uint8_t *buf)
50{
51 int val;
52
53 val = (buf[3 + 2 * num] & 0xe) | ((buf[4 + 2 * num] << 4) & 0xf0);
54
55 switch (val) {
56 case 0xbe: return 0;
57 case 0xa0: return 1;
58 case 0xda: return 2;
59 case 0xf8: return 3;
60 case 0xe4: return 4;
61 case 0x7c: return 5;
62 case 0x7e: return 6;
63 case 0xa8: return 7;
64 case 0xfe: return 8;
65 case 0xfc: return 9;
66 case 0x00: return ' ';
67 case 0x40: return '-';
68 case 0x16: return 'L';
69 case 0x1e: return 'C';
70 case 0x4e: return 'F';
71 case 0x5e: return 'E';
72 case 0x62: return 'n';
73 case 0x42: return 'r';
74 default:
75 sr_dbg("Unknown digit: 0x%02x.", val);
76 return -1;
77 }
78}
79
80static int decode_point(const uint8_t *buf)
81{
82 int i, p = 0;
83
84 for (i = 1; i < MAX_DIGITS; i++) {
85 if ((buf[11 - 2 * i] & 1) == 0)
86 continue;
87 if (p != 0) {
88 sr_spew("Multiple decimal points found!");
89 return -1;
90 }
91 p = i;
92 }
93
94 return p;
95}
96
97static float scale_value(float val, int point, int digits)
98{
99 int pos;
100
101 pos = point ? point + digits - MAX_DIGITS : 0;
102
103 switch (pos) {
104 case 0: return val;
105 case 1: return val * 1e-1;
106 case 2: return val * 1e-2;
107 case 3: return val * 1e-3;
108 }
109
110 sr_dbg("Invalid decimal point %d (%d digits).", point, digits);
111
112 return NAN;
113}
114
115static float decode_prefix(const uint8_t *buf)
116{
117 if (buf[11] & 2) return 1e+6;
118 if (buf[11] & 1) return 1e+3;
119 if (buf[13] & 1) return 1e-3;
120 if (buf[13] & 2) return 1e-6;
121 if (buf[12] & 1) return 1e-9;
122
123 return 1.0f;
124}
125
126static float decode_value(const uint8_t *buf)
127{
128 float val = 0.0f;
129 int i, digit;
130
131 for (i = 0; i < MAX_DIGITS; i++) {
132 digit = decode_digit(i, buf);
133 if (i == 3 && (digit == 'C' || digit == 'F'))
134 break;
135 if (digit < 0 || digit > 9)
136 goto special;
137 val = 10.0 * val + digit;
138 }
139
140 return scale_value(val, decode_point(buf), i);
141
142special:
143 if (decode_digit(1, buf) == 0 && decode_digit(2, buf) == 'L')
144 return INFINITY;
145
146 return NAN;
147}
148
149SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
5faebab2 150 struct sr_datafeed_analog_old *analog, void *info)
a24c3f4a
JH
151{
152 float val;
153
154 (void)info;
155
156 analog->mq = SR_MQ_GAIN;
157 analog->unit = SR_UNIT_UNITLESS;
158 analog->mqflags = 0;
159
160 if (buf[1] & 8)
161 analog->mqflags |= SR_MQFLAG_AUTORANGE;
162 if (buf[1] & 4)
163 analog->mqflags |= SR_MQFLAG_DC;
164 if (buf[1] & 2)
165 analog->mqflags |= SR_MQFLAG_AC;
166 if (buf[1] & 1)
167 analog->mqflags |= SR_MQFLAG_RELATIVE;
168 if (buf[11] & 8)
169 analog->mqflags |= SR_MQFLAG_HOLD;
170 if (buf[13] & 8)
171 analog->mqflags |= SR_MQFLAG_MAX;
172 if (buf[14] & 8)
173 analog->mqflags |= SR_MQFLAG_MIN;
174
175 if (buf[14] & 4) {
176 analog->mq = SR_MQ_VOLTAGE;
177 analog->unit = SR_UNIT_VOLT;
178 if ((analog->mqflags & (SR_MQFLAG_DC | SR_MQFLAG_AC)) == 0)
179 analog->mqflags |= SR_MQFLAG_DIODE;
180 }
181 if (buf[14] & 2) {
182 analog->mq = SR_MQ_CURRENT;
183 analog->unit = SR_UNIT_AMPERE;
184 }
185 if (buf[12] & 4) {
186 analog->mq = SR_MQ_RESISTANCE;
187 analog->unit = SR_UNIT_OHM;
188 }
189 if (buf[13] & 4) {
190 analog->mq = SR_MQ_CAPACITANCE;
191 analog->unit = SR_UNIT_FARAD;
192 }
193 if (buf[12] & 2) {
194 analog->mq = SR_MQ_FREQUENCY;
195 analog->unit = SR_UNIT_HERTZ;
196 }
197
198 if (decode_digit(3, buf) == 'C') {
199 analog->mq = SR_MQ_TEMPERATURE;
200 analog->unit = SR_UNIT_CELSIUS;
201 }
202 if (decode_digit(3, buf) == 'F') {
203 analog->mq = SR_MQ_TEMPERATURE;
204 analog->unit = SR_UNIT_FAHRENHEIT;
205 }
206
207 val = decode_value(buf) * decode_prefix(buf);
208
209 if (buf[3] & 1)
210 val = -val;
211
212 *floatval = val;
213
214 return SR_OK;
215}