m2110.c 2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * This file is part of the libsigrok project.
 *
 * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20 21
/**
 * @file
22
 *
23 24 25
 * BBC Goerz Metrawatt M2110 ASCII protocol parser.
 *
 * Most probably the simplest multimeter protocol ever ;-) .
26 27
 */

28
#include <config.h>
29 30 31
#include <string.h>
#include <math.h>
#include <glib.h>
32
#include <libsigrok/libsigrok.h>
33 34
#include "libsigrok-internal.h"

35 36
#define LOG_PREFIX "m2110"

37 38 39 40 41 42 43
SR_PRIV gboolean sr_m2110_packet_valid(const uint8_t *buf)
{
	float val;

	if ((buf[7] != '\r') || (buf[8] != '\n'))
		return FALSE;

44
	if (!strncmp((const char *)buf, "OVERRNG", 7))
45 46
		return TRUE;

47
	if (sr_atof_ascii((const char *)buf, &val) == SR_OK)
48 49 50 51 52 53
		return TRUE;
	else
		return FALSE;
}

SR_PRIV int sr_m2110_parse(const uint8_t *buf, float *floatval,
54
				struct sr_datafeed_analog *analog, void *info)
55
{
56
	int dot_pos, digits = 0;
57 58 59 60
	float val;

	(void)info;

61
	/* We don't know the unit, so that's the best we can do. */
62 63 64
	analog->meaning->mq = SR_MQ_GAIN;
	analog->meaning->unit = SR_UNIT_UNITLESS;
	analog->meaning->mqflags = 0;
65

66
	if (!strncmp((const char *)buf, "OVERRNG", 7))
67
		*floatval = INFINITY;
68
	else if (sr_atof_ascii((const char *)buf, &val) == SR_OK) {
69
		*floatval = val;
70 71 72 73 74 75 76
		dot_pos = strcspn((const char *)buf, ".");
		if (dot_pos < 7)
			digits = 6 - dot_pos;
	}

	analog->encoding->digits  = digits;
	analog->spec->spec_digits = digits;
77 78 79

	return SR_OK;
}