2 * This file is part of the sigrok 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/>.
20 #include "libsigrok.h"
21 #include "libsigrok-internal.h"
22 #include "genericdmm.h"
27 /* Message logging helpers with driver-specific prefix string. */
28 #define DRIVER_LOG_DOMAIN "victor-dmm: "
29 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
30 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
31 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
36 #define DMM_DATA_SIZE 14
38 /* Reverse the high nibble into the low nibble */
39 static uint8_t decode_digit(uint8_t in)
45 for (i = 0x08; i; i >>= 1) {
54 static void decode_buf(struct dev_context *devc, unsigned char *data)
56 struct sr_datafeed_packet packet;
57 struct sr_datafeed_analog analog;
60 gboolean is_duty, is_continuity, is_diode, is_ac, is_dc, is_auto;
61 gboolean is_hold, is_max, is_min, is_relative, minus;
64 digits[0] = decode_digit(data[12]);
65 digits[1] = decode_digit(data[11]);
66 digits[2] = decode_digit(data[10]);
67 digits[3] = decode_digit(data[9]);
69 if (digits[0] == 0x0f && digits[1] == 0x00 && digits[2] == 0x0a &&
71 /* The "over limit" (OL) display comes through like this */
73 else if (digits[0] > 9 || digits[1] > 9 || digits[2] > 9 || digits[3] > 9)
74 /* An invalid digit in any position denotes no value. */
77 ivalue = digits[0] * 1000;
78 ivalue += digits[1] * 100;
79 ivalue += digits[2] * 10;
83 /* Decimal point position */
84 switch (data[7] >> 4) {
98 sr_err("Unknown decimal point value %.2x.", data[7]);
102 minus = data[2] & 0x01;
104 /* Mode detail symbols on the right side of the digits */
105 is_duty = is_continuity = is_diode = FALSE;
127 /* Continuity shows up as Ohm + this bit */
128 is_continuity = TRUE;
131 /* Diode tester is Volt + this bit */
139 sr_dbg("Unknown mode right detail %.2x.", data[4]);
142 sr_dbg("Unknown/invalid mode right detail %.2x.", data[4]);
145 /* Scale flags on the right, continued */
146 is_max = is_min = TRUE;
155 /* Mode detail symbols on the left side of the digits */
156 is_auto = is_dc = is_ac = is_hold = is_relative = FALSE;
168 fvalue = (float)ivalue / pow(10, factor);
172 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
174 /* Measurement mode */
179 analog.mq = SR_MQ_DUTY_CYCLE;
180 analog.unit = SR_UNIT_PERCENTAGE;
182 sr_dbg("Unknown measurement mode %.2x.", data[3]);
186 analog.mq = SR_MQ_VOLTAGE;
187 analog.unit = SR_UNIT_VOLT;
188 analog.mqflags |= SR_MQFLAG_DIODE;
194 analog.mq = SR_MQ_VOLTAGE;
195 analog.unit = SR_UNIT_VOLT;
197 analog.mqflags |= SR_MQFLAG_AC;
199 analog.mqflags |= SR_MQFLAG_DC;
203 analog.mq = SR_MQ_CURRENT;
204 analog.unit = SR_UNIT_AMPERE;
206 analog.mqflags |= SR_MQFLAG_AC;
208 analog.mqflags |= SR_MQFLAG_DC;
212 analog.mq = SR_MQ_CONTINUITY;
213 analog.unit = SR_UNIT_BOOLEAN;
214 fvalue = ivalue < 0 ? 0.0 : 1.0;
216 analog.mq = SR_MQ_RESISTANCE;
217 analog.unit = SR_UNIT_OHM;
224 sr_dbg("Unknown measurement mode %.2x.", data[3]);
227 analog.mq = SR_MQ_FREQUENCY;
228 analog.unit = SR_UNIT_HERTZ;
231 analog.mq = SR_MQ_CAPACITANCE;
232 analog.unit = SR_UNIT_FARAD;
235 analog.mq = SR_MQ_TEMPERATURE;
236 analog.unit = SR_UNIT_CELSIUS;
239 analog.mq = SR_MQ_TEMPERATURE;
240 analog.unit = SR_UNIT_FAHRENHEIT;
243 sr_dbg("Unknown/invalid measurement mode %.2x.", data[3]);
249 analog.mqflags |= SR_MQFLAG_AUTORANGE;
251 analog.mqflags |= SR_MQFLAG_HOLD;
253 analog.mqflags |= SR_MQFLAG_MAX;
255 analog.mqflags |= SR_MQFLAG_MIN;
257 analog.mqflags |= SR_MQFLAG_RELATIVE;
259 analog.num_samples = 1;
260 analog.data = &fvalue;
261 packet.type = SR_DF_ANALOG;
262 packet.payload = &analog;
263 sr_session_send(devc->cb_data, &packet);
268 static int victor70c_data(struct sr_dev_inst *sdi)
270 struct dev_context *devc;
273 unsigned char buf[DMM_DATA_SIZE], data[DMM_DATA_SIZE];
274 unsigned char obfuscation[DMM_DATA_SIZE] = "jodenxunickxia";
275 unsigned char shuffle[DMM_DATA_SIZE] = {
276 6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1
281 if (sdi->status == SR_ST_INACTIVE) {
282 /* First time through. */
283 if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) {
284 if (libusb_detach_kernel_driver(devc->usb->devhdl, 0) < 0) {
285 sr_err("Failed to detach kernel driver.");
290 if (libusb_claim_interface(devc->usb->devhdl, 0)) {
291 sr_err("Failed to claim interface 0.");
294 sdi->status = SR_ST_ACTIVE;
297 ret = libusb_interrupt_transfer(devc->usb->devhdl, 0x81, buf, DMM_DATA_SIZE,
300 sr_err("Failed to get data: libusb error %d.", ret);
304 if (len != DMM_DATA_SIZE) {
305 sr_dbg("Short packet: received %d/%d bytes.",
310 for (i = 0; i < DMM_DATA_SIZE && buf[i] == 0; i++);
311 if (i == DMM_DATA_SIZE) {
312 /* This DMM outputs all zeroes from time to time, just ignore it. */
313 sr_dbg("Received all zeroes.");
317 /* Deobfuscate and reorder data. */
318 for (i = 0; i < DMM_DATA_SIZE; i++)
319 data[shuffle[i]] = (buf[i] - obfuscation[i]) & 0xff;
321 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
322 dbg = g_string_sized_new(128);
323 g_string_printf(dbg, "Deobfuscated.");
324 for (i = 0; i < DMM_DATA_SIZE; i++)
325 g_string_append_printf(dbg, " %.2x", data[i]);
326 sr_spew("%s", dbg->str);
327 g_string_free(dbg, TRUE);
330 decode_buf(devc, data);
335 SR_PRIV struct dmmchip dmmchip_victor70c = {
336 .data = victor70c_data,