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.
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
30 #define LOG_PREFIX "brymen-bm25x"
34 SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf)
41 for (i = 1; i < BRYMEN_BM25X_PACKET_SIZE; i++)
42 if ((buf[i] >> 4) != i)
48 static int decode_digit(int num, const uint8_t *buf)
52 val = (buf[3 + 2 * num] & 0xe) | ((buf[4 + 2 * num] << 4) & 0xf0);
65 case 0x00: return ' ';
66 case 0x40: return '-';
67 case 0x16: return 'L';
68 case 0x1e: return 'C';
69 case 0x4e: return 'F';
70 case 0x5e: return 'E';
71 case 0x62: return 'n';
72 case 0x42: return 'r';
74 sr_dbg("Unknown digit: 0x%02x.", val);
79 static int decode_point(const uint8_t *buf)
83 for (i = 1; i < MAX_DIGITS; i++) {
84 if ((buf[11 - 2 * i] & 1) == 0)
87 sr_spew("Multiple decimal points found!");
96 static float scale_value(float val, int point, int digits)
100 pos = point ? point + digits - MAX_DIGITS : 0;
104 case 1: return val * 1e-1;
105 case 2: return val * 1e-2;
106 case 3: return val * 1e-3;
109 sr_dbg("Invalid decimal point %d (%d digits).", point, digits);
114 static float decode_prefix(const uint8_t *buf)
116 if (buf[11] & 2) return 1e+6;
117 if (buf[11] & 1) return 1e+3;
118 if (buf[13] & 1) return 1e-3;
119 if (buf[13] & 2) return 1e-6;
120 if (buf[12] & 1) return 1e-9;
125 static float decode_value(const uint8_t *buf)
130 for (i = 0; i < MAX_DIGITS; i++) {
131 digit = decode_digit(i, buf);
132 if (i == 3 && (digit == 'C' || digit == 'F'))
134 if (digit < 0 || digit > 9)
136 val = 10.0 * val + digit;
139 return scale_value(val, decode_point(buf), i);
142 if (decode_digit(1, buf) == 0 && decode_digit(2, buf) == 'L')
148 SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
149 struct sr_datafeed_analog *analog, void *info)
155 analog->mq = SR_MQ_GAIN;
156 analog->unit = SR_UNIT_UNITLESS;
160 analog->mqflags |= SR_MQFLAG_AUTORANGE;
162 analog->mqflags |= SR_MQFLAG_DC;
164 analog->mqflags |= SR_MQFLAG_AC;
166 analog->mqflags |= SR_MQFLAG_RELATIVE;
168 analog->mqflags |= SR_MQFLAG_HOLD;
170 analog->mqflags |= SR_MQFLAG_MAX;
172 analog->mqflags |= SR_MQFLAG_MIN;
175 analog->mq = SR_MQ_VOLTAGE;
176 analog->unit = SR_UNIT_VOLT;
177 if ((analog->mqflags & (SR_MQFLAG_DC | SR_MQFLAG_AC)) == 0)
178 analog->mqflags |= SR_MQFLAG_DIODE;
181 analog->mq = SR_MQ_CURRENT;
182 analog->unit = SR_UNIT_AMPERE;
185 analog->mq = SR_MQ_RESISTANCE;
186 analog->unit = SR_UNIT_OHM;
189 analog->mq = SR_MQ_CAPACITANCE;
190 analog->unit = SR_UNIT_FARAD;
193 analog->mq = SR_MQ_FREQUENCY;
194 analog->unit = SR_UNIT_HERTZ;
197 if (decode_digit(3, buf) == 'C') {
198 analog->mq = SR_MQ_TEMPERATURE;
199 analog->unit = SR_UNIT_CELSIUS;
201 if (decode_digit(3, buf) == 'F') {
202 analog->mq = SR_MQ_TEMPERATURE;
203 analog->unit = SR_UNIT_FAHRENHEIT;
206 val = decode_value(buf) * decode_prefix(buf);