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/>.
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
27 /* Reverse the high nibble into the low nibble */
28 static uint8_t decode_digit(uint8_t in)
34 for (i = 0x08; i; i >>= 1) {
43 static void decode_buf(struct sr_dev_inst *sdi, unsigned char *data)
45 struct sr_datafeed_packet packet;
46 struct sr_datafeed_analog2 analog;
47 struct sr_analog_encoding encoding;
48 struct sr_analog_meaning meaning;
49 struct sr_analog_spec spec;
50 struct dev_context *devc;
53 gboolean is_duty, is_continuity, is_diode, is_ac, is_dc, is_auto;
54 gboolean is_hold, is_max, is_min, is_relative, minus;
59 digits[0] = decode_digit(data[12]);
60 digits[1] = decode_digit(data[11]);
61 digits[2] = decode_digit(data[10]);
62 digits[3] = decode_digit(data[9]);
64 if (digits[0] == 0x0f && digits[1] == 0x00 && digits[2] == 0x0a &&
66 /* The "over limit" (OL) display comes through like this */
68 else if (digits[0] > 9 || digits[1] > 9 || digits[2] > 9 || digits[3] > 9)
69 /* An invalid digit in any position denotes no value. */
72 ivalue = digits[0] * 1000;
73 ivalue += digits[1] * 100;
74 ivalue += digits[2] * 10;
78 /* Decimal point position */
80 switch (data[7] >> 4) {
94 sr_err("Unknown decimal point byte: 0x%.2x.", data[7]);
99 minus = data[2] & 0x01;
101 /* Mode detail symbols on the right side of the digits */
102 is_duty = is_continuity = is_diode = FALSE;
124 /* Continuity shows up as Ohm + this bit */
125 is_continuity = TRUE;
128 /* Diode tester is Volt + this bit */
136 sr_dbg("Unknown mode right detail: 0x%.2x.", data[4]);
139 sr_dbg("Unknown/invalid mode right detail: 0x%.2x.", data[4]);
143 /* Scale flags on the right, continued */
144 is_max = is_min = FALSE;
153 /* Mode detail symbols on the left side of the digits */
154 is_auto = is_dc = is_ac = is_hold = is_relative = FALSE;
166 fvalue = (float)ivalue / pow(10, factor);
170 memset(&analog, 0, sizeof(struct sr_datafeed_analog2));
171 memset(&encoding, 0, sizeof(struct sr_analog_encoding));
172 memset(&meaning, 0, sizeof(struct sr_analog_meaning));
173 memset(&spec, 0, sizeof(struct sr_analog_spec));
175 /* Measurement mode */
176 meaning.channels = sdi->channels;
181 meaning.mq = SR_MQ_DUTY_CYCLE;
182 meaning.unit = SR_UNIT_PERCENTAGE;
184 sr_dbg("Unknown measurement mode: %.2x.", data[3]);
188 meaning.mq = SR_MQ_VOLTAGE;
189 meaning.unit = SR_UNIT_VOLT;
190 meaning.mqflags |= SR_MQFLAG_DIODE;
196 meaning.mq = SR_MQ_VOLTAGE;
197 meaning.unit = SR_UNIT_VOLT;
199 meaning.mqflags |= SR_MQFLAG_AC;
201 meaning.mqflags |= SR_MQFLAG_DC;
205 meaning.mq = SR_MQ_CURRENT;
206 meaning.unit = SR_UNIT_AMPERE;
208 meaning.mqflags |= SR_MQFLAG_AC;
210 meaning.mqflags |= SR_MQFLAG_DC;
214 meaning.mq = SR_MQ_CONTINUITY;
215 meaning.unit = SR_UNIT_BOOLEAN;
216 fvalue = ivalue < 0 ? 0.0 : 1.0;
218 meaning.mq = SR_MQ_RESISTANCE;
219 meaning.unit = SR_UNIT_OHM;
226 sr_dbg("Unknown measurement mode: 0x%.2x.", data[3]);
229 meaning.mq = SR_MQ_FREQUENCY;
230 meaning.unit = SR_UNIT_HERTZ;
233 meaning.mq = SR_MQ_CAPACITANCE;
234 meaning.unit = SR_UNIT_FARAD;
237 meaning.mq = SR_MQ_TEMPERATURE;
238 meaning.unit = SR_UNIT_CELSIUS;
241 meaning.mq = SR_MQ_TEMPERATURE;
242 meaning.unit = SR_UNIT_FAHRENHEIT;
245 sr_dbg("Unknown/invalid measurement mode: 0x%.2x.", data[3]);
252 meaning.mqflags |= SR_MQFLAG_AUTORANGE;
254 meaning.mqflags |= SR_MQFLAG_HOLD;
256 meaning.mqflags |= SR_MQFLAG_MAX;
258 meaning.mqflags |= SR_MQFLAG_MIN;
260 meaning.mqflags |= SR_MQFLAG_RELATIVE;
262 encoding.unitsize = sizeof(float);
263 encoding.is_float = TRUE;
264 #ifdef WORDS_BIGENDIAN
265 encoding.is_bigendian = TRUE;
267 encoding.is_bigendian = FALSE;
269 encoding.digits = 4; /* Values are always 4-digit numbers. */
270 encoding.is_digits_decimal = TRUE;
271 encoding.scale.p = 1;
272 encoding.scale.q = 1;
273 encoding.offset.p = 0;
274 encoding.offset.q = 1;
276 spec.spec_digits = encoding.digits;
278 analog.data = &fvalue;
279 analog.num_samples = 1;
280 analog.encoding = &encoding;
281 analog.meaning = &meaning;
284 packet.type = SR_DF_ANALOG2;
285 packet.payload = &analog;
286 sr_session_send(devc->cb_data, &packet);
291 SR_PRIV int victor_dmm_receive_data(struct sr_dev_inst *sdi, unsigned char *buf)
295 unsigned char data[DMM_DATA_SIZE];
296 unsigned char obfuscation[DMM_DATA_SIZE] = "jodenxunickxia";
297 unsigned char shuffle[DMM_DATA_SIZE] = {
298 6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1
301 for (i = 0; i < DMM_DATA_SIZE && buf[i] == 0; i++);
302 if (i == DMM_DATA_SIZE) {
303 /* This DMM outputs all zeroes from time to time, just ignore it. */
304 sr_dbg("Received all zeroes.");
308 /* Deobfuscate and reorder data. */
309 for (i = 0; i < DMM_DATA_SIZE; i++)
310 data[shuffle[i]] = (buf[i] - obfuscation[i]) & 0xff;
312 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
313 dbg = g_string_sized_new(128);
314 g_string_printf(dbg, "Deobfuscated.");
315 for (i = 0; i < DMM_DATA_SIZE; i++)
316 g_string_append_printf(dbg, " %.2x", data[i]);
317 sr_spew("%s", dbg->str);
318 g_string_free(dbg, TRUE);
321 decode_buf(sdi, data);