]> sigrok.org Git - libsigrok.git/blame - hardware/genericdmm/victor-70c.c
sr: Refactored sr_samplerate_string into a more general function: sr_si_string_u64
[libsigrok.git] / hardware / genericdmm / victor-70c.c
CommitLineData
b84c13d7
BV
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include "libsigrok.h"
21#include "libsigrok-internal.h"
22#include "genericdmm.h"
23#include <libusb.h>
24#include <string.h>
25#include <math.h>
26
27#define DMM_DATA_SIZE 14
28
29
30/* Reverse the high nibble into the low nibble */
31static uint8_t decode_digit(uint8_t in)
32{
33 uint8_t out, i;
34
35 out = 0;
36 in >>= 4;
37 for (i = 0x08; i; i >>= 1) {
38 out >>= 1;
39 if (in & i)
40 out |= 0x08;
41 }
42
43 return out;
44}
45
46static void decode_buf(struct dev_context *devc, unsigned char *data)
47{
48 struct sr_datafeed_packet packet;
49 struct sr_datafeed_analog analog;
50 long factor, ivalue;
51 uint8_t digits[4];
52 gboolean is_duty, is_continuity, is_diode, is_ac, is_dc, is_auto,
53 is_hold, is_max, is_min, is_relative, minus;
54 float fvalue;
55
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]);
60 if (digits[0] == 0x0f && digits[1] == 0x00 && digits[2] == 0x0a &&
61 digits[3] == 0x0f)
62 /* The "over limit" (OL) display comes through like this */
63 ivalue = -1;
64 else if (digits[0] > 9 || digits[1] > 9 || digits[2] > 9 || digits[3] > 9)
65 /* An invalid digit in any position denotes no value. */
66 ivalue = -2;
67 else {
68 ivalue = digits[0] * 1000;
69 ivalue += digits[1] * 100;
70 ivalue += digits[2] * 10;
71 ivalue += digits[3];
72 }
73
74 /* Decimal point position */
75 switch (data[7] >> 4) {
76 case 0x00:
77 factor = 0;
78 break;
79 case 0x02:
80 factor = 1;
81 break;
82 case 0x04:
83 factor = 2;
84 break;
85 case 0x08:
86 factor = 3;
87 break;
88 default:
89 sr_err("genericdmm/victor-70c: unknown decimal point value %.2x", data[7]);
90 }
91
92 /* Minus flag */
93 minus = data[2] & 0x01;
94
95 /* Mode detail symbols on the right side of the digits */
96 is_duty = is_continuity = is_diode = FALSE;
97 switch (data[4]) {
98 case 0x00:
99 /* None. */
100 break;
101 case 0x01:
102 /* Micro */
103 factor += 6;
104 break;
105 case 0x02:
106 /* Milli */
107 factor += 3;
108 break;
109 case 0x04:
110 /* Kilo */
111 ivalue *= 1000;
112 break;
113 case 0x08:
114 /* Mega */
115 ivalue *= 1000000;
116 break;
117 case 0x10:
118 /* Continuity shows up as Ohm + this bit */
119 is_continuity = TRUE;
120 break;
121 case 0x20:
122 /* Diode tester is Volt + this bit */
123 is_diode = TRUE;
124 break;
125 case 0x40:
126 is_duty = TRUE;
127 break;
128 case 0x80:
129 /* Never seen */
130 sr_dbg("genericdmm/victor-70c: unknown mode right detail %.2x", data[4]);
131 break;
132 default:
133 sr_dbg("genericdmm/victor-70c: unknown/invalid mode right detail %.2x", data[4]);
134 }
135
136 /* Scale flags on the right, continued */
137 is_max = is_min = TRUE;
138 if (data[5] & 0x04)
139 is_max = TRUE;
140 if (data[5] & 0x08)
141 is_min = TRUE;
142 if (data[5] & 0x40)
143 /* Nano */
144 factor += 9;
145
146 /* Mode detail symbols on the left side of the digits */
147 is_auto = is_dc = is_ac = is_hold = is_relative = FALSE;
148 if (data[6] & 0x04)
149 is_auto = TRUE;
150 if (data[6] & 0x08)
151 is_dc = TRUE;
152 if (data[6] & 0x10)
153 is_ac = TRUE;
154 if (data[6] & 0x20)
155 is_relative = TRUE;
156 if (data[6] & 0x40)
157 is_hold = TRUE;
158
159 fvalue = (float)ivalue / pow(10, factor);
160 if (minus)
161 fvalue = -fvalue;
162
163 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
164
165 /* Measurement mode */
166 analog.mq = -1;
167 switch (data[3]) {
168 case 0x00:
169 if (is_duty) {
170 analog.mq = SR_MQ_DUTY_CYCLE;
171 analog.unit = SR_UNIT_PERCENTAGE;
172 } else
173 sr_dbg("genericdmm/victor-70c: unknown measurement mode %.2x", data[3]);
174 break;
175 case 0x01:
176 if (is_diode) {
177 analog.mq = SR_MQ_VOLTAGE;
178 analog.unit = SR_UNIT_VOLT;
179 analog.mqflags |= SR_MQFLAG_DIODE;
180 if (ivalue < 0)
181 fvalue = NAN;
182 } else {
183 if (ivalue < 0)
184 break;
185 analog.mq = SR_MQ_VOLTAGE;
186 analog.unit = SR_UNIT_VOLT;
187 if (is_ac)
188 analog.mqflags |= SR_MQFLAG_AC;
189 if (is_dc)
190 analog.mqflags |= SR_MQFLAG_DC;
191 }
192 break;
193 case 0x02:
194 analog.mq = SR_MQ_CURRENT;
195 analog.unit = SR_UNIT_AMPERE;
196 if (is_ac)
197 analog.mqflags |= SR_MQFLAG_AC;
198 if (is_dc)
199 analog.mqflags |= SR_MQFLAG_DC;
200 break;
201 case 0x04:
202 if (is_continuity) {
203 analog.mq = SR_MQ_CONTINUITY;
204 analog.unit = SR_UNIT_BOOLEAN;
205 fvalue = ivalue < 0 ? 0.0 : 1.0;
206 } else {
207 analog.mq = SR_MQ_RESISTANCE;
208 analog.unit = SR_UNIT_OHM;
209 }
210 break;
211 case 0x08:
212 /* Never seen */
213 sr_dbg("genericdmm/victor-70c: unknown measurement mode %.2x", data[3]);
214 break;
215 case 0x10:
216 analog.mq = SR_MQ_FREQUENCY;
217 analog.mq = SR_UNIT_HERTZ;
218 break;
219 case 0x20:
220 analog.mq = SR_MQ_CAPACITANCE;
221 analog.unit = SR_UNIT_FARAD;
222 break;
223 case 0x40:
224 analog.mq = SR_MQ_TEMPERATURE;
225 analog.unit = SR_UNIT_CELSIUS;
226 break;
227 case 0x80:
228 analog.mq = SR_MQ_TEMPERATURE;
229 analog.unit = SR_UNIT_FAHRENHEIT;
230 break;
231 default:
232 sr_dbg("genericdmm/victor-70c: unknown/invalid measurement mode %.2x", data[3]);
233 }
234 if (analog.mq == -1)
235 return;
236
237 if (is_auto)
238 analog.mqflags |= SR_MQFLAG_AUTORANGE;
239 if (is_hold)
240 analog.mqflags |= SR_MQFLAG_HOLD;
241 if (is_max)
242 analog.mqflags |= SR_MQFLAG_MAX;
243 if (is_min)
244 analog.mqflags |= SR_MQFLAG_MIN;
245 if (is_relative)
246 analog.mqflags |= SR_MQFLAG_RELATIVE;
247
248 analog.num_samples = 1;
249 analog.data = &fvalue;
250 packet.type = SR_DF_ANALOG;
251 packet.payload = &analog;
252 sr_session_send(devc->cb_data, &packet);
253
254 devc->num_samples++;
255
256}
257
258static int victor70c_data(struct sr_dev_inst *sdi)
259{
260 struct dev_context *devc;
261 GString *dbg;
262 int len, ret, i;
263 unsigned char buf[DMM_DATA_SIZE], data[DMM_DATA_SIZE];
264 unsigned char obfuscation[DMM_DATA_SIZE] = "jodenxunickxia";
265 unsigned char shuffle[DMM_DATA_SIZE] = {
266 6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1
267 };
268
269 devc = sdi->priv;
270
271 if (sdi->status == SR_ST_INACTIVE) {
272 /* First time through. */
f6b8ffa6
BV
273 if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) {
274 if (libusb_detach_kernel_driver(devc->usb->devhdl, 0) < 0) {
275 sr_err("genericdmm/victor-70c: failed to detach kernel driver");
276 return SR_ERR;
277 }
278 }
b84c13d7
BV
279
280 if (libusb_claim_interface(devc->usb->devhdl, 0)) {
281 sr_err("genericdmm/victor-70c: failed to claim interface 0");
282 return SR_ERR;
283 }
284 sdi->status = SR_ST_ACTIVE;
285 }
286
287 ret = libusb_interrupt_transfer(devc->usb->devhdl, 0x81, buf, DMM_DATA_SIZE,
288 &len, 100);
289 if (ret != 0) {
290 sr_err("genericdmm/victor-70c: failed to get data: libusb error %d", ret);
291 return SR_ERR;
292 }
293
294 if (len != DMM_DATA_SIZE) {
295 sr_dbg("genericdmm/victor-70c: short packet: received %d/%d bytes",
296 len, DMM_DATA_SIZE);
297 return SR_ERR;
298 }
299
300 for (i = 0; i < DMM_DATA_SIZE && buf[i] == 0; i++);
301 if (i == DMM_DATA_SIZE) {
302 /* This DMM outputs all zeroes from time to time, just ignore it. */
303 sr_dbg("genericdmm/victor-70c: received all zeroes");
304 return SR_OK;
305 }
306
307 /* Deobfuscate and reorder data. */
308 for (i = 0; i < DMM_DATA_SIZE; i++)
309 data[shuffle[i]] = (buf[i] - obfuscation[i]) & 0xff;
310
311 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
312 dbg = g_string_sized_new(128);
313 g_string_printf(dbg, "genericdmm/victor-70c: deobfuscated");
314 for (i = 0; i < DMM_DATA_SIZE; i++)
315 g_string_append_printf(dbg, " %.2x", data[i]);
316 sr_spew("%s", dbg->str);
317 g_string_free(dbg, TRUE);
318 }
319
320 decode_buf(devc, data);
321
322 return SR_OK;
323}
324
325
326SR_PRIV struct dmmchip dmmchip_victor70c = {
327 .data = victor70c_data,
328};
329