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_analog analog;
47 struct dev_context *devc;
50 gboolean is_duty, is_continuity, is_diode, is_ac, is_dc, is_auto;
51 gboolean is_hold, is_max, is_min, is_relative, minus;
56 digits[0] = decode_digit(data[12]);
57 digits[1] = decode_digit(data[11]);
58 digits[2] = decode_digit(data[10]);
59 digits[3] = decode_digit(data[9]);
61 if (digits[0] == 0x0f && digits[1] == 0x00 && digits[2] == 0x0a &&
63 /* The "over limit" (OL) display comes through like this */
65 else if (digits[0] > 9 || digits[1] > 9 || digits[2] > 9 || digits[3] > 9)
66 /* An invalid digit in any position denotes no value. */
69 ivalue = digits[0] * 1000;
70 ivalue += digits[1] * 100;
71 ivalue += digits[2] * 10;
75 /* Decimal point position */
77 switch (data[7] >> 4) {
91 sr_err("Unknown decimal point byte: 0x%.2x.", data[7]);
96 minus = data[2] & 0x01;
98 /* Mode detail symbols on the right side of the digits */
99 is_duty = is_continuity = is_diode = FALSE;
121 /* Continuity shows up as Ohm + this bit */
122 is_continuity = TRUE;
125 /* Diode tester is Volt + this bit */
133 sr_dbg("Unknown mode right detail: 0x%.2x.", data[4]);
136 sr_dbg("Unknown/invalid mode right detail: 0x%.2x.", data[4]);
140 /* Scale flags on the right, continued */
141 is_max = is_min = TRUE;
150 /* Mode detail symbols on the left side of the digits */
151 is_auto = is_dc = is_ac = is_hold = is_relative = FALSE;
163 fvalue = (float)ivalue / pow(10, factor);
167 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
169 /* Measurement mode */
174 analog.mq = SR_MQ_DUTY_CYCLE;
175 analog.unit = SR_UNIT_PERCENTAGE;
177 sr_dbg("Unknown measurement mode: %.2x.", data[3]);
181 analog.mq = SR_MQ_VOLTAGE;
182 analog.unit = SR_UNIT_VOLT;
183 analog.mqflags |= SR_MQFLAG_DIODE;
189 analog.mq = SR_MQ_VOLTAGE;
190 analog.unit = SR_UNIT_VOLT;
192 analog.mqflags |= SR_MQFLAG_AC;
194 analog.mqflags |= SR_MQFLAG_DC;
198 analog.mq = SR_MQ_CURRENT;
199 analog.unit = SR_UNIT_AMPERE;
201 analog.mqflags |= SR_MQFLAG_AC;
203 analog.mqflags |= SR_MQFLAG_DC;
207 analog.mq = SR_MQ_CONTINUITY;
208 analog.unit = SR_UNIT_BOOLEAN;
209 fvalue = ivalue < 0 ? 0.0 : 1.0;
211 analog.mq = SR_MQ_RESISTANCE;
212 analog.unit = SR_UNIT_OHM;
219 sr_dbg("Unknown measurement mode: 0x%.2x.", data[3]);
222 analog.mq = SR_MQ_FREQUENCY;
223 analog.unit = SR_UNIT_HERTZ;
226 analog.mq = SR_MQ_CAPACITANCE;
227 analog.unit = SR_UNIT_FARAD;
230 analog.mq = SR_MQ_TEMPERATURE;
231 analog.unit = SR_UNIT_CELSIUS;
234 analog.mq = SR_MQ_TEMPERATURE;
235 analog.unit = SR_UNIT_FAHRENHEIT;
238 sr_dbg("Unknown/invalid measurement mode: 0x%.2x.", data[3]);
245 analog.mqflags |= SR_MQFLAG_AUTORANGE;
247 analog.mqflags |= SR_MQFLAG_HOLD;
249 analog.mqflags |= SR_MQFLAG_MAX;
251 analog.mqflags |= SR_MQFLAG_MIN;
253 analog.mqflags |= SR_MQFLAG_RELATIVE;
255 analog.probes = sdi->probes;
256 analog.num_samples = 1;
257 analog.data = &fvalue;
258 packet.type = SR_DF_ANALOG;
259 packet.payload = &analog;
260 sr_session_send(devc->cb_data, &packet);
265 SR_PRIV int victor_dmm_receive_data(struct sr_dev_inst *sdi, unsigned char *buf)
269 unsigned char data[DMM_DATA_SIZE];
270 unsigned char obfuscation[DMM_DATA_SIZE] = "jodenxunickxia";
271 unsigned char shuffle[DMM_DATA_SIZE] = {
272 6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1
275 for (i = 0; i < DMM_DATA_SIZE && buf[i] == 0; i++);
276 if (i == DMM_DATA_SIZE) {
277 /* This DMM outputs all zeroes from time to time, just ignore it. */
278 sr_dbg("Received all zeroes.");
282 /* Deobfuscate and reorder data. */
283 for (i = 0; i < DMM_DATA_SIZE; i++)
284 data[shuffle[i]] = (buf[i] - obfuscation[i]) & 0xff;
286 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
287 dbg = g_string_sized_new(128);
288 g_string_printf(dbg, "Deobfuscated.");
289 for (i = 0; i < DMM_DATA_SIZE; i++)
290 g_string_append_printf(dbg, " %.2x", data[i]);
291 sr_spew("%s", dbg->str);
292 g_string_free(dbg, TRUE);
295 decode_buf(sdi, data);