2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
6 * Copyright (C) 2015 Matthieu Gaillet <matthieu@gaillet.be>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 * Dream Tech International DTM0660 protocol parser.
26 * 6000 counts (5 5/6 digits)
29 * - Communication parameters: Unidirectional, 2400/8n1
30 * - The protocol is similar to FS9721 but with 15 bytes and reversed nibbles.
38 #include <libsigrok/libsigrok.h>
39 #include "libsigrok-internal.h"
41 #define LOG_PREFIX "dtm0660"
43 static int parse_digit(uint8_t b)
67 sr_dbg("Invalid digit byte: 0x%02x.", b);
72 static gboolean sync_nibbles_valid(const uint8_t *buf)
76 /* Check the synchronization nibbles, and make sure they all match. */
77 for (i = 0; i < DTM0660_PACKET_SIZE; i++) {
78 if (((buf[i] >> 4) & 0x0f) != (i + 1)) {
79 sr_dbg("Sync nibble in byte %d (0x%02x) is invalid.",
88 static gboolean flags_valid(const struct dtm0660_info *info)
92 /* Does the packet have more than one multiplier? */
94 count += (info->is_nano) ? 1 : 0;
95 count += (info->is_micro) ? 1 : 0;
96 count += (info->is_milli) ? 1 : 0;
97 count += (info->is_kilo) ? 1 : 0;
98 count += (info->is_mega) ? 1 : 0;
100 sr_dbg("More than one multiplier detected in packet.");
104 /* Does the packet "measure" more than one type of value? */
106 count += (info->is_hz) ? 1 : 0;
107 count += (info->is_ohm) ? 1 : 0;
108 count += (info->is_farad) ? 1 : 0;
109 count += (info->is_ampere) ? 1 : 0;
110 count += (info->is_volt) ? 1 : 0;
111 count += (info->is_percent) ? 1 : 0;
113 sr_dbg("More than one measurement type detected in packet.");
117 /* Both AC and DC set? */
118 if (info->is_ac && info->is_dc) {
119 sr_dbg("Both AC and DC flags detected in packet.");
123 /* RS232 flag not set? */
124 if (!info->is_rs232) {
125 sr_dbg("No RS232 flag detected in packet.");
132 static int parse_value(const uint8_t *buf, float *result)
134 int i, sign, intval = 0, digits[4];
135 uint8_t digit_bytes[4];
138 /* Byte 1 contains sign in bit 0. */
139 sign = ((buf[1] & (1 << 0)) != 0) ? -1 : 1;
142 * Bytes 1-8: Value (4 decimal digits, sign, decimal point)
144 * Over limit: "0L" (LCD), 0x00 0xeb 0x61 0x00 (digit bytes).
147 /* Merge the two nibbles for a digit into one byte. */
148 for (i = 0; i < 4; i++) {
149 digit_bytes[i] = ((buf[1 + (i * 2)] & 0x0f) << 4);
150 digit_bytes[i] |= (buf[1 + (i * 2) + 1] & 0x0f);
152 /* Bit 4 in the byte is not part of the digit. */
153 digit_bytes[i] &= ~(1 << 4);
156 /* Check for "OL". */
157 if (digit_bytes[0] == 0x00 && digit_bytes[1] == 0xeb &&
158 digit_bytes[2] == 0x61 && digit_bytes[3] == 0x00) {
159 sr_spew("Over limit.");
164 /* Parse the digits. */
165 for (i = 0; i < 4; i++)
166 digits[i] = parse_digit(digit_bytes[i]);
167 sr_spew("Digits: %02x %02x %02x %02x (%d%d%d%d).",
168 digit_bytes[0], digit_bytes[1], digit_bytes[2], digit_bytes[3],
169 digits[0], digits[1], digits[2], digits[3]);
171 /* Merge all digits into an integer value. */
172 for (i = 0; i < 4; i++) {
177 floatval = (float)intval;
179 /* Decimal point position. */
180 if ((buf[3] & 0x01) != 0) {
182 sr_spew("Decimal point after first digit.");
183 } else if ((buf[5] & 0x01) != 0) {
185 sr_spew("Decimal point after second digit.");
186 } else if ((buf[7] & 0x01) != 0) {
188 sr_spew("Decimal point after third digit.");
190 sr_spew("No decimal point in the number.");
196 sr_spew("The display value is %f.", floatval);
203 static void parse_flags(const uint8_t *buf, struct dtm0660_info *info)
205 /* Byte 0: LCD SEG1 */
206 info->is_ac = (buf[0] & (1 << 0)) != 0;
207 info->is_dc = (buf[0] & (1 << 1)) != 0;
208 info->is_auto = (buf[0] & (1 << 2)) != 0;
209 info->is_rs232 = (buf[0] & (1 << 3)) != 0;
211 /* Byte 1: LCD SEG2 */
212 info->is_sign = (buf[1] & (1 << 0)) != 0;
214 /* Byte 9: LCD SEG10 */
215 info->is_micro = (buf[9] & (1 << 0)) != 0;
216 info->is_nano = (buf[9] & (1 << 1)) != 0;
217 info->is_kilo = (buf[9] & (1 << 2)) != 0;
218 info->is_diode = (buf[9] & (1 << 3)) != 0;
220 /* Byte 10: LCD SEG11 */
221 info->is_milli = (buf[10] & (1 << 0)) != 0;
222 info->is_percent = (buf[10] & (1 << 1)) != 0;
223 info->is_mega = (buf[10] & (1 << 2)) != 0;
224 info->is_beep = (buf[10] & (1 << 3)) != 0;
226 /* Byte 11: LCD SEG12 */
227 info->is_farad = (buf[11] & (1 << 0)) != 0;
228 info->is_ohm = (buf[11] & (1 << 1)) != 0;
229 info->is_rel = (buf[11] & (1 << 2)) != 0;
230 info->is_hold = (buf[11] & (1 << 3)) != 0;
232 /* Byte 12: LCD SEG13 */
233 info->is_ampere = (buf[12] & (1 << 0)) != 0;
234 info->is_volt = (buf[12] & (1 << 1)) != 0;
235 info->is_hz = (buf[12] & (1 << 2)) != 0;
236 info->is_bat = (buf[12] & (1 << 3)) != 0;
238 /* Byte 13: LCD SEG14 */
239 info->is_degf = (buf[13] & (1 << 0)) != 0;
240 info->is_degc = (buf[13] & (1 << 1)) != 0;
241 info->is_c2c1_00 = (buf[13] & (1 << 2)) != 0;
242 info->is_c2c1_01 = (buf[13] & (1 << 3)) != 0;
244 /* Byte 14: LCD SEG15 */
245 info->is_apo = (buf[14] & (1 << 0)) != 0;
246 info->is_min = (buf[14] & (1 << 1)) != 0;
247 info->is_minmax = (buf[14] & (1 << 2)) != 0;
248 info->is_max = (buf[14] & (1 << 3)) != 0;
251 static void handle_flags(struct sr_datafeed_analog_old *analog, float *floatval,
252 const struct dtm0660_info *info)
256 *floatval /= 1000000000;
258 *floatval /= 1000000;
264 *floatval *= 1000000;
266 /* Measurement modes */
268 analog->mq = SR_MQ_VOLTAGE;
269 analog->unit = SR_UNIT_VOLT;
271 if (info->is_ampere) {
272 analog->mq = SR_MQ_CURRENT;
273 analog->unit = SR_UNIT_AMPERE;
276 analog->mq = SR_MQ_RESISTANCE;
277 analog->unit = SR_UNIT_OHM;
280 analog->mq = SR_MQ_FREQUENCY;
281 analog->unit = SR_UNIT_HERTZ;
283 if (info->is_farad) {
284 analog->mq = SR_MQ_CAPACITANCE;
285 analog->unit = SR_UNIT_FARAD;
288 analog->mq = SR_MQ_CONTINUITY;
289 analog->unit = SR_UNIT_BOOLEAN;
290 *floatval = (*floatval == INFINITY) ? 0.0 : 1.0;
292 if (info->is_diode) {
293 analog->mq = SR_MQ_VOLTAGE;
294 analog->unit = SR_UNIT_VOLT;
296 if (info->is_percent) {
297 analog->mq = SR_MQ_DUTY_CYCLE;
298 analog->unit = SR_UNIT_PERCENTAGE;
301 analog->mq = SR_MQ_TEMPERATURE;
302 analog->unit = SR_UNIT_CELSIUS;
305 analog->mq = SR_MQ_TEMPERATURE;
306 analog->unit = SR_UNIT_FAHRENHEIT;
309 /* Measurement related flags */
311 analog->mqflags |= SR_MQFLAG_AC;
313 analog->mqflags |= SR_MQFLAG_DC;
315 analog->mqflags |= SR_MQFLAG_AUTORANGE;
317 analog->mqflags |= SR_MQFLAG_DIODE;
319 analog->mqflags |= SR_MQFLAG_HOLD;
321 analog->mqflags |= SR_MQFLAG_RELATIVE;
323 analog->mqflags |= SR_MQFLAG_MIN;
325 analog->mqflags |= SR_MQFLAG_MAX;
329 sr_spew("RS232 enabled.");
331 sr_spew("Battery is low.");
333 sr_spew("Auto power-off mode is active.");
335 sr_spew("Min/max mode active.");
336 if (info->is_c2c1_00)
337 sr_spew("User-defined LCD symbol 0 is active.");
338 if (info->is_c2c1_01)
339 sr_spew("User-defined LCD symbol 1 is active.");
342 SR_PRIV gboolean sr_dtm0660_packet_valid(const uint8_t *buf)
344 struct dtm0660_info info;
346 parse_flags(buf, &info);
348 return (sync_nibbles_valid(buf) && flags_valid(&info));
352 * Parse a protocol packet.
354 * @param buf Buffer containing the 15-byte protocol packet. Must not be NULL.
355 * @param floatval Pointer to a float variable. That variable will contain the
356 * result value upon parsing success. Must not be NULL.
357 * @param analog Pointer to a struct sr_datafeed_analog_old. The struct will be
358 * filled with data according to the protocol packet.
360 * @param info Pointer to a struct dtm0660_info. The struct will be filled
361 * with data according to the protocol packet. Must not be NULL.
363 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
364 * 'analog' variable contents are undefined and should not be used.
366 SR_PRIV int sr_dtm0660_parse(const uint8_t *buf, float *floatval,
367 struct sr_datafeed_analog_old *analog, void *info)
370 struct dtm0660_info *info_local;
372 info_local = (struct dtm0660_info *)info;
374 if ((ret = parse_value(buf, floatval)) != SR_OK) {
375 sr_dbg("Error parsing value: %d.", ret);
379 parse_flags(buf, info_local);
380 handle_flags(analog, floatval, info_local);