2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.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/>.
24 #include <libsigrok/libsigrok.h>
25 #include "libsigrok-internal.h"
28 /* Reverse the high nibble into the low nibble */
29 static uint8_t decode_digit(uint8_t in)
35 for (i = 0x08; i; i >>= 1) {
44 static void decode_buf(struct sr_dev_inst *sdi, unsigned char *data)
46 struct sr_datafeed_packet packet;
47 struct sr_datafeed_analog analog;
48 struct sr_analog_encoding encoding;
49 struct sr_analog_meaning meaning;
50 struct sr_analog_spec spec;
51 struct dev_context *devc;
54 gboolean is_duty, is_continuity, is_diode, is_ac, is_dc, is_auto;
55 gboolean is_hold, is_max, is_min, is_relative, minus;
60 digits[0] = decode_digit(data[12]);
61 digits[1] = decode_digit(data[11]);
62 digits[2] = decode_digit(data[10]);
63 digits[3] = decode_digit(data[9]);
65 if (digits[0] == 0x0f && digits[1] == 0x00 && digits[2] == 0x0a &&
67 /* The "over limit" (OL) display comes through like this */
69 else if (digits[0] > 9 || digits[1] > 9 || digits[2] > 9 || digits[3] > 9)
70 /* An invalid digit in any position denotes no value. */
73 ivalue = digits[0] * 1000;
74 ivalue += digits[1] * 100;
75 ivalue += digits[2] * 10;
79 /* Decimal point position */
81 switch (data[7] >> 4) {
95 sr_err("Unknown decimal point byte: 0x%.2x.", data[7]);
100 minus = data[2] & 0x01;
102 /* Mode detail symbols on the right side of the digits */
103 is_duty = is_continuity = is_diode = FALSE;
125 /* Continuity shows up as Ohm + this bit */
126 is_continuity = TRUE;
129 /* Diode tester is Volt + this bit */
137 sr_dbg("Unknown mode right detail: 0x%.2x.", data[4]);
140 sr_dbg("Unknown/invalid mode right detail: 0x%.2x.", data[4]);
144 /* Scale flags on the right, continued */
145 is_max = is_min = FALSE;
154 /* Mode detail symbols on the left side of the digits */
155 is_auto = is_dc = is_ac = is_hold = is_relative = FALSE;
167 fvalue = (float)ivalue / pow(10, factor);
171 sr_analog_init(&analog, &encoding, &meaning, &spec, 4);
173 /* Measurement mode */
174 meaning.channels = sdi->channels;
179 meaning.mq = SR_MQ_DUTY_CYCLE;
180 meaning.unit = SR_UNIT_PERCENTAGE;
182 sr_dbg("Unknown measurement mode: %.2x.", data[3]);
186 meaning.mq = SR_MQ_VOLTAGE;
187 meaning.unit = SR_UNIT_VOLT;
188 meaning.mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
194 meaning.mq = SR_MQ_VOLTAGE;
195 meaning.unit = SR_UNIT_VOLT;
197 meaning.mqflags |= SR_MQFLAG_AC;
199 meaning.mqflags |= SR_MQFLAG_DC;
203 meaning.mq = SR_MQ_CURRENT;
204 meaning.unit = SR_UNIT_AMPERE;
206 meaning.mqflags |= SR_MQFLAG_AC;
208 meaning.mqflags |= SR_MQFLAG_DC;
212 meaning.mq = SR_MQ_CONTINUITY;
213 meaning.unit = SR_UNIT_BOOLEAN;
214 fvalue = ivalue < 0 ? 0.0 : 1.0;
216 meaning.mq = SR_MQ_RESISTANCE;
217 meaning.unit = SR_UNIT_OHM;
224 sr_dbg("Unknown measurement mode: 0x%.2x.", data[3]);
227 meaning.mq = SR_MQ_FREQUENCY;
228 meaning.unit = SR_UNIT_HERTZ;
231 meaning.mq = SR_MQ_CAPACITANCE;
232 meaning.unit = SR_UNIT_FARAD;
235 meaning.mq = SR_MQ_TEMPERATURE;
236 meaning.unit = SR_UNIT_CELSIUS;
239 meaning.mq = SR_MQ_TEMPERATURE;
240 meaning.unit = SR_UNIT_FAHRENHEIT;
243 sr_dbg("Unknown/invalid measurement mode: 0x%.2x.", data[3]);
250 meaning.mqflags |= SR_MQFLAG_AUTORANGE;
252 meaning.mqflags |= SR_MQFLAG_HOLD;
254 meaning.mqflags |= SR_MQFLAG_MAX;
256 meaning.mqflags |= SR_MQFLAG_MIN;
258 meaning.mqflags |= SR_MQFLAG_RELATIVE;
260 analog.data = &fvalue;
261 analog.num_samples = 1;
263 packet.type = SR_DF_ANALOG;
264 packet.payload = &analog;
265 sr_session_send(sdi, &packet);
267 sr_sw_limits_update_samples_read(&devc->limits, 1);
270 SR_PRIV int victor_dmm_receive_data(struct sr_dev_inst *sdi, unsigned char *buf)
272 static const unsigned char obfuscation[DMM_DATA_SIZE] = "jodenxunickxia";
273 static const unsigned char shuffle[DMM_DATA_SIZE] = {
274 6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1
278 unsigned char data[DMM_DATA_SIZE];
280 for (i = 0; i < DMM_DATA_SIZE && buf[i] == 0; i++);
281 if (i == DMM_DATA_SIZE) {
282 /* This DMM outputs all zeroes from time to time, just ignore it. */
283 sr_dbg("Received all zeroes.");
287 /* Deobfuscate and reorder data. */
288 for (i = 0; i < DMM_DATA_SIZE; i++)
289 data[shuffle[i]] = (buf[i] - obfuscation[i]) & 0xff;
291 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
292 dbg = g_string_sized_new(128);
293 g_string_printf(dbg, "Deobfuscated.");
294 for (i = 0; i < DMM_DATA_SIZE; i++)
295 g_string_append_printf(dbg, " %.2x", data[i]);
296 sr_spew("%s", dbg->str);
297 g_string_free(dbg, TRUE);
300 decode_buf(sdi, data);