* Communication parameters: Unidirectional, 2400/7o1
*/
+#include <config.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <glib.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
#define LOG_PREFIX "ut71x"
/*
- * Factors for the respective measurement mode (0 means "invalid").
+ * Exponents for the respective measurement mode.
*
* The Conrad/Voltcraft protocol descriptions have a typo (they suggest
* index 0 for the 10A range (which is incorrect, it's range 1).
*/
-static const float factors[16][8] = {
- {1e-5, 0, 0, 0, 0, 0, 0, 0 }, /* AC mV */
- {0, 1e-4, 1e-3, 1e-2, 1e-1, 0, 0, 0 }, /* DC V */
- {0, 1e-4, 1e-3, 1e-2, 1e-1, 0, 0, 0 }, /* AC V */
- {1e-5, 0, 0, 0, 0, 0, 0, 0 }, /* DC mV */
- {0, 1e-1, 1, 1e1, 1e2, 1e3, 1e4, 0 }, /* Resistance */
- {0, 1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6}, /* Capacitance */
- {1e-1, 0, 0, 0, 0, 0, 0, 0 }, /* Temp (C) */
- {1e-8, 1e-7, 0, 0, 0, 0, 0, 0 }, /* uA */
- {1e-6, 1e-5, 0, 0, 0, 0, 0, 0 }, /* mA */
- {0, 1e-3, 0, 0, 0, 0, 0, 0 }, /* 10A */
- {1e-1, 0, 0, 0, 0, 0, 0, 0 }, /* Continuity */
- {1e-4, 0, 0, 0, 0, 0, 0, 0 }, /* Diode */
- {1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3, 1e4 }, /* Frequency */
- {1e-1, 0, 0, 0, 0, 0, 0, 0 }, /* Temp (F) */
- {0, 0, 0, 1, 0, 0, 0, 0 }, /* Power */
- {1e-2, 0, 0, 0, 0, 0, 0, 0 }, /* Loop current */
+static const int exponents[16][8] = {
+ { -5, 0, 0, 0, 0, 0, 0, 0 }, /* AC mV */
+ { 0, -4, -3, -2, -1, 0, 0, 0 }, /* DC V */
+ { 0, -4, -3, -2, -1, 0, 0, 0 }, /* AC V */
+ { -5, 0, 0, 0, 0, 0, 0, 0 }, /* DC mV */
+ { 0, -1, 0, 1, 2, 3, 4, 0 }, /* Resistance */
+ { 0, -12, -11, -10, -9, -8, -7, -6 }, /* Capacitance */
+ { -1, 0, 0, 0, 0, 0, 0, 0 }, /* Temp (C) */
+ { -8, -7, 0, 0, 0, 0, 0, 0 }, /* uA */
+ { -6, -5, 0, 0, 0, 0, 0, 0 }, /* mA */
+ { 0, -3, 0, 0, 0, 0, 0, 0 }, /* 10A */
+ { -1, 0, 0, 0, 0, 0, 0, 0 }, /* Continuity */
+ { -4, 0, 0, 0, 0, 0, 0, 0 }, /* Diode */
+ { -3, -2, -1, 0, 1, 2, 3, 4 }, /* Frequency */
+ { -1, 0, 0, 0, 0, 0, 0, 0 }, /* Temp (F) */
+ { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Power */
+ { -2, 0, 0, 0, 0, 0, 0, 0 }, /* Loop current */
};
static int parse_value(const uint8_t *buf, struct ut71x_info *info, float *result)
return SR_OK;
}
-static int parse_range(const uint8_t *buf, float *floatval)
+static int parse_range(const uint8_t *buf, float *floatval, int *exponent)
{
int idx, mode;
- float factor = 0;
idx = buf[5] - '0';
if (idx < 0 || idx > 7) {
sr_spew("mode/idx = %d/%d", mode, idx);
- factor = factors[mode][idx];
- if (factor == 0) {
- sr_dbg("Invalid factor for range byte: 0x%02x.", buf[5]);
- return SR_ERR;
- }
+ *exponent = exponents[mode][idx];
- /* Apply respective factor (mode-dependent) on the value. */
- *floatval *= factor;
- sr_dbg("Applying factor %f, new value is %f.", factor, *floatval);
+ /* Apply respective exponent (mode-dependent) on the value. */
+ *floatval *= powf(10, *exponent);
+ sr_dbg("Applying exponent %d, new value is %f.", *exponent, *floatval);
return SR_OK;
}
{
/* Measurement modes */
if (info->is_voltage) {
- analog->mq = SR_MQ_VOLTAGE;
- analog->unit = SR_UNIT_VOLT;
+ analog->meaning->mq = SR_MQ_VOLTAGE;
+ analog->meaning->unit = SR_UNIT_VOLT;
}
if (info->is_current) {
- analog->mq = SR_MQ_CURRENT;
- analog->unit = SR_UNIT_AMPERE;
+ analog->meaning->mq = SR_MQ_CURRENT;
+ analog->meaning->unit = SR_UNIT_AMPERE;
}
if (info->is_resistance) {
- analog->mq = SR_MQ_RESISTANCE;
- analog->unit = SR_UNIT_OHM;
+ analog->meaning->mq = SR_MQ_RESISTANCE;
+ analog->meaning->unit = SR_UNIT_OHM;
}
if (info->is_frequency) {
- analog->mq = SR_MQ_FREQUENCY;
- analog->unit = SR_UNIT_HERTZ;
+ analog->meaning->mq = SR_MQ_FREQUENCY;
+ analog->meaning->unit = SR_UNIT_HERTZ;
}
if (info->is_capacitance) {
- analog->mq = SR_MQ_CAPACITANCE;
- analog->unit = SR_UNIT_FARAD;
+ analog->meaning->mq = SR_MQ_CAPACITANCE;
+ analog->meaning->unit = SR_UNIT_FARAD;
}
if (info->is_temperature && info->is_celsius) {
- analog->mq = SR_MQ_TEMPERATURE;
- analog->unit = SR_UNIT_CELSIUS;
+ analog->meaning->mq = SR_MQ_TEMPERATURE;
+ analog->meaning->unit = SR_UNIT_CELSIUS;
}
if (info->is_temperature && info->is_fahrenheit) {
- analog->mq = SR_MQ_TEMPERATURE;
- analog->unit = SR_UNIT_FAHRENHEIT;
+ analog->meaning->mq = SR_MQ_TEMPERATURE;
+ analog->meaning->unit = SR_UNIT_FAHRENHEIT;
}
if (info->is_continuity) {
- analog->mq = SR_MQ_CONTINUITY;
- analog->unit = SR_UNIT_BOOLEAN;
+ analog->meaning->mq = SR_MQ_CONTINUITY;
+ analog->meaning->unit = SR_UNIT_BOOLEAN;
*floatval = (*floatval < 0.0 || *floatval > 60.0) ? 0.0 : 1.0;
}
if (info->is_diode) {
- analog->mq = SR_MQ_VOLTAGE;
- analog->unit = SR_UNIT_VOLT;
+ analog->meaning->mq = SR_MQ_VOLTAGE;
+ analog->meaning->unit = SR_UNIT_VOLT;
}
if (info->is_duty_cycle) {
- analog->mq = SR_MQ_DUTY_CYCLE;
- analog->unit = SR_UNIT_PERCENTAGE;
+ analog->meaning->mq = SR_MQ_DUTY_CYCLE;
+ analog->meaning->unit = SR_UNIT_PERCENTAGE;
}
if (info->is_power) {
- analog->mq = SR_MQ_POWER;
- analog->unit = SR_UNIT_WATT;
+ analog->meaning->mq = SR_MQ_POWER;
+ analog->meaning->unit = SR_UNIT_WATT;
}
if (info->is_loop_current) {
/* 4mA = 0%, 20mA = 100% */
- analog->mq = SR_MQ_CURRENT;
- analog->unit = SR_UNIT_PERCENTAGE;
+ analog->meaning->mq = SR_MQ_CURRENT;
+ analog->meaning->unit = SR_UNIT_PERCENTAGE;
}
/* Measurement related flags */
if (info->is_ac)
- analog->mqflags |= SR_MQFLAG_AC;
+ analog->meaning->mqflags |= SR_MQFLAG_AC;
if (info->is_dc)
- analog->mqflags |= SR_MQFLAG_DC;
+ analog->meaning->mqflags |= SR_MQFLAG_DC;
if (info->is_ac)
/* All AC modes do True-RMS measurements. */
- analog->mqflags |= SR_MQFLAG_RMS;
+ analog->meaning->mqflags |= SR_MQFLAG_RMS;
if (info->is_auto)
- analog->mqflags |= SR_MQFLAG_AUTORANGE;
+ analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE;
if (info->is_diode)
- analog->mqflags |= SR_MQFLAG_DIODE;
+ analog->meaning->mqflags |= SR_MQFLAG_DIODE;
}
static gboolean flags_valid(const struct ut71x_info *info)
SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval,
struct sr_datafeed_analog *analog, void *info)
{
- int ret;
+ int ret, exponent = 0;
struct ut71x_info *info_local;
info_local = (struct ut71x_info *)info;
return ret;
}
- if ((ret = parse_range(buf, floatval)) != SR_OK)
+ if ((ret = parse_range(buf, floatval, &exponent)) != SR_OK)
return ret;
handle_flags(analog, floatval, info);
+ analog->encoding->digits = -exponent;
+ analog->spec->spec_digits = -exponent;
+
return SR_OK;
}