2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Janne Huttunen <jahuttun@gmail.com>
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.
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.
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/>.
23 * Brymen BM25x serial protocol parser.
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "brymen-bm25x"
35 SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf)
42 for (i = 1; i < BRYMEN_BM25X_PACKET_SIZE; i++)
43 if ((buf[i] >> 4) != i)
49 static int decode_digit(int num, const uint8_t *buf)
53 val = (buf[3 + 2 * num] & 0xe) | ((buf[4 + 2 * num] << 4) & 0xf0);
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';
75 sr_dbg("Unknown digit: 0x%02x.", val);
80 static int decode_point(const uint8_t *buf)
84 for (i = 1; i < MAX_DIGITS; i++) {
85 if ((buf[11 - 2 * i] & 1) == 0)
88 sr_spew("Multiple decimal points found!");
97 static int decode_scale(int point, int digits)
101 pos = point ? point + digits - MAX_DIGITS : 0;
103 if (pos < 0 || pos > 3) {
104 sr_dbg("Invalid decimal point %d (%d digits).", point, digits);
110 static int decode_prefix(const uint8_t *buf)
112 if (buf[11] & 2) return 6;
113 if (buf[11] & 1) return 3;
114 if (buf[13] & 1) return -3;
115 if (buf[13] & 2) return -6;
116 if (buf[12] & 1) return -9;
121 static float decode_value(const uint8_t *buf, int *exponent)
126 for (i = 0; i < MAX_DIGITS; i++) {
127 digit = decode_digit(i, buf);
128 if (i == 3 && (digit == 'C' || digit == 'F'))
130 if (digit < 0 || digit > 9)
132 val = 10.0 * val + digit;
135 *exponent = decode_scale(decode_point(buf), i);
139 if (decode_digit(1, buf) == 0 && decode_digit(2, buf) == 'L')
145 SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
146 struct sr_datafeed_analog *analog, void *info)
153 analog->meaning->mq = SR_MQ_GAIN;
154 analog->meaning->unit = SR_UNIT_UNITLESS;
155 analog->meaning->mqflags = 0;
158 analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE;
160 analog->meaning->mqflags |= SR_MQFLAG_DC;
162 analog->meaning->mqflags |= SR_MQFLAG_AC;
164 analog->meaning->mqflags |= SR_MQFLAG_RELATIVE;
166 analog->meaning->mqflags |= SR_MQFLAG_HOLD;
168 analog->meaning->mqflags |= SR_MQFLAG_MAX;
170 analog->meaning->mqflags |= SR_MQFLAG_MIN;
173 analog->meaning->mq = SR_MQ_VOLTAGE;
174 analog->meaning->unit = SR_UNIT_VOLT;
175 if ((analog->meaning->mqflags & (SR_MQFLAG_DC | SR_MQFLAG_AC)) == 0)
176 analog->meaning->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
179 analog->meaning->mq = SR_MQ_CURRENT;
180 analog->meaning->unit = SR_UNIT_AMPERE;
183 analog->meaning->mq = SR_MQ_RESISTANCE;
184 analog->meaning->unit = SR_UNIT_OHM;
187 analog->meaning->mq = SR_MQ_CAPACITANCE;
188 analog->meaning->unit = SR_UNIT_FARAD;
191 analog->meaning->mq = SR_MQ_FREQUENCY;
192 analog->meaning->unit = SR_UNIT_HERTZ;
195 if (decode_digit(3, buf) == 'C') {
196 analog->meaning->mq = SR_MQ_TEMPERATURE;
197 analog->meaning->unit = SR_UNIT_CELSIUS;
199 if (decode_digit(3, buf) == 'F') {
200 analog->meaning->mq = SR_MQ_TEMPERATURE;
201 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
204 val = decode_value(buf, &exponent);
205 exponent += decode_prefix(buf);
206 val *= powf(10, exponent);
212 analog->encoding->digits = -exponent;
213 analog->spec->spec_digits = -exponent;